@@ -3,6 +3,7 @@ import { createRule } from '../utils/index.js';
3
3
import { ReferenceTracker } from '@eslint-community/eslint-utils' ;
4
4
import { getSourceCode } from '../utils/compat.js' ;
5
5
import { findVariable } from '../utils/ast-utils.js' ;
6
+ import { extractExpressionPrefixVariable } from '../utils/expression-affixes.js' ;
6
7
import type { RuleContext } from '../types.js' ;
7
8
import type { SvelteLiteral } from 'svelte-eslint-parser/lib/ast' ;
8
9
@@ -224,87 +225,8 @@ function expressionStartsWithBase(
224
225
url : TSESTree . Expression ,
225
226
basePathNames : Set < TSESTree . Identifier >
226
227
) : boolean {
227
- switch ( url . type ) {
228
- case 'BinaryExpression' :
229
- return binaryExpressionStartsWithBase ( context , url , basePathNames ) ;
230
- case 'Identifier' :
231
- return variableStartsWithBase ( context , url , basePathNames ) ;
232
- case 'MemberExpression' :
233
- return memberExpressionStartsWithBase ( url , basePathNames ) ;
234
- case 'TemplateLiteral' :
235
- return templateLiteralStartsWithBase ( context , url , basePathNames ) ;
236
- default :
237
- return false ;
238
- }
239
- }
240
-
241
- function binaryExpressionStartsWithBase (
242
- context : RuleContext ,
243
- url : TSESTree . BinaryExpression ,
244
- basePathNames : Set < TSESTree . Identifier >
245
- ) : boolean {
246
- return (
247
- url . left . type !== 'PrivateIdentifier' &&
248
- expressionStartsWithBase ( context , url . left , basePathNames )
249
- ) ;
250
- }
251
-
252
- function memberExpressionStartsWithBase (
253
- url : TSESTree . MemberExpression ,
254
- basePathNames : Set < TSESTree . Identifier >
255
- ) : boolean {
256
- return url . property . type === 'Identifier' && basePathNames . has ( url . property ) ;
257
- }
258
-
259
- function variableStartsWithBase (
260
- context : RuleContext ,
261
- url : TSESTree . Identifier ,
262
- basePathNames : Set < TSESTree . Identifier >
263
- ) : boolean {
264
- if ( basePathNames . has ( url ) ) {
265
- return true ;
266
- }
267
- const variable = findVariable ( context , url ) ;
268
- if (
269
- variable === null ||
270
- variable . identifiers . length !== 1 ||
271
- variable . identifiers [ 0 ] . parent . type !== 'VariableDeclarator' ||
272
- variable . identifiers [ 0 ] . parent . init === null
273
- ) {
274
- return false ;
275
- }
276
- return expressionStartsWithBase ( context , variable . identifiers [ 0 ] . parent . init , basePathNames ) ;
277
- }
278
-
279
- function templateLiteralStartsWithBase (
280
- context : RuleContext ,
281
- url : TSESTree . TemplateLiteral ,
282
- basePathNames : Set < TSESTree . Identifier >
283
- ) : boolean {
284
- const startingIdentifier = extractLiteralStartingExpression ( url ) ;
285
- return (
286
- startingIdentifier !== undefined &&
287
- expressionStartsWithBase ( context , startingIdentifier , basePathNames )
288
- ) ;
289
- }
290
-
291
- function extractLiteralStartingExpression (
292
- templateLiteral : TSESTree . TemplateLiteral
293
- ) : TSESTree . Expression | undefined {
294
- const literalParts = [ ...templateLiteral . expressions , ...templateLiteral . quasis ] . sort ( ( a , b ) =>
295
- a . range [ 0 ] < b . range [ 0 ] ? - 1 : 1
296
- ) ;
297
- for ( const part of literalParts ) {
298
- if ( part . type === 'TemplateElement' && part . value . raw === '' ) {
299
- // Skip empty quasi in the begining
300
- continue ;
301
- }
302
- if ( part . type !== 'TemplateElement' ) {
303
- return part ;
304
- }
305
- return undefined ;
306
- }
307
- return undefined ;
228
+ const prefixVariable = extractExpressionPrefixVariable ( context , url ) ;
229
+ return prefixVariable !== null && basePathNames . has ( prefixVariable ) ;
308
230
}
309
231
310
232
function expressionIsEmpty ( url : TSESTree . Expression ) : boolean {
0 commit comments