Skip to content

Commit

Permalink
fix: use astUtils.getModuleExportName
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jan 20, 2023
1 parent 037b30c commit 4bf7380
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/rules/no-restricted-exports.js
Expand Up @@ -118,27 +118,38 @@ module.exports = {
}

if (name === "default") {
const isSourceSpecified = node.parent.source || node.parent.parent.source;
const specifierLocalName = node.parent.local && node.parent.local.name;
if (node.parent.type === "ExportAllDeclaration") {
const isSourceSpecified = node.parent.source;

if (!isSourceSpecified && restrictDefaultExports && restrictDefaultExports.named) {
context.report({
node,
messageId: "restrictedDefault"
});
return;
}
if (isSourceSpecified && restrictDefaultExports && restrictDefaultExports.namespaceFrom) {
context.report({
node,
messageId: "restrictedDefault"
});
}

} else { // ExportSpecifier
const isSourceSpecified = node.parent.parent.source;
const specifierLocalName = astUtils.getModuleExportName(node.parent.local);

if (isSourceSpecified && restrictDefaultExports) {
if (
(specifierLocalName === "default" && restrictDefaultExports.defaultFrom) ||
(specifierLocalName !== "default" && restrictDefaultExports.namedFrom) ||
(node.parent.type === "ExportAllDeclaration" && restrictDefaultExports.namespaceFrom)
) {
if (!isSourceSpecified && restrictDefaultExports && restrictDefaultExports.named) {
context.report({
node,
messageId: "restrictedDefault"
});
return;
}

if (isSourceSpecified && restrictDefaultExports) {
if (
(specifierLocalName === "default" && restrictDefaultExports.defaultFrom) ||
(specifierLocalName !== "default" && restrictDefaultExports.namedFrom)
) {
context.report({
node,
messageId: "restrictedDefault"
});
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-restricted-exports.js
Expand Up @@ -122,11 +122,13 @@ ruleTester.run("no-restricted-exports", rule, {
{ code: "export { default as default } from 'mod';", options: [{ restrictDefaultExports: { defaultFrom: false } }] },
{ code: "export { foo as default } from 'mod';", options: [{ restrictDefaultExports: { defaultFrom: true } }] },
{ code: "export { default } from 'mod';", options: [{ restrictDefaultExports: { named: true, defaultFrom: false } }] },
{ code: "export { 'default' } from 'mod'; ", options: [{ restrictDefaultExports: { defaultFrom: false } }] },

// restrictDefaultExports.namedFrom option
{ code: "export { foo as default } from 'mod';", options: [{ restrictDefaultExports: { namedFrom: false } }] },
{ code: "export { default as default } from 'mod';", options: [{ restrictDefaultExports: { namedFrom: true } }] },
{ code: "export { default as default } from 'mod';", options: [{ restrictDefaultExports: { namedFrom: false } }] },
{ code: "export { 'default' } from 'mod'; ", options: [{ restrictDefaultExports: { defaultFrom: false, namedFrom: true } }] },

// restrictDefaultExports.namespaceFrom option
{ code: "export * as default from 'mod';", options: [{ restrictDefaultExports: { namespaceFrom: false } }] }
Expand Down Expand Up @@ -583,6 +585,11 @@ ruleTester.run("no-restricted-exports", rule, {
options: [{ restrictDefaultExports: { defaultFrom: true } }],
errors: [{ messageId: "restrictedDefault", type: "Identifier", line: 1, column: 21 }]
},
{
code: "export { 'default' } from 'mod';",
options: [{ restrictDefaultExports: { defaultFrom: true } }],
errors: [{ messageId: "restrictedDefault", type: "Literal", line: 1, column: 10 }]
},

// restrictDefaultExports.namedFrom option
{
Expand Down

0 comments on commit 4bf7380

Please sign in to comment.