Skip to content

Commit 5dcf301

Browse files
committed
Merge remote-tracking branch 'origin/main' into chenjiahan/refactor-fmt-tinypool
# Conflicts: # packages/rstack/src/fmt/worker.ts
2 parents 7a1165c + f3df538 commit 5dcf301

10 files changed

Lines changed: 23 additions & 41 deletions

File tree

packages/rstack/src/fmt/discovery.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,10 @@ const createFileRequest = (
88
filePath: string,
99
config: ResolvedFmtConfig,
1010
resolvePlugins: FmtPluginResolver,
11-
): FmtFileRequest => {
12-
const options = resolvePlugins(resolveFmtOptions(filePath, config));
13-
14-
return {
15-
path: filePath,
16-
options: {
17-
...options,
18-
filepath: filePath,
19-
},
20-
};
21-
};
11+
): FmtFileRequest => ({
12+
path: filePath,
13+
options: resolvePlugins(resolveFmtOptions(filePath, config)),
14+
});
2215

2316
/** Discovers worker-ready files without reading Prettier config files or `.prettierignore`. */
2417
const discoverFmtFiles = async ({

packages/rstack/src/fmt/prettierPlugins.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ const fmtOptionsPlugin = {
1818
const defaultFmtPlugins: PrettierPlugins = [yukuPlugin, fmtOptionsPlugin];
1919

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

packages/rstack/src/fmt/runner.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,21 @@ const runFmtFile = async (
1616
shouldWrite: boolean,
1717
formatFile: FormatFile,
1818
): Promise<FmtFileResult | undefined> => {
19-
const startTime = performance.now();
20-
2119
try {
2220
const result = await formatFile(file, shouldWrite);
23-
if (result === 'unsupported') {
21+
if (result !== 'changed') {
2422
return;
2523
}
2624

2725
return {
2826
path: file.path,
29-
status: result === 'changed' ? (shouldWrite ? 'written' : 'different') : 'unchanged',
30-
durationMs: performance.now() - startTime,
27+
status: shouldWrite ? 'written' : 'different',
3128
};
3229
} catch (error) {
3330
return {
3431
path: file.path,
3532
status: 'error',
3633
error,
37-
durationMs: performance.now() - startTime,
3834
};
3935
}
4036
};
@@ -80,15 +76,13 @@ const runFmtFiles = async ({
8076
mode,
8177
maxWorkers,
8278
}: RunFmtFilesOptions): Promise<FmtRunResult> => {
83-
const startTime = performance.now();
8479
const shouldWrite = mode === 'write';
8580
const results =
8681
files.length === 0 ? [] : await runFmtFilesInWorkerPool(files, shouldWrite, maxWorkers);
8782

8883
return {
8984
files: results,
9085
exitCode: getFmtExitCode(results),
91-
durationMs: performance.now() - startTime,
9286
};
9387
};
9488

packages/rstack/src/fmt/types.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ interface DiscoverFmtFilesOptions {
5757
interface FmtFileRequest {
5858
/** Absolute path to the file. */
5959
path: string;
60-
/** Final per-file options with project plugins and the file path resolved. */
61-
options: ResolvedFmtOptions & Required<Pick<PrettierOptions, 'filepath'>>;
60+
/** Final per-file options with project plugins resolved. */
61+
options: ResolvedFmtOptions;
6262
}
6363

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

7676
interface SuccessfulFmtFileResult {
7777
path: string;
78-
status: 'unchanged' | 'written' | 'different';
79-
durationMs: number;
78+
status: 'written' | 'different';
8079
}
8180

8281
interface FailedFmtFileResult {
8382
path: string;
8483
status: 'error';
8584
error: unknown;
86-
durationMs: number;
8785
}
8886

8987
type FmtFileResult = SuccessfulFmtFileResult | FailedFmtFileResult;
@@ -92,7 +90,6 @@ interface FmtRunResult {
9290
files: FmtFileResult[];
9391
/** Recommended CLI exit code. */
9492
exitCode: FmtExitCode;
95-
durationMs: number;
9693
}
9794

9895
export type {

packages/rstack/src/fmt/worker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const formatFile = async ({
4646
file: { path, options },
4747
shouldWrite,
4848
}: FormatFileTask): Promise<FormatFileResult> => {
49-
const plugins = await getPrettierPlugins(options);
49+
const plugins = await getPrettierPlugins(options, path);
5050
const parser = await resolveFmtParser(path, options, plugins);
5151
if (!parser) {
5252
return 'unsupported';
@@ -55,6 +55,7 @@ const formatFile = async ({
5555
const source = readFileSync(path, 'utf8');
5656
const formatted = await format(source, {
5757
...options,
58+
filepath: path,
5859
parser,
5960
plugins,
6061
});

packages/rstack/tests/fmt/discovery.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ test('defers parser inference to workers and preserves an explicit parser', asyn
6464
'unknown.extension',
6565
]);
6666
expect(inferredFiles.every((file) => file.options.parser === undefined)).toBe(true);
67-
expect(configuredFiles[0].options).toMatchObject({
68-
filepath: path.join(rootPath, 'source.custom'),
69-
parser: 'babel',
67+
expect(configuredFiles[0]).toEqual({
68+
path: path.join(rootPath, 'source.custom'),
69+
options: { parser: 'babel' },
7070
});
7171
});
7272
});

packages/rstack/tests/fmt/runner.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { withTempProject } from './helpers.ts';
88
const createRequest = (filePath: string): FmtFileRequest => ({
99
path: filePath,
1010
options: {
11-
filepath: filePath,
1211
parser: 'typescript',
1312
},
1413
});
@@ -31,11 +30,9 @@ test('does not rewrite unchanged files', async () => {
3130

3231
expect(result).toMatchObject({
3332
exitCode: 0,
34-
files: [{ path: filePath, status: 'unchanged' }],
33+
files: [],
3534
});
3635
expect(statSync(filePath).mtimeMs).toBe(mtimeMs);
37-
expect(result.durationMs).toBeGreaterThanOrEqual(0);
38-
expect(result.files[0].durationMs).toBeGreaterThanOrEqual(0);
3936
});
4037
});
4138

@@ -112,7 +109,7 @@ test('omits unsupported files from the result', async () => {
112109
const result = await run([
113110
{
114111
path: filePath,
115-
options: { filepath: filePath },
112+
options: {},
116113
},
117114
]);
118115

packages/rstack/tests/fmt/runnerWorkerPreflight.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ beforeEach(() => {
2222
const createRequest = (filePath: string): FmtFileRequest => ({
2323
path: filePath,
2424
options: {
25-
filepath: filePath,
2625
parser: 'typescript',
2726
},
2827
});

packages/rstack/tests/fmt/runnerWriteFailure.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ test('returns an error when a file write fails', async () => {
2424
{
2525
path: filePath,
2626
options: {
27-
filepath: filePath,
2827
parser: 'typescript',
2928
},
3029
},

packages/rstack/tests/fmt/worker.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ test('writes formatted files', async () => {
1313
file: {
1414
path: filePath,
1515
options: {
16-
filepath: filePath,
1716
parser: 'typescript',
1817
},
1918
},
@@ -34,7 +33,7 @@ test('infers the parser for an explicitly provided node_modules file', async ()
3433
formatFile({
3534
file: {
3635
path: filePath,
37-
options: { filepath: filePath },
36+
options: {},
3837
},
3938
shouldWrite: false,
4039
}),
@@ -52,7 +51,7 @@ test('skips unsupported files before reading them', async () => {
5251
formatFile({
5352
file: {
5453
path: filePath,
55-
options: { filepath: filePath },
54+
options: {},
5655
},
5756
shouldWrite: true,
5857
}),

0 commit comments

Comments
 (0)