Skip to content

Commit a00a199

Browse files
committed
Replace external ignore package with custom ignore utility
- in `CodeWebChat/apps/editor/src/utils` create a lean 'ignore' util that lets us remove dependency on 'ignore' package in `CodeWebChat/apps/editor/src/context/providers/workspace/workspace-provider.ts` └── apps └── editor └── src ├── context │ └── providers │ └── workspace │ └── workspace-provider.ts └── utils ├── diff-lines │ ├── diff-lines.spec.ts │ ├── diff-lines.ts │ └── index.ts └── ignore ├── ignore.spec.ts ├── ignore.ts └── index.ts
1 parent fa9724b commit a00a199

5 files changed

Lines changed: 413 additions & 8 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,6 @@
13961396
"dayjs": "1.11.10",
13971397
"dotenv": "16.4.5",
13981398
"glob": "14.0.4",
1399-
"ignore": "6.0.2",
14001399
"linkedom": "0.18.12",
14011400
"marked": "17.0.1",
14021401
"react": "18.2.0",

‎apps/editor/src/context/providers/workspace/workspace-provider.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
33
import * as vscode from 'vscode'
4-
import ignore, { Ignore } from 'ignore'
54
import { CONTEXT_SELECTED_PATHS_STATE_KEY } from '@/constants/state-keys'
65
import { IGNORED_LOCK_FILES } from '@/constants/ignored-lock-files'
76
import { natural_sort } from '@/utils/natural-sort'
@@ -10,6 +9,7 @@ import { display_token_count } from '@shared/utils/display-token-count'
109
import { TokenCalculator } from './modules/token-calculator'
1110
import { normalize_path } from '@/utils/normalize-path'
1211
import { RangesStorage } from '@/utils/global-workspace-storage'
12+
import { Ignore } from '@/utils/ignore'
1313

1414
export interface IWorkspaceProvider {
1515
get_workspace_roots(): string[]
@@ -50,9 +50,9 @@ export class WorkspaceProvider
5050
private _checked_items = new Map<string, vscode.TreeItemCheckboxState>()
5151
private _partially_selected_dirs = new Set<string>()
5252

53-
private _combined_gitignore = ignore()
54-
private _user_ignore_patterns: Ignore = ignore()
55-
private _user_allow_patterns: Ignore = ignore()
53+
private _combined_gitignore = new Ignore()
54+
private _user_ignore_patterns = new Ignore()
55+
private _user_allow_patterns = new Ignore()
5656
private _watcher: vscode.FileSystemWatcher
5757
private _ranges_watcher: vscode.FileSystemWatcher
5858
private _global_ranges_watcher: vscode.FileSystemWatcher
@@ -1361,7 +1361,7 @@ export class WorkspaceProvider
13611361
}
13621362
}
13631363

1364-
this._combined_gitignore = ignore()
1364+
this._combined_gitignore = new Ignore()
13651365

13661366
for (const gitignore_path of gitignore_files) {
13671367
const workspace_root = this.get_workspace_root_for_file(gitignore_path)
@@ -1434,7 +1434,7 @@ export class WorkspaceProvider
14341434
private _load_ignore_patterns() {
14351435
const config = vscode.workspace.getConfiguration('codeWebChat')
14361436
const patterns = config.get<string[]>('ignorePatterns')
1437-
this._user_ignore_patterns = ignore()
1437+
this._user_ignore_patterns = new Ignore()
14381438
if (patterns) {
14391439
this._user_ignore_patterns.add(patterns)
14401440
}
@@ -1446,7 +1446,7 @@ export class WorkspaceProvider
14461446
])
14471447

14481448
const allow_patterns = config.get<string[]>('allowPatterns')
1449-
this._user_allow_patterns = ignore()
1449+
this._user_allow_patterns = new Ignore()
14501450
if (allow_patterns) {
14511451
this._user_allow_patterns.add(allow_patterns)
14521452
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { Ignore } from './ignore'
2+
3+
const make = (...patterns: string[]) => new Ignore().add(patterns)
4+
5+
describe('Ignore', () => {
6+
it('should ignore nothing without rules', () => {
7+
expect(new Ignore().ignores('a/b.js')).toBe(false)
8+
})
9+
10+
it('should return false for an empty path', () => {
11+
expect(make('*').ignores('')).toBe(false)
12+
})
13+
14+
it('should skip blank lines and comments, and support escaped hash', () => {
15+
const ig = make('# comment', '', ' ', '\\#hash')
16+
expect(ig.ignores('# comment')).toBe(false)
17+
expect(ig.ignores('#hash')).toBe(true)
18+
})
19+
20+
it('should split multi-line strings', () => {
21+
const ig = new Ignore().add('a\r\nb\n#c\n')
22+
expect(ig.ignores('a')).toBe(true)
23+
expect(ig.ignores('b')).toBe(true)
24+
expect(ig.ignores('#c')).toBe(false)
25+
})
26+
27+
it('should match a basename pattern at any depth and below', () => {
28+
const ig = make('foo')
29+
expect(ig.ignores('foo')).toBe(true)
30+
expect(ig.ignores('a/foo')).toBe(true)
31+
expect(ig.ignores('a/b/foo')).toBe(true)
32+
expect(ig.ignores('foo/bar')).toBe(true)
33+
expect(ig.ignores('foobar')).toBe(false)
34+
})
35+
36+
it('should not let wildcards cross a slash', () => {
37+
expect(make('*.log').ignores('x/y.log')).toBe(true)
38+
expect(make('*.log').ignores('a.txt')).toBe(false)
39+
expect(make('a*c').ignores('a/c')).toBe(false)
40+
expect(make('a*c').ignores('abc')).toBe(true)
41+
})
42+
43+
it('should support question marks', () => {
44+
const ig = make('foo?bar')
45+
expect(ig.ignores('fooxbar')).toBe(true)
46+
expect(ig.ignores('foo/bar')).toBe(false)
47+
expect(ig.ignores('fooxxbar')).toBe(false)
48+
})
49+
50+
it('should anchor patterns with a leading or middle slash', () => {
51+
expect(make('/a.js').ignores('a.js')).toBe(true)
52+
expect(make('/a.js').ignores('b/a.js')).toBe(false)
53+
expect(make('doc/frotz').ignores('doc/frotz')).toBe(true)
54+
expect(make('doc/frotz').ignores('a/doc/frotz')).toBe(false)
55+
})
56+
57+
it('should match only directories for a trailing slash', () => {
58+
const ig = make('build/')
59+
expect(ig.ignores('build')).toBe(false)
60+
expect(ig.ignores('build/')).toBe(true)
61+
expect(ig.ignores('build/x.js')).toBe(true)
62+
expect(ig.ignores('src/build/')).toBe(true)
63+
})
64+
65+
it('should re-include with negation', () => {
66+
const ig = make('*.js', '!keep.js')
67+
expect(ig.ignores('keep.js')).toBe(false)
68+
expect(ig.ignores('other.js')).toBe(true)
69+
})
70+
71+
it('should let the last matching rule win', () => {
72+
expect(make('a', '!a', 'a').ignores('a')).toBe(true)
73+
})
74+
75+
it('should not re-include a file inside an ignored directory', () => {
76+
const ig = make('/abc/', '!/abc/a.js')
77+
expect(ig.ignores('abc/a.js')).toBe(true)
78+
})
79+
80+
it('should re-include a file when only its contents are ignored', () => {
81+
const ig = make('foo/*', '!foo/bar.js')
82+
expect(ig.ignores('foo/bar.js')).toBe(false)
83+
expect(ig.ignores('foo/baz.js')).toBe(true)
84+
expect(ig.ignores('foo/bar/x')).toBe(true)
85+
})
86+
87+
it('should support a leading globstar', () => {
88+
const ig = make('**/foo')
89+
expect(ig.ignores('foo')).toBe(true)
90+
expect(ig.ignores('a/b/foo')).toBe(true)
91+
expect(ig.ignores('a/b/foo/c')).toBe(true)
92+
})
93+
94+
it('should support a trailing globstar without matching the folder', () => {
95+
const ig = make('abc/**')
96+
expect(ig.ignores('abc')).toBe(false)
97+
expect(ig.ignores('abc/a')).toBe(true)
98+
expect(ig.ignores('abc/d/e')).toBe(true)
99+
})
100+
101+
it('should support a middle globstar', () => {
102+
const ig = make('a/**/b')
103+
expect(ig.ignores('a/b')).toBe(true)
104+
expect(ig.ignores('a/x/b')).toBe(true)
105+
expect(ig.ignores('a/x/y/b')).toBe(true)
106+
expect(ig.ignores('a/x/c')).toBe(false)
107+
expect(ig.ignores('b/a/b')).toBe(false)
108+
})
109+
110+
it('should support bracket expressions', () => {
111+
expect(make('*.[oa]').ignores('x.o')).toBe(true)
112+
expect(make('*.[oa]').ignores('x.c')).toBe(false)
113+
expect(make('file[0-9]').ignores('file5')).toBe(true)
114+
expect(make('file[0-9]').ignores('filea')).toBe(false)
115+
expect(make('[!a].txt').ignores('a.txt')).toBe(false)
116+
expect(make('[!a].txt').ignores('b.txt')).toBe(true)
117+
expect(make('a[/]b').ignores('a/b')).toBe(false)
118+
})
119+
120+
it('should match nothing for an unterminated bracket expression', () => {
121+
const ig = make('*.[a-')
122+
expect(ig.ignores('a.a')).toBe(false)
123+
expect(ig.ignores('a.-')).toBe(false)
124+
})
125+
126+
it('should treat an escaped star as a literal', () => {
127+
const ig = make('a\\*')
128+
expect(ig.ignores('a*')).toBe(true)
129+
expect(ig.ignores('ab')).toBe(false)
130+
})
131+
132+
it('should trim unescaped trailing spaces only', () => {
133+
expect(make('abc ').ignores('abc')).toBe(true)
134+
const ig = make('abc\\ ')
135+
expect(ig.ignores('abc ')).toBe(true)
136+
expect(ig.ignores('abc')).toBe(false)
137+
})
138+
139+
it('should ignore a trailing unpaired backslash', () => {
140+
expect(make('test\\').ignores('test')).toBe(false)
141+
})
142+
143+
it('should be case-insensitive by default and configurable', () => {
144+
expect(make('README').ignores('readme')).toBe(true)
145+
146+
const strict = new Ignore({ ignore_case: false }).add('README')
147+
expect(strict.ignores('readme')).toBe(false)
148+
expect(strict.ignores('README')).toBe(true)
149+
})
150+
151+
it('should invalidate its cache when rules are added', () => {
152+
const ig = new Ignore().add('a')
153+
expect(ig.ignores('a')).toBe(true)
154+
ig.add('!a')
155+
expect(ig.ignores('a')).toBe(false)
156+
})
157+
})

0 commit comments

Comments
 (0)