@@ -116,9 +119,8 @@ def test_remove_unused_exports(tmpdir) -> None:
)
}
"""
- adj_filename = "adjacent.tsx"
# language=typescript jsx
- adj_content = """
+ ADJ_CONTENT = """
import MainComponent from 'Component'
import { SharedComponent } from 'Component'
import { StateComponent } from 'utils'
@@ -127,26 +129,79 @@ def test_remove_unused_exports(tmpdir) -> None:
return (
)
}
"""
- misc_filename = "misc.tsx"
# language=typescript jsx
- misc_content = """
+ MISC_CONTENT = """
export { UnusedComponent } from 'Component'
function Helper({ props }: HelperProps) {}
export { Helper }
"""
- import_filename = "import.tsx"
# language=typescript jsx
- import_content = """
+ IMPORT_CONTENT = """
import { UnusedComponent } from 'misc'
"""
- files = {src_filename: src_content, adj_filename: adj_content, misc_filename: misc_content, import_filename: import_content}
+ # ========== [ AFTER ] ==========
+ # language=typescript jsx
+ EXPECTED_SRC_CONTENT = """
+import { SubComponent } from 'new';
+
+export default function MainComponent() {
+ const [state, setState] = useState
()
+ return ()
+}
+
+export function UnusedComponent({ props }: UnusedProps) {
+ return (
+ Unused
+ )
+}
+"""
+ # language=typescript jsx
+ EXPECTED_NEW_CONTENT = """
+export function SubComponent({ props }: SubComponentProps) {
+ return (
+
+ )
+}
+
+function HelperComponent({ props }: HelperComponentProps) {
+ return (
+
+ )
+}
+
+export function SharedComponent({ props }: SharedComponentProps) {
+ return (
+
+ )
+}
+"""
+ # language=typescript jsx
+ EXPECTED_ADJ_CONTENT = """
+import MainComponent from 'Component'
+import { SharedComponent } from 'new'
+import { StateComponent } from 'utils'
+
+function Container(props: ContainerProps) {
+ return ()
+}
+"""
+ # language=typescript jsx
+ EXPECTED_MISC_CONTENT = """
+function Helper({ props }: HelperProps) {}
+"""
+
+ files = {"Component.tsx": SRC_CONTENT, "adjacent.tsx": ADJ_CONTENT, "misc.tsx": MISC_CONTENT, "import.tsx": IMPORT_CONTENT}
with get_codebase_session(tmpdir=tmpdir, programming_language=ProgrammingLanguage.TYPESCRIPT, files=files) as codebase:
- src_file = codebase.get_file(src_filename)
- adj_file = codebase.get_file(adj_filename)
- misc_file = codebase.get_file(misc_filename)
+ src_file = codebase.get_file("Component.tsx")
+ adj_file = codebase.get_file("adjacent.tsx")
+ misc_file = codebase.get_file("misc.tsx")
new_file = codebase.create_file("new.tsx")
sub_component = src_file.get_symbol("SubComponent")
@@ -159,20 +214,7 @@ def test_remove_unused_exports(tmpdir) -> None:
src_file.remove_unused_exports()
misc_file.remove_unused_exports()
- # Verify exports in new file
- assert "export function SubComponent" in new_file.content
- assert "function HelperComponent" in new_file.content
- assert "export function HelperComponent" not in new_file.content
- assert "export function SharedComponent" in new_file.content
-
- # Verify imports updated
- assert "import { SharedComponent } from 'new'" in adj_file.content
-
- # Verify original file exports
- assert "export default function MainComponent()" in src_file.content
- assert "function UnusedComponent" in src_file.content
- assert "export function UnusedComponent" not in src_file.content
-
- # Verify misc file exports cleaned up
- assert "export { Helper }" not in misc_file.content
- assert "export { UnusedComponent } from 'Component'" not in misc_file.content
+ assert src_file.content.strip() == EXPECTED_SRC_CONTENT.strip()
+ assert new_file.content.strip() == EXPECTED_NEW_CONTENT.strip()
+ assert adj_file.content.strip() == EXPECTED_ADJ_CONTENT.strip()
+ assert misc_file.content.strip() == EXPECTED_MISC_CONTENT.strip()