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

Commit

Permalink
feat(core): Fixing false positives on wildcard exports (#55)
Browse files Browse the repository at this point in the history
* feat(core): Supporting export wildcard

* Refactor code
  • Loading branch information
nadeesha authored May 23, 2020
1 parent 51faf23 commit 0790a03
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions integration/testproject/src/barrel/F.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const one = 1;
export const two = 2;
1 change: 1 addition & 0 deletions integration/testproject/src/barrel/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { KualaOrLumpur } from './E';
export * from "./F";
2 changes: 1 addition & 1 deletion integration/testproject/src/importE.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import { KualaOrLumpur } from './barrel';
import * as barrel from './barrel';
30 changes: 22 additions & 8 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,35 @@ function getExported(file: SourceFile) {
return exported;
}

const emitDefinitelyUsed = (file: SourceFile, onResult: OnResultType) => {
const importWildCards = (file: SourceFile) =>
file
.getImportDeclarations()
.map(decl => ({
moduleSourceFile: getModuleSourceFile(decl),
definitelyUsed: isDefinitelyUsedImport(decl)
}))
.filter(meta => meta.definitelyUsed && !!meta.moduleSourceFile)
.forEach(({ moduleSourceFile }) => {
onResult({
file: moduleSourceFile,
symbols: [],
type: AnalysisResultTypeEnum.DEFINITELY_USED
});
});
.map(({ moduleSourceFile }) => ({
file: moduleSourceFile,
symbols: [],
type: AnalysisResultTypeEnum.DEFINITELY_USED
}));

const exportWildCards = (file: SourceFile) =>
file
.getExportDeclarations()
.filter(decl => decl.getText().includes("*"))
.map((decl) => ({
file: getModuleSourceFile(decl),
symbols: [],
type: AnalysisResultTypeEnum.DEFINITELY_USED
}));

const emitDefinitelyUsed = (file: SourceFile, onResult: OnResultType) => {
[
...importWildCards(file),
...exportWildCards(file)
].forEach(onResult);
};

const emitPotentiallyUnused = (file: SourceFile, onResult: OnResultType) => {
Expand Down
4 changes: 2 additions & 2 deletions src/util/getModuleSourceFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ImportDeclaration } from "ts-morph";
import { ImportDeclaration, ExportDeclaration } from "ts-morph";
import { Maybe } from "true-myth";

export const getModuleSourceFile = (decl: ImportDeclaration) =>
export const getModuleSourceFile = (decl: ImportDeclaration | ExportDeclaration) =>
Maybe.fromNullable(decl.getModuleSpecifierSourceFile()).mapOr(
null as string | null,
file => file.getFilePath()
Expand Down
1 change: 1 addition & 0 deletions src/util/isDefinitelyUsedImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const containsWildcardImport = (decl: ImportDeclaration) =>
.mapOr("" as string, clause => clause.getText())
.includes("*");


const containsUnnamedImport = (decl: ImportDeclaration) =>
!decl.getImportClause();

Expand Down

0 comments on commit 0790a03

Please sign in to comment.