diff --git a/CHANGELOG.md b/CHANGELOG.md index f77180e..f7ef8a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [9.0.6] - 26 Jun 2023 + +- Handled the case when import statement contains '.ts' and '.tsx' extensions in path + ## [9.0.5] - 24 Jun 2023 - Add --ignoreLocallyUsed flag which means that exports which are used in the same file they are defined in won't be reported as unused diff --git a/example/import-with-ts-extension/src/a.ts b/example/import-with-ts-extension/src/a.ts new file mode 100644 index 0000000..9d157cc --- /dev/null +++ b/example/import-with-ts-extension/src/a.ts @@ -0,0 +1 @@ +export let a = 5; \ No newline at end of file diff --git a/example/import-with-ts-extension/src/b.ts b/example/import-with-ts-extension/src/b.ts new file mode 100644 index 0000000..d075f51 --- /dev/null +++ b/example/import-with-ts-extension/src/b.ts @@ -0,0 +1 @@ +import {a} from "./a.js"; \ No newline at end of file diff --git a/example/import-with-ts-extension/src/unused-1.ts b/example/import-with-ts-extension/src/unused-1.ts new file mode 100644 index 0000000..6c9193d --- /dev/null +++ b/example/import-with-ts-extension/src/unused-1.ts @@ -0,0 +1 @@ +export const unused1 = 1; diff --git a/example/import-with-ts-extension/src/unused-2.ts b/example/import-with-ts-extension/src/unused-2.ts new file mode 100644 index 0000000..f9bfc82 --- /dev/null +++ b/example/import-with-ts-extension/src/unused-2.ts @@ -0,0 +1 @@ +export const unused2 = 1; diff --git a/example/import-with-ts-extension/tsconfig.json b/example/import-with-ts-extension/tsconfig.json new file mode 100644 index 0000000..8eb53e1 --- /dev/null +++ b/example/import-with-ts-extension/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "target": "es5", + "noEmit": true, + "noImplicitAny": true, + "strictNullChecks": true, + "moduleResolution": "node", + "paths": { + "utils": ["utils/src"], + "utils/*": ["utils/src/*"] + }, + }, + "include": ["src/**/*.ts"], +} diff --git a/ispec/run.sh b/ispec/run.sh index e5cf89f..991a148 100755 --- a/ispec/run.sh +++ b/ispec/run.sh @@ -18,6 +18,9 @@ function install_and_run_itest() { npm ci > /dev/null && run_itest } +pushd ../example/import-with-ts-extension +run_itest +popd pushd ../example/simple-zero-issues run_itest_expect_zero_issues diff --git a/src/analyzer.ts b/src/analyzer.ts index 8a45033..7d5efb3 100644 --- a/src/analyzer.ts +++ b/src/analyzer.ts @@ -9,7 +9,7 @@ import { indexCandidateExtensions, indexCandidates, removeExportStarPrefix, - removeFileExtensionToAllowForJs, + removeFileExtensionToAllowForJsTsJsxTsx, } from './parser/util'; export { Analysis } from './types'; @@ -78,7 +78,7 @@ const getExportMap = (files: File[]): ExportMap => { const processImports = (file: File, exportMap: ExportMap): void => { Object.keys(file.imports).forEach((key) => { const importedFileExports = - exportMap[removeFileExtensionToAllowForJs(key)] || null; + exportMap[removeFileExtensionToAllowForJsTsJsxTsx(key)] || null; let ex = importedFileExports?.exports || null; diff --git a/src/parser/util.ts b/src/parser/util.ts index 0096433..a6dc120 100644 --- a/src/parser/util.ts +++ b/src/parser/util.ts @@ -27,9 +27,9 @@ export const indexCandidateExtensions = [ '.mjs', ]; -export function removeFileExtensionToAllowForJs(path: string): string { +export function removeFileExtensionToAllowForJsTsJsxTsx(path: string): string { // ref: https://www.typescriptlang.org/docs/handbook/esm-node.html - const extensionsToStrip = ['.js', '.cjs', '.mjs']; + const extensionsToStrip = ['.js', '.jsx', '.cjs', '.mjs', '.ts', '.tsx']; return stripExtensionsFromPath(extensionsToStrip, path); }