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: add restrictedNamedExportsPattern to no-restricted-exports #18431

Merged
29 changes: 28 additions & 1 deletion lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ module.exports = {
},
additionalProperties: false
},
{
type: "object",
properties: {
restrictedNamedExports: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
},
restrictedNamedExportsPattern: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
},
additionalProperties: false
},
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
{
type: "object",
properties: {
Expand Down Expand Up @@ -98,6 +118,7 @@ module.exports = {
create(context) {

const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
const restrictedNamePatterns = context.options[0] && context.options[0].restrictedNamedExportsPattern || [];
kecrily marked this conversation as resolved.
Show resolved Hide resolved
const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports;
const sourceCode = context.sourceCode;

Expand All @@ -109,7 +130,13 @@ module.exports = {
function checkExportedName(node) {
const name = astUtils.getModuleExportName(node);

if (restrictedNames.has(name)) {
const hasRestrictedNamePattern = restrictedNamePatterns.some(pattern => {
const patternRegex = new RegExp(pattern, "u");

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

if (hasRestrictedNamePattern || restrictedNames.has(name)) {
context.report({
node,
messageId: "restrictedNamed",
Expand Down
35 changes: 35 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: [{ restrictedNamedExportsPattern: ["^get"] }]
},
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

// does not check re-export all declarations
{ code: "export * from 'foo';", options: [{ restrictedNamedExports: ["a"] }] },
Expand Down Expand Up @@ -533,6 +537,37 @@ ruleTester.run("no-restricted-exports", rule, {
]
},

// restrictedNamedExportsPattern
{
code: "var getSomething; export { getSomething };",
options: [{ restrictedNamedExportsPattern: ["get*"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "getSomething" }, type: "Identifier" }
]
},
{
code: "var getSomethingFromUser; export { getSomethingFromUser };",
options: [{ restrictedNamedExportsPattern: ["User$"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "getSomethingFromUser" }, type: "Identifier" }
]
},
{
code: "var foo, ab, xy; export { foo, ab, xy };",
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)$"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "ab" }, type: "Identifier" }
]
},

// reports "default" in named export declarations (when configured)
{
code: "var a; export { a as default };",
Expand Down