From 0164d0addba29f62a0fdc21ee1a0de30077f6508 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 20 Oct 2026 16:19:27 +0800 Subject: [PATCH] refactor(fmt): use native matcher for default ignore rules --- packages/rstack/src/fmt/ignore.ts | 52 ++++++--------------- packages/rstack/tests/fmt/discovery.test.ts | 2 +- packages/rstack/tests/fmt/ignore.test.ts | 2 +- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/packages/rstack/src/fmt/ignore.ts b/packages/rstack/src/fmt/ignore.ts index e1959c00..f1f40c0d 100644 --- a/packages/rstack/src/fmt/ignore.ts +++ b/packages/rstack/src/fmt/ignore.ts @@ -13,7 +13,7 @@ import type { ResolvedFmtConfig } from './types.ts'; * Prettier already skips other generated lock files when it cannot infer a parser, so this list * contains only the additional defaults owned by `rs fmt`. */ -const defaultIgnoreNames = [ +const defaultIgnorePatterns = [ 'package-lock.json', 'pnpm-lock.yaml', 'skills-lock.json', @@ -44,20 +44,6 @@ interface CreateIgnoreMatcherOptions { precheck?: IgnorePredicate; } -const createDefaultMatcher = (): IgnoreMatcher => { - const suffixes = defaultIgnoreNames.map((name) => `${path.sep}${name}`); - - return (filePath) => suffixes.some((suffix) => filePath.endsWith(suffix)); -}; - -const combineIgnorePredicates = ( - first: IgnorePredicate, - second: IgnorePredicate, -): IgnorePredicate => { - return (filePath, isDirectory = false) => - first(filePath, isDirectory) || second(filePath, isDirectory); -}; - const createSourceMatcher = ( sources: IgnoreSource[], precheck?: IgnorePredicate, @@ -107,30 +93,18 @@ const createIgnoreMatcher = async ({ const ignoreFileSources = await Promise.all( ignorePaths.map((ignorePath) => loadIgnoreSource(cwd, ignorePath)), ); - if (config.ignorePatterns.length) { - return createSourceMatcher( - [ - { - rootPath: config.rootPath, - patterns: [...defaultIgnoreNames, ...config.ignorePatterns].join( - '\n', - ), - }, - ...ignoreFileSources, - ], - precheck, - ); - } - - const defaultMatcher = createDefaultMatcher(); - const matcher = precheck - ? combineIgnorePredicates(precheck, defaultMatcher) - : defaultMatcher; - if (ignoreFileSources.length === 0) { - return matcher; - } - - return createSourceMatcher(ignoreFileSources, matcher); + return createSourceMatcher( + [ + { + rootPath: config.rootPath, + patterns: [...defaultIgnorePatterns, ...config.ignorePatterns].join( + '\n', + ), + }, + ...ignoreFileSources, + ], + precheck, + ); }; export { createIgnoreMatcher }; diff --git a/packages/rstack/tests/fmt/discovery.test.ts b/packages/rstack/tests/fmt/discovery.test.ts index e55635e2..eeb9b44d 100644 --- a/packages/rstack/tests/fmt/discovery.test.ts +++ b/packages/rstack/tests/fmt/discovery.test.ts @@ -125,7 +125,7 @@ test('excludes .rstack from discovery', async () => { }); const customCacheCases: { config?: FmtConfig; name: string }[] = [ - { name: 'with scalar matching' }, + { name: 'without configured ignore patterns' }, { name: 'before native matching', config: { ignorePatterns: ['generated/'] }, diff --git a/packages/rstack/tests/fmt/ignore.test.ts b/packages/rstack/tests/fmt/ignore.test.ts index b8aa2126..d9fc620b 100644 --- a/packages/rstack/tests/fmt/ignore.test.ts +++ b/packages/rstack/tests/fmt/ignore.test.ts @@ -78,7 +78,7 @@ test('ignores common lock files by default and allows explicit negation', async true, ); expect(isIgnored(path.join(rootPath, 'packages/app/PNPM-LOCK.YAML'))).toBe( - false, + true, ); expect(isIgnored(path.join(rootPath, '../shared/pnpm-lock.yaml'))).toBe(true); expect(isIgnored(path.join(rootPath, 'pnpm-lock.yaml.backup'))).toBe(false);