From 5b2769f27ab7b3517f426fa602024d665e64c0c7 Mon Sep 17 00:00:00 2001 From: Abhishek Anand <54681081+demonhue@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:03:38 +0530 Subject: [PATCH] Handled the case when import statement contains '.ts' and '.tsx' extensions in path (#288) Co-authored-by: Abhishek Co-authored-by: Sean Ryan --- CHANGELOG.md | 4 ++++ example/import-with-ts-extension/src/a.ts | 1 + example/import-with-ts-extension/src/b.ts | 1 + example/import-with-ts-extension/src/unused-1.ts | 1 + example/import-with-ts-extension/src/unused-2.ts | 1 + example/import-with-ts-extension/tsconfig.json | 15 +++++++++++++++ ispec/run.sh | 3 +++ src/analyzer.ts | 4 ++-- src/parser/util.ts | 4 ++-- 9 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 example/import-with-ts-extension/src/a.ts create mode 100644 example/import-with-ts-extension/src/b.ts create mode 100644 example/import-with-ts-extension/src/unused-1.ts create mode 100644 example/import-with-ts-extension/src/unused-2.ts create mode 100644 example/import-with-ts-extension/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index f77180ec..f7ef8a01 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 00000000..9d157cc3 --- /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 00000000..d075f51f --- /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 00000000..6c9193d0 --- /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 00000000..f9bfc82d --- /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 00000000..8eb53e13 --- /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 e5cf89fa..991a1489 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 8a45033f..7d5efb3f 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 00964337..a6dc1200 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); }