Skip to content

Commit 9a07b52

Browse files
committed
feat(no-goto-without-base): checking references
1 parent 3c6d843 commit 9a07b52

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/rules/no-goto-without-base.ts

+25-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/types';
22
import { createRule } from '../utils';
33
import { ReferenceTracker } from '@eslint-community/eslint-utils';
44
import { getSourceCode } from '../utils/compat';
5+
import { findVariable } from '../utils/ast-utils';
56
import type { RuleContext } from '../types';
67

78
export default createRule('no-goto-without-base', {
@@ -24,7 +25,7 @@ export default createRule('no-goto-without-base', {
2425
const referenceTracker = new ReferenceTracker(
2526
getSourceCode(context).scopeManager.globalScope!
2627
);
27-
const basePathNames = extractBasePathNames(referenceTracker);
28+
const basePathNames = extractBasePathReferences(referenceTracker, context);
2829
for (const gotoCall of extractGotoReferences(referenceTracker)) {
2930
if (gotoCall.arguments.length < 1) {
3031
continue;
@@ -52,20 +53,20 @@ export default createRule('no-goto-without-base', {
5253
function checkBinaryExpression(
5354
context: RuleContext,
5455
path: TSESTree.BinaryExpression,
55-
basePathNames: string[]
56+
basePathNames: Set<TSESTree.Identifier>
5657
): void {
57-
if (path.left.type !== 'Identifier' || !basePathNames.includes(path.left.name)) {
58+
if (path.left.type !== 'Identifier' || !basePathNames.has(path.left)) {
5859
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
5960
}
6061
}
6162

6263
function checkTemplateLiteral(
6364
context: RuleContext,
6465
path: TSESTree.TemplateLiteral,
65-
basePathNames: string[]
66+
basePathNames: Set<TSESTree.Identifier>
6667
): void {
6768
const startingIdentifier = extractStartingIdentifier(path);
68-
if (startingIdentifier === undefined || !basePathNames.includes(startingIdentifier.name)) {
69+
if (startingIdentifier === undefined || !basePathNames.has(startingIdentifier)) {
6970
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
7071
}
7172
}
@@ -110,16 +111,24 @@ function extractGotoReferences(referenceTracker: ReferenceTracker): TSESTree.Cal
110111
);
111112
}
112113

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
121124
}
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;
125134
}

0 commit comments

Comments
 (0)