/
triple-slash-reference.ts
123 lines (118 loc) · 3.42 KB
/
triple-slash-reference.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as util from '../util';
import { TSESTree } from '@typescript-eslint/experimental-utils';
type Options = [
{
lib?: 'always' | 'never';
path?: 'always' | 'never';
types?: 'always' | 'never' | 'prefer-import';
},
];
type MessageIds = 'tripleSlashReference';
export default util.createRule<Options, MessageIds>({
name: 'triple-slash-reference',
meta: {
type: 'suggestion',
docs: {
description:
'Sets preference level for triple slash directives versus ES6-style import declarations',
category: 'Best Practices',
recommended: 'error',
},
messages: {
tripleSlashReference:
'Do not use a triple slash reference for {{module}}, use `import` style instead.',
},
schema: [
{
type: 'object',
properties: {
lib: {
enum: ['always', 'never'],
},
path: {
enum: ['always', 'never'],
},
types: {
enum: ['always', 'never', 'prefer-import'],
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{
lib: 'always',
path: 'never',
types: 'prefer-import',
},
],
create(context, [{ lib, path, types }]) {
let programNode: TSESTree.Node;
const sourceCode = context.getSourceCode();
const references: ({
comment: TSESTree.Comment;
importName: string;
})[] = [];
function hasMatchingReference(source: TSESTree.Literal): void {
references.forEach(reference => {
if (reference.importName === source.value) {
context.report({
node: reference.comment,
messageId: 'tripleSlashReference',
data: {
module: reference.importName,
},
});
}
});
}
return {
ImportDeclaration(node): void {
if (programNode) {
hasMatchingReference(node.source);
}
},
TSImportEqualsDeclaration(node): void {
if (programNode) {
const source = (node.moduleReference as TSESTree.TSExternalModuleReference)
.expression as TSESTree.Literal;
hasMatchingReference(source);
}
},
Program(node): void {
if (lib === 'always' && path === 'always' && types == 'always') {
return;
}
programNode = node;
const referenceRegExp = /^\/\s*<reference\s*(types|path|lib)\s*=\s*["|'](.*)["|']/;
const commentsBefore = sourceCode.getCommentsBefore(programNode);
commentsBefore.forEach(comment => {
if (comment.type !== 'Line') {
return;
}
const referenceResult = referenceRegExp.exec(comment.value);
if (referenceResult) {
if (
(referenceResult[1] === 'types' && types === 'never') ||
(referenceResult[1] === 'path' && path === 'never') ||
(referenceResult[1] === 'lib' && lib === 'never')
) {
context.report({
node: comment,
messageId: 'tripleSlashReference',
data: {
module: referenceResult[2],
},
});
return;
}
if (referenceResult[1] === 'types' && types === 'prefer-import') {
references.push({ comment, importName: referenceResult[2] });
}
}
});
},
};
},
});