2 * @fileoverview Restrict usage of specified node imports.
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const ignore
= require("ignore");
13 const arrayOfStringsOrObjects
= {
21 name
: { type
: "string" },
33 additionalProperties
: false,
41 const arrayOfStringsOrObjectPatterns
= {
68 additionalProperties
: false,
81 description
: "disallow specified modules when loaded by `import`",
82 category
: "ECMAScript 6",
84 url
: "https://eslint.org/docs/rules/no-restricted-imports"
88 path
: "'{{importSource}}' import is restricted from being used.",
89 // eslint-disable-next-line eslint-plugin/report-message-format
90 pathWithCustomMessage
: "'{{importSource}}' import is restricted from being used. {{customMessage}}",
92 patterns
: "'{{importSource}}' import is restricted from being used by a pattern.",
93 // eslint-disable-next-line eslint-plugin/report-message-format
94 patternWithCustomMessage
: "'{{importSource}}' import is restricted from being used by a pattern. {{customMessage}}",
96 everything
: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.",
97 // eslint-disable-next-line eslint-plugin/report-message-format
98 everythingWithCustomMessage
: "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}",
100 importName
: "'{{importName}}' import from '{{importSource}}' is restricted.",
101 // eslint-disable-next-line eslint-plugin/report-message-format
102 importNameWithCustomMessage
: "'{{importName}}' import from '{{importSource}}' is restricted. {{customMessage}}"
107 arrayOfStringsOrObjects
,
113 paths
: arrayOfStringsOrObjects
,
114 patterns
: arrayOfStringsOrObjectPatterns
116 additionalProperties
: false
118 additionalItems
: false
125 const sourceCode
= context
.getSourceCode();
126 const options
= Array
.isArray(context
.options
) ? context
.options
: [];
127 const isPathAndPatternsObject
=
128 typeof options
[0] === "object" &&
129 (Object
.prototype.hasOwnProperty
.call(options
[0], "paths") || Object
.prototype.hasOwnProperty
.call(options
[0], "patterns"));
131 const restrictedPaths
= (isPathAndPatternsObject
? options
[0].paths
: context
.options
) || [];
132 const restrictedPathMessages
= restrictedPaths
.reduce((memo
, importSource
) => {
133 if (typeof importSource
=== "string") {
134 memo
[importSource
] = { message
: null };
136 memo
[importSource
.name
] = {
137 message
: importSource
.message
,
138 importNames
: importSource
.importNames
144 // Handle patterns too, either as strings or groups
145 const restrictedPatterns
= (isPathAndPatternsObject
? options
[0].patterns
: []) || [];
146 const restrictedPatternGroups
= restrictedPatterns
.length
> 0 && typeof restrictedPatterns
[0] === "string"
147 ? [{ matcher
: ignore().add(restrictedPatterns
) }]
148 : restrictedPatterns
.map(({ group
, message
}) => ({ matcher
: ignore().add(group
), customMessage
: message
}));
150 // if no imports are restricted we don"t need to check
151 if (Object
.keys(restrictedPaths
).length
=== 0 && restrictedPatternGroups
.length
=== 0) {
156 * Report a restricted path.
157 * @param {string} importSource path of the import
158 * @param {Map<string,Object[]>} importNames Map of import names that are being imported
159 * @param {node} node representing the restricted path reference
163 function checkRestrictedPathAndReport(importSource
, importNames
, node
) {
164 if (!Object
.prototype.hasOwnProperty
.call(restrictedPathMessages
, importSource
)) {
168 const customMessage
= restrictedPathMessages
[importSource
].message
;
169 const restrictedImportNames
= restrictedPathMessages
[importSource
].importNames
;
171 if (restrictedImportNames
) {
172 if (importNames
.has("*")) {
173 const specifierData
= importNames
.get("*")[0];
177 messageId
: customMessage
? "everythingWithCustomMessage" : "everything",
178 loc
: specifierData
.loc
,
181 importNames
: restrictedImportNames
,
187 restrictedImportNames
.forEach(importName
=> {
188 if (importNames
.has(importName
)) {
189 const specifiers
= importNames
.get(importName
);
191 specifiers
.forEach(specifier
=> {
194 messageId
: customMessage
? "importNameWithCustomMessage" : "importName",
208 messageId
: customMessage
? "pathWithCustomMessage" : "path",
218 * Report a restricted path specifically for patterns.
219 * @param {node} node representing the restricted path reference
220 * @param {Object} group contains a Ignore instance for paths, and the customMessage to show if it fails
224 function reportPathForPatterns(node
, group
) {
225 const importSource
= node
.source
.value
.trim();
229 messageId
: group
.customMessage
? "patternWithCustomMessage" : "patterns",
232 customMessage
: group
.customMessage
238 * Check if the given importSource is restricted by a pattern.
239 * @param {string} importSource path of the import
240 * @param {Object} group contains a Ignore instance for paths, and the customMessage to show if it fails
241 * @returns {boolean} whether the variable is a restricted pattern or not
244 function isRestrictedPattern(importSource
, group
) {
245 return group
.matcher
.ignores(importSource
);
249 * Checks a node to see if any problems should be reported.
250 * @param {ASTNode} node The node to check.
254 function checkNode(node
) {
255 const importSource
= node
.source
.value
.trim();
256 const importNames
= new Map();
258 if (node
.type
=== "ExportAllDeclaration") {
259 const starToken
= sourceCode
.getFirstToken(node
, 1);
261 importNames
.set("*", [{ loc
: starToken
.loc
}]);
262 } else if (node
.specifiers
) {
263 for (const specifier
of node
.specifiers
) {
265 const specifierData
= { loc
: specifier
.loc
};
267 if (specifier
.type
=== "ImportDefaultSpecifier") {
269 } else if (specifier
.type
=== "ImportNamespaceSpecifier") {
271 } else if (specifier
.imported
) {
272 name
= specifier
.imported
.name
;
273 } else if (specifier
.local
) {
274 name
= specifier
.local
.name
;
278 if (importNames
.has(name
)) {
279 importNames
.get(name
).push(specifierData
);
281 importNames
.set(name
, [specifierData
]);
287 checkRestrictedPathAndReport(importSource
, importNames
, node
);
288 restrictedPatternGroups
.forEach(group
=> {
289 if (isRestrictedPattern(importSource
, group
)) {
290 reportPathForPatterns(node
, group
);
296 ImportDeclaration
: checkNode
,
297 ExportNamedDeclaration(node
) {
302 ExportAllDeclaration
: checkNode