UNPKG

4.96 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const utils_1 = require("@typescript-eslint/utils");
23const util = __importStar(require("../util"));
24exports.default = util.createRule({
25 name: 'triple-slash-reference',
26 meta: {
27 type: 'suggestion',
28 docs: {
29 description: 'Sets preference level for triple slash directives versus ES6-style import declarations',
30 recommended: 'error',
31 },
32 messages: {
33 tripleSlashReference: 'Do not use a triple slash reference for {{module}}, use `import` style instead.',
34 },
35 schema: [
36 {
37 type: 'object',
38 properties: {
39 lib: {
40 enum: ['always', 'never'],
41 },
42 path: {
43 enum: ['always', 'never'],
44 },
45 types: {
46 enum: ['always', 'never', 'prefer-import'],
47 },
48 },
49 additionalProperties: false,
50 },
51 ],
52 },
53 defaultOptions: [
54 {
55 lib: 'always',
56 path: 'never',
57 types: 'prefer-import',
58 },
59 ],
60 create(context, [{ lib, path, types }]) {
61 let programNode;
62 const sourceCode = context.getSourceCode();
63 const references = [];
64 function hasMatchingReference(source) {
65 references.forEach(reference => {
66 if (reference.importName === source.value) {
67 context.report({
68 node: reference.comment,
69 messageId: 'tripleSlashReference',
70 data: {
71 module: reference.importName,
72 },
73 });
74 }
75 });
76 }
77 return {
78 ImportDeclaration(node) {
79 if (programNode) {
80 hasMatchingReference(node.source);
81 }
82 },
83 TSImportEqualsDeclaration(node) {
84 if (programNode) {
85 const reference = node.moduleReference;
86 if (reference.type === utils_1.AST_NODE_TYPES.TSExternalModuleReference) {
87 hasMatchingReference(reference.expression);
88 }
89 }
90 },
91 Program(node) {
92 if (lib === 'always' && path === 'always' && types == 'always') {
93 return;
94 }
95 programNode = node;
96 const referenceRegExp = /^\/\s*<reference\s*(types|path|lib)\s*=\s*["|'](.*)["|']/;
97 const commentsBefore = sourceCode.getCommentsBefore(programNode);
98 commentsBefore.forEach(comment => {
99 if (comment.type !== utils_1.AST_TOKEN_TYPES.Line) {
100 return;
101 }
102 const referenceResult = referenceRegExp.exec(comment.value);
103 if (referenceResult) {
104 if ((referenceResult[1] === 'types' && types === 'never') ||
105 (referenceResult[1] === 'path' && path === 'never') ||
106 (referenceResult[1] === 'lib' && lib === 'never')) {
107 context.report({
108 node: comment,
109 messageId: 'tripleSlashReference',
110 data: {
111 module: referenceResult[2],
112 },
113 });
114 return;
115 }
116 if (referenceResult[1] === 'types' && types === 'prefer-import') {
117 references.push({ comment, importName: referenceResult[2] });
118 }
119 }
120 });
121 },
122 };
123 },
124});
125//# sourceMappingURL=triple-slash-reference.js.map
\No newline at end of file