Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: fix no-restricted-imports export * false negative (fixes #12737) #12798

Merged
merged 1 commit into from Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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