@@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/types';
2
2
import { createRule } from '../utils' ;
3
3
import { ReferenceTracker } from '@eslint-community/eslint-utils' ;
4
4
import { getSourceCode } from '../utils/compat' ;
5
+ import { findVariable } from '../utils/ast-utils' ;
5
6
import type { RuleContext } from '../types' ;
6
7
7
8
export default createRule ( 'no-goto-without-base' , {
@@ -24,7 +25,7 @@ export default createRule('no-goto-without-base', {
24
25
const referenceTracker = new ReferenceTracker (
25
26
getSourceCode ( context ) . scopeManager . globalScope !
26
27
) ;
27
- const basePathNames = extractBasePathNames ( referenceTracker ) ;
28
+ const basePathNames = extractBasePathReferences ( referenceTracker , context ) ;
28
29
for ( const gotoCall of extractGotoReferences ( referenceTracker ) ) {
29
30
if ( gotoCall . arguments . length < 1 ) {
30
31
continue ;
@@ -52,20 +53,20 @@ export default createRule('no-goto-without-base', {
52
53
function checkBinaryExpression (
53
54
context : RuleContext ,
54
55
path : TSESTree . BinaryExpression ,
55
- basePathNames : string [ ]
56
+ basePathNames : Set < TSESTree . Identifier >
56
57
) : void {
57
- if ( path . left . type !== 'Identifier' || ! basePathNames . includes ( path . left . name ) ) {
58
+ if ( path . left . type !== 'Identifier' || ! basePathNames . has ( path . left ) ) {
58
59
context . report ( { loc : path . loc , messageId : 'isNotPrefixedWithBasePath' } ) ;
59
60
}
60
61
}
61
62
62
63
function checkTemplateLiteral (
63
64
context : RuleContext ,
64
65
path : TSESTree . TemplateLiteral ,
65
- basePathNames : string [ ]
66
+ basePathNames : Set < TSESTree . Identifier >
66
67
) : void {
67
68
const startingIdentifier = extractStartingIdentifier ( path ) ;
68
- if ( startingIdentifier === undefined || ! basePathNames . includes ( startingIdentifier . name ) ) {
69
+ if ( startingIdentifier === undefined || ! basePathNames . has ( startingIdentifier ) ) {
69
70
context . report ( { loc : path . loc , messageId : 'isNotPrefixedWithBasePath' } ) ;
70
71
}
71
72
}
@@ -110,16 +111,24 @@ function extractGotoReferences(referenceTracker: ReferenceTracker): TSESTree.Cal
110
111
) ;
111
112
}
112
113
113
- function extractBasePathNames ( referenceTracker : ReferenceTracker ) : string [ ] {
114
- return Array . from (
115
- referenceTracker . iterateEsmReferences ( {
116
- '$app/paths' : {
117
- [ ReferenceTracker . ESM ] : true ,
118
- base : {
119
- [ ReferenceTracker . READ ] : true
120
- }
114
+ function extractBasePathReferences (
115
+ referenceTracker : ReferenceTracker ,
116
+ context : RuleContext
117
+ ) : Set < TSESTree . Identifier > {
118
+ const set = new Set < TSESTree . Identifier > ( ) ;
119
+ for ( const { node } of referenceTracker . iterateEsmReferences ( {
120
+ '$app/paths' : {
121
+ [ ReferenceTracker . ESM ] : true ,
122
+ base : {
123
+ [ ReferenceTracker . READ ] : true
121
124
}
122
- } ) ,
123
- ( { node } ) => node . local . name
124
- ) ;
125
+ }
126
+ } ) ) {
127
+ const variable = findVariable ( context , ( node as TSESTree . ImportSpecifier ) . local ) ;
128
+ if ( ! variable ) continue ;
129
+ for ( const reference of variable . references ) {
130
+ if ( reference . identifier . type === 'Identifier' ) set . add ( reference . identifier ) ;
131
+ }
132
+ }
133
+ return set ;
125
134
}
0 commit comments