Skip to content

Commit

Permalink
fix: allow glob patterns for restrictedNamedExports in `no-restrict…
Browse files Browse the repository at this point in the history
…ed-exports`
  • Loading branch information
akulsr0 committed May 8, 2024
1 parent 37eba48 commit 965754e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"use strict";

const minimatch = require("minimatch");

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -109,7 +111,9 @@ module.exports = {
function checkExportedName(node) {
const name = astUtils.getModuleExportName(node);

if (restrictedNames.has(name)) {
const hasRestrictedName = [...restrictedNames].some(restrictedName => name === restrictedName || (name && minimatch(name, restrictedName)));

if (hasRestrictedName) {
context.report({
node,
messageId: "restrictedNamed",
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ ruleTester.run("no-restricted-exports", rule, {
{ code: "import a from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
{ code: "import { a } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
{ code: "import { b as a } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
{
code: "var setSomething; export { setSomething };",
options: [{ restrictedNamedExports: ["get*"] }]
},

// does not check re-export all declarations
{ code: "export * from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
Expand Down Expand Up @@ -532,6 +536,35 @@ ruleTester.run("no-restricted-exports", rule, {
{ messageId: "restrictedNamed", data: { name: "f" }, type: "Identifier" }
]
},
{
code: "var getSomething; export { getSomething };",
options: [{ restrictedNamedExports: ["get*"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "getSomething" }, type: "Identifier" }
]
},
{
code: "var getSomethingFromUser; export { getSomethingFromUser };",
options: [{ restrictedNamedExports: ["*User"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "getSomethingFromUser" }, type: "Identifier" }
]
},
{
code: "var foo, ab, xy; export { foo, ab, xy };",
options: [{ restrictedNamedExports: ["*+(b|y)"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "ab" }, type: "Identifier" },
{ messageId: "restrictedNamed", data: { name: "xy" }, type: "Identifier" }
]
},
{
code: "var foo; export { foo as ab };",
options: [{ restrictedNamedExports: ["*+(b|y)"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "ab" }, type: "Identifier" }
]
},

// reports "default" in named export declarations (when configured)
{
Expand Down

0 comments on commit 965754e

Please sign in to comment.