Skip to content

Commit cbd84c4

Browse files
committed
Pattern: make patternList and globList fields private
1 parent 5af05e7 commit cbd84c4

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

src/pattern.ts

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// this is just a very light wrapper around 2 arrays with an offset index
22

33
import { GLOBSTAR } from 'minimatch'
4-
import { Path } from 'path-scurry'
54
type MMRegExp = RegExp & {
65
_glob?: string
76
_src?: string
@@ -26,11 +25,11 @@ const isPatternList = (pl: MMPattern[]): pl is PatternList =>
2625
const isGlobList = (gl: string[]): gl is GlobList => gl.length >= 1
2726

2827
export class Pattern {
29-
readonly patternList: PatternList
30-
readonly globList: GlobList
28+
readonly #patternList: PatternList
29+
readonly #globList: GlobList
3130
readonly #index: number
3231
readonly length: number
33-
#platform: NodeJS.Platform
32+
readonly #platform: NodeJS.Platform
3433
#rest?: Pattern | null
3534
#globString?: string
3635
#isDrive?: boolean
@@ -58,19 +57,19 @@ export class Pattern {
5857
if (index >= this.length) {
5958
throw new TypeError('index out of range')
6059
}
61-
this.patternList = patternList
62-
this.globList = globList
60+
this.#patternList = patternList
61+
this.#globList = globList
6362
this.#index = index
6463
this.#globstarFollowed = globstarFollowed
6564
this.#platform = platform
6665

6766
// if the current item is not globstar, and the next item is .., skip ahead
6867
if (
69-
this.patternList[this.#index] !== GLOBSTAR &&
70-
this.patternList[this.#index] !== '..' &&
71-
this.patternList[this.#index] !== '.' &&
72-
this.patternList[this.#index] !== '' &&
73-
this.patternList[this.#index + 1] === '..' &&
68+
this.#patternList[this.#index] !== GLOBSTAR &&
69+
this.#patternList[this.#index] !== '..' &&
70+
this.#patternList[this.#index] !== '.' &&
71+
this.#patternList[this.#index] !== '' &&
72+
this.#patternList[this.#index + 1] === '..' &&
7473
this.length > this.#index + 2
7574
) {
7675
this.#index += 2
@@ -87,31 +86,31 @@ export class Pattern {
8786
// /etc => ['/', 'etc']
8887
// / => ['/']
8988
if (this.isUNC()) {
90-
const [p1, p2, p3, ...prest] = this.patternList
91-
const [g1, g2, g3, ...grest] = this.globList
89+
const [p1, p2, p3, ...prest] = this.#patternList
90+
const [g1, g2, g3, ...grest] = this.#globList
9291
if (prest[0] === '') {
9392
// ends in /
9493
prest.shift()
9594
grest.shift()
9695
}
9796
const p = [p1, p2, p3, ''].join('/')
9897
const g = [g1, g2, g3, ''].join('/')
99-
this.patternList = [p, ...prest]
100-
this.globList = [g, ...grest]
101-
this.length = this.patternList.length
98+
this.#patternList = [p, ...prest]
99+
this.#globList = [g, ...grest]
100+
this.length = this.#patternList.length
102101
} else if (this.isDrive() || this.isAbsolute()) {
103-
const [p1, ...prest] = this.patternList
104-
const [g1, ...grest] = this.globList
102+
const [p1, ...prest] = this.#patternList
103+
const [g1, ...grest] = this.#globList
105104
if (prest[0] === '') {
106105
// ends in /
107106
prest.shift()
108107
grest.shift()
109108
}
110109
const p = (p1 as string) + '/'
111110
const g = g1 + '/'
112-
this.patternList = [p, ...prest]
113-
this.globList = [g, ...grest]
114-
this.length = this.patternList.length
111+
this.#patternList = [p, ...prest]
112+
this.#globList = [g, ...grest]
113+
this.length = this.#patternList.length
115114
}
116115
} else {
117116
// discard any empty path portions, except the last one.
@@ -122,34 +121,34 @@ export class Pattern {
122121
}
123122

124123
pattern(): MMPattern {
125-
return this.patternList[this.#index]
124+
return this.#patternList[this.#index]
126125
}
127126

128127
isString(): boolean {
129-
return typeof this.patternList[this.#index] === 'string'
128+
return typeof this.#patternList[this.#index] === 'string'
130129
}
131130
isGlobstar(): boolean {
132-
return this.patternList[this.#index] === GLOBSTAR
131+
return this.#patternList[this.#index] === GLOBSTAR
133132
}
134133
isGlobstarDotDot(): boolean {
135-
return this.isGlobstar() && this.globList[this.#index + 1] === '..'
134+
return this.isGlobstar() && this.#globList[this.#index + 1] === '..'
136135
}
137136
isRegExp(): boolean {
138-
return this.patternList[this.#index] instanceof RegExp
137+
return this.#patternList[this.#index] instanceof RegExp
139138
}
140139

141140
glob(): string {
142-
return this.globList[this.#index]
141+
return this.#globList[this.#index]
143142
}
144143

145144
globString(): string {
146145
return (this.#globString =
147146
this.#globString ||
148147
(this.#index === 0
149148
? this.isAbsolute()
150-
? this.globList[0] + this.globList.slice(1).join('/')
151-
: this.globList.join('/')
152-
: this.globList.slice(this.#index).join('/')))
149+
? this.#globList[0] + this.#globList.slice(1).join('/')
150+
: this.#globList.join('/')
151+
: this.#globList.slice(this.#index).join('/')))
153152
}
154153

155154
hasMore(): boolean {
@@ -160,8 +159,8 @@ export class Pattern {
160159
if (this.#rest !== undefined) return this.#rest
161160
if (!this.hasMore()) return (this.#rest = null)
162161
this.#rest = new Pattern(
163-
this.patternList,
164-
this.globList,
162+
this.#patternList,
163+
this.#globList,
165164
this.#index + 1,
166165
this.#platform,
167166
this.#globstarFollowed
@@ -189,7 +188,7 @@ export class Pattern {
189188

190189
// pattern like: //host/share/...
191190
// split = [ '', '', 'host', 'share', ... ]
192-
isUNC(pl = this.patternList): pl is UNCPatternList {
191+
isUNC(pl = this.#patternList): pl is UNCPatternList {
193192
return this.#isUNC !== undefined
194193
? this.#isUNC
195194
: (this.#isUNC =
@@ -208,7 +207,7 @@ export class Pattern {
208207
// XXX: would be nice to handle patterns like `c:*` to test the cwd
209208
// in c: for *, but I don't know of a way to even figure out what that
210209
// cwd is without actually chdir'ing into it?
211-
isDrive(pl = this.patternList): pl is DrivePatternList {
210+
isDrive(pl = this.#patternList): pl is DrivePatternList {
212211
return this.#isDrive !== undefined
213212
? this.#isDrive
214213
: (this.#isDrive =
@@ -222,7 +221,7 @@ export class Pattern {
222221
// pattern = '/' or '/...' or '/x/...'
223222
// split = ['', ''] or ['', ...] or ['', 'x', ...]
224223
// Drive and UNC both considered absolute on windows
225-
isAbsolute(pl = this.patternList): pl is AbsolutePatternList {
224+
isAbsolute(pl = this.#patternList): pl is AbsolutePatternList {
226225
return this.#isAbsolute !== undefined
227226
? this.#isAbsolute
228227
: (this.#isAbsolute =
@@ -233,9 +232,18 @@ export class Pattern {
233232

234233
// consume the root of the pattern, and return it
235234
root(): string {
236-
const p = this.patternList[0]
235+
const p = this.#patternList[0]
237236
return typeof p === 'string' && this.isAbsolute() && this.#index === 0
238237
? p
239238
: ''
240239
}
240+
241+
hasMagic(): boolean {
242+
for (let i = 0; i < this.length; i++) {
243+
if (typeof this.#patternList[i] !== 'string') {
244+
return true
245+
}
246+
}
247+
return false
248+
}
241249
}

0 commit comments

Comments
 (0)