Skip to content

Commit 641b0aa

Browse files
committed
test remove unused imports
1 parent 48c3ffb commit 641b0aa

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

tests/unit/codegen/sdk/typescript/file/test_file_remove.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,108 @@ def tets_remove_existing_file(tmpdir) -> None:
1616
file.remove()
1717

1818
assert not os.path.exists(file.filepath)
19+
20+
21+
def test_remove_unused_imports_complete_removal(tmpdir):
22+
content = """
23+
import { unused1, unused2 } from './module1';
24+
import type { UnusedType } from './types';
25+
26+
const x = 5;
27+
"""
28+
expected = """
29+
const x = 5;
30+
"""
31+
32+
with get_codebase_session(
33+
tmpdir=tmpdir,
34+
programming_language=ProgrammingLanguage.TYPESCRIPT,
35+
files={"test.ts": content}
36+
) as codebase:
37+
file = codebase.get_file("test.ts")
38+
file.remove_unused_imports()
39+
assert file.content.strip() == expected.strip()
40+
41+
42+
def test_remove_unused_imports_partial_removal(tmpdir):
43+
content = """
44+
import { used, unused } from './module1';
45+
46+
console.log(used);
47+
"""
48+
expected = """
49+
import { used } from './module1';
50+
51+
console.log(used);
52+
"""
53+
54+
with get_codebase_session(
55+
tmpdir=tmpdir,
56+
programming_language=ProgrammingLanguage.TYPESCRIPT,
57+
files={"test.ts": content}
58+
) as codebase:
59+
file = codebase.get_file("test.ts")
60+
file.remove_unused_imports()
61+
assert file.content.strip() == expected.strip()
62+
63+
64+
def test_remove_unused_imports_with_side_effects(tmpdir):
65+
content = """
66+
import './styles.css';
67+
import { unused } from './module1';
68+
69+
const x = 5;
70+
"""
71+
expected = """
72+
import './styles.css';
73+
74+
const x = 5;
75+
"""
76+
77+
with get_codebase_session(
78+
tmpdir=tmpdir,
79+
programming_language=ProgrammingLanguage.TYPESCRIPT,
80+
files={"test.ts": content}
81+
) as codebase:
82+
file = codebase.get_file("test.ts")
83+
file.remove_unused_imports()
84+
assert file.content.strip() == expected.strip()
85+
86+
87+
def test_remove_unused_imports_with_moved_symbols(tmpdir):
88+
content1 = """
89+
import { helper } from './utils';
90+
91+
export function foo() {
92+
return helper();
93+
}
94+
"""
95+
expected1 = """
96+
export function foo() {
97+
return helper();
98+
}
99+
"""
100+
101+
content2 = """
102+
export function helper() {
103+
return true;
104+
}
105+
"""
106+
107+
with get_codebase_session(
108+
tmpdir=tmpdir,
109+
programming_language=ProgrammingLanguage.TYPESCRIPT,
110+
files={
111+
"main.ts": content1,
112+
"utils.ts": content2
113+
}
114+
) as codebase:
115+
main_file = codebase.get_file("main.ts")
116+
foo = main_file.get_function("foo")
117+
118+
# Move foo to a new file
119+
new_file = codebase.create_file("new.ts")
120+
foo.move_to_file(new_file)
121+
122+
assert main_file.content.strip() == expected1.strip()
123+

0 commit comments

Comments
 (0)