|
| 1 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 2 | +import { createRule } from '../utils/index.js'; |
| 3 | +import { ReferenceTracker } from '@eslint-community/eslint-utils'; |
| 4 | +import { getSourceCode } from '../utils/compat.js'; |
| 5 | +import { findVariable } from '../utils/ast-utils.js'; |
| 6 | +import type { RuleContext } from '../types.js'; |
| 7 | + |
| 8 | +export default createRule('no-navigation-without-base', { |
| 9 | + meta: { |
| 10 | + docs: { |
| 11 | + description: 'disallow using goto() without the base path', |
| 12 | + category: 'SvelteKit', |
| 13 | + recommended: false |
| 14 | + }, |
| 15 | + schema: [], |
| 16 | + messages: { |
| 17 | + isNotPrefixedWithBasePath: |
| 18 | + "Found a goto() call with a url that isn't prefixed with the base path." |
| 19 | + }, |
| 20 | + type: 'suggestion' |
| 21 | + }, |
| 22 | + create(context) { |
| 23 | + return { |
| 24 | + Program() { |
| 25 | + const referenceTracker = new ReferenceTracker( |
| 26 | + getSourceCode(context).scopeManager.globalScope! |
| 27 | + ); |
| 28 | + const basePathNames = extractBasePathReferences(referenceTracker, context); |
| 29 | + for (const gotoCall of extractGotoReferences(referenceTracker)) { |
| 30 | + if (gotoCall.arguments.length < 1) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + const path = gotoCall.arguments[0]; |
| 34 | + switch (path.type) { |
| 35 | + case 'BinaryExpression': |
| 36 | + checkBinaryExpression(context, path, basePathNames); |
| 37 | + break; |
| 38 | + case 'Literal': |
| 39 | + checkLiteral(context, path); |
| 40 | + break; |
| 41 | + case 'TemplateLiteral': |
| 42 | + checkTemplateLiteral(context, path, basePathNames); |
| 43 | + break; |
| 44 | + default: |
| 45 | + context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' }); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | + } |
| 51 | +}); |
| 52 | + |
| 53 | +function checkBinaryExpression( |
| 54 | + context: RuleContext, |
| 55 | + path: TSESTree.BinaryExpression, |
| 56 | + basePathNames: Set<TSESTree.Identifier> |
| 57 | +): void { |
| 58 | + if (path.left.type !== 'Identifier' || !basePathNames.has(path.left)) { |
| 59 | + context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' }); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function checkTemplateLiteral( |
| 64 | + context: RuleContext, |
| 65 | + path: TSESTree.TemplateLiteral, |
| 66 | + basePathNames: Set<TSESTree.Identifier> |
| 67 | +): void { |
| 68 | + const startingIdentifier = extractStartingIdentifier(path); |
| 69 | + if (startingIdentifier === undefined || !basePathNames.has(startingIdentifier)) { |
| 70 | + context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function checkLiteral(context: RuleContext, path: TSESTree.Literal): void { |
| 75 | + const absolutePathRegex = /^(?:[+a-z]+:)?\/\//i; |
| 76 | + if (!absolutePathRegex.test(path.value?.toString() ?? '')) { |
| 77 | + context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function extractStartingIdentifier( |
| 82 | + templateLiteral: TSESTree.TemplateLiteral |
| 83 | +): TSESTree.Identifier | undefined { |
| 84 | + const literalParts = [...templateLiteral.expressions, ...templateLiteral.quasis].sort((a, b) => |
| 85 | + a.range[0] < b.range[0] ? -1 : 1 |
| 86 | + ); |
| 87 | + for (const part of literalParts) { |
| 88 | + if (part.type === 'TemplateElement' && part.value.raw === '') { |
| 89 | + // Skip empty quasi in the begining |
| 90 | + continue; |
| 91 | + } |
| 92 | + if (part.type === 'Identifier') { |
| 93 | + return part; |
| 94 | + } |
| 95 | + return undefined; |
| 96 | + } |
| 97 | + return undefined; |
| 98 | +} |
| 99 | + |
| 100 | +function extractGotoReferences(referenceTracker: ReferenceTracker): TSESTree.CallExpression[] { |
| 101 | + return Array.from( |
| 102 | + referenceTracker.iterateEsmReferences({ |
| 103 | + '$app/navigation': { |
| 104 | + [ReferenceTracker.ESM]: true, |
| 105 | + goto: { |
| 106 | + [ReferenceTracker.CALL]: true |
| 107 | + } |
| 108 | + } |
| 109 | + }), |
| 110 | + ({ node }) => node |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 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 |
| 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; |
| 134 | +} |
0 commit comments