Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Commit

Permalink
fix dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
phiresky committed Jun 24, 2020
1 parent bc5b96a commit 22c3430
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
SourceFileReferencingNodes,
ts,
Symbol,
CallExpression,
Node
} from "ts-morph";
import { isDefinitelyUsedImport } from "./util/isDefinitelyUsedImport";
import { getModuleSourceFile } from "./util/getModuleSourceFile";
Expand Down Expand Up @@ -50,9 +52,16 @@ function handleImportDeclaration(node: SourceFileReferencingNodes) {
return referenced;
}

// like import("../xyz")
function handleDynamicImport(node: SourceFileReferencingNodes) {
// a dynamic import always imports all elements, so we can't tell if only some are used
return ["*"];
}

const nodeHandlers = {
[ts.SyntaxKind.ExportDeclaration.toString()]: handleExportDeclaration,
[ts.SyntaxKind.ImportDeclaration.toString()]: handleImportDeclaration
[ts.SyntaxKind.ImportDeclaration.toString()]: handleImportDeclaration,
[ts.SyntaxKind.CallExpression.toString()]: handleDynamicImport,
};

const mustIgnore = (symbol: Symbol, file: SourceFile) => {
Expand Down Expand Up @@ -124,7 +133,7 @@ const emitPotentiallyUnused = (file: SourceFile, onResult: OnResultType) => {

const referenced = ([] as string[]).concat(...referenced2D);

const unused = exported.filter(exp => !referenced.includes(exp.name));
const unused = referenced.includes("*") ? [] : exported.filter(exp => !referenced.includes(exp.name));

onResult({
file: realpathSync(file.getFilePath()),
Expand Down

0 comments on commit 22c3430

Please sign in to comment.