Skip to content

Commit 435ec84

Browse files
committed
refactor(fmt): simplify internal names
1 parent 3e14999 commit 435ec84

12 files changed

Lines changed: 47 additions & 51 deletions

File tree

packages/rstack/src/fmt/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const assertExclusiveMode = (option: string, hasMode: boolean, positionals: stri
8282
}
8383
};
8484

85-
const parseFmtCLIArgs = (args: string[]): ParsedFmtCLIArgs => {
85+
const parseFmtArgs = (args: string[]): ParsedFmtCLIArgs => {
8686
const { values, positionals } = parseArgs({
8787
args,
8888
options: {
@@ -292,7 +292,7 @@ const runFmtCLI = async (args: string[]): Promise<void> => {
292292
patterns,
293293
stdinFilepath,
294294
withNodeModules,
295-
} = parseFmtCLIArgs(args);
295+
} = parseFmtArgs(args);
296296
if (help) {
297297
logger.log(renderFmtHelp());
298298
return;

packages/rstack/src/fmt/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const normalizeFmtConfig = (config: FmtConfig | undefined, rootPath: string): Re
9090
};
9191

9292
/** Creates a reusable resolver for applying per-file formatter overrides. */
93-
const createFmtOptionsResolver = (config: ResolvedFmtConfig): FmtOptionsResolver => {
93+
const createOptionsResolver = (config: ResolvedFmtConfig): FmtOptionsResolver => {
9494
if (config.overrides.length === 0) {
9595
return () => config.baseOptions;
9696
}
@@ -127,5 +127,5 @@ const resolveFmtConfig = async ({
127127
return normalizeFmtConfig(config, rootPath);
128128
};
129129

130-
export { createFmtOptionsResolver, normalizeFmtConfig, resolveFmtConfig };
130+
export { createOptionsResolver, normalizeFmtConfig, resolveFmtConfig };
131131
export type { FmtOptionsResolver };

packages/rstack/src/fmt/discovery.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path';
2-
import { createFmtOptionsResolver, type FmtOptionsResolver } from './config.ts';
2+
import { createOptionsResolver, type FmtOptionsResolver } from './config.ts';
33
import { discoverFmtPaths } from './discoverPaths.ts';
44
import { createIgnoreMatcher } from './ignore.ts';
55
import type { FmtPluginResolver } from './plugins.ts';
@@ -21,7 +21,7 @@ const createLazyPluginResolver = (rootPath: string): (() => Promise<FmtPluginRes
2121
(resolver ??= import(
2222
/* rspackChunkName: 'fmtPlugins' */
2323
'./plugins.ts'
24-
).then(({ createFmtPluginResolver }) => createFmtPluginResolver(rootPath)));
24+
).then(({ createPluginResolver }) => createPluginResolver(rootPath)));
2525
};
2626

2727
/** Resolves the plugin specifiers of a request whose options configure plugins. */
@@ -63,7 +63,7 @@ const discoverFmtFiles = async ({
6363
return [];
6464
}
6565

66-
const resolveOptions = createFmtOptionsResolver(config);
66+
const resolveOptions = createOptionsResolver(config);
6767
const getPluginResolver = createLazyPluginResolver(config.rootPath);
6868

6969
return Promise.all(

packages/rstack/src/fmt/ignore.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ interface CreateIgnoreMatcherOptions {
2121
ignorePaths?: string[];
2222
}
2323

24-
const createDefaultIgnoreMatcher = (): IgnorePredicate => {
24+
const createDefaultMatcher = (): IgnorePredicate => {
2525
const suffixes = defaultIgnoreNames.map((name) => `${path.sep}${name}`);
2626

2727
return (filePath) => suffixes.some((suffix) => filePath.endsWith(suffix));
2828
};
2929

30-
const createPatternMatcherSet = (sources: IgnoreSource[]): IgnorePredicate => {
30+
const createSourceMatcher = (sources: IgnoreSource[]): IgnorePredicate => {
3131
const matcher = new (loadNativeBinding().IgnoreMatcher)(sources);
3232
return (filePath, isDirectory = false) => matcher.isIgnored(filePath, isDirectory);
3333
};
@@ -60,7 +60,7 @@ const createIgnoreMatcher = async ({
6060
ignorePaths.map((ignorePath) => loadIgnoreSource(cwd, ignorePath)),
6161
);
6262
if (config.ignorePatterns.length) {
63-
return createPatternMatcherSet([
63+
return createSourceMatcher([
6464
{
6565
rootPath: config.rootPath,
6666
patterns: [...defaultIgnoreNames, ...config.ignorePatterns].join('\n'),
@@ -69,12 +69,12 @@ const createIgnoreMatcher = async ({
6969
]);
7070
}
7171

72-
const defaultMatcher = createDefaultIgnoreMatcher();
72+
const defaultMatcher = createDefaultMatcher();
7373
if (ignoreFileSources.length === 0) {
7474
return defaultMatcher;
7575
}
7676

77-
const cliMatcher = createPatternMatcherSet(ignoreFileSources);
77+
const cliMatcher = createSourceMatcher(ignoreFileSources);
7878
return (filePath, isDirectory = false) =>
7979
defaultMatcher(filePath, isDirectory) || cliMatcher(filePath, isDirectory);
8080
};

packages/rstack/src/fmt/lsp/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
type InitializeParams,
1010
type TextEdit,
1111
} from 'vscode-languageserver/node';
12-
import { createFmtOptionsResolver, type FmtOptionsResolver } from '../config.ts';
12+
import { createOptionsResolver, type FmtOptionsResolver } from '../config.ts';
1313
import {
1414
createFileRequest,
1515
createLazyPluginResolver,
@@ -122,7 +122,7 @@ const createFmtLspSession = async ({
122122

123123
return {
124124
isIgnored,
125-
resolveOptions: createFmtOptionsResolver(config),
125+
resolveOptions: createOptionsResolver(config),
126126
getPluginResolver: createLazyPluginResolver(config.rootPath),
127127
};
128128
};

packages/rstack/src/fmt/plugins.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const createFingerprintResolver = (): FingerprintResolver => {
9292
};
9393

9494
/** Creates a project-root resolver for plugins in final per-file options. */
95-
const createFmtPluginResolver = (rootPath: string): FmtPluginResolver => {
95+
const createPluginResolver = (rootPath: string): FmtPluginResolver => {
9696
const parentUrl = pathToFileURL(join(rootPath, 'index.js'));
9797
const cache = new Map<string, string>();
9898

@@ -143,5 +143,5 @@ const createFmtPluginResolver = (rootPath: string): FmtPluginResolver => {
143143
};
144144
};
145145

146-
export { createFingerprintResolver, createFmtPluginResolver };
146+
export { createFingerprintResolver, createPluginResolver };
147147
export type { FingerprintResolver, FmtPluginResolver };

packages/rstack/src/fmt/runner.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const loadPluginFingerprints = async (
8989
};
9090

9191
/** Resolves the portable cache identity before work is dispatched. */
92-
const createFmtFileRunTask = (file: FmtFileRequest, cache?: RunCache): FmtFileRunTask => {
92+
const createRunTask = (file: FmtFileRequest, cache?: RunCache): FmtFileRunTask => {
9393
let key: string | undefined;
9494
let fileCache: FmtFileCache | undefined;
9595

@@ -160,7 +160,7 @@ const runFmtFile = async (
160160
};
161161

162162
/** Starts slower Markdown parsers first while preserving order within both priority groups. */
163-
const runPriorityFmtFiles = async (
163+
const runPriorityTasks = async (
164164
tasks: FmtFileRunTask[],
165165
shouldWrite: boolean,
166166
formatFile: FormatFile,
@@ -184,13 +184,13 @@ const runPriorityFmtFiles = async (
184184
};
185185

186186
/** Processes files in a worker pool while preserving input order. */
187-
const runFmtFilesInWorkerPool = async (
187+
const runWithWorkers = async (
188188
files: FmtFileRequest[],
189189
shouldWrite: boolean,
190190
maxWorkers?: number,
191191
cache?: RunCache,
192192
): Promise<FmtWorkerPoolResult> => {
193-
const tasks = files.map((file) => createFmtFileRunTask(file, cache));
193+
const tasks = files.map((file) => createRunTask(file, cache));
194194
const pendingFileCount = tasks.reduce(
195195
(count, task) => count + (isCachedUnsupported(task) ? 0 : 1),
196196
0,
@@ -199,13 +199,13 @@ const runFmtFilesInWorkerPool = async (
199199
return { files: [], processedFileCount: 0 };
200200
}
201201

202-
const { createFmtWorkerPool } = await import('./workerPool.ts');
203-
const workerPool = await createFmtWorkerPool(pendingFileCount, maxWorkers);
202+
const { createWorkerPool } = await import('./workerPool.ts');
203+
const workerPool = await createWorkerPool(pendingFileCount, maxWorkers);
204204

205205
try {
206206
const results =
207207
workerPool.workerCount >= minPriorityWorkers
208-
? await runPriorityFmtFiles(tasks, shouldWrite, workerPool.formatFile)
208+
? await runPriorityTasks(tasks, shouldWrite, workerPool.formatFile)
209209
: await Promise.all(
210210
tasks.map((task) => runFmtFile(task, shouldWrite, workerPool.formatFile)),
211211
);
@@ -233,7 +233,7 @@ const runFmtFilesInWorkerPool = async (
233233
};
234234

235235
/** Maps file results to the Prettier-compatible CLI exit code. */
236-
const getFmtExitCode = (files: FmtFileResult[]): FmtExitCode => {
236+
const getExitCode = (files: FmtFileResult[]): FmtExitCode => {
237237
let exitCode: FmtExitCode = 0;
238238

239239
for (const file of files) {
@@ -272,13 +272,12 @@ const runFmtFiles = async ({
272272
const result =
273273
files.length === 0
274274
? { files: [], processedFileCount: 0 }
275-
: await runFmtFilesInWorkerPool(files, shouldWrite, maxWorkers, runCache);
275+
: await runWithWorkers(files, shouldWrite, maxWorkers, runCache);
276276
await runCache?.store.save().catch(() => false);
277277

278278
return {
279279
...result,
280-
exitCode:
281-
files.length > 0 && result.processedFileCount === 0 ? 2 : getFmtExitCode(result.files),
280+
exitCode: files.length > 0 && result.processedFileCount === 0 ? 2 : getExitCode(result.files),
282281
};
283282
};
284283

packages/rstack/src/fmt/stdin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { resolve } from 'node:path';
2-
import { createFmtOptionsResolver } from './config.ts';
2+
import { createOptionsResolver } from './config.ts';
33
import {
44
createFileRequest,
55
createLazyPluginResolver,
@@ -84,7 +84,7 @@ const runFmtStdin = async ({
8484
}
8585

8686
const file = await resolveFileRequestPlugins(
87-
createFileRequest(absolutePath, createFmtOptionsResolver(config)),
87+
createFileRequest(absolutePath, createOptionsResolver(config)),
8888
createLazyPluginResolver(config.rootPath),
8989
);
9090
const result = await formatFmtSource(file, () => source);

packages/rstack/src/fmt/workerPool.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ interface FmtWorkerPool {
2121
* plateau before all CPU cores are occupied, while additional workers increase
2222
* scheduling and memory pressure.
2323
*/
24-
const getFmtWorkerCount = (fileCount: number, maxWorkers?: number): number =>
24+
const getWorkerCount = (fileCount: number, maxWorkers?: number): number =>
2525
Math.min(fileCount, maxWorkers ?? Math.min(8, Math.max(1, availableParallelism() - 1)));
2626

27-
const getFmtWorkerUrl = (): URL => {
27+
const getWorkerUrl = (): URL => {
2828
// Source tests run after build and exercise the same worker artifact as the CLI.
2929
const workerPath = new URL(import.meta.url).pathname.endsWith('.ts')
3030
? '../../dist/fmtWorker.js'
@@ -33,13 +33,10 @@ const getFmtWorkerUrl = (): URL => {
3333
};
3434

3535
/** Creates and starts every worker before formatting can begin. */
36-
const createFmtWorkerPool = async (
37-
fileCount: number,
38-
maxWorkers?: number,
39-
): Promise<FmtWorkerPool> => {
40-
const workerCount = getFmtWorkerCount(fileCount, maxWorkers);
36+
const createWorkerPool = async (fileCount: number, maxWorkers?: number): Promise<FmtWorkerPool> => {
37+
const workerCount = getWorkerCount(fileCount, maxWorkers);
4138
const pool = new Tinypool({
42-
filename: getFmtWorkerUrl().href,
39+
filename: getWorkerUrl().href,
4340
name: 'initializeFmtWorker',
4441
minThreads: workerCount,
4542
maxThreads: workerCount,
@@ -64,5 +61,5 @@ const createFmtWorkerPool = async (
6461
};
6562
};
6663

67-
export { createFmtWorkerPool };
64+
export { createWorkerPool };
6865
export type { FmtWorkerPool };

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { expect, test } from 'rstack/test';
3-
import { createFmtOptionsResolver, normalizeFmtConfig } from '../../src/fmt/config.ts';
3+
import { createOptionsResolver, normalizeFmtConfig } from '../../src/fmt/config.ts';
44

55
const rootPath = path.join(import.meta.dirname, 'project');
66

@@ -12,7 +12,7 @@ test('reuses base options when no override matches', () => {
1212
},
1313
rootPath,
1414
);
15-
const resolveOptions = createFmtOptionsResolver(config);
15+
const resolveOptions = createOptionsResolver(config);
1616

1717
expect(resolveOptions(path.join(rootPath, 'index.js'))).toBe(config.baseOptions);
1818
});
@@ -39,7 +39,7 @@ test('applies basename and path overrides in declaration order', () => {
3939
},
4040
rootPath,
4141
);
42-
const resolveOptions = createFmtOptionsResolver(config);
42+
const resolveOptions = createOptionsResolver(config);
4343

4444
const options = resolveOptions(path.join(rootPath, 'src/index.ts'));
4545
const testOptions = resolveOptions(path.join(rootPath, 'src/index.test.ts'));
@@ -57,7 +57,7 @@ test('applies overrides outside the config root', () => {
5757
},
5858
rootPath,
5959
);
60-
const resolveOptions = createFmtOptionsResolver(config);
60+
const resolveOptions = createOptionsResolver(config);
6161

6262
expect(resolveOptions(path.join(rootPath, '../shared/index.ts'))).toEqual({ semi: false });
6363
});

0 commit comments

Comments
 (0)