Skip to content

Commit c0427c7

Browse files
committed
chore(no-goto-without-base): Split code
1 parent e132cee commit c0427c7

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

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

+41-29
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 type { RuleContext } from '../types';
56

67
export default createRule('no-goto-without-base', {
78
meta: {
@@ -29,42 +30,53 @@ export default createRule('no-goto-without-base', {
2930
continue;
3031
}
3132
const path = gotoCall.arguments[0];
32-
if (path.type === 'BinaryExpression') {
33-
if (path.left.type === 'Identifier' && basePathNames.includes(path.left.name)) {
34-
// The URL is in the form `base + "/foo"`, which is OK
35-
continue;
36-
}
37-
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
38-
continue;
39-
}
40-
if (path.type === 'TemplateLiteral') {
41-
const startingIdentifier = extractStartingIdentifier(path);
42-
if (
43-
startingIdentifier !== undefined &&
44-
basePathNames.includes(startingIdentifier.name)
45-
) {
46-
// The URL is in the form `${base}/foo`, which is OK
47-
continue;
48-
}
49-
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
50-
continue;
33+
switch (path.type) {
34+
case 'BinaryExpression':
35+
checkBinaryExpression(context, path, basePathNames);
36+
break;
37+
case 'Literal':
38+
checkLiteral(context, path);
39+
break;
40+
case 'TemplateLiteral':
41+
checkTemplateLiteral(context, path, basePathNames);
42+
break;
43+
default:
44+
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
5145
}
52-
if (path.type === 'Literal') {
53-
const absolutePathRegex = /^(?:[+a-z]+:)?\/\//i;
54-
if (absolutePathRegex.test(path.value?.toString() ?? '')) {
55-
// The URL is absolute, which is OK
56-
continue;
57-
}
58-
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
59-
continue;
60-
}
61-
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
6246
}
6347
}
6448
};
6549
}
6650
});
6751

52+
function checkBinaryExpression(
53+
context: RuleContext,
54+
path: TSESTree.BinaryExpression,
55+
basePathNames: string[]
56+
): void {
57+
if (path.left.type !== 'Identifier' || !basePathNames.includes(path.left.name)) {
58+
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
59+
}
60+
}
61+
62+
function checkTemplateLiteral(
63+
context: RuleContext,
64+
path: TSESTree.TemplateLiteral,
65+
basePathNames: string[]
66+
): void {
67+
const startingIdentifier = extractStartingIdentifier(path);
68+
if (startingIdentifier === undefined || !basePathNames.includes(startingIdentifier.name)) {
69+
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
70+
}
71+
}
72+
73+
function checkLiteral(context: RuleContext, path: TSESTree.Literal): void {
74+
const absolutePathRegex = /^(?:[+a-z]+:)?\/\//i;
75+
if (!absolutePathRegex.test(path.value?.toString() ?? '')) {
76+
context.report({ loc: path.loc, messageId: 'isNotPrefixedWithBasePath' });
77+
}
78+
}
79+
6880
function extractStartingIdentifier(
6981
templateLiteral: TSESTree.TemplateLiteral
7082
): TSESTree.Identifier | undefined {

0 commit comments

Comments
 (0)