Skip to content

Commit b498140

Browse files
committed
perf(fmt): avoid redundant discovery ignore checks
1 parent 49c5d83 commit b498140

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

‎packages/rstack/src/fmt/discoverPaths.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface DiscoverFmtPathsOptions {
2626
patterns?: string[];
2727
/** Whether files inside node_modules may be discovered. */
2828
withNodeModules?: boolean;
29-
/** Returns whether a scanned path can be excluded during traversal. */
29+
/** Returns whether a candidate path should be excluded. */
3030
isIgnored?: (filePath: string, isDirectory: boolean) => boolean;
3131
}
3232

@@ -384,7 +384,9 @@ const discoverFmtPaths = async ({
384384
} = await classifyPatterns(cwd, patterns, ignoredDirNames);
385385
const directoryRoots = getOutermostPaths(directories);
386386
const globMatchers = globs.map((pattern) => micromatch.matcher(pattern, { dot: true }));
387-
const candidates = new Set(explicitFiles);
387+
const candidates = new Set(
388+
isIgnored ? explicitFiles.filter((filePath) => !isIgnored(filePath, false)) : explicitFiles,
389+
);
388390
const traversalRoots = getTraversalRoots(cwd, directoryRoots, globs);
389391

390392
if (traversalRoots.length) {

‎packages/rstack/src/fmt/discovery.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ const discoverFmtFiles = async ({
3232
? (filePath: string, isDirectory = false) =>
3333
isExcluded(filePath) || isIgnored(filePath, isDirectory)
3434
: isIgnored;
35-
const candidates = await discoverFmtPaths({
35+
const filePaths = await discoverFmtPaths({
3636
cwd,
3737
patterns,
3838
withNodeModules,
3939
isIgnored: shouldIgnore,
4040
});
41-
if (candidates.length === 0) {
41+
if (filePaths.length === 0) {
4242
return [];
4343
}
4444

45-
const filePaths = candidates.filter((filePath) => !shouldIgnore(filePath));
4645
const resolveOptions = createFmtOptionsResolver(config);
4746
const files = filePaths.map((filePath) => createFileRequest(filePath, resolveOptions));
4847
if (!files.some((file) => file.options.plugins?.length)) {

‎packages/rstack/tests/fmt/discoverPaths.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ test('lets explicit files bypass gitignore', async () => {
139139
});
140140
});
141141

142-
test('applies an external ignore matcher during traversal', async () => {
142+
test('applies an external ignore matcher to traversed and explicit paths', async () => {
143143
await withTempProject(async (rootPath) => {
144144
writeProjectFile(rootPath, 'generated/nested/output.ts');
145145
const ignoredFilePath = writeProjectFile(rootPath, 'src/ignored.ts');
@@ -160,9 +160,15 @@ test('applies an external ignore matcher during traversal', async () => {
160160
patterns: ['generated'],
161161
isIgnored,
162162
});
163+
const explicitIgnoredFile = await discoverFmtPaths({
164+
cwd: rootPath,
165+
patterns: [ignoredFilePath],
166+
isIgnored,
167+
});
163168

164169
expect(relativePaths(rootPath, files)).toEqual([path.join('src', 'index.ts')]);
165170
expect(ignoredRoot).toEqual([]);
171+
expect(explicitIgnoredFile).toEqual([]);
166172
expect(checkedPaths).toContainEqual({ path: 'generated', isDirectory: true });
167173
expect(checkedPaths).toContainEqual({
168174
path: path.join('src', 'ignored.ts'),

0 commit comments

Comments
 (0)