UNPKG

5.32 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const ignore_1 = __importDefault(require("ignore"));
7const util_1 = require("../util");
8const getESLintCoreRule_1 = require("../util/getESLintCoreRule");
9const baseRule = (0, getESLintCoreRule_1.getESLintCoreRule)('no-restricted-imports');
10const allowTypeImportsOptionSchema = {
11 allowTypeImports: {
12 type: 'boolean',
13 default: false,
14 },
15};
16const schemaForMergeArrayOfStringsOrObjects = {
17 items: {
18 anyOf: [
19 {},
20 {
21 properties: allowTypeImportsOptionSchema,
22 },
23 ],
24 },
25};
26const schemaForMergeArrayOfStringsOrObjectPatterns = {
27 anyOf: [
28 {},
29 {
30 items: {
31 properties: allowTypeImportsOptionSchema,
32 },
33 },
34 ],
35};
36const schema = (0, util_1.deepMerge)(Object.assign({}, baseRule.meta.schema), {
37 anyOf: [
38 schemaForMergeArrayOfStringsOrObjects,
39 {
40 items: {
41 properties: {
42 paths: schemaForMergeArrayOfStringsOrObjects,
43 patterns: schemaForMergeArrayOfStringsOrObjectPatterns,
44 },
45 },
46 },
47 ],
48});
49function isObjectOfPaths(obj) {
50 return Object.prototype.hasOwnProperty.call(obj, 'paths');
51}
52function isObjectOfPatterns(obj) {
53 return Object.prototype.hasOwnProperty.call(obj, 'patterns');
54}
55function isOptionsArrayOfStringOrObject(options) {
56 if (isObjectOfPaths(options[0])) {
57 return false;
58 }
59 if (isObjectOfPatterns(options[0])) {
60 return false;
61 }
62 return true;
63}
64function getRestrictedPaths(options) {
65 if (isOptionsArrayOfStringOrObject(options)) {
66 return options;
67 }
68 if (isObjectOfPaths(options[0])) {
69 return options[0].paths;
70 }
71 return [];
72}
73function getRestrictedPatterns(options) {
74 if (isObjectOfPatterns(options[0])) {
75 return options[0].patterns;
76 }
77 return [];
78}
79exports.default = (0, util_1.createRule)({
80 name: 'no-restricted-imports',
81 meta: {
82 type: 'suggestion',
83 docs: {
84 description: 'Disallow specified modules when loaded by `import`',
85 recommended: false,
86 extendsBaseRule: true,
87 },
88 messages: baseRule.meta.messages,
89 fixable: baseRule.meta.fixable,
90 schema,
91 },
92 defaultOptions: [],
93 create(context) {
94 const rules = baseRule.create(context);
95 const { options } = context;
96 if (options.length === 0) {
97 return {};
98 }
99 const restrictedPaths = getRestrictedPaths(options);
100 const allowedTypeImportPathNameSet = new Set();
101 for (const restrictedPath of restrictedPaths) {
102 if (typeof restrictedPath === 'object' &&
103 restrictedPath.allowTypeImports) {
104 allowedTypeImportPathNameSet.add(restrictedPath.name);
105 }
106 }
107 function isAllowedTypeImportPath(importSource) {
108 return allowedTypeImportPathNameSet.has(importSource);
109 }
110 const restrictedPatterns = getRestrictedPatterns(options);
111 const allowedImportTypeMatchers = [];
112 for (const restrictedPattern of restrictedPatterns) {
113 if (typeof restrictedPattern === 'object' &&
114 restrictedPattern.allowTypeImports) {
115 // Following how ignore is configured in the base rule
116 allowedImportTypeMatchers.push((0, ignore_1.default)({
117 allowRelativePaths: true,
118 ignoreCase: !restrictedPattern.caseSensitive,
119 }).add(restrictedPattern.group));
120 }
121 }
122 function isAllowedTypeImportPattern(importSource) {
123 return (allowedImportTypeMatchers.length > 0 &&
124 allowedImportTypeMatchers.every(matcher => {
125 return matcher.ignores(importSource);
126 }));
127 }
128 return {
129 ImportDeclaration(node) {
130 if (node.importKind === 'type') {
131 const importSource = node.source.value.trim();
132 if (!isAllowedTypeImportPath(importSource) &&
133 !isAllowedTypeImportPattern(importSource)) {
134 return rules.ImportDeclaration(node);
135 }
136 }
137 else {
138 return rules.ImportDeclaration(node);
139 }
140 },
141 'ExportNamedDeclaration[source]'(node) {
142 if (node.exportKind === 'type') {
143 const importSource = node.source.value.trim();
144 if (!isAllowedTypeImportPath(importSource) &&
145 !isAllowedTypeImportPattern(importSource)) {
146 return rules.ExportNamedDeclaration(node);
147 }
148 }
149 else {
150 return rules.ExportNamedDeclaration(node);
151 }
152 },
153 ExportAllDeclaration: rules.ExportAllDeclaration,
154 };
155 },
156});
157//# sourceMappingURL=no-restricted-imports.js.map
\No newline at end of file