Skip to content

Commit b2f0998

Browse files
committed
perf(fmt): optimize default lock file matching
1 parent 3525ebc commit b2f0998

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

packages/rstack/src/fmt/ignore.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { ResolvedFmtConfig } from './types.ts';
99
* Prettier already skips other generated lock files when it cannot infer a parser, so this list
1010
* contains only the additional defaults owned by `rs fmt`.
1111
*/
12-
const defaultIgnorePatterns = ['package-lock.json', 'pnpm-lock.yaml'];
12+
const defaultIgnoreNames = new Set(['package-lock.json', 'pnpm-lock.yaml']);
1313

1414
type IgnoreMatcher = (filePath: string, isDirectory?: boolean) => boolean;
1515

@@ -20,6 +20,21 @@ interface CreateIgnoreMatcherOptions {
2020
ignorePaths?: string[];
2121
}
2222

23+
const createDefaultIgnoreMatcher = (rootPath: string): IgnoreMatcher => {
24+
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
25+
26+
return (filePath) => {
27+
const relativePath = filePath.startsWith(rootPrefix)
28+
? filePath.slice(rootPrefix.length)
29+
: path.relative(rootPath, filePath);
30+
31+
return (
32+
relativePath !== '' &&
33+
relativePath.split(path.sep).some((segment) => defaultIgnoreNames.has(segment))
34+
);
35+
};
36+
};
37+
2338
const createPatternMatcher = (rootPath: string, patterns: string): IgnoreMatcher => {
2439
const matcher = createIgnore({ allowRelativePaths: true }).add(patterns);
2540
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
@@ -58,10 +73,12 @@ const createIgnoreMatcher = async ({
5873
cwd,
5974
ignorePaths = [],
6075
}: CreateIgnoreMatcherOptions): Promise<IgnoreMatcher> => {
61-
const configMatcher = createPatternMatcher(
62-
config.rootPath,
63-
[...defaultIgnorePatterns, ...config.ignorePatterns].join('\n'),
64-
);
76+
const configMatcher = config.ignorePatterns.length
77+
? createPatternMatcher(
78+
config.rootPath,
79+
[...defaultIgnoreNames, ...config.ignorePatterns].join('\n'),
80+
)
81+
: createDefaultIgnoreMatcher(config.rootPath);
6582
if (ignorePaths.length === 0) {
6683
return configMatcher;
6784
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ test('ignores common lock files by default and allows explicit negation', async
6464

6565
expect(isIgnored(path.join(rootPath, 'package-lock.json'))).toBe(true);
6666
expect(isIgnored(path.join(rootPath, 'packages/app/pnpm-lock.yaml'))).toBe(true);
67+
expect(isIgnored(path.join(rootPath, 'packages/app/PNPM-LOCK.YAML'))).toBe(false);
68+
expect(isIgnored(path.join(rootPath, 'package-lock.json/index.js'))).toBe(true);
69+
expect(isIgnored(path.join(rootPath, '../shared/pnpm-lock.yaml'))).toBe(true);
70+
expect(isIgnored(path.join(rootPath, 'pnpm-lock.yaml.backup'))).toBe(false);
6771
expect(isIgnoredAfterReinclude(path.join(rootPath, 'pnpm-lock.yaml'))).toBe(false);
6872
});
6973

0 commit comments

Comments
 (0)