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

feat: Support arbitrary module namespace names in no-restricted-imports #15491

Merged
merged 1 commit into from Jan 10, 2022
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
4 changes: 4 additions & 0 deletions docs/rules/no-restricted-imports.md
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/rules/no-restricted-imports.js
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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 {
Expand Down
171 changes: 170 additions & 1 deletion tests/lib/rules/no-restricted-imports.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [{
Expand All @@ -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: [{
Expand Down Expand Up @@ -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: [{
Expand Down Expand Up @@ -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: [{
Expand Down Expand Up @@ -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: [{
Expand Down