Skip to content

Commit

Permalink
Handled the case when import statement contains '.ts' and '.tsx' exte…
Browse files Browse the repository at this point in the history
…nsions in path (#288)

Co-authored-by: Abhishek <abhishek.anand1@sprinklr.com>
Co-authored-by: Sean Ryan <sean.ryan@mendix.com>
  • Loading branch information
3 people committed Aug 9, 2023
1 parent 4b37c10 commit 5b2769f
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions example/import-with-ts-extension/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export let a = 5;
1 change: 1 addition & 0 deletions example/import-with-ts-extension/src/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import {a} from "./a.js";
1 change: 1 addition & 0 deletions example/import-with-ts-extension/src/unused-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const unused1 = 1;
1 change: 1 addition & 0 deletions example/import-with-ts-extension/src/unused-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const unused2 = 1;
15 changes: 15 additions & 0 deletions example/import-with-ts-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"],
}
3 changes: 3 additions & 0 deletions ispec/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
indexCandidateExtensions,
indexCandidates,
removeExportStarPrefix,
removeFileExtensionToAllowForJs,
removeFileExtensionToAllowForJsTsJsxTsx,
} from './parser/util';

export { Analysis } from './types';
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/parser/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5b2769f

Please sign in to comment.