Skip to content

Commit cee50b2

Browse files
committed
revert github workflow file, py code, ut changes
1 parent a225999 commit cee50b2

File tree

5 files changed

+148
-103
lines changed

5 files changed

+148
-103
lines changed

.github/workflows/release.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ jobs:
101101
files: dist/*
102102
fail_on_unmatched_files: true
103103
generate_release_notes: true
104+
105+
# TODO: use python exec instead
106+
- uses: slackapi/[email protected]
107+
if: always()
108+
with:
109+
method: chat.postMessage
110+
token: ${{ secrets.SLACK_BOT_TOKEN }}
111+
payload: |
112+
username: ${{ job.status == 'success' && format('Released codegen@{0}', github.ref_name) || format('Failed to release codegen@{0}', github.ref_name) }}
113+
channel: "#release"
114+
icon_emoji: "${{ job.status == 'success' && ':white_check_mark:' || ':x:' }}"
115+
text: |
116+
Actor: `${{ github.triggering_actor }}`
117+
Author: `${{ github.event.head_commit.author.username }}`
118+
${{ format('Commit: <{0}/{1}/commit/{2}|{1}@{2}>', github.server_url, github.repository, github.sha) || ''}}
119+
View <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|GHA logs>

src/codegen/sdk/python/file.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ def remove_unused_imports(self) -> None:
200200

201201
self.G.commit_transactions()
202202

203+
def remove_unused_exports(self) -> None:
204+
"""Removes unused exports from the file.
205+
In Python this is equivalent to removing unused imports since Python doesn't have
206+
explicit export statements. Calls remove_unused_imports() internally.
207+
"""
208+
self.remove_unused_imports()
209+
203210
@cached_property
204211
@noapidoc
205212
@reader(cache=True)

src/codegen/sdk/typescript/file.py

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from codegen.sdk.core.autocommit import commiter, mover, reader, writer
77
from codegen.sdk.core.file import SourceFile
88
from codegen.sdk.core.interfaces.exportable import Exportable
9-
from codegen.sdk.enums import ImportType, NodeType, ProgrammingLanguage, SymbolType
9+
from codegen.sdk.enums import ImportType, ProgrammingLanguage, SymbolType
1010
from codegen.sdk.extensions.sort import sort_editables
1111
from codegen.sdk.extensions.utils import cached_property
1212
from codegen.sdk.typescript.assignment import TSAssignment
@@ -449,56 +449,31 @@ def remove_unused_exports(self) -> None:
449449
- Exports used by other files through imports
450450
- Exports used within the same file
451451
"""
452+
exports_to_remove = []
453+
452454
for export in self.exports:
453-
# Skip type exports and default exports
454-
if export.is_type_export() or export.is_default_export():
455+
# Skip type exports
456+
if export.is_type_export():
457+
continue
458+
459+
# Skip default exports
460+
if export.is_default_export():
455461
continue
456462

457-
symbol_export_unused = True
458-
symbols_to_remove = []
459-
460-
exported_symbol = export.resolved_symbol
461-
for export_usage in export.symbol_usages:
462-
if export_usage.node_type == NodeType.IMPORT or (export_usage.node_type == NodeType.EXPORT and export_usage.resolved_symbol != exported_symbol):
463-
# If the import has no usages then we can add the import to the list of symbols to remove
464-
reexport_usages = export_usage.symbol_usages
465-
if len(reexport_usages) == 0:
466-
symbols_to_remove.append(export_usage)
467-
break
468-
469-
# If any of the import's usages are valid symbol usages, export is used.
470-
if any(usage.node_type == NodeType.SYMBOL for usage in reexport_usages):
471-
symbol_export_unused = False
472-
break
473-
474-
symbols_to_remove.append(export_usage)
475-
476-
elif export_usage.node_type == NodeType.SYMBOL:
477-
symbol_export_unused = False
478-
break
479-
480-
# export is not used, remove it
481-
if symbol_export_unused:
482-
# remove the unused imports
483-
for imp in symbols_to_remove:
484-
imp.remove()
485-
486-
# Handle different export types
487-
if hasattr(export, "source") and export.source:
488-
# Re-export case (export { x } from 'y')
489-
export.remove()
490-
elif exported_symbol and hasattr(exported_symbol, "export") and exported_symbol.export:
491-
if exported_symbol.export.declared_symbol == exported_symbol:
492-
# Direct export case (export function x)
493-
if exported_symbol.source.startswith("export default "):
494-
exported_symbol.replace("export default ", "")
495-
else:
496-
exported_symbol.replace("export ", "")
497-
else:
498-
# Export statement case (export { x })
499-
exported_symbol.export.remove()
500-
else:
501-
# Fallback - just remove the export
502-
export.remove()
463+
# Check if export is used
464+
has_usages = bool(export.symbol_usages)
465+
466+
# For re-exports, check if the re-exported symbol is used
467+
if export.is_reexport():
468+
if export.resolved_symbol and export.resolved_symbol.symbol_usages:
469+
continue
470+
471+
# Remove if no usages found
472+
if not has_usages:
473+
exports_to_remove.append(export)
474+
475+
# Remove unused exports
476+
for export in exports_to_remove:
477+
export.remove()
503478

504479
self.G.commit_transactions()

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22

3+
import pytest
4+
35
from codegen.sdk.codebase.factory.get_session import get_codebase_session
46
from codegen.sdk.enums import ProgrammingLanguage
57

@@ -103,19 +105,20 @@ def test_remove_unused_imports_with_moved_symbols(tmpdir):
103105
assert main_file.content.strip() == ""
104106

105107

108+
@pytest.mark.skip(reason="This test is not implemented properly yet")
106109
def test_remove_unused_exports_with_side_effects(tmpdir):
107110
content = """
108-
import './styles.css';
109-
export const unused = 5;
110-
export function usedFunction() { return true; }
111+
import './styles.css';
112+
export const unused = 5;
113+
export function usedFunction() { return true; }
111114
112-
const x = usedFunction();
115+
const x = usedFunction();
113116
"""
114117
expected = """
115-
import './styles.css';
116-
export function usedFunction() { return true; }
118+
import './styles.css';
119+
export function usedFunction() { return true; }
117120
118-
const x = usedFunction();
121+
const x = usedFunction();
119122
"""
120123

121124
with get_codebase_session(tmpdir=tmpdir, programming_language=ProgrammingLanguage.TYPESCRIPT, files={"test.ts": content}) as codebase:
@@ -124,22 +127,23 @@ def test_remove_unused_exports_with_side_effects(tmpdir):
124127
assert file.content.strip() == expected.strip()
125128

126129

130+
@pytest.mark.skip(reason="This test is not implemented properly yet")
127131
def test_remove_unused_exports_with_multiple_types(tmpdir):
128132
content = """
129-
export const UNUSED_CONSTANT = 42;
130-
export type UnusedType = string;
131-
export interface UnusedInterface {}
132-
export default function main() { return true; }
133-
export function usedFunction() { return true; }
134-
const x = usedFunction();
133+
export const UNUSED_CONSTANT = 42;
134+
export type UnusedType = string;
135+
export interface UnusedInterface {}
136+
export default function main() { return true; }
137+
export function usedFunction() { return true; }
138+
const x = usedFunction();
135139
"""
136140
# Only value exports that are unused should be removed
137141
expected = """
138-
export type UnusedType = string;
139-
export interface UnusedInterface {}
140-
export default function main() { return true; }
141-
export function usedFunction() { return true; }
142-
const x = usedFunction();
142+
export type UnusedType = string;
143+
export interface UnusedInterface {}
144+
export default function main() { return true; }
145+
export function usedFunction() { return true; }
146+
const x = usedFunction();
143147
"""
144148

145149
with get_codebase_session(tmpdir=tmpdir, programming_language=ProgrammingLanguage.TYPESCRIPT, files={"test.ts": content}) as codebase:
@@ -148,19 +152,20 @@ def test_remove_unused_exports_with_multiple_types(tmpdir):
148152
assert file.content.strip() == expected.strip()
149153

150154

155+
@pytest.mark.skip(reason="This test is not implemented properly yet")
151156
def test_remove_unused_exports_with_reexports(tmpdir):
152157
content1 = """
153-
export { helper } from './utils';
154-
export { unused } from './other';
155-
export function localFunction() { return true; }
158+
export { helper } from './utils';
159+
export { unused } from './other';
160+
export function localFunction() { return true; }
156161
"""
157162
content2 = """
158-
import { helper } from './main';
159-
const x = helper();
163+
import { helper } from './main';
164+
const x = helper();
160165
"""
161166
expected1 = """
162-
export { helper } from './utils';
163-
export function localFunction() { return true; }
167+
export { helper } from './utils';
168+
export function localFunction() { return true; }
164169
"""
165170

166171
with get_codebase_session(tmpdir=tmpdir, programming_language=ProgrammingLanguage.TYPESCRIPT, files={"main.ts": content1, "other.ts": content2}) as codebase:

tests/unit/codegen/sdk/typescript/move_symbol_to_file/test_move_tsx_to_file.py

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def test_move_component_with_dependencies(tmpdir) -> None:
7474

7575
def test_remove_unused_exports(tmpdir) -> None:
7676
"""Tests removing unused exports when moving components between files"""
77-
src_filename = "Component.tsx"
77+
# ========== [ BEFORE ] ==========
7878
# language=typescript jsx
79-
src_content = """
79+
SRC_CONTENT = """
8080
export default function MainComponent() {
8181
const [state, setState] = useState<StateType | null>()
8282
return (<div>
@@ -116,9 +116,8 @@ def test_remove_unused_exports(tmpdir) -> None:
116116
)
117117
}
118118
"""
119-
adj_filename = "adjacent.tsx"
120119
# language=typescript jsx
121-
adj_content = """
120+
ADJ_CONTENT = """
122121
import MainComponent from 'Component'
123122
import { SharedComponent } from 'Component'
124123
import { StateComponent } from 'utils'
@@ -127,26 +126,82 @@ def test_remove_unused_exports(tmpdir) -> None:
127126
return (<Wrapper components={[MainComponent, SharedComponent]}/>)
128127
}
129128
"""
130-
misc_filename = "misc.tsx"
131129
# language=typescript jsx
132-
misc_content = """
130+
MISC_CONTENT = """
133131
export { UnusedComponent } from 'Component'
134132
function Helper({ props }: HelperProps) {}
135133
136134
export { Helper }
137135
"""
138-
import_filename = "import.tsx"
139136
# language=typescript jsx
140-
import_content = """
137+
IMPORT_CONTENT = """
141138
import { UnusedComponent } from 'misc'
142139
"""
143140

144-
files = {src_filename: src_content, adj_filename: adj_content, misc_filename: misc_content, import_filename: import_content}
141+
# ========== [ AFTER ] ==========
142+
# language=typescript jsx
143+
EXPECTED_SRC_CONTENT = """
144+
export default function MainComponent() {
145+
const [state, setState] = useState<StateType | null>()
146+
return (<div>
147+
<div>
148+
<SubComponent/>
149+
</div>
150+
</div>)
151+
}
152+
153+
function UnusedComponent({ props }: UnusedProps) {
154+
return (
155+
<div> Unused </div>
156+
)
157+
}
158+
"""
159+
# language=typescript jsx
160+
EXPECTED_NEW_CONTENT = """
161+
export function SubComponent({ props }: SubComponentProps) {
162+
return (
163+
<HelperComponent size='s'/>
164+
)
165+
}
166+
167+
function HelperComponent({ props }: HelperComponentProps) {
168+
return (
169+
<SharedComponent size='l'/>
170+
)
171+
}
172+
173+
export function SharedComponent({ props }: SharedComponentProps) {
174+
return (
175+
<div> <StateComponent/> </div>
176+
)
177+
}
178+
"""
179+
# language=typescript jsx
180+
EXPECTED_ADJ_CONTENT = """
181+
import MainComponent from 'Component'
182+
import { SharedComponent } from 'new'
183+
import { StateComponent } from 'utils'
184+
185+
function Container(props: ContainerProps) {
186+
return (<Wrapper components={[MainComponent, SharedComponent]}/>)
187+
}
188+
"""
189+
# language=typescript jsx
190+
EXPECTED_MISC_CONTENT = """
191+
function Helper({ props }: HelperProps) {}
192+
"""
193+
194+
files = {
195+
"Component.tsx": SRC_CONTENT,
196+
"adjacent.tsx": ADJ_CONTENT,
197+
"misc.tsx": MISC_CONTENT,
198+
"import.tsx": IMPORT_CONTENT
199+
}
145200

146201
with get_codebase_session(tmpdir=tmpdir, programming_language=ProgrammingLanguage.TYPESCRIPT, files=files) as codebase:
147-
src_file = codebase.get_file(src_filename)
148-
adj_file = codebase.get_file(adj_filename)
149-
misc_file = codebase.get_file(misc_filename)
202+
src_file = codebase.get_file("Component.tsx")
203+
adj_file = codebase.get_file("adjacent.tsx")
204+
misc_file = codebase.get_file("misc.tsx")
150205
new_file = codebase.create_file("new.tsx")
151206

152207
sub_component = src_file.get_symbol("SubComponent")
@@ -159,20 +214,7 @@ def test_remove_unused_exports(tmpdir) -> None:
159214
src_file.remove_unused_exports()
160215
misc_file.remove_unused_exports()
161216

162-
# Verify exports in new file
163-
assert "export function SubComponent" in new_file.content
164-
assert "function HelperComponent" in new_file.content
165-
assert "export function HelperComponent" not in new_file.content
166-
assert "export function SharedComponent" in new_file.content
167-
168-
# Verify imports updated
169-
assert "import { SharedComponent } from 'new'" in adj_file.content
170-
171-
# Verify original file exports
172-
assert "export default function MainComponent()" in src_file.content
173-
assert "function UnusedComponent" in src_file.content
174-
assert "export function UnusedComponent" not in src_file.content
175-
176-
# Verify misc file exports cleaned up
177-
assert "export { Helper }" not in misc_file.content
178-
assert "export { UnusedComponent } from 'Component'" not in misc_file.content
217+
assert src_file.content.strip() == EXPECTED_SRC_CONTENT.strip()
218+
assert new_file.content.strip() == EXPECTED_NEW_CONTENT.strip()
219+
assert adj_file.content.strip() == EXPECTED_ADJ_CONTENT.strip()
220+
assert misc_file.content.strip() == EXPECTED_MISC_CONTENT.strip()

0 commit comments

Comments
 (0)