Skip to content
Merged
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
15 changes: 4 additions & 11 deletions packages/rstack/src/fmt/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ const createFileRequest = (
filePath: string,
config: ResolvedFmtConfig,
resolvePlugins: FmtPluginResolver,
): FmtFileRequest => {
const options = resolvePlugins(resolveFmtOptions(filePath, config));

return {
path: filePath,
options: {
...options,
filepath: filePath,
},
};
};
): FmtFileRequest => ({
path: filePath,
options: resolvePlugins(resolveFmtOptions(filePath, config)),
});

/** Discovers worker-ready files without reading Prettier config files or `.prettierignore`. */
const discoverFmtFiles = async ({
Expand Down
7 changes: 5 additions & 2 deletions packages/rstack/src/fmt/prettierPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ const fmtOptionsPlugin = {
const defaultFmtPlugins: PrettierPlugins = [yukuPlugin, fmtOptionsPlugin];

/** Prepends bundled plugins so project plugins can override their parsers. */
const getPrettierPlugins = async (options: ResolvedFmtOptions): Promise<PrettierPlugins> => {
const getPrettierPlugins = async (
options: ResolvedFmtOptions,
filePath: string,
): Promise<PrettierPlugins> => {
const plugins =
options.sortPackageJson === true && /(^|[/\\])package\.json$/.test(options.filepath ?? '')
options.sortPackageJson === true && /(^|[/\\])package\.json$/.test(filePath)
? [...defaultFmtPlugins, (await import('./sortPackageJsonPlugin.ts')).sortPackageJsonPlugin]
: defaultFmtPlugins;

Expand Down
10 changes: 2 additions & 8 deletions packages/rstack/src/fmt/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,21 @@ const runFmtFile = async (
shouldWrite: boolean,
formatFile: FormatFile,
): Promise<FmtFileResult | undefined> => {
const startTime = performance.now();

try {
const result = await formatFile(file, shouldWrite);
if (result === 'unsupported') {
if (result !== 'changed') {
return;
}

return {
path: file.path,
status: result === 'changed' ? (shouldWrite ? 'written' : 'different') : 'unchanged',
durationMs: performance.now() - startTime,
status: shouldWrite ? 'written' : 'different',
};
} catch (error) {
return {
path: file.path,
status: 'error',
error,
durationMs: performance.now() - startTime,
};
}
};
Expand Down Expand Up @@ -80,15 +76,13 @@ const runFmtFiles = async ({
mode,
maxWorkers,
}: RunFmtFilesOptions): Promise<FmtRunResult> => {
const startTime = performance.now();
const shouldWrite = mode === 'write';
const results =
files.length === 0 ? [] : await runFmtFilesInWorkerPool(files, shouldWrite, maxWorkers);

return {
files: results,
exitCode: getFmtExitCode(results),
durationMs: performance.now() - startTime,
};
};

Expand Down
9 changes: 3 additions & 6 deletions packages/rstack/src/fmt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ interface DiscoverFmtFilesOptions {
interface FmtFileRequest {
/** Absolute path to the file. */
path: string;
/** Final per-file options with project plugins and the file path resolved. */
options: ResolvedFmtOptions & Required<Pick<PrettierOptions, 'filepath'>>;
/** Final per-file options with project plugins resolved. */
options: ResolvedFmtOptions;
}

type FmtMode = 'write' | 'check' | 'list-different';
Expand All @@ -75,15 +75,13 @@ interface RunFmtFilesOptions {

interface SuccessfulFmtFileResult {
path: string;
status: 'unchanged' | 'written' | 'different';
durationMs: number;
status: 'written' | 'different';
}

interface FailedFmtFileResult {
path: string;
status: 'error';
error: unknown;
durationMs: number;
}

type FmtFileResult = SuccessfulFmtFileResult | FailedFmtFileResult;
Expand All @@ -92,7 +90,6 @@ interface FmtRunResult {
files: FmtFileResult[];
/** Recommended CLI exit code. */
exitCode: FmtExitCode;
durationMs: number;
}

export type {
Expand Down
3 changes: 2 additions & 1 deletion packages/rstack/src/fmt/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const formatFile = async (
{ path, options }: FmtFileRequest,
shouldWrite: boolean,
): Promise<FormatFileResult> => {
const plugins = await getPrettierPlugins(options);
const plugins = await getPrettierPlugins(options, path);
const parser = await resolveFmtParser(path, options, plugins);
if (!parser) {
return 'unsupported';
Expand All @@ -50,6 +50,7 @@ const formatFile = async (
const source = readFileSync(path, 'utf8');
const formatted = await format(source, {
...options,
filepath: path,
parser,
plugins,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/rstack/tests/fmt/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ test('defers parser inference to workers and preserves an explicit parser', asyn
'unknown.extension',
]);
expect(inferredFiles.every((file) => file.options.parser === undefined)).toBe(true);
expect(configuredFiles[0].options).toMatchObject({
filepath: path.join(rootPath, 'source.custom'),
parser: 'babel',
expect(configuredFiles[0]).toEqual({
path: path.join(rootPath, 'source.custom'),
options: { parser: 'babel' },
});
});
});
Expand Down
7 changes: 2 additions & 5 deletions packages/rstack/tests/fmt/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { withTempProject } from './helpers.ts';
const createRequest = (filePath: string): FmtFileRequest => ({
path: filePath,
options: {
filepath: filePath,
parser: 'typescript',
},
});
Expand All @@ -31,11 +30,9 @@ test('does not rewrite unchanged files', async () => {

expect(result).toMatchObject({
exitCode: 0,
files: [{ path: filePath, status: 'unchanged' }],
files: [],
});
expect(statSync(filePath).mtimeMs).toBe(mtimeMs);
expect(result.durationMs).toBeGreaterThanOrEqual(0);
expect(result.files[0].durationMs).toBeGreaterThanOrEqual(0);
});
});

Expand Down Expand Up @@ -112,7 +109,7 @@ test('omits unsupported files from the result', async () => {
const result = await run([
{
path: filePath,
options: { filepath: filePath },
options: {},
},
]);

Expand Down
1 change: 0 additions & 1 deletion packages/rstack/tests/fmt/runnerWorkerPreflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ beforeEach(() => {
const createRequest = (filePath: string): FmtFileRequest => ({
path: filePath,
options: {
filepath: filePath,
parser: 'typescript',
},
});
Expand Down
1 change: 0 additions & 1 deletion packages/rstack/tests/fmt/runnerWriteFailure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ test('returns an error when a file write fails', async () => {
{
path: filePath,
options: {
filepath: filePath,
parser: 'typescript',
},
},
Expand Down
5 changes: 2 additions & 3 deletions packages/rstack/tests/fmt/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ test('writes formatted files', async () => {
{
path: filePath,
options: {
filepath: filePath,
parser: 'typescript',
},
},
Expand All @@ -34,7 +33,7 @@ test('infers the parser for an explicitly provided node_modules file', async ()
formatFile(
{
path: filePath,
options: { filepath: filePath },
options: {},
},
false,
),
Expand All @@ -52,7 +51,7 @@ test('skips unsupported files before reading them', async () => {
formatFile(
{
path: filePath,
options: { filepath: filePath },
options: {},
},
true,
),
Expand Down