Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-forks-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'theme-check-vscode': minor
---

Respect .prettierignore when formatting local VSCode files
74 changes: 74 additions & 0 deletions packages/vscode-extension/src/node/formatter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed okay to add a test here as src/*.spec.ts are excluded, but i'm happy to move or remove if preferred!

import type * as vscode from 'vscode';
import * as prettier from 'prettier';
import { vscodePrettierFormat, nodePrettierFormat } from './formatter';

vi.mock('prettier', () => ({
format: vi.fn(),
getFileInfo: vi.fn(),
resolveConfig: vi.fn(),
}));

vi.mock('vscode', () => ({
workspace: {
getWorkspaceFolder: vi.fn(() => ({
uri: { fsPath: '/workspace' },
})),
},
}));

describe('formatter', () => {
const localFile = {
uri: {
scheme: 'file',
fsPath: '/snippets/button.liquid',
},
getText: vi.fn(() => '<div>{{ product.title }}</div>'),
} as unknown as vscode.TextDocument;

const remoteFile = {
...localFile,
uri: { ...localFile.uri, scheme: 'vscode-remote' },
} as vscode.TextDocument;

beforeEach(() => {
vi.clearAllMocks();
});

describe('vscodePrettierFormat', () => {
it('should format remote files without config options', async () => {
await vscodePrettierFormat(remoteFile);

expect(prettier.getFileInfo).not.toHaveBeenCalled();
expect(prettier.resolveConfig).not.toHaveBeenCalled();
expect(prettier.format).toHaveBeenCalledWith('<div>{{ product.title }}</div>', {
parser: 'liquid-html',
plugins: [expect.anything()],
});
});

it('should format local files with config options', async () => {
await vscodePrettierFormat(localFile);

expect(prettier.getFileInfo).toHaveBeenCalled();
expect(prettier.resolveConfig).toHaveBeenCalled();
expect(prettier.format).toHaveBeenCalledWith('<div>{{ product.title }}</div>', {
parser: 'liquid-html',
plugins: [expect.anything()],
});
});

it('should not format ignored local files', async () => {
vi.mocked(prettier.getFileInfo).mockResolvedValue({
ignored: true,
} as prettier.FileInfoResult);

const result = await nodePrettierFormat(localFile);

expect(prettier.getFileInfo).toHaveBeenCalled();
expect(prettier.resolveConfig).not.toHaveBeenCalled();
expect(prettier.format).not.toHaveBeenCalled();
expect(result).toBe('<div>{{ product.title }}</div>');
});
});
});
14 changes: 14 additions & 0 deletions packages/vscode-extension/src/node/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'node:path';
import { workspace } from 'vscode';
import LiquidPrettierPlugin from '@shopify/prettier-plugin-liquid';
import * as prettier from 'prettier';
import { Format } from '../common/formatter';
Expand All @@ -18,6 +20,18 @@ export const vscodePrettierFormat: Format = async (textDocument) => {

export const nodePrettierFormat: Format = async (textDocument) => {
const text = textDocument.getText();

const workspaceFolder = workspace.getWorkspaceFolder(textDocument.uri);
const fileInfo =
workspaceFolder &&
(await prettier.getFileInfo(textDocument.uri.fsPath, {
ignorePath: path.join(workspaceFolder.uri.fsPath, '.prettierignore'),
}));

if (fileInfo?.ignored) {
return text;
}

const options = await prettier.resolveConfig(textDocument.uri.fsPath, { useCache: false });
return prettier.format(text, {
...options,
Expand Down