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
28 changes: 28 additions & 0 deletions packages/rstack/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

This package includes software developed by third parties.

## fast-ignore

This package includes bundled code from [fast-ignore](https://github.com/fabiospampinato/fast-ignore).

License: MIT

The MIT License (MIT)

Copyright (c) 2023-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## fresh-import

This package includes bundled code from [fresh-import](https://github.com/sapphi-red/fresh-import).
Expand Down
1 change: 1 addition & 0 deletions packages/rstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@rstest/adapter-rslib": "catalog:",
"@types/micromatch": "catalog:",
"@types/node": "catalog:",
"fast-ignore": "catalog:",
"lint-staged": "catalog:",
"micromatch": "catalog:",
"rslog": "catalog:",
Expand Down
12 changes: 12 additions & 0 deletions packages/rstack/src/fmt/ignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { relative } from 'node:path';
import fastIgnore from 'fast-ignore';
import type { ResolvedFmtConfig } from './types.ts';

/** Creates a reusable matcher for config-level ignore patterns. */
const createFmtIgnoreMatcher = (config: ResolvedFmtConfig): ((filePath: string) => boolean) => {
const matches = fastIgnore(config.ignorePatterns.join('\n'));

return (filePath) => matches(relative(config.rootPath, filePath));
Comment thread
chenjiahan marked this conversation as resolved.
};

export { createFmtIgnoreMatcher };
52 changes: 52 additions & 0 deletions packages/rstack/tests/fmt/ignore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import path from 'node:path';
import { expect, test } from 'rstack/test';
import { normalizeFmtConfig } from '../../src/fmt/config.ts';
import { createFmtIgnoreMatcher } from '../../src/fmt/ignore.ts';

const rootPath = path.join(import.meta.dirname, 'project');

const createMatcher = (ignorePatterns: string[]) =>
createFmtIgnoreMatcher(normalizeFmtConfig({ ignorePatterns }, rootPath));

test('matches gitignore patterns relative to the config root', () => {
const isIgnored = createMatcher(['dist/', '*.snap', '/root.js', '# comment', '\\#generated.js']);

expect(isIgnored(path.join(rootPath, 'dist/index.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'src/data.snap'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'root.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'nested/root.js'))).toBe(false);
expect(isIgnored(path.join(rootPath, '#generated.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'src/index.js'))).toBe(false);
});

test('applies negated patterns in declaration order', () => {
const isIgnored = createMatcher(['*.js', '!src/keep.js']);
const isIgnoredAgain = createMatcher(['*.js', '!src/keep.js', 'src/keep.js']);
const isReincluded = createMatcher(['dist', '!dist']);
const filePath = path.join(rootPath, 'src/keep.js');

expect(isIgnored(filePath)).toBe(false);
expect(isIgnored(path.join(rootPath, 'src/drop.js'))).toBe(true);
expect(isIgnoredAgain(filePath)).toBe(true);
expect(isReincluded(path.join(rootPath, 'dist'))).toBe(false);
});

test('does not let explicit files bypass ignore patterns', () => {
const isIgnored = createMatcher(['generated/']);
const explicitFilePath = path.join(rootPath, 'generated/output.js');

expect(isIgnored(explicitFilePath)).toBe(true);
});

test('matches parent directory patterns without validation', () => {
const isIgnored = createMatcher(['../shared/*.js']);

expect(isIgnored(path.join(rootPath, '../shared/index.js'))).toBe(true);
expect(isIgnored(path.join(rootPath, 'shared/index.js'))).toBe(false);
});

test('does not ignore files when no patterns are configured', () => {
const isIgnored = createMatcher([]);

expect(isIgnored(path.join(rootPath, 'src/index.js'))).toBe(false);
});
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ catalog:
'@types/react-dom': '^19.2.3'
'@shikijs/transformers': '^4.3.1'
'cspell-ban-words': '^0.0.4'
'fast-ignore': '2.0.0'
'happy-dom': '^20.11.1'
'heading-case': '^1.1.4'
'lint-staged': '^17.2.0'
Expand Down