diff --git a/rules/prefer-export-from.js b/rules/prefer-export-from.js index 1fe0015f7f..deb20a48c4 100644 --- a/rules/prefer-export-from.js +++ b/rules/prefer-export-from.js @@ -207,7 +207,7 @@ function isVariableUnused(node, context) { } function getImported(variable, sourceCode) { - const specifier = variable.identifiers[0].parent; + const specifier = variable.defs[0].node; const result = { node: specifier, declaration: specifier.parent, @@ -297,17 +297,22 @@ function create(context) { }, * 'Program:exit'(program) { for (const importDeclaration of importDeclarations) { - const variables = context.getDeclaredVariables(importDeclaration) - .map(variable => { - const imported = getImported(variable, sourceCode); - const exports = getExports(imported, context, sourceCode); + let variables = context.getDeclaredVariables(importDeclaration); - return { - variable, - imported, - exports, - }; - }); + if (variables.some(variable => variable.defs.length !== 1 || variable.defs[0].parent !== importDeclaration)) { + continue; + } + + variables = variables.map(variable => { + const imported = getImported(variable, sourceCode); + const exports = getExports(imported, context, sourceCode); + + return { + variable, + imported, + exports, + }; + }); if ( ignoreUsedVariables diff --git a/test/prefer-export-from.mjs b/test/prefer-export-from.mjs index 027cf82da1..dab638c045 100644 --- a/test/prefer-export-from.mjs +++ b/test/prefer-export-from.mjs @@ -330,6 +330,26 @@ test.typescript({ export const useDispatch: () => DispatchAllActions = reduxUseDispatch `, + // #1645 + outdent` + import React from "react"; + import React from "react"; + export {React} + `, + outdent` + type AceEditor = import("ace-builds").Ace.Editor; + import AceEditor from "./advanced-editor"; + `, + outdent` + type AceEditor = import("ace-builds").Ace.Editor; + import AceEditor from "./advanced-editor"; + export {AceEditor}; + `, + outdent` + import AceEditor from "./advanced-editor"; + type AceEditor = import("ace-builds").Ace.Editor; + export {AceEditor}; + `, ], invalid: [], });