From 4bf73804d94bf8443bcc868febd594f3c1253768 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 20 Jan 2023 07:29:23 +0530 Subject: [PATCH] fix: use astUtils.getModuleExportName --- lib/rules/no-restricted-exports.js | 41 +++++++++++++++--------- tests/lib/rules/no-restricted-exports.js | 7 ++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/rules/no-restricted-exports.js b/lib/rules/no-restricted-exports.js index c3b44b68387..d57c5438859 100644 --- a/lib/rules/no-restricted-exports.js +++ b/lib/rules/no-restricted-exports.js @@ -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" + }); + } } } } diff --git a/tests/lib/rules/no-restricted-exports.js b/tests/lib/rules/no-restricted-exports.js index 49827c38443..18c8db99008 100644 --- a/tests/lib/rules/no-restricted-exports.js +++ b/tests/lib/rules/no-restricted-exports.js @@ -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 } }] } @@ -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 {