UNPKG

4.83 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 getESLintCoreRule_1 = require("../util/getESLintCoreRule");
24const util = __importStar(require("../util"));
25const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-duplicate-imports');
26exports.default = util.createRule({
27 name: 'no-duplicate-imports',
28 meta: {
29 type: 'problem',
30 docs: {
31 description: 'Disallow duplicate imports',
32 recommended: false,
33 extendsBaseRule: true,
34 },
35 hasSuggestions: baseRule.meta.hasSuggestions,
36 schema: baseRule.meta.schema,
37 messages: Object.assign(Object.assign({}, baseRule.meta.messages), { importType: '{{module}} type import is duplicated.', importTypeAs: '{{module}} type import is duplicated as type export.', exportType: '{{module}} type export is duplicated.', exportTypeAs: '{{module}} type export is duplicated as type import.' }),
38 },
39 defaultOptions: [
40 {
41 includeExports: false,
42 },
43 ],
44 create(context, [{ includeExports }]) {
45 const rules = baseRule.create(context);
46 const typeMemberImports = new Set();
47 const typeDefaultImports = new Set();
48 const typeExports = new Set();
49 function report(messageId, node, module) {
50 context.report({
51 messageId,
52 node,
53 data: {
54 module,
55 },
56 });
57 }
58 function isAllMemberImport(node) {
59 return node.specifiers.every(specifier => specifier.type === utils_1.AST_NODE_TYPES.ImportSpecifier);
60 }
61 function checkTypeImport(node) {
62 if (node.source) {
63 const value = node.source.value;
64 const isMemberImport = isAllMemberImport(node);
65 if (isMemberImport
66 ? typeMemberImports.has(value)
67 : typeDefaultImports.has(value)) {
68 report('importType', node, value);
69 }
70 if (includeExports && typeExports.has(value)) {
71 report('importTypeAs', node, value);
72 }
73 if (isMemberImport) {
74 typeMemberImports.add(value);
75 }
76 else {
77 typeDefaultImports.add(value);
78 }
79 }
80 }
81 function checkTypeExport(node) {
82 if (node.source) {
83 const value = node.source.value;
84 if (typeExports.has(value)) {
85 report('exportType', node, value);
86 }
87 if (typeMemberImports.has(value) || typeDefaultImports.has(value)) {
88 report('exportTypeAs', node, value);
89 }
90 typeExports.add(value);
91 }
92 }
93 return Object.assign(Object.assign({}, rules), { ImportDeclaration(node) {
94 if (node.importKind === 'type') {
95 checkTypeImport(node);
96 return;
97 }
98 rules.ImportDeclaration(node);
99 },
100 ExportNamedDeclaration(node) {
101 var _a;
102 if (includeExports && node.exportKind === 'type') {
103 checkTypeExport(node);
104 return;
105 }
106 (_a = rules.ExportNamedDeclaration) === null || _a === void 0 ? void 0 : _a.call(rules, node);
107 },
108 ExportAllDeclaration(node) {
109 var _a;
110 if (includeExports && node.exportKind === 'type') {
111 checkTypeExport(node);
112 return;
113 }
114 (_a = rules.ExportAllDeclaration) === null || _a === void 0 ? void 0 : _a.call(rules, node);
115 } });
116 },
117});
118//# sourceMappingURL=no-duplicate-imports.js.map
\No newline at end of file