Skip to content

Commit af69b59

Browse files
authored
fix(fmt): use ignore for pattern matching (#179)
1 parent afb7bbf commit af69b59

6 files changed

Lines changed: 19 additions & 58 deletions

File tree

packages/rstack/THIRD_PARTY_NOTICES.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5959
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
6060
SOFTWARE.
6161

62-
## fast-ignore
63-
64-
This package includes bundled code from [fast-ignore](https://github.com/fabiospampinato/fast-ignore).
65-
66-
License: MIT
67-
68-
The MIT License (MIT)
69-
70-
Copyright (c) 2023-present Fabio Spampinato
71-
72-
Permission is hereby granted, free of charge, to any person obtaining a
73-
copy of this software and associated documentation files (the "Software"),
74-
to deal in the Software without restriction, including without limitation
75-
the rights to use, copy, modify, merge, publish, distribute, sublicense,
76-
and/or sell copies of the Software, and to permit persons to whom the
77-
Software is furnished to do so, subject to the following conditions:
78-
79-
The above copyright notice and this permission notice shall be included in
80-
all copies or substantial portions of the Software.
81-
82-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
85-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
87-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
88-
SOFTWARE.
89-
9062
## fresh-import
9163

9264
This package includes bundled code from [fresh-import](https://github.com/sapphi-red/fresh-import).

packages/rstack/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"@rstest/adapter-rslib": "catalog:",
7070
"@types/micromatch": "catalog:",
7171
"@types/node": "catalog:",
72-
"fast-ignore": "catalog:",
7372
"ignore": "catalog:",
7473
"import-meta-resolve": "catalog:",
7574
"is-binary-path": "catalog:",

packages/rstack/src/fmt/ignore.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFile } from 'node:fs/promises';
22
import path from 'node:path';
3-
import fastIgnore from 'fast-ignore';
3+
import createIgnore from 'ignore';
44
import type { ResolvedFmtConfig } from './types.ts';
55

66
/**
@@ -21,10 +21,17 @@ interface CreateIgnoreMatcherOptions {
2121
}
2222

2323
const createPatternMatcher = (rootPath: string, patterns: string): IgnoreMatcher => {
24-
const matches = fastIgnore(patterns);
24+
const matcher = createIgnore({ allowRelativePaths: true }).add(patterns);
2525

26-
return (filePath, isDirectory = false) =>
27-
matches(path.relative(rootPath, filePath), { isDirectory });
26+
return (filePath, isDirectory = false) => {
27+
const relativePath = path.relative(rootPath, filePath);
28+
if (relativePath === '') {
29+
return false;
30+
}
31+
32+
const posixPath = path.sep === '\\' ? relativePath.replaceAll('\\', '/') : relativePath;
33+
return matcher.ignores(isDirectory ? `${posixPath}/` : posixPath);
34+
};
2835
};
2936

3037
const loadIgnoreMatcher = async (cwd: string, ignorePath: string): Promise<IgnoreMatcher> => {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ test('distinguishes directory-only patterns from files', async () => {
3838
expect(isIgnored(path.join(directoryPath, 'index.js'))).toBe(true);
3939
});
4040

41+
test('does not apply negated directory patterns to files', async () => {
42+
const isIgnored = await createMatcher(['fixtures/**/*', '!fixtures/**/']);
43+
const directoryPath = path.join(rootPath, 'fixtures/case');
44+
45+
expect(isIgnored(directoryPath, true)).toBe(false);
46+
expect(isIgnored(path.join(directoryPath, 'index.js'))).toBe(true);
47+
});
48+
4149
test('applies negated patterns in declaration order', async () => {
4250
const isIgnored = await createMatcher(['*.js', '!src/keep.js']);
4351
const isIgnoredAgain = await createMatcher(['*.js', '!src/keep.js', 'src/keep.js']);

pnpm-lock.yaml

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ catalog:
3535
'@types/react-dom': '^19.2.4'
3636
'@shikijs/transformers': '^4.3.1'
3737
'cspell-ban-words': '^0.0.4'
38-
'fast-ignore': '2.0.0'
3938
'happy-dom': '^20.11.1'
4039
'heading-case': '^1.1.4'
4140
ignore: 7.0.6

0 commit comments

Comments
 (0)