Skip to content

Commit

Permalink
Handle case where import path is removed inside handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone authored and generic-probot-app-workflow[bot] committed Apr 3, 2019
1 parent f3274b5 commit 3c5af7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/utils/visit-js-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,12 @@ export const visitJsImport = (
if (sourceName !== sourceNode.source.value) {
return;
}
ipath.node.specifiers.forEach(targetSpecifier => {
const targetSpecifier = ipath.node.specifiers.find(targetSpecifier => {
const targetName = targetSpecifier.local.name;
// $FlowFixMe
const binding = ipath.scope.bindings[targetName];
const refPaths = binding ? binding.referencePaths : [];
const targetKind = targetSpecifier.importKind || ipath.node.importKind;
const sourceKind = sourceSpecifier.importKind || sourceNode.importKind;
if (targetKind !== sourceKind) {
return;
return false;
}
if (
// specifier case
Expand All @@ -86,9 +83,17 @@ export const visitJsImport = (
(isImportDefaultSpecifier(targetSpecifier) &&
isImportDefaultSpecifier(sourceSpecifier))
) {
handler(ipath, refPaths);
return true;
}
return false;
});
if (targetSpecifier) {
const targetName = targetSpecifier.local.name;
// $FlowFixMe
const binding = ipath.scope.bindings[targetName];
const refPaths = binding ? binding.referencePaths : [];
handler(ipath, refPaths);
}
},
});
};
9 changes: 9 additions & 0 deletions src/utils/visit-js-import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,12 @@ test('visitJsImport finds type imports correctly', () => {
);
expect(handler).toHaveBeenCalledTimes(4);
});

test('visitJsImport handling removed paths', () => {
const program = parseJs(`import {a, b} from 'c'`);
expect(() =>
visitJsImport(program, `import {a} from 'c'`, (path, refPaths) => {
path.remove();
})
).not.toThrow();
});

0 comments on commit 3c5af7b

Please sign in to comment.