2 * @fileoverview Restrict usage of specified node imports.
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils
= require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
17 const ignore
= require("ignore");
19 const arrayOfStringsOrObjects
= {
27 name
: { type
: "string" },
39 additionalProperties
: false,
47 const arrayOfStringsOrObjectPatterns
= {
85 additionalProperties
: false,
93 /** @type {import('../shared/types').Rule} */
99 description
: "Disallow specified modules when loaded by `import`",
101 url
: "https://eslint.org/docs/rules/no-restricted-imports"
105 path
: "'{{importSource}}' import is restricted from being used.",
106 // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
107 pathWithCustomMessage
: "'{{importSource}}' import is restricted from being used. {{customMessage}}",
109 patterns
: "'{{importSource}}' import is restricted from being used by a pattern.",
110 // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
111 patternWithCustomMessage
: "'{{importSource}}' import is restricted from being used by a pattern. {{customMessage}}",
113 patternAndImportName
: "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern.",
114 // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
115 patternAndImportNameWithCustomMessage
: "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}",
117 patternAndEverything
: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern.",
118 // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
119 patternAndEverythingWithCustomMessage
: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}",
121 everything
: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.",
122 // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
123 everythingWithCustomMessage
: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}",
125 importName
: "'{{importName}}' import from '{{importSource}}' is restricted.",
126 // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
127 importNameWithCustomMessage
: "'{{importName}}' import from '{{importSource}}' is restricted. {{customMessage}}"
132 arrayOfStringsOrObjects
,
138 paths
: arrayOfStringsOrObjects
,
139 patterns
: arrayOfStringsOrObjectPatterns
141 additionalProperties
: false
143 additionalItems
: false
150 const sourceCode
= context
.getSourceCode();
151 const options
= Array
.isArray(context
.options
) ? context
.options
: [];
152 const isPathAndPatternsObject
=
153 typeof options
[0] === "object" &&
154 (Object
.prototype.hasOwnProperty
.call(options
[0], "paths") || Object
.prototype.hasOwnProperty
.call(options
[0], "patterns"));
156 const restrictedPaths
= (isPathAndPatternsObject
? options
[0].paths
: context
.options
) || [];
157 const restrictedPathMessages
= restrictedPaths
.reduce((memo
, importSource
) => {
158 if (typeof importSource
=== "string") {
159 memo
[importSource
] = { message
: null };
161 memo
[importSource
.name
] = {
162 message
: importSource
.message
,
163 importNames
: importSource
.importNames
169 // Handle patterns too, either as strings or groups
170 let restrictedPatterns
= (isPathAndPatternsObject
? options
[0].patterns
: []) || [];
172 // standardize to array of objects if we have an array of strings
173 if (restrictedPatterns
.length
> 0 && typeof restrictedPatterns
[0] === "string") {
174 restrictedPatterns
= [{ group
: restrictedPatterns
}];
177 // relative paths are supported for this rule
178 const restrictedPatternGroups
= restrictedPatterns
.map(({ group
, message
, caseSensitive
, importNames
}) => ({
179 matcher
: ignore({ allowRelativePaths
: true, ignorecase
: !caseSensitive
}).add(group
),
180 customMessage
: message
,
184 // if no imports are restricted we don't need to check
185 if (Object
.keys(restrictedPaths
).length
=== 0 && restrictedPatternGroups
.length
=== 0) {
190 * Report a restricted path.
191 * @param {string} importSource path of the import
192 * @param {Map<string,Object[]>} importNames Map of import names that are being imported
193 * @param {node} node representing the restricted path reference
197 function checkRestrictedPathAndReport(importSource
, importNames
, node
) {
198 if (!Object
.prototype.hasOwnProperty
.call(restrictedPathMessages
, importSource
)) {
202 const customMessage
= restrictedPathMessages
[importSource
].message
;
203 const restrictedImportNames
= restrictedPathMessages
[importSource
].importNames
;
205 if (restrictedImportNames
) {
206 if (importNames
.has("*")) {
207 const specifierData
= importNames
.get("*")[0];
211 messageId
: customMessage
? "everythingWithCustomMessage" : "everything",
212 loc
: specifierData
.loc
,
215 importNames
: restrictedImportNames
,
221 restrictedImportNames
.forEach(importName
=> {
222 if (importNames
.has(importName
)) {
223 const specifiers
= importNames
.get(importName
);
225 specifiers
.forEach(specifier
=> {
228 messageId
: customMessage
? "importNameWithCustomMessage" : "importName",
242 messageId
: customMessage
? "pathWithCustomMessage" : "path",
252 * Report a restricted path specifically for patterns.
253 * @param {node} node representing the restricted path reference
254 * @param {Object} group contains an Ignore instance for paths, the customMessage to show on failure,
255 * and any restricted import names that have been specified in the config
256 * @param {Map<string,Object[]>} importNames Map of import names that are being imported
260 function reportPathForPatterns(node
, group
, importNames
) {
261 const importSource
= node
.source
.value
.trim();
263 const customMessage
= group
.customMessage
;
264 const restrictedImportNames
= group
.importNames
;
267 * If we are not restricting to any specific import names and just the pattern itself,
268 * report the error and move on
270 if (!restrictedImportNames
) {
273 messageId
: customMessage
? "patternWithCustomMessage" : "patterns",
282 if (importNames
.has("*")) {
283 const specifierData
= importNames
.get("*")[0];
287 messageId
: customMessage
? "patternAndEverythingWithCustomMessage" : "patternAndEverything",
288 loc
: specifierData
.loc
,
291 importNames
: restrictedImportNames
,
297 restrictedImportNames
.forEach(importName
=> {
298 if (!importNames
.has(importName
)) {
302 const specifiers
= importNames
.get(importName
);
304 specifiers
.forEach(specifier
=> {
307 messageId
: customMessage
? "patternAndImportNameWithCustomMessage" : "patternAndImportName",
320 * Check if the given importSource is restricted by a pattern.
321 * @param {string} importSource path of the import
322 * @param {Object} group contains a Ignore instance for paths, and the customMessage to show if it fails
323 * @returns {boolean} whether the variable is a restricted pattern or not
326 function isRestrictedPattern(importSource
, group
) {
327 return group
.matcher
.ignores(importSource
);
331 * Checks a node to see if any problems should be reported.
332 * @param {ASTNode} node The node to check.
336 function checkNode(node
) {
337 const importSource
= node
.source
.value
.trim();
338 const importNames
= new Map();
340 if (node
.type
=== "ExportAllDeclaration") {
341 const starToken
= sourceCode
.getFirstToken(node
, 1);
343 importNames
.set("*", [{ loc
: starToken
.loc
}]);
344 } else if (node
.specifiers
) {
345 for (const specifier
of node
.specifiers
) {
347 const specifierData
= { loc
: specifier
.loc
};
349 if (specifier
.type
=== "ImportDefaultSpecifier") {
351 } else if (specifier
.type
=== "ImportNamespaceSpecifier") {
353 } else if (specifier
.imported
) {
354 name
= astUtils
.getModuleExportName(specifier
.imported
);
355 } else if (specifier
.local
) {
356 name
= astUtils
.getModuleExportName(specifier
.local
);
359 if (typeof name
=== "string") {
360 if (importNames
.has(name
)) {
361 importNames
.get(name
).push(specifierData
);
363 importNames
.set(name
, [specifierData
]);
369 checkRestrictedPathAndReport(importSource
, importNames
, node
);
370 restrictedPatternGroups
.forEach(group
=> {
371 if (isRestrictedPattern(importSource
, group
)) {
372 reportPathForPatterns(node
, group
, importNames
);
378 ImportDeclaration
: checkNode
,
379 ExportNamedDeclaration(node
) {
384 ExportAllDeclaration
: checkNode