Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/rstack/src/fmt/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 defaultIgnorePatterns = ['package-lock.json', 'pnpm-lock.yaml'];
const defaultIgnoreNames = ['package-lock.json', 'pnpm-lock.yaml'];

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

Expand All @@ -20,6 +20,12 @@ interface CreateIgnoreMatcherOptions {
ignorePaths?: string[];
}

const createDefaultIgnoreMatcher = (): IgnoreMatcher => {
const suffixes = defaultIgnoreNames.map((name) => `${path.sep}${name}`);

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

const createPatternMatcher = (rootPath: string, patterns: string): IgnoreMatcher => {
const matcher = createIgnore({ allowRelativePaths: true }).add(patterns);
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
Expand Down Expand Up @@ -58,10 +64,12 @@ const createIgnoreMatcher = async ({
cwd,
ignorePaths = [],
}: CreateIgnoreMatcherOptions): Promise<IgnoreMatcher> => {
const configMatcher = createPatternMatcher(
config.rootPath,
[...defaultIgnorePatterns, ...config.ignorePatterns].join('\n'),
);
const configMatcher = config.ignorePatterns.length
? createPatternMatcher(
config.rootPath,
[...defaultIgnoreNames, ...config.ignorePatterns].join('\n'),
)
: createDefaultIgnoreMatcher();
if (ignorePaths.length === 0) {
return configMatcher;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/rstack/tests/fmt/ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ test('ignores common lock files by default and allows explicit negation', async

expect(isIgnored(path.join(rootPath, 'package-lock.json'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'packages/app/pnpm-lock.yaml'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'packages/app/PNPM-LOCK.YAML'))).toBe(false);
expect(isIgnored(path.join(rootPath, '../shared/pnpm-lock.yaml'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'pnpm-lock.yaml.backup'))).toBe(false);
expect(isIgnoredAfterReinclude(path.join(rootPath, 'pnpm-lock.yaml'))).toBe(false);
});

Expand Down