feat(ts): ts/unused-import rule — whole-statement delete of dead TS/JS imports
- binds names via tree-sitter (default, namespace, named + alias, type-only), keeps a statement when ANY bound name occurs outside it (word scan over comments/strings included): under-delete is the safe direction, mirroring the Java rule's asymmetry - ESM caveat encoded in the policy: mixed live/dead statements are not touched — removing a specifier would still drop the module's side effects - side-effect imports and 'export .. from' re-exports are never candidates - parser/ts became a folder (mod/extract/unused_imports) to keep the 4-entries-per-module rule; word_occurs shared via crate::parser - java/rules/unused_imports now reuses the shared word scanner (DRY)
This commit is contained in:
parent
d5d34afb00
commit
7c8498bc2a
15 changed files with 340 additions and 38 deletions
|
|
@ -247,3 +247,27 @@ fn three_rules_converge_on_one_project() {
|
|||
.stdout(predicate::str::contains("nothing to change"));
|
||||
jmove(&tmp, &["check"]).success();
|
||||
}
|
||||
|
||||
fn ts_unused() -> tempfile::TempDir {
|
||||
common::copy_fixture("typescript", "unused")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ts_unused_import_deletes_dead_type_import_and_keeps_the_rest() {
|
||||
let tmp = ts_unused();
|
||||
// The whole-statement delete must fire for the dead `Ghost` type import.
|
||||
jmove(&tmp, &["fix", "--rule", "ts/unused-import", "--json"])
|
||||
.success()
|
||||
.stdout(
|
||||
predicate::str::contains("\"rule\": \"ts/unused-import\"")
|
||||
.and(predicate::str::contains("\"applied\": true")),
|
||||
);
|
||||
let app = read(&in_root(tmp.path(), "src/app.ts"));
|
||||
assert!(!app.contains("Ghost"), "{app}");
|
||||
// Side-effect import, the mixed used/unused statement and the default
|
||||
// class import all stay (under-delete safety).
|
||||
assert!(app.contains("import './side-effects';"), "{app}");
|
||||
assert!(app.contains("unused"), "{app}");
|
||||
assert!(app.contains("import Logger"), "{app}");
|
||||
jmove(&tmp, &["check"]).success();
|
||||
}
|
||||
|
|
|
|||
1
tests/typescript/unused/package.json
Normal file
1
tests/typescript/unused/package.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{ "name": "unused-fixture" }
|
||||
10
tests/typescript/unused/src/app.ts
Normal file
10
tests/typescript/unused/src/app.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { used, unused } from './lib';
|
||||
import './side-effects';
|
||||
import Logger from './logger'; // used below, keep it
|
||||
import type { Ghost } from './types';
|
||||
|
||||
export function run(): number {
|
||||
const log = new Logger();
|
||||
log.write('hello');
|
||||
return used(1, 2);
|
||||
}
|
||||
6
tests/typescript/unused/src/lib.ts
Normal file
6
tests/typescript/unused/src/lib.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export function used(a: number, b: number): number {
|
||||
return a + b;
|
||||
}
|
||||
export function unused(a: number): number {
|
||||
return a;
|
||||
}
|
||||
3
tests/typescript/unused/src/logger.ts
Normal file
3
tests/typescript/unused/src/logger.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export default class Logger {
|
||||
write(_s: string) {}
|
||||
}
|
||||
0
tests/typescript/unused/src/side-effects.ts
Normal file
0
tests/typescript/unused/src/side-effects.ts
Normal file
0
tests/typescript/unused/src/types.ts
Normal file
0
tests/typescript/unused/src/types.ts
Normal file
Loading…
Add table
Add a link
Reference in a new issue