From 48fe86538f53069a3fcac5f19142c208bbce56e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C3=B3dkiewicz?= Date: Tue, 6 Aug 2024 11:16:19 +0200 Subject: [PATCH] [New] `no-named-as-default`: ignore if imported obj is exported both as default and named --- src/rules/no-named-as-default.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/rules/no-named-as-default.js b/src/rules/no-named-as-default.js index 5b24f8e88..4ae6abf41 100644 --- a/src/rules/no-named-as-default.js +++ b/src/rules/no-named-as-default.js @@ -15,8 +15,15 @@ module.exports = { create(context) { function checkDefault(nameKey, defaultSpecifier) { + + /** + * For ImportDefaultSpecifier we're interested in the "local" name (`foo` for `import {bar as foo} ...`) + * For ExportDefaultSpecifier we're interested in the "exported" name (`foo` for `export {bar as foo} ...`) + */ + const analyzedName = defaultSpecifier[nameKey].name; + // #566: default is a valid specifier - if (defaultSpecifier[nameKey].name === 'default') { return; } + if (analyzedName === 'default') { return; } const declaration = importDeclaration(context); @@ -28,7 +35,15 @@ module.exports = { return; } - if (imports.has('default') && imports.has(defaultSpecifier[nameKey].name)) { + if (imports.has('default') && imports.has(analyzedName)) { + + // #1594: the imported module exports the same thing via a default export and a named export + const namedExportInImportedModule = imports.reexports.get(analyzedName); + const defaultExportInImportedModule = imports.reexports.get('default'); + if (defaultExportInImportedModule.getImport().path === namedExportInImportedModule.getImport().path + && defaultExportInImportedModule.local === namedExportInImportedModule.local) { + return; + } context.report( defaultSpecifier,