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

Commit

Permalink
fix(core): Ignore files explicitly mentioned in tsconfig.json > files
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
nadeesha committed May 24, 2020
1 parent 5b7ac51 commit b6440ba
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions integration/testproject/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const libExport = "foo";
13 changes: 10 additions & 3 deletions integration/testproject/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
"lib": ["es6"]
"lib": [
"es6"
]
},
"include": ["./src/**/*"]
}
"include": [
"./src/**/*"
],
"files": [
"./src/index.ts"
]
}
16 changes: 14 additions & 2 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "ts-morph";
import { isDefinitelyUsedImport } from "./util/isDefinitelyUsedImport";
import { getModuleSourceFile } from "./util/getModuleSourceFile";
import * as typescript from "typescript";

type OnResultType = (result: IAnalysedResult) => void;

Expand Down Expand Up @@ -84,6 +85,8 @@ const importWildCards = (file: SourceFile) =>
type: AnalysisResultTypeEnum.DEFINITELY_USED
}));



const exportWildCards = (file: SourceFile) =>
file
.getExportDeclarations()
Expand All @@ -97,7 +100,7 @@ const exportWildCards = (file: SourceFile) =>
const emitDefinitelyUsed = (file: SourceFile, onResult: OnResultType) => {
[
...importWildCards(file),
...exportWildCards(file)
...exportWildCards(file),
].forEach(onResult);
};

Expand Down Expand Up @@ -127,9 +130,18 @@ const emitPotentiallyUnused = (file: SourceFile, onResult: OnResultType) => {
});
};

export const analyze = (project: Project, onResult: OnResultType) => {
const emitTsConfigEntrypoints = (entrypoints: string[], onResult: OnResultType) =>
entrypoints.map(file => ({
file,
symbols: [],
type: AnalysisResultTypeEnum.DEFINITELY_USED,
})).forEach(emittable => onResult(emittable))

export const analyze = (project: Project, onResult: OnResultType, entrypoints: string[]) => {
project.getSourceFiles().forEach(file => {
emitPotentiallyUnused(file, onResult);
emitDefinitelyUsed(file, onResult);
});

emitTsConfigEntrypoints(entrypoints, onResult);
};
3 changes: 2 additions & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { present } from "./presenter";
export const run = (argv = process.argv.slice(2), output = console.log) => {
const tsConfigPath = minimist(argv).p || "tsconfig.json";
const { project } = initialize(path.join(process.cwd(), tsConfigPath));
const entrypoints: string[] = require(path.join(process.cwd(), tsConfigPath))?.files?.map((file: string) => path.join(process.cwd(), file));

const state = new State();

analyze(project, state.onResult);
analyze(project, state.onResult, entrypoints);

const presented = present(state);

Expand Down
15 changes: 11 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
"preserveConstEnums": true,
"outDir": "lib",
"sourceMap": true,
"lib": ["esnext"],
"lib": [
"esnext"
],
"esModuleInterop": true
},
"include": ["src/index.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
"files": [
"src/index.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}

0 comments on commit b6440ba

Please sign in to comment.