Skip to content

Commit d56fe77

Browse files
authored
refactor(fmt): use native matcher for default ignore rules (#585)
1 parent 6ea87b7 commit d56fe77

3 files changed

Lines changed: 15 additions & 41 deletions

File tree

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

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { ResolvedFmtConfig } from './types.ts';
1313
* Prettier already skips other generated lock files when it cannot infer a parser, so this list
1414
* contains only the additional defaults owned by `rs fmt`.
1515
*/
16-
const defaultIgnoreNames = [
16+
const defaultIgnorePatterns = [
1717
'package-lock.json',
1818
'pnpm-lock.yaml',
1919
'skills-lock.json',
@@ -44,20 +44,6 @@ interface CreateIgnoreMatcherOptions {
4444
precheck?: IgnorePredicate;
4545
}
4646

47-
const createDefaultMatcher = (): IgnoreMatcher => {
48-
const suffixes = defaultIgnoreNames.map((name) => `${path.sep}${name}`);
49-
50-
return (filePath) => suffixes.some((suffix) => filePath.endsWith(suffix));
51-
};
52-
53-
const combineIgnorePredicates = (
54-
first: IgnorePredicate,
55-
second: IgnorePredicate,
56-
): IgnorePredicate => {
57-
return (filePath, isDirectory = false) =>
58-
first(filePath, isDirectory) || second(filePath, isDirectory);
59-
};
60-
6147
const createSourceMatcher = (
6248
sources: IgnoreSource[],
6349
precheck?: IgnorePredicate,
@@ -107,30 +93,18 @@ const createIgnoreMatcher = async ({
10793
const ignoreFileSources = await Promise.all(
10894
ignorePaths.map((ignorePath) => loadIgnoreSource(cwd, ignorePath)),
10995
);
110-
if (config.ignorePatterns.length) {
111-
return createSourceMatcher(
112-
[
113-
{
114-
rootPath: config.rootPath,
115-
patterns: [...defaultIgnoreNames, ...config.ignorePatterns].join(
116-
'\n',
117-
),
118-
},
119-
...ignoreFileSources,
120-
],
121-
precheck,
122-
);
123-
}
124-
125-
const defaultMatcher = createDefaultMatcher();
126-
const matcher = precheck
127-
? combineIgnorePredicates(precheck, defaultMatcher)
128-
: defaultMatcher;
129-
if (ignoreFileSources.length === 0) {
130-
return matcher;
131-
}
132-
133-
return createSourceMatcher(ignoreFileSources, matcher);
96+
return createSourceMatcher(
97+
[
98+
{
99+
rootPath: config.rootPath,
100+
patterns: [...defaultIgnorePatterns, ...config.ignorePatterns].join(
101+
'\n',
102+
),
103+
},
104+
...ignoreFileSources,
105+
],
106+
precheck,
107+
);
134108
};
135109

136110
export { createIgnoreMatcher };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ test('excludes .rstack from discovery', async () => {
125125
});
126126

127127
const customCacheCases: { config?: FmtConfig; name: string }[] = [
128-
{ name: 'with scalar matching' },
128+
{ name: 'without configured ignore patterns' },
129129
{
130130
name: 'before native matching',
131131
config: { ignorePatterns: ['generated/'] },

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ test('ignores common lock files by default and allows explicit negation', async
7878
true,
7979
);
8080
expect(isIgnored(path.join(rootPath, 'packages/app/PNPM-LOCK.YAML'))).toBe(
81-
false,
81+
true,
8282
);
8383
expect(isIgnored(path.join(rootPath, '../shared/pnpm-lock.yaml'))).toBe(true);
8484
expect(isIgnored(path.join(rootPath, 'pnpm-lock.yaml.backup'))).toBe(false);

0 commit comments

Comments
 (0)