Skip to content

Commit

Permalink
chore: use regex instead of glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed May 8, 2024
1 parent aa547b9 commit 0e66cc8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 12 additions & 3 deletions lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

"use strict";

const minimatch = require("minimatch");

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -46,6 +44,13 @@ module.exports = {
{
type: "object",
properties: {
restrictedNamedExports: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
},
restrictedNamedExportsPattern: {
type: "array",
items: {
Expand Down Expand Up @@ -125,7 +130,11 @@ module.exports = {
function checkExportedName(node) {
const name = astUtils.getModuleExportName(node);

const hasRestrictedNamePattern = restrictedNamePatterns.some(pattern => name && minimatch(name, pattern));
const hasRestrictedNamePattern = restrictedNamePatterns.some(pattern => {
const patternRegex = new RegExp(pattern, "u");

return name && patternRegex.test(name);
});

if (hasRestrictedNamePattern || restrictedNames.has(name)) {
context.report({
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ruleTester.run("no-restricted-exports", rule, {
{ code: "import { b as a } from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
{
code: "var setSomething; export { setSomething };",
options: [{ restrictedNamedExportsPattern: ["get*"] }]
options: [{ restrictedNamedExportsPattern: ["^get"] }]
},

// does not check re-export all declarations
Expand Down Expand Up @@ -547,22 +547,22 @@ ruleTester.run("no-restricted-exports", rule, {
},
{
code: "var getSomethingFromUser; export { getSomethingFromUser };",
options: [{ restrictedNamedExportsPattern: ["*User"] }],
options: [{ restrictedNamedExportsPattern: ["User$"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "getSomethingFromUser" }, type: "Identifier" }
]
},
{
code: "var foo, ab, xy; export { foo, ab, xy };",
options: [{ restrictedNamedExportsPattern: ["*+(b|y)"] }],
options: [{ restrictedNamedExportsPattern: ["(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: [{ restrictedNamedExportsPattern: ["*+(b|y)"] }],
options: [{ restrictedNamedExportsPattern: ["(b|y)$"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "ab" }, type: "Identifier" }
]
Expand Down

0 comments on commit 0e66cc8

Please sign in to comment.