Skip to content

Commit

Permalink
prefer-export-from: Fix crash in TypeScript files (#1647)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 20, 2021
1 parent e59a856 commit 1ff8e42
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
27 changes: 16 additions & 11 deletions rules/prefer-export-from.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/prefer-export-from.mjs
Expand Up @@ -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: [],
});
Expand Down

0 comments on commit 1ff8e42

Please sign in to comment.