diff --git a/docs/rules/no-restricted-imports.md b/docs/rules/no-restricted-imports.md index d8e06101b41..414485fdf62 100644 --- a/docs/rules/no-restricted-imports.md +++ b/docs/rules/no-restricted-imports.md @@ -150,7 +150,11 @@ import DisallowedObject from "foo"; message: "Please import 'DisallowedObject' from '/bar/baz/' instead." }]}]*/ +import { DisallowedObject } from "foo"; + import { DisallowedObject as AllowedObject } from "foo"; + +import { "DisallowedObject" as AllowedObject } from "foo"; ``` ```js diff --git a/lib/rules/no-restricted-imports.js b/lib/rules/no-restricted-imports.js index 6813037f13a..455cc336c5f 100644 --- a/lib/rules/no-restricted-imports.js +++ b/lib/rules/no-restricted-imports.js @@ -4,6 +4,12 @@ */ "use strict"; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const astUtils = require("./utils/ast-utils"); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -269,12 +275,12 @@ module.exports = { } else if (specifier.type === "ImportNamespaceSpecifier") { name = "*"; } else if (specifier.imported) { - name = specifier.imported.name; + name = astUtils.getModuleExportName(specifier.imported); } else if (specifier.local) { - name = specifier.local.name; + name = astUtils.getModuleExportName(specifier.local); } - if (name) { + if (typeof name === "string") { if (importNames.has(name)) { importNames.get(name).push(specifierData); } else { diff --git a/tests/lib/rules/no-restricted-imports.js b/tests/lib/rules/no-restricted-imports.js index c86e67ecf65..3cf7eb371a7 100644 --- a/tests/lib/rules/no-restricted-imports.js +++ b/tests/lib/rules/no-restricted-imports.js @@ -16,7 +16,7 @@ const rule = require("../../../lib/rules/no-restricted-imports"), // Tests //------------------------------------------------------------------------------ -const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020, sourceType: "module" } }); +const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2022, sourceType: "module" } }); ruleTester.run("no-restricted-imports", rule, { valid: [ @@ -92,6 +92,34 @@ ruleTester.run("no-restricted-imports", rule, { }] }] }, + { + code: "import { 'AllowedObject' as bar } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: ["DisallowedObject"], + message: "Please import 'DisallowedObject' from /bar/ instead." + }] + }] + }, + { + code: "import { ' ' as bar } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: [""] + }] + }] + }, + { + code: "import { '' as bar } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: [" "] + }] + }] + }, { code: "import { DisallowedObject } from \"foo\";", options: [{ @@ -112,6 +140,16 @@ ruleTester.run("no-restricted-imports", rule, { }] }] }, + { + code: "import { 'AllowedObject' as DisallowedObject } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: ["DisallowedObject"], + message: "Please import 'DisallowedObject' from /bar/ instead." + }] + }] + }, { code: "import { AllowedObject, AllowedObjectTwo } from \"foo\";", options: [{ @@ -196,6 +234,24 @@ ruleTester.run("no-restricted-imports", rule, { name: "bar", importNames: ["DisallowedObject"] }] + }, + { + code: "export { 'AllowedObject' } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: ["DisallowedObject"] + }] + }] + }, + { + code: "export { 'AllowedObject' as DisallowedObject } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: ["DisallowedObject"] + }] + }] } ], invalid: [{ @@ -344,6 +400,70 @@ ruleTester.run("no-restricted-imports", rule, { column: 9, endColumn: 17 }] + }, { + code: "export {'foo' as b} from \"fs\";", + options: [{ + paths: [{ + name: "fs", + importNames: ["foo"], + message: "Don't import 'foo'." + }] + }], + errors: [{ + message: "'foo' import from 'fs' is restricted. Don't import 'foo'.", + type: "ExportNamedDeclaration", + line: 1, + column: 9, + endColumn: 19 + }] + }, { + code: "export {'foo'} from \"fs\";", + options: [{ + paths: [{ + name: "fs", + importNames: ["foo"], + message: "Don't import 'foo'." + }] + }], + errors: [{ + message: "'foo' import from 'fs' is restricted. Don't import 'foo'.", + type: "ExportNamedDeclaration", + line: 1, + column: 9, + endColumn: 14 + }] + }, { + code: "export {'👍'} from \"fs\";", + options: [{ + paths: [{ + name: "fs", + importNames: ["👍"], + message: "Don't import '👍'." + }] + }], + errors: [{ + message: "'👍' import from 'fs' is restricted. Don't import '👍'.", + type: "ExportNamedDeclaration", + line: 1, + column: 9, + endColumn: 13 + }] + }, { + code: "export {''} from \"fs\";", + options: [{ + paths: [{ + name: "fs", + importNames: [""], + message: "Don't import ''." + }] + }], + errors: [{ + message: "'' import from 'fs' is restricted. Don't import ''.", + type: "ExportNamedDeclaration", + line: 1, + column: 9, + endColumn: 11 + }] }, { code: "export * as ns from \"fs\";", options: [{ @@ -505,6 +625,55 @@ ruleTester.run("no-restricted-imports", rule, { endColumn: 43 }] }, + { + code: "import { 'DisallowedObject' as AllowedObject } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: ["DisallowedObject"], + message: "Please import 'DisallowedObject' from /bar/ instead." + }] + }], + errors: [{ + message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.", + type: "ImportDeclaration", + line: 1, + column: 10, + endColumn: 45 + }] + }, + { + code: "import { '👍' as bar } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: ["👍"] + }] + }], + errors: [{ + message: "'👍' import from 'foo' is restricted.", + type: "ImportDeclaration", + line: 1, + column: 10, + endColumn: 21 + }] + }, + { + code: "import { '' as bar } from \"foo\";", + options: [{ + paths: [{ + name: "foo", + importNames: [""] + }] + }], + errors: [{ + message: "'' import from 'foo' is restricted.", + type: "ImportDeclaration", + line: 1, + column: 10, + endColumn: 19 + }] + }, { code: "import { AllowedObject, DisallowedObject } from \"foo\";", options: [{