-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinvalid-relative-Import-prefix.ts
41 lines (39 loc) · 1.53 KB
/
invalid-relative-Import-prefix.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { createRule } from '../utils/create-rule';
import { TSESTree } from '@typescript-eslint/utils';
type Options = string[];
type MessageIds = 'invalidRelativeImportPrefix';
export const RULE_NAME = 'invalid-relative-import-prefix';
export default createRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Avoids inconsistent import paths.',
recommended: 'recommended'
},
schema: [],
messages: { invalidRelativeImportPrefix: 'Relative import statements cannot start with "./../' },
fixable: 'code'
},
defaultOptions: [],
create: function (context) {
function reportIfInvalidRelativeImport(node: TSESTree.ImportDeclaration & TSESTree.Node) {
const importSource = (node.source.value as string).trim();
const invalidRelativeImportStart = importSource.substring(0, 5);
if (typeof invalidRelativeImportStart === 'string' && invalidRelativeImportStart === './../') {
context.report({
messageId: 'invalidRelativeImportPrefix',
node: node.source,
fix: function (fixer) {
return fixer.replaceTextRange([node.source.range[0], node.source.range[0] + 3], `${node.source.raw[0]}../`)
}
});
}
}
return {
ImportDeclaration(node) {
reportIfInvalidRelativeImport(node);
}
}
}
});