Skip to content

Commit

Permalink
feat: add restrictDefaultExports option to no-restricted-exports rule
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jan 14, 2023
1 parent 5981296 commit 206d305
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
48 changes: 47 additions & 1 deletion lib/rules/no-restricted-exports.js
Expand Up @@ -35,19 +35,51 @@ module.exports = {
type: "string"
},
uniqueItems: true
},
restrictDefaultExports: {
type: "object",
properties: {

// Allow/Disallow `export default foo; export default 42; export default function foo() {}` format
direct: {
type: "boolean"
},

// Allow/Disallow `export { foo as default };` format
named: {
type: "boolean"
},

// Allow/Disallow `export { default } from "mod"; export { default as default } from "mod";` format
defaultFrom: {
type: "boolean"
},

// Allow/Disallow `export { foo as default } from "mod";` format
namedFrom: {
type: "boolean"
},

// Allow/Disallow `export * as default from "mod"`; format
namespaceFrom: {
type: "boolean"
}
}
}
},
additionalProperties: false
}],

messages: {
restrictedNamed: "'{{name}}' is restricted from being used as an exported name."
restrictedNamed: "'{{name}}' is restricted from being used as an exported name.",
restrictedDefault: "Exporting the 'default' value is restricted."
}
},

create(context) {

const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
const restrictDefaultExports = context.options[0] && !restrictedNames.has("default") && context.options[0].restrictDefaultExports;

/**
* Checks and reports given exported name.
Expand All @@ -63,6 +95,11 @@ module.exports = {
messageId: "restrictedNamed",
data: { name }
});
} else if (name === "default" && restrictDefaultExports && restrictDefaultExports.named) {
context.report({
node,
messageId: "restrictedDefault"
});
}
}

Expand All @@ -73,6 +110,15 @@ module.exports = {
}
},

ExportDefaultDeclaration(node) {
if (restrictDefaultExports && restrictDefaultExports.direct) {
context.report({
node,
messageId: "restrictedDefault"
});
}
},

ExportNamedDeclaration(node) {
const declaration = node.declaration;

Expand Down
52 changes: 51 additions & 1 deletion tests/lib/rules/no-restricted-exports.js
Expand Up @@ -107,7 +107,16 @@ ruleTester.run("no-restricted-exports", rule, {
{ code: "export default 1;", options: [{ restrictedNamedExports: ["default"] }] },

// "default" does not disallow re-exporting a renamed default export from another module
{ code: "export { default as a } from 'foo';", options: [{ restrictedNamedExports: ["default"] }] }
{ code: "export { default as a } from 'foo';", options: [{ restrictedNamedExports: ["default"] }] },

// restrictDefaultExports.direct option
{ code: "export default foo;", options: [{ restrictDefaultExports: { direct: false } }] },
{ code: "export default 42;", options: [{ restrictDefaultExports: { direct: false } }] },
{ code: "export default function foo() {}", options: [{ restrictDefaultExports: { direct: false } }] },
{ code: "export default foo;", options: [{ restrictedNamedExports: ["default"], restrictDefaultExports: { direct: true } }] },

// restrictDefaultExports.named option
{ code: "const foo = 123;\nexport { foo as default };", options: [{ restrictDefaultExports: { named: false } }] }
],

invalid: [
Expand Down Expand Up @@ -519,6 +528,47 @@ ruleTester.run("no-restricted-exports", rule, {
code: "export { default } from 'foo';",
options: [{ restrictedNamedExports: ["default"] }],
errors: [{ messageId: "restrictedNamed", data: { name: "default" }, type: "Identifier", column: 10 }]
},

// restrictDefaultExports.direct option
{
code: "export default foo;",
options: [{ restrictDefaultExports: { direct: true } }],
errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
},
{
code: "export default 42;",
options: [{ restrictDefaultExports: { direct: true } }],
errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
},
{
code: "export default function foo() {};",
options: [{ restrictDefaultExports: { direct: true } }],
errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
},
{
code: "export default foo;",
options: [{ restrictedNamedExports: ["bar"], restrictDefaultExports: { direct: true } }],
errors: [{ messageId: "restrictedDefault", type: "ExportDefaultDeclaration", column: 1 }]
},

// restrictDefaultExports.named option
{
code: "const foo = 123;\nexport { foo as default };",
options: [{ restrictDefaultExports: { named: true } }],
errors: [{ messageId: "restrictedDefault", type: "Identifier", line: 2, column: 17 }]
},

// restrictedNamedExports should take priority over restrictDefaultExports.named
{
code: "const foo = 123;\nexport { foo as default };",
options: [{ restrictedNamedExports: ["default"], restrictDefaultExports: { named: false } }],
errors: [{ messageId: "restrictedNamed", type: "Identifier", line: 2, column: 17 }]
},
{
code: "const foo = 123;\nexport { foo as default };",
options: [{ restrictedNamedExports: ["default"], restrictDefaultExports: { named: true } }],
errors: [{ messageId: "restrictedNamed", type: "Identifier", line: 2, column: 17 }]
}
]
});

0 comments on commit 206d305

Please sign in to comment.