From a14db2d520997a624977ccc0ef324704332e0450 Mon Sep 17 00:00:00 2001 From: Sean Ryan Date: Sun, 5 Feb 2023 20:26:16 +0100 Subject: [PATCH] Improve support for importing from index file, with or without custom paths or /index.x - #266 (#267) Improve support for importing from index file, with or without custom paths or /index.x * Add example to reproduce with import @x * Working example - issue is the imported file is index.ts * Improve support for importing from index file, with or without custom paths or /index.x * Run itests using npm ci to preserve package lock versions * Add more index itest cases * Bump version * Up CHANGELOG --- CHANGELOG.md | 7 + CONTRIBUTING.md | 2 +- example/.gitignore | 1 + .../package-lock.json | 38 ++- .../absolute-paths-2-tsconfigs/package.json | 2 +- .../project/project-1/src/app.ts | 3 + .../src/config/components/index.ts | 1 + .../absolute-paths-simple/package-lock.json | 24 +- example/absolute-paths-simple/package.json | 2 +- .../src/components/index.ts | 1 + example/absolute-paths-simple/src/main.ts | 1 + .../library-usage-via-JavaScript/execute.sh | 2 +- .../library-usage-via-TypeScript/execute.sh | 2 +- .../library-usage-via-TypeScript/package.json | 2 +- .../.gitignore | 1 + .../execute.sh | 1 + .../package-lock.json | 248 ++++++++++++++++++ .../package.json | 12 + .../src/foo/index.ts | 3 + .../src/foo/other.ts | 1 + .../src/foo2/index.ts | 5 + .../src/foo2/other.ts | 1 + .../src/foo3/index.ts | 2 + .../src/foo3/other.ts | 1 + .../src/index.ts | 12 + .../tsconfig.json | 36 +++ .../.gitignore | 1 + .../execute.sh | 1 + .../package-lock.json | 248 ++++++++++++++++++ .../package.json | 12 + .../src/foo/index.ts | 2 + .../src/foo/other.ts | 2 + .../src/foo2/index.ts | 1 + .../src/foo2/other.ts | 1 + .../src/index.ts | 10 + .../tsconfig.json | 35 +++ example/simple-es6/package.json | 2 +- example/tsx/package-lock.json | 83 +++++- example/tsx/package.json | 2 +- example/with-paths/package-lock.json | 37 ++- example/with-paths/package.json | 2 +- ispec/run.sh | 6 +- package-lock.json | 2 +- package.json | 2 +- src/analyzer.ts | 23 +- src/parser/common.ts | 4 +- src/parser/util.ts | 10 + 47 files changed, 864 insertions(+), 33 deletions(-) create mode 100644 example/.gitignore create mode 100644 example/absolute-paths-2-tsconfigs/src/config/components/index.ts create mode 100644 example/absolute-paths-simple/src/components/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/.gitignore create mode 100755 example/path-alias-and-sub-folders-import-from-index-2/execute.sh create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/package-lock.json create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/package.json create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/src/foo/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/src/foo/other.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/src/foo2/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/src/foo2/other.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/src/foo3/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/src/foo3/other.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/src/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index-2/tsconfig.json create mode 100644 example/path-alias-and-sub-folders-import-from-index/.gitignore create mode 100755 example/path-alias-and-sub-folders-import-from-index/execute.sh create mode 100644 example/path-alias-and-sub-folders-import-from-index/package-lock.json create mode 100644 example/path-alias-and-sub-folders-import-from-index/package.json create mode 100644 example/path-alias-and-sub-folders-import-from-index/src/foo/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index/src/foo/other.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index/src/foo2/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index/src/foo2/other.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index/src/index.ts create mode 100644 example/path-alias-and-sub-folders-import-from-index/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ab65b0b..898ff0ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [9.0.3] - 5 Feb 2023 + +### Changed + +- Fix for import from index file via a custom path - avoids false positives on index files - #266. Now tested with more cases such as import from "foo" OR import from "foo/index" with various file extensions supported. +- ts-unused-exports npm package: Updated the version in package-lock.json + ## [9.0.2] - 15 Jan 2023 ### Added diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6cebc303..6e432dff 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,7 +16,7 @@ Fork, then clone the repo: Install dependencies: - npm i + npm ci Make sure the tests pass: diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 00000000..79b5594d --- /dev/null +++ b/example/.gitignore @@ -0,0 +1 @@ +**/.DS_Store diff --git a/example/absolute-paths-2-tsconfigs/package-lock.json b/example/absolute-paths-2-tsconfigs/package-lock.json index ac4f3e2a..3b64b215 100644 --- a/example/absolute-paths-2-tsconfigs/package-lock.json +++ b/example/absolute-paths-2-tsconfigs/package-lock.json @@ -1,11 +1,33 @@ { - "requires": true, - "lockfileVersion": 1, - "dependencies": { - "typescript": { - "version": "3.6.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", - "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==" - } + "name": "absolute-paths-2-tsconfigs", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "license": "MIT", + "dependencies": { + "typescript": "^3.6.4" + }, + "devDependencies": {} + }, + "node_modules/typescript": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", + "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } } + }, + "dependencies": { + "typescript": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", + "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==" + } + } } diff --git a/example/absolute-paths-2-tsconfigs/package.json b/example/absolute-paths-2-tsconfigs/package.json index 81c9cba4..ae1bb81f 100644 --- a/example/absolute-paths-2-tsconfigs/package.json +++ b/example/absolute-paths-2-tsconfigs/package.json @@ -7,7 +7,7 @@ "license": "MIT", "scripts": { "build": "tsc -p ./tsconfig.json", - "test": "npm i && npm run build && npm run test:test", + "test": "npm ci && npm run build && npm run test:test", "test:test": "../../bin/ts-unused-exports ./tsconfig.json" }, "dependencies": { diff --git a/example/absolute-paths-2-tsconfigs/project/project-1/src/app.ts b/example/absolute-paths-2-tsconfigs/project/project-1/src/app.ts index 23242d24..ecbe20fe 100644 --- a/example/absolute-paths-2-tsconfigs/project/project-1/src/app.ts +++ b/example/absolute-paths-2-tsconfigs/project/project-1/src/app.ts @@ -1,5 +1,8 @@ import { MyComponent1 } from 'components/MyComponent'; +import { fromIndex } from 'components/index'; const comp1: MyComponent1 = new MyComponent1(); export class UnusedComponent {} + +console.log(fromIndex); diff --git a/example/absolute-paths-2-tsconfigs/src/config/components/index.ts b/example/absolute-paths-2-tsconfigs/src/config/components/index.ts new file mode 100644 index 00000000..1782a525 --- /dev/null +++ b/example/absolute-paths-2-tsconfigs/src/config/components/index.ts @@ -0,0 +1 @@ +export const fromIndex = "foo2"; diff --git a/example/absolute-paths-simple/package-lock.json b/example/absolute-paths-simple/package-lock.json index 56b33326..7b970b45 100644 --- a/example/absolute-paths-simple/package-lock.json +++ b/example/absolute-paths-simple/package-lock.json @@ -1,8 +1,30 @@ { "name": "absolute-paths-simple", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "absolute-paths-simple", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "typescript": "^3.7.4" + } + }, + "node_modules/typescript": { + "version": "3.7.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.5.tgz", + "integrity": "sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + } + }, "dependencies": { "typescript": { "version": "3.7.5", diff --git a/example/absolute-paths-simple/package.json b/example/absolute-paths-simple/package.json index ec4dc463..8696c302 100644 --- a/example/absolute-paths-simple/package.json +++ b/example/absolute-paths-simple/package.json @@ -5,7 +5,7 @@ "main": "main.js", "scripts": { "build": "tsc", - "test": "npm i && npm run build && ../../bin/ts-unused-exports ./tsconfig.json --ignorePaths=to-ignore" + "test": "npm ci && npm run build && ../../bin/ts-unused-exports ./tsconfig.json --ignorePaths=to-ignore" }, "dependencies": { "typescript": "^3.7.4" diff --git a/example/absolute-paths-simple/src/components/index.ts b/example/absolute-paths-simple/src/components/index.ts new file mode 100644 index 00000000..1782a525 --- /dev/null +++ b/example/absolute-paths-simple/src/components/index.ts @@ -0,0 +1 @@ +export const fromIndex = "foo2"; diff --git a/example/absolute-paths-simple/src/main.ts b/example/absolute-paths-simple/src/main.ts index c8fab618..bfaaec07 100644 --- a/example/absolute-paths-simple/src/main.ts +++ b/example/absolute-paths-simple/src/main.ts @@ -1,3 +1,4 @@ import { MyComponent1 } from 'components/MyComponent'; +import { fromIndex } from 'components'; export class UnusedClassFromMain {} diff --git a/example/library-usage-via-JavaScript/execute.sh b/example/library-usage-via-JavaScript/execute.sh index 1deb5700..7f6f733d 100755 --- a/example/library-usage-via-JavaScript/execute.sh +++ b/example/library-usage-via-JavaScript/execute.sh @@ -1 +1 @@ -npm i && npm start +npm ci && npm start diff --git a/example/library-usage-via-TypeScript/execute.sh b/example/library-usage-via-TypeScript/execute.sh index 580c6e24..1c200c3a 100755 --- a/example/library-usage-via-TypeScript/execute.sh +++ b/example/library-usage-via-TypeScript/execute.sh @@ -1 +1 @@ -npm i && npm build && npm start \ No newline at end of file +npm ci && npm build && npm start \ No newline at end of file diff --git a/example/library-usage-via-TypeScript/package.json b/example/library-usage-via-TypeScript/package.json index fe0b47cb..efa641a2 100644 --- a/example/library-usage-via-TypeScript/package.json +++ b/example/library-usage-via-TypeScript/package.json @@ -4,7 +4,7 @@ "scripts": { "build": "tsc", "start": "node ./dist/app.js", - "test": "npm i && ./node_modules/bin/ts-unused-exports ./tsconfig.json" + "test": "npm ci && ./node_modules/bin/ts-unused-exports ./tsconfig.json" }, "dependencies": { "ts-unused-exports": "^9.0.1", diff --git a/example/path-alias-and-sub-folders-import-from-index-2/.gitignore b/example/path-alias-and-sub-folders-import-from-index-2/.gitignore new file mode 100644 index 00000000..2cb7d2a2 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/.gitignore @@ -0,0 +1 @@ +**/*.js diff --git a/example/path-alias-and-sub-folders-import-from-index-2/execute.sh b/example/path-alias-and-sub-folders-import-from-index-2/execute.sh new file mode 100755 index 00000000..a62365bf --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/execute.sh @@ -0,0 +1 @@ +npm ci && npm test diff --git a/example/path-alias-and-sub-folders-import-from-index-2/package-lock.json b/example/path-alias-and-sub-folders-import-from-index-2/package-lock.json new file mode 100644 index 00000000..771e9bc8 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/package-lock.json @@ -0,0 +1,248 @@ +{ + "name": "path-alias-and-sub-folders-2", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "dependencies": { + "ts-unused-exports": "^9.0.2", + "typescript": "^4.9.4" + } + }, + "node_modules/@types/json5": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", + "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==" + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "engines": { + "node": ">=8" + } + }, + "node_modules/json5": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz", + "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==", + "dependencies": { + "minimist": "^1.2.0" + }, + "bin": { + "json5": "lib/cli.js" + } + }, + "node_modules/minimist": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==", + "engines": { + "node": ">=4" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/ts-unused-exports": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/ts-unused-exports/-/ts-unused-exports-9.0.2.tgz", + "integrity": "sha512-rBHG0Hkz61uyOHf95ZQdbiYioqOZKk8Y+bFuj4FUZ1VRCDHelBjkwckOgOcQKvli5fQhH4NHWkw4TTgoFxNS/A==", + "dependencies": { + "chalk": "^4.0.0", + "tsconfig-paths": "^3.9.0" + }, + "bin": { + "ts-unused-exports": "bin/ts-unused-exports" + }, + "funding": { + "url": "https://github.com/pzavolinsky/ts-unused-exports?sponsor=1" + }, + "peerDependencies": { + "typescript": ">=3.8.3" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": false + } + } + }, + "node_modules/tsconfig-paths": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz", + "integrity": "sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==", + "dependencies": { + "@types/json5": "^0.0.29", + "json5": "^1.0.1", + "minimist": "^1.2.6", + "strip-bom": "^3.0.0" + } + }, + "node_modules/typescript": { + "version": "4.9.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", + "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + } + }, + "dependencies": { + "@types/json5": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", + "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==" + }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, + "json5": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz", + "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==", + "requires": { + "minimist": "^1.2.0" + } + }, + "minimist": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==" + }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==" + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + }, + "ts-unused-exports": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/ts-unused-exports/-/ts-unused-exports-9.0.2.tgz", + "integrity": "sha512-rBHG0Hkz61uyOHf95ZQdbiYioqOZKk8Y+bFuj4FUZ1VRCDHelBjkwckOgOcQKvli5fQhH4NHWkw4TTgoFxNS/A==", + "requires": { + "chalk": "^4.0.0", + "tsconfig-paths": "^3.9.0" + } + }, + "tsconfig-paths": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz", + "integrity": "sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==", + "requires": { + "@types/json5": "^0.0.29", + "json5": "^1.0.1", + "minimist": "^1.2.6", + "strip-bom": "^3.0.0" + } + }, + "typescript": { + "version": "4.9.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", + "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==" + } + } +} diff --git a/example/path-alias-and-sub-folders-import-from-index-2/package.json b/example/path-alias-and-sub-folders-import-from-index-2/package.json new file mode 100644 index 00000000..05d32bd8 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/package.json @@ -0,0 +1,12 @@ +{ + "type": "module", + "scripts": { + "build": "tsc -p ./tsconfig.json", + "start": "npm run-script build && node ./dist/index.js", + "test": "../../bin/ts-unused-exports ./tsconfig.json" + }, + "dependencies": { + "ts-unused-exports": "^9.0.2", + "typescript": "^4.9.4" + } +} diff --git a/example/path-alias-and-sub-folders-import-from-index-2/src/foo/index.ts b/example/path-alias-and-sub-folders-import-from-index-2/src/foo/index.ts new file mode 100644 index 00000000..dafb816a --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/src/foo/index.ts @@ -0,0 +1,3 @@ +export const fooFromIndex = "foo"; + +export function unusedFromFooIndex() {} diff --git a/example/path-alias-and-sub-folders-import-from-index-2/src/foo/other.ts b/example/path-alias-and-sub-folders-import-from-index-2/src/foo/other.ts new file mode 100644 index 00000000..a280770e --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/src/foo/other.ts @@ -0,0 +1 @@ +export const fooOther = "foo"; diff --git a/example/path-alias-and-sub-folders-import-from-index-2/src/foo2/index.ts b/example/path-alias-and-sub-folders-import-from-index-2/src/foo2/index.ts new file mode 100644 index 00000000..c06d826d --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/src/foo2/index.ts @@ -0,0 +1,5 @@ +export const foo2FromIndex = "foo2"; +export function unusedFromFoo2Index() {} + +import * as foo3 from "foo3"; +import * as foo3other from "foo3/other"; diff --git a/example/path-alias-and-sub-folders-import-from-index-2/src/foo2/other.ts b/example/path-alias-and-sub-folders-import-from-index-2/src/foo2/other.ts new file mode 100644 index 00000000..143cb3cb --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/src/foo2/other.ts @@ -0,0 +1 @@ +export const foo2Other = "foo2"; diff --git a/example/path-alias-and-sub-folders-import-from-index-2/src/foo3/index.ts b/example/path-alias-and-sub-folders-import-from-index-2/src/foo3/index.ts new file mode 100644 index 00000000..696bcf63 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/src/foo3/index.ts @@ -0,0 +1,2 @@ +export const foo3FromIndex = "foo2"; +export function unusedFromFoo3Index() {} diff --git a/example/path-alias-and-sub-folders-import-from-index-2/src/foo3/other.ts b/example/path-alias-and-sub-folders-import-from-index-2/src/foo3/other.ts new file mode 100644 index 00000000..143cb3cb --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/src/foo3/other.ts @@ -0,0 +1 @@ +export const foo2Other = "foo2"; diff --git a/example/path-alias-and-sub-folders-import-from-index-2/src/index.ts b/example/path-alias-and-sub-folders-import-from-index-2/src/index.ts new file mode 100644 index 00000000..99f91f8a --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/src/index.ts @@ -0,0 +1,12 @@ +import { foo2FromIndex } from "@foo2"; +import { foo2Other } from "@foo2/other"; +import { fooFromIndex } from "@foo/index"; +import { fooOther } from "@foo/other"; + +export function unusedFromIndex() {} + +console.log(fooFromIndex); +console.log(fooOther); + +console.log(foo2FromIndex); +console.log(foo2Other); diff --git a/example/path-alias-and-sub-folders-import-from-index-2/tsconfig.json b/example/path-alias-and-sub-folders-import-from-index-2/tsconfig.json new file mode 100644 index 00000000..2a9475b8 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index-2/tsconfig.json @@ -0,0 +1,36 @@ +{ + "include": ["src"], + + "compilerOptions": { + // Type Checking + "strict": true, + + // Modules + "baseUrl": ".", + "module": "ESNext", + "moduleResolution": "node", + "paths": { + "@foo/*": ["src/foo/*"], + "@foo2": ["src/foo2/index"], + "@foo2/*": ["src/foo2/*"], + "@foo3/*": ["src/foo3/*"], + }, + + // Emit + // "noEmit": true, + + // Interop Constraints + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "isolatedModules": true, + + // Language and Environment + // "jsx": "react-jsx", + "lib": ["DOM", "DOM.Iterable", "ESNext"], + "target": "ESNext", + + // Completeness + "skipLibCheck": true, + "outDir": "./dist" + } +} diff --git a/example/path-alias-and-sub-folders-import-from-index/.gitignore b/example/path-alias-and-sub-folders-import-from-index/.gitignore new file mode 100644 index 00000000..2cb7d2a2 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/.gitignore @@ -0,0 +1 @@ +**/*.js diff --git a/example/path-alias-and-sub-folders-import-from-index/execute.sh b/example/path-alias-and-sub-folders-import-from-index/execute.sh new file mode 100755 index 00000000..a62365bf --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/execute.sh @@ -0,0 +1 @@ +npm ci && npm test diff --git a/example/path-alias-and-sub-folders-import-from-index/package-lock.json b/example/path-alias-and-sub-folders-import-from-index/package-lock.json new file mode 100644 index 00000000..771e9bc8 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/package-lock.json @@ -0,0 +1,248 @@ +{ + "name": "path-alias-and-sub-folders-2", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "dependencies": { + "ts-unused-exports": "^9.0.2", + "typescript": "^4.9.4" + } + }, + "node_modules/@types/json5": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", + "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==" + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "engines": { + "node": ">=8" + } + }, + "node_modules/json5": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz", + "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==", + "dependencies": { + "minimist": "^1.2.0" + }, + "bin": { + "json5": "lib/cli.js" + } + }, + "node_modules/minimist": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==", + "engines": { + "node": ">=4" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/ts-unused-exports": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/ts-unused-exports/-/ts-unused-exports-9.0.2.tgz", + "integrity": "sha512-rBHG0Hkz61uyOHf95ZQdbiYioqOZKk8Y+bFuj4FUZ1VRCDHelBjkwckOgOcQKvli5fQhH4NHWkw4TTgoFxNS/A==", + "dependencies": { + "chalk": "^4.0.0", + "tsconfig-paths": "^3.9.0" + }, + "bin": { + "ts-unused-exports": "bin/ts-unused-exports" + }, + "funding": { + "url": "https://github.com/pzavolinsky/ts-unused-exports?sponsor=1" + }, + "peerDependencies": { + "typescript": ">=3.8.3" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": false + } + } + }, + "node_modules/tsconfig-paths": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz", + "integrity": "sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==", + "dependencies": { + "@types/json5": "^0.0.29", + "json5": "^1.0.1", + "minimist": "^1.2.6", + "strip-bom": "^3.0.0" + } + }, + "node_modules/typescript": { + "version": "4.9.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", + "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + } + }, + "dependencies": { + "@types/json5": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", + "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==" + }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, + "json5": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.2.tgz", + "integrity": "sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==", + "requires": { + "minimist": "^1.2.0" + } + }, + "minimist": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==" + }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==" + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + }, + "ts-unused-exports": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/ts-unused-exports/-/ts-unused-exports-9.0.2.tgz", + "integrity": "sha512-rBHG0Hkz61uyOHf95ZQdbiYioqOZKk8Y+bFuj4FUZ1VRCDHelBjkwckOgOcQKvli5fQhH4NHWkw4TTgoFxNS/A==", + "requires": { + "chalk": "^4.0.0", + "tsconfig-paths": "^3.9.0" + } + }, + "tsconfig-paths": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz", + "integrity": "sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==", + "requires": { + "@types/json5": "^0.0.29", + "json5": "^1.0.1", + "minimist": "^1.2.6", + "strip-bom": "^3.0.0" + } + }, + "typescript": { + "version": "4.9.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz", + "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==" + } + } +} diff --git a/example/path-alias-and-sub-folders-import-from-index/package.json b/example/path-alias-and-sub-folders-import-from-index/package.json new file mode 100644 index 00000000..05d32bd8 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/package.json @@ -0,0 +1,12 @@ +{ + "type": "module", + "scripts": { + "build": "tsc -p ./tsconfig.json", + "start": "npm run-script build && node ./dist/index.js", + "test": "../../bin/ts-unused-exports ./tsconfig.json" + }, + "dependencies": { + "ts-unused-exports": "^9.0.2", + "typescript": "^4.9.4" + } +} diff --git a/example/path-alias-and-sub-folders-import-from-index/src/foo/index.ts b/example/path-alias-and-sub-folders-import-from-index/src/foo/index.ts new file mode 100644 index 00000000..fe7805c3 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/src/foo/index.ts @@ -0,0 +1,2 @@ +export const fooFromIndex = "foo"; +export function unusedFromFooIndex() {} diff --git a/example/path-alias-and-sub-folders-import-from-index/src/foo/other.ts b/example/path-alias-and-sub-folders-import-from-index/src/foo/other.ts new file mode 100644 index 00000000..018ce00f --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/src/foo/other.ts @@ -0,0 +1,2 @@ +export const fooOther = "foo"; +export function unusedFromMain() {} diff --git a/example/path-alias-and-sub-folders-import-from-index/src/foo2/index.ts b/example/path-alias-and-sub-folders-import-from-index/src/foo2/index.ts new file mode 100644 index 00000000..f7cf45da --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/src/foo2/index.ts @@ -0,0 +1 @@ +export const foo2FromIndex = "foo2"; diff --git a/example/path-alias-and-sub-folders-import-from-index/src/foo2/other.ts b/example/path-alias-and-sub-folders-import-from-index/src/foo2/other.ts new file mode 100644 index 00000000..143cb3cb --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/src/foo2/other.ts @@ -0,0 +1 @@ +export const foo2Other = "foo2"; diff --git a/example/path-alias-and-sub-folders-import-from-index/src/index.ts b/example/path-alias-and-sub-folders-import-from-index/src/index.ts new file mode 100644 index 00000000..6845eb8c --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/src/index.ts @@ -0,0 +1,10 @@ +import { foo2FromIndex } from "@foo2"; +import { foo2Other } from "@foo2/other"; +import { fooFromIndex } from "@foo/index"; +import { fooOther } from "@foo/other"; + +console.log(fooFromIndex); +console.log(fooOther); + +console.log(foo2FromIndex); +console.log(foo2Other); diff --git a/example/path-alias-and-sub-folders-import-from-index/tsconfig.json b/example/path-alias-and-sub-folders-import-from-index/tsconfig.json new file mode 100644 index 00000000..26f92ad2 --- /dev/null +++ b/example/path-alias-and-sub-folders-import-from-index/tsconfig.json @@ -0,0 +1,35 @@ +{ + "include": ["src"], + + "compilerOptions": { + // Type Checking + "strict": true, + + // Modules + "baseUrl": ".", + "module": "ESNext", + "moduleResolution": "node", + "paths": { + "@foo/*": ["src/foo/*"], + "@foo2": ["src/foo2/index"], + "@foo2/*": ["src/foo2/*"], + }, + + // Emit + // "noEmit": true, + + // Interop Constraints + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "isolatedModules": true, + + // Language and Environment + // "jsx": "react-jsx", + "lib": ["DOM", "DOM.Iterable", "ESNext"], + "target": "ESNext", + + // Completeness + "skipLibCheck": true, + "outDir": "./dist" + } +} diff --git a/example/simple-es6/package.json b/example/simple-es6/package.json index a301b2bb..50d7a348 100644 --- a/example/simple-es6/package.json +++ b/example/simple-es6/package.json @@ -6,7 +6,7 @@ }, "license": "MIT", "scripts": { - "test": "npm i && ../../bin/ts-unused-exports ./tsconfig.json" + "test": "npm ci && ../../bin/ts-unused-exports ./tsconfig.json" }, "type": "module", "dependencies": { diff --git a/example/tsx/package-lock.json b/example/tsx/package-lock.json index 32583b31..41f5b6b9 100644 --- a/example/tsx/package-lock.json +++ b/example/tsx/package-lock.json @@ -1,6 +1,87 @@ { + "name": "tsx", + "lockfileVersion": 2, "requires": true, - "lockfileVersion": 1, + "packages": { + "": { + "license": "MIT", + "dependencies": { + "@types/react": "^16.4.18", + "react": "^16.6.0" + } + }, + "node_modules/@types/prop-types": { + "version": "15.7.3", + "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.3.tgz", + "integrity": "sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==" + }, + "node_modules/@types/react": { + "version": "16.9.9", + "resolved": "https://registry.npmjs.org/@types/react/-/react-16.9.9.tgz", + "integrity": "sha512-L+AudFJkDukk+ukInYvpoAPyJK5q1GanFOINOJnM0w6tUgITuWvJ4jyoBPFL7z4/L8hGLd+K/6xR5uUjXu0vVg==", + "dependencies": { + "@types/prop-types": "*", + "csstype": "^2.2.0" + } + }, + "node_modules/csstype": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/csstype/-/csstype-2.6.7.tgz", + "integrity": "sha512-9Mcn9sFbGBAdmimWb2gLVDtFJzeKtDGIr76TUqmjZrw9LFXBMSU70lcs+C0/7fyCd6iBDqmksUcCOUIkisPHsQ==" + }, + "node_modules/js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + }, + "node_modules/loose-envify": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "dependencies": { + "js-tokens": "^3.0.0 || ^4.0.0" + }, + "bin": { + "loose-envify": "cli.js" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/prop-types": { + "version": "15.7.2", + "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", + "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "dependencies": { + "loose-envify": "^1.4.0", + "object-assign": "^4.1.1", + "react-is": "^16.8.1" + } + }, + "node_modules/react": { + "version": "16.10.2", + "resolved": "https://registry.npmjs.org/react/-/react-16.10.2.tgz", + "integrity": "sha512-MFVIq0DpIhrHFyqLU0S3+4dIcBhhOvBE8bJ/5kHPVOVaGdo0KuiQzpcjCPsf585WvhypqtrMILyoE2th6dT+Lw==", + "dependencies": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1", + "prop-types": "^15.6.2" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/react-is": { + "version": "16.10.2", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.10.2.tgz", + "integrity": "sha512-INBT1QEgtcCCgvccr5/86CfD71fw9EPmDxgiJX4I2Ddr6ZsV6iFXsuby+qWJPtmNuMY0zByTsG4468P7nHuNWA==" + } + }, "dependencies": { "@types/prop-types": { "version": "15.7.3", diff --git a/example/tsx/package.json b/example/tsx/package.json index 016109a6..bc10dce8 100644 --- a/example/tsx/package.json +++ b/example/tsx/package.json @@ -6,7 +6,7 @@ }, "license": "MIT", "scripts": { - "test": "npm i && ../../bin/ts-unused-exports ./tsconfig.json" + "test": "npm ci && ../../bin/ts-unused-exports ./tsconfig.json" }, "dependencies": { "@types/react": "^16.4.18", diff --git a/example/with-paths/package-lock.json b/example/with-paths/package-lock.json index ac4f3e2a..0897b2e2 100644 --- a/example/with-paths/package-lock.json +++ b/example/with-paths/package-lock.json @@ -1,11 +1,32 @@ { - "requires": true, - "lockfileVersion": 1, - "dependencies": { - "typescript": { - "version": "3.6.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", - "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==" - } + "name": "with-paths", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "license": "MIT", + "dependencies": { + "typescript": "^3.6.4" + } + }, + "node_modules/typescript": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", + "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } } + }, + "dependencies": { + "typescript": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.4.tgz", + "integrity": "sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==" + } + } } diff --git a/example/with-paths/package.json b/example/with-paths/package.json index 181deab0..163b6f97 100644 --- a/example/with-paths/package.json +++ b/example/with-paths/package.json @@ -7,7 +7,7 @@ "license": "MIT", "scripts": { "build": "tsc", - "test": "npm i && npm run build && ../../bin/ts-unused-exports ./tsconfig.json --excludePathsFromReport=to-ignore" + "test": "npm ci && npm run build && ../../bin/ts-unused-exports ./tsconfig.json --excludePathsFromReport=to-ignore" }, "dependencies": { "typescript": "^3.6.4" diff --git a/ispec/run.sh b/ispec/run.sh index ad11df3d..f8104948 100755 --- a/ispec/run.sh +++ b/ispec/run.sh @@ -16,7 +16,7 @@ function run_itest_expect_zero_issues() function install_and_run_itest() { - npm i > /dev/null && run_itest + npm ci > /dev/null && run_itest } pushd ../example/simple-zero-issues @@ -79,6 +79,10 @@ pushd ../example/path-alias-and-sub-folders run_itest popd +pushd ../example/path-alias-and-sub-folders-import-from-index +run_itest +popd + pushd ../example/with-js run_itest popd diff --git a/package-lock.json b/package-lock.json index 54e2b05a..b639a841 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ts-unused-exports", - "version": "9.0.0", + "version": "9.0.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5e25e216..20c352b9 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-unused-exports", - "version": "9.0.2", + "version": "9.0.3", "description": "ts-unused-exports finds unused exported symbols in your Typescript project", "main": "lib/app.js", "repository": { diff --git a/src/analyzer.ts b/src/analyzer.ts index 32d85508..fe047fac 100644 --- a/src/analyzer.ts +++ b/src/analyzer.ts @@ -5,6 +5,7 @@ import { LocationInFile, } from './types'; import { + indexCandidateExtensions, indexCandidates, removeExportStarPrefix, removeFileExtensionToAllowForJs, @@ -76,15 +77,33 @@ const processImports = (file: File, exportMap: ExportMap): void => { let ex = exportMap[removeFileExtensionToAllowForJs(key)]?.exports; // Handle imports from an index file - if (!ex && key === '.') { + if (!ex) { + // Try matching import from /a/b/c -> /a/b/c/index.x for (let c = 0; c < indexCandidates.length; c++) { const indexKey = indexCandidates[c]; ex = exportMap[indexKey]?.exports || undefined; if (ex) break; } + + if (!ex && key.endsWith('index')) { + // Try matching import from /a/b/c/index -> /a/b/c/index.x + for (let c = 0; c < indexCandidateExtensions.length; c++) { + const indexKey = key + indexCandidateExtensions[c]; + ex = exportMap[indexKey]?.exports || undefined; + if (ex) break; + } + if (!ex) { + // Try matching import from /a/b/c/index -> /a/b/c + const indexKey = key.substring(0, key.length - '/index'.length); + ex = exportMap[indexKey]?.exports || undefined; + } + } } - if (!ex) return; + if (!ex) { + // DEV DEBUG - console.warn(`Could not resolve ${key}`); + return; + } const addUsage = (imp: string): void => { if (!ex[imp]) { diff --git a/src/parser/common.ts b/src/parser/common.ts index f4fce03c..4894f756 100644 --- a/src/parser/common.ts +++ b/src/parser/common.ts @@ -11,7 +11,9 @@ export interface FromWhat { const TRIM_QUOTES = /^['"](.*)['"]$/; export const getFromText = (moduleSpecifier: string): string => - moduleSpecifier.replace(TRIM_QUOTES, '$1').replace(/\/index(.[mc]?js)?$/, ''); + moduleSpecifier + .replace(TRIM_QUOTES, '$1') + .replace(/\/index(.[mc]?js)?$/, '/index'); // Do not completely remove /index as then is ambiguous between file or folder name export const getFrom = (moduleSpecifier: ts.Expression): string => getFromText(moduleSpecifier.getText()); diff --git a/src/parser/util.ts b/src/parser/util.ts index 141e3117..d3361a33 100644 --- a/src/parser/util.ts +++ b/src/parser/util.ts @@ -17,6 +17,16 @@ export const indexCandidates = [ '/index.mjs', ]; +export const indexCandidateExtensions = [ + '.ts', + '.cts', + '.mts', + '.tsx', + '.js', + '.cjs', + '.mjs', +]; + export function removeFileExtensionToAllowForJs(path: string): string { // ref: https://www.typescriptlang.org/docs/handbook/esm-node.html const extensionsToStrip = ['.js', '.cjs', '.mjs'];