@@ -126,46 +126,27 @@ export const ignoreNewArrayOptionSchema: JSONSchema4 = {
126
126
} ;
127
127
128
128
/**
129
- * Should the given node be ignored?
129
+ * Recursive callback of `getNodeText`.
130
+ *
131
+ * This function not be called from anywhere else.
130
132
*/
131
- export function shouldIgnore (
132
- node : TSESTree . Node ,
133
- context : RuleContext < string , BaseOptions > ,
134
- options : Partial < IgnoreOptions >
135
- ) : boolean {
136
- return (
137
- // Ignore if in a function and ignoreLocal is set.
138
- ( Boolean ( options . ignoreLocal ) && inFunction ( node ) ) ||
139
- // Ignore if in a class and ignoreClass is set.
140
- ( Boolean ( options . ignoreClass ) && inClass ( node ) ) ||
141
- // Ignore if in an interface and ignoreInterface is set.
142
- ( Boolean ( options . ignoreInterface ) && inInterface ( node ) ) ||
143
- ( ( texts : ReadonlyArray < string > ) : boolean =>
144
- texts . length > 0
145
- ? // Ignore if ignorePattern is set and a pattern matches.
146
- ( options . ignorePattern !== undefined &&
147
- texts . every ( text =>
148
- isIgnoredPattern ( text , options . ignorePattern ! )
149
- ) ) ||
150
- // Ignore if ignoreAccessorPattern is set and an accessor pattern matches.
151
- ( options . ignoreAccessorPattern !== undefined &&
152
- texts . every ( text =>
153
- isIgnoredAccessorPattern ( text , options . ignoreAccessorPattern ! )
154
- ) )
155
- : false ) ( getNodeTexts ( node , context ) )
156
- ) ;
157
- }
158
-
159
- function getNodeTexts (
133
+ function _getNodeText (
160
134
node : TSESTree . Node ,
161
135
context : RuleContext < string , BaseOptions >
162
- ) : ReadonlyArray < string > {
163
- return ( isVariableDeclaration ( node )
164
- ? node . declarations . flatMap ( declarator => getNodeText ( declarator , context ) )
165
- : [ getNodeText ( node , context ) ]
166
- ) . filter ( name => name !== undefined ) as ReadonlyArray < string > ;
136
+ ) : string {
137
+ return isIdentifier ( node )
138
+ ? node . name
139
+ : isMemberExpression ( node )
140
+ ? `${ _getNodeText ( node . object , context ) } .${ _getNodeText (
141
+ node . property ,
142
+ context
143
+ ) } `
144
+ : context . getSourceCode ( ) . getText ( node ) ;
167
145
}
168
146
147
+ /**
148
+ * Get the text of the given node.
149
+ */
169
150
function getNodeText (
170
151
node : TSESTree . Node ,
171
152
context : RuleContext < string , BaseOptions >
@@ -183,21 +164,25 @@ function getNodeText(
183
164
: _getNodeText ( node , context ) ;
184
165
}
185
166
186
- function _getNodeText (
167
+ /**
168
+ * Get all the important bits of texts from the given node.
169
+ */
170
+ function getNodeTexts (
187
171
node : TSESTree . Node ,
188
172
context : RuleContext < string , BaseOptions >
189
- ) : string {
190
- return isIdentifier ( node )
191
- ? node . name
192
- : isMemberExpression ( node )
193
- ? `${ _getNodeText ( node . object , context ) } .${ _getNodeText (
194
- node . property ,
195
- context
196
- ) } `
197
- : context . getSourceCode ( ) . getText ( node ) ;
173
+ ) : ReadonlyArray < string > {
174
+ return ( isVariableDeclaration ( node )
175
+ ? node . declarations . flatMap ( declarator => getNodeText ( declarator , context ) )
176
+ : [ getNodeText ( node , context ) ]
177
+ ) . filter ( name => name !== undefined ) as ReadonlyArray < string > ;
198
178
}
199
179
200
- function isIgnoredPattern (
180
+ /**
181
+ * Should the given text be ignore?
182
+ *
183
+ * Test using the given pattern(s).
184
+ */
185
+ function shouldIgnoreViaPattern (
201
186
text : string ,
202
187
ignorePattern : ReadonlyArray < string > | string
203
188
) : boolean {
@@ -209,21 +194,14 @@ function isIgnoredPattern(
209
194
return patterns . some ( pattern => new RegExp ( pattern ) . test ( text ) ) ;
210
195
}
211
196
212
- function isIgnoredAccessorPattern (
213
- text : string ,
214
- ignorePattern : ReadonlyArray < string > | string
215
- ) : boolean {
216
- const patterns : ReadonlyArray < string > = Array . isArray ( ignorePattern )
217
- ? ignorePattern
218
- : [ ignorePattern ] ;
219
-
220
- // One or more patterns match?
221
- return patterns . some ( pattern =>
222
- findMatch ( pattern . split ( "." ) , text . split ( "." ) )
223
- ) ;
224
- }
225
-
226
- function findMatch (
197
+ /**
198
+ * Recursive callback of `shouldIgnoreViaAccessorPattern`.
199
+ *
200
+ * This function not be called from anywhere else.
201
+ *
202
+ * Does the given text match the given pattern.
203
+ */
204
+ function accessorPatternMatch (
227
205
[ pattern , ...remainingPatternParts ] : ReadonlyArray < string > ,
228
206
textParts : ReadonlyArray < string > ,
229
207
allowExtra : boolean = false
@@ -233,18 +211,84 @@ function findMatch(
233
211
: // Match any depth (including 0)?
234
212
pattern === "**"
235
213
? textParts . length === 0
236
- ? findMatch ( remainingPatternParts , [ ] , allowExtra )
214
+ ? accessorPatternMatch ( remainingPatternParts , [ ] , allowExtra )
237
215
: Array . from ( { length : textParts . length } )
238
216
. map ( ( _element , index ) => index )
239
217
. some ( offset =>
240
- findMatch ( remainingPatternParts , textParts . slice ( offset ) , true )
218
+ accessorPatternMatch (
219
+ remainingPatternParts ,
220
+ textParts . slice ( offset ) ,
221
+ true
222
+ )
241
223
)
242
224
: // Match anything?
243
225
pattern === "*"
244
226
? textParts . length > 0 &&
245
- findMatch ( remainingPatternParts , textParts . slice ( 1 ) , allowExtra )
227
+ accessorPatternMatch (
228
+ remainingPatternParts ,
229
+ textParts . slice ( 1 ) ,
230
+ allowExtra
231
+ )
246
232
: // Text matches pattern?
247
233
new RegExp ( "^" + escapeRegExp ( pattern ) . replace ( / \\ \* / g, ".*" ) + "$" ) . test (
248
234
textParts [ 0 ]
249
- ) && findMatch ( remainingPatternParts , textParts . slice ( 1 ) , allowExtra ) ;
235
+ ) &&
236
+ accessorPatternMatch (
237
+ remainingPatternParts ,
238
+ textParts . slice ( 1 ) ,
239
+ allowExtra
240
+ ) ;
241
+ }
242
+
243
+ /**
244
+ * Should the given text be ignore?
245
+ *
246
+ * Test using the given accessor pattern(s).
247
+ */
248
+ function shouldIgnoreViaAccessorPattern (
249
+ text : string ,
250
+ ignorePattern : ReadonlyArray < string > | string
251
+ ) : boolean {
252
+ const patterns : ReadonlyArray < string > = Array . isArray ( ignorePattern )
253
+ ? ignorePattern
254
+ : [ ignorePattern ] ;
255
+
256
+ // One or more patterns match?
257
+ return patterns . some ( pattern =>
258
+ accessorPatternMatch ( pattern . split ( "." ) , text . split ( "." ) )
259
+ ) ;
260
+ }
261
+
262
+ /**
263
+ * Should the given node be ignored?
264
+ */
265
+ export function shouldIgnore (
266
+ node : TSESTree . Node ,
267
+ context : RuleContext < string , BaseOptions > ,
268
+ options : Partial < IgnoreOptions >
269
+ ) : boolean {
270
+ return (
271
+ // Ignore if in a function and ignoreLocal is set.
272
+ ( Boolean ( options . ignoreLocal ) && inFunction ( node ) ) ||
273
+ // Ignore if in a class and ignoreClass is set.
274
+ ( Boolean ( options . ignoreClass ) && inClass ( node ) ) ||
275
+ // Ignore if in an interface and ignoreInterface is set.
276
+ ( Boolean ( options . ignoreInterface ) && inInterface ( node ) ) ||
277
+ ( ( texts : ReadonlyArray < string > ) : boolean =>
278
+ texts . length > 0
279
+ ? // Ignore if ignorePattern is set and a pattern matches.
280
+ ( options . ignorePattern !== undefined &&
281
+ texts . every ( text =>
282
+ shouldIgnoreViaPattern ( text , options . ignorePattern ! )
283
+ ) ) ||
284
+ // Ignore if ignoreAccessorPattern is set and an accessor pattern matches.
285
+ ( options . ignoreAccessorPattern !== undefined &&
286
+ texts . every ( text =>
287
+ shouldIgnoreViaAccessorPattern (
288
+ text ,
289
+ options . ignoreAccessorPattern !
290
+ )
291
+ ) )
292
+ : false ) ( getNodeTexts ( node , context ) )
293
+ ) ;
250
294
}
0 commit comments