@@ -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 type { RuleContext } from '../types' ;
5
6
6
7
export default createRule ( 'no-goto-without-base' , {
7
8
meta : {
@@ -29,42 +30,53 @@ export default createRule('no-goto-without-base', {
29
30
continue ;
30
31
}
31
32
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' } ) ;
51
45
}
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' } ) ;
62
46
}
63
47
}
64
48
} ;
65
49
}
66
50
} ) ;
67
51
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
+
68
80
function extractStartingIdentifier (
69
81
templateLiteral : TSESTree . TemplateLiteral
70
82
) : TSESTree . Identifier | undefined {
0 commit comments