Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature from brandonxiang to Get all completely unused files #256

Merged
merged 11 commits into from
Dec 26, 2022
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

### Added

Allow for importing of 'x.js' files as opposed to simply 'x'. This aims towards supporting es6 and higher modules as opposed to CommonJS.
- Allow for importing of 'x.js' files as opposed to simply 'x'. This aims towards supporting es6 and higher modules as opposed to CommonJS.

- Add option `--findCompletelyUnusedFiles` to check for *completely* unused files

## [8.0.5] - 10 Dec 2022

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ Options:
| `allowUnusedEnums` | Allow unused `enum`s. | `--allowUnusedEnums` |
| `allowUnusedTypes` | Allow unused `type` or `interface`. | `--allowUnusedTypes` |
| `excludeDeclarationFiles` | Exclude `.d.ts` files when looking for unused exports. | `--excludeDeclarationFiles` |
| `maxIssues` | Return successfully for up to a given number of modules with unused exports. | `--maxIssues=7` |
| `excludePathsFromReport` | Exclude files from the _output_ that match the given path segments. | `--excludePathsFromReport=math;utils` |
| `exitWithCount` | Set the process exit code to be the count of files that have unused exports. | `--exitWithCount` |
| `exitWithUnusedTypesCount` | Set the process exit code to be the total count of unused exported types. | `--exitWithUnusedTypesCount` |
| `findCompletelyUnusedFiles` | Find all *completely* unused files (where *all* exports are unused). | `--findCompletelyUnusedFiles` |
| `ignoreFiles` | Ignore files with filenames that match the given regex. Use this to exclude groups of files - for example test files and their utilities. | `--ignoreFiles=.*spec` |
| `ignoreProductionFiles` | Only scan **test** files (so ignore non-test 'production' files). | `--ignoreProductionFiles` |
| `ignoreTestFiles` | Only scan **production** files (ignore all test files, like `spec.ts(x)` or `test.ts(x)` or `TestUtils.ts`). Use this to detect production code that is only used in tests (so is dead code). Note: this will NOT detect unused exports in test code - for that, you can run `ts-unused-exports` separately with the `--ignoreProductionFiles` option. | `--ignoreTestFiles` |
| `excludePathsFromReport` | Exclude files from the _output_ that match the given path segments. | `--excludePathsFromReport=math;utils` |
| `maxIssues` | Return successfully for up to a given number of modules with unused exports. | `--maxIssues=7` |
| `searchNamespaces` | Enable searching for unused exports within namespaces. Note: this can affect performance on large codebases. | `--searchNamespaces` |
| `showLineNumber` | Show the line number and column of the unused export. | `--showLineNumber` |
| `silent` | Don't write on stdout on success. | `--silent` |
Expand Down
3 changes: 3 additions & 0 deletions example/simple-unused-file/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import inc from './math';

console.log('two', inc(1));
1 change: 1 addition & 0 deletions example/simple-unused-file/execute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx tsc && node ./lib/app.js
6 changes: 6 additions & 0 deletions example/simple-unused-file/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function unused1(x: number) { return x + 1; }

// ts-unused-exports:disable-next-line
export function unusedButDisabled(x: number) { return x + 2; }

export default (x: number) => x + 1;
11 changes: 11 additions & 0 deletions example/simple-unused-file/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions":
{
"target": "ES5",
"module": "commonjs",
"noImplicitAny": true,
"strictNullChecks": true,
"outDir": "./lib",
},
"include": ["./**/*.ts"],
}
1 change: 1 addition & 0 deletions example/simple-unused-file/unused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function unused(x: number) { return x + 2; }
29 changes: 29 additions & 0 deletions features/find-completely-unused-files.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Feature: Find Completely Unused Files

Background:
Given file "a.ts" is
"""
export function sum(a, b) { return a + b; };
export const a_unused = 1;
"""
Given file "b.ts" is import {sum} from './a';
Given file "c.ts" is
"""
export function minus(a, b) { return a - b; };
export const b_unused = 1;
"""

Scenario: No Parameter Find Completely Unused Files
When analyzing "tsconfig.json"
Then the result is { "a.ts": ["a_unused"], "c.ts": ["minus", "b_unused"]}
Then the unused file is undefined

Scenario: With Parameter Find Completely Unused Files
When analyzing "tsconfig.json" with files ["--findCompletelyUnusedFiles"]
Then the result is { "a.ts": ["a_unused"], "c.ts": ["minus", "b_unused"]}
Then the unused file is ["c.ts"]

Scenario: Run CLI With Parameter Find Completely Unused Files
When running ts-unused-exports "tsconfig.json" --findCompletelyUnusedFiles
Then the CLI result at status is 1
And the CLI result at stdout contains "c.ts"
6 changes: 3 additions & 3 deletions ispec/_run-and-check-exit-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set +e;

function run {
node ../../bin/ts-unused-exports tsconfig.json --exitWithUnusedTypesCount --excludePathsFromReport=to-ignore $1
node ../../bin/ts-unused-exports tsconfig.json --exitWithUnusedTypesCount --excludePathsFromReport=to-ignore $1 $2
ERROR_COUNT=$?
if [ $ERROR_COUNT -ne 2 ]
then
Expand All @@ -13,5 +13,5 @@ function run {
fi
}

run
run --showLineNumber
run $1
run --showLineNumber $1
4 changes: 4 additions & 0 deletions ispec/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,9 @@ pushd ../example/with-js
run_itest
popd

pushd ../example/simple-unused-file
../../ispec/_run-and-check-exit-code.sh --findCompletelyUnusedFiles
popd

# Test running from another directory
./_run_from_directory_not_project.sh
Loading