Skip to content

Commit

Permalink
chore: add docs and use set for given regex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed May 8, 2024
1 parent 0e66cc8 commit 2c4163f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/src/rules/no-restricted-exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +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.
* `"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 @@ -130,6 +131,20 @@ export default function foo() {}

:::

### restrictedNamedExportsPattern

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

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

var privateUserEmail = '...';
export { privateUserEmail };
```

### restrictDefaultExports

This option allows you to restrict certain `default` declarations. The option works only if the `restrictedNamedExports` option does not contain the `"default"` value. This option accepts the following properties:
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +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 || [];
const restrictedNamePatterns = new Set(context.options[0] && context.options[0].restrictedNamedExportsPattern || []);
const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports;
const sourceCode = context.sourceCode;

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

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

return name && patternRegex.test(name);
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,13 @@ ruleTester.run("no-restricted-exports", rule, {
{ messageId: "restrictedNamed", data: { name: "ab" }, type: "Identifier" }
]
},
{
code: "var privateUserEmail; export { privateUserEmail };",
options: [{ restrictedNamedExportsPattern: ["^privateUser"] }],
errors: [
{ messageId: "restrictedNamed", data: { name: "privateUserEmail" }, type: "Identifier" }
]
},

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

0 comments on commit 2c4163f

Please sign in to comment.