Skip to content

Commit

Permalink
Update: fix no-restricted-imports export * false negative (fixes #12737
Browse files Browse the repository at this point in the history
…) (#12798)
  • Loading branch information
mdjermanovic authored and kaicataldo committed Jan 17, 2020
1 parent 0d8c0af commit 9a93d9e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
49 changes: 28 additions & 21 deletions lib/rules/no-restricted-imports.js
Expand Up @@ -91,6 +91,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const options = Array.isArray(context.options) ? context.options : [];
const isPathAndPatternsObject =
typeof options[0] === "object" &&
Expand Down Expand Up @@ -216,30 +217,36 @@ module.exports = {
*/
function checkNode(node) {
const importSource = node.source.value.trim();
const importNames = node.specifiers ? node.specifiers.reduce((map, specifier) => {
let name;
const specifierData = { loc: specifier.loc };

if (specifier.type === "ImportDefaultSpecifier") {
name = "default";
} else if (specifier.type === "ImportNamespaceSpecifier") {
name = "*";
} else if (specifier.imported) {
name = specifier.imported.name;
} else if (specifier.local) {
name = specifier.local.name;
}
const importNames = new Map();

if (node.type === "ExportAllDeclaration") {
const starToken = sourceCode.getFirstToken(node, 1);

importNames.set("*", [{ loc: starToken.loc }]);
} else if (node.specifiers) {
for (const specifier of node.specifiers) {
let name;
const specifierData = { loc: specifier.loc };

if (specifier.type === "ImportDefaultSpecifier") {
name = "default";
} else if (specifier.type === "ImportNamespaceSpecifier") {
name = "*";
} else if (specifier.imported) {
name = specifier.imported.name;
} else if (specifier.local) {
name = specifier.local.name;
}

if (name) {
if (map.has(name)) {
map.get(name).push(specifierData);
} else {
map.set(name, [specifierData]);
if (name) {
if (importNames.has(name)) {
importNames.get(name).push(specifierData);
} else {
importNames.set(name, [specifierData]);
}
}
}

return map;
}, new Map()) : new Map();
}

checkRestrictedPathAndReport(importSource, importNames, node);

Expand Down
42 changes: 42 additions & 0 deletions tests/lib/rules/no-restricted-imports.js
Expand Up @@ -168,6 +168,17 @@ ruleTester.run("no-restricted-imports", rule, {
{
code: "import {\nAllowedObject,\nDisallowedObject, // eslint-disable-line\n} from \"foo\";",
options: [{ paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]
},
{
code: "export * from \"foo\";",
options: ["bar"]
},
{
code: "export * from \"foo\";",
options: [{
name: "bar",
importNames: ["DisallowedObject"]
}]
}
],
invalid: [{
Expand Down Expand Up @@ -356,6 +367,37 @@ ruleTester.run("no-restricted-imports", rule, {
endColumn: 16
}]
},
{
code: "export * from \"foo\";",
options: [{
paths: [{
name: "foo",
importNames: ["DisallowedObject"],
message: "Please import 'DisallowedObject' from /bar/ instead."
}]
}],
errors: [{
message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
type: "ExportAllDeclaration",
line: 1,
column: 8,
endColumn: 9
}]
},
{
code: "export * from \"foo\";",
options: [{
name: "foo",
importNames: ["DisallowedObject1, DisallowedObject2"]
}],
errors: [{
message: "* import is invalid because 'DisallowedObject1, DisallowedObject2' from 'foo' is restricted.",
type: "ExportAllDeclaration",
line: 1,
column: 8,
endColumn: 9
}]
},
{
code: "import { DisallowedObject } from \"foo\";",
options: [{
Expand Down

0 comments on commit 9a93d9e

Please sign in to comment.