Skip to content

Commit

Permalink
chore: review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed May 9, 2024
1 parent 2c4163f commit c881975
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
6 changes: 3 additions & 3 deletions docs/src/rules/no-restricted-exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ By default, this rule doesn't disallow any names. Only the names you specify in
This rule has an object option:

* `"restrictedNamedExports"` is an array of strings, where each string is a name to be restricted.
* `"restrictedNamedExportsPattern"` is an array of regex strings, where each string regex pattern would be restricted.
* `"restrictedNamedExportsPattern"` is a regex string, and any named export matching that pattern would be restricted.
* `"restrictDefaultExports"` is an object option with boolean properties to restrict certain default export declarations. The option works only if the `restrictedNamedExports` option does not contain the `"default"` value. The following properties are allowed:
* `direct`: restricts `export default` declarations.
* `named`: restricts `export { foo as default };` declarations.
Expand Down Expand Up @@ -133,12 +133,12 @@ export default function foo() {}

### restrictedNamedExportsPattern

This option allows you to restrict regex patterns for named exports.
This option allows you to restrict regex pattern for named exports.

```js
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExports": [],
"restrictedNamedExportsPattern": ["^privateUser"]
"restrictedNamedExportsPattern": "^privateUser"
}]*/

var privateUserEmail = '...';
Expand Down
34 changes: 9 additions & 25 deletions lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ module.exports = {

schema: [{
anyOf: [
{
type: "object",
properties: {
restrictedNamedExports: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
},
additionalProperties: false
},
{
type: "object",
properties: {
Expand All @@ -51,13 +38,7 @@ module.exports = {
},
uniqueItems: true
},
restrictedNamedExportsPattern: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
restrictedNamedExportsPattern: { type: "string" }
},
additionalProperties: false
},
Expand All @@ -72,6 +53,7 @@ module.exports = {
},
uniqueItems: true
},
restrictedNamedExportsPattern: { type: "string" },
restrictDefaultExports: {
type: "object",
properties: {
Expand Down Expand Up @@ -118,7 +100,7 @@ module.exports = {
create(context) {

const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
const restrictedNamePatterns = new Set(context.options[0] && context.options[0].restrictedNamedExportsPattern || []);
const restrictedNamePattern = context.options[0] && context.options[0].restrictedNamedExportsPattern;
const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports;
const sourceCode = context.sourceCode;

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

const hasRestrictedNamePattern = [...restrictedNamePatterns].some(pattern => {
const patternRegex = new RegExp(pattern, "u");
let hasRestrictedNamePattern = false;

return name && patternRegex.test(name);
});
if (restrictedNamePattern) {
const patternRegex = new RegExp(restrictedNamePattern, "u");

hasRestrictedNamePattern = name && patternRegex.test(name);
}

if (hasRestrictedNamePattern || restrictedNames.has(name)) {
context.report({
Expand Down
12 changes: 6 additions & 6 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 @@ -540,36 +540,36 @@ ruleTester.run("no-restricted-exports", rule, {
// restrictedNamedExportsPattern
{
code: "var getSomething; export { getSomething };",
options: [{ restrictedNamedExportsPattern: ["get*"] }],
options: [{ restrictedNamedExportsPattern: "get*" }],
errors: [
{ messageId: "restrictedNamed", data: { name: "getSomething" }, type: "Identifier" }
]
},
{
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" }
]
},
{
code: "var privateUserEmail; export { privateUserEmail };",
options: [{ restrictedNamedExportsPattern: ["^privateUser"] }],
options: [{ restrictedNamedExportsPattern: "^privateUser" }],
errors: [
{ messageId: "restrictedNamed", data: { name: "privateUserEmail" }, type: "Identifier" }
]
Expand Down

0 comments on commit c881975

Please sign in to comment.