Skip to content

fix: empty collection remove #324

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

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/codegen/sdk/core/symbol_groups/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Parent = TypeVar("Parent")


class Collection(SymbolGroup[Child, Parent], MutableSequence[Child], Generic[Child, Parent]):

Check failure on line 22 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: Definition of "__contains__" in base class "Editable" is incompatible with definition in base class "Sequence" [misc]

Check failure on line 22 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: Definition of "__contains__" in base class "Editable" is incompatible with definition in base class "Container" [misc]

Check failure on line 22 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: Type argument "Parent" of "SymbolGroup" must be a subtype of "Editable[Any]" [type-var]
"""Ordered collection of nodes
Attributes:
_bracket_size: Number of characters wrapping the collection
Expand Down Expand Up @@ -47,7 +47,7 @@
if children is not None:
self._init_children(children)

def _init_children(self, symbols: list[Child]):

Check failure on line 50 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "_init_children" incompatible with supertype "SymbolGroup" [override]
"""Call this after setting self._symbols."""
if self.ts_node.start_point[0] != self.ts_node.end_point[0] and symbols:
# This is a multiline collection.
Expand All @@ -70,7 +70,7 @@
if isinstance(key, slice):
assert isinstance(value, Iterable)
for idx, item in zip(range(key.start, key.stop, key.step), value):
self[idx] = item

Check failure on line 73 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: No overload variant of "__setitem__" of "Collection" matches argument types "int", "object" [call-overload]
else:
assert not isinstance(value, Iterable)
if isinstance(value, Editable):
Expand All @@ -94,7 +94,7 @@
return self._elements + self._inserts_till()

@writer
def remove(self, value: Child | None = None, *args, **kwargs) -> None:

Check failure on line 97 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 of "remove" is incompatible with supertype "SymbolGroup"; supertype defines the argument type as "bool" [override]
"""Removes an element from a Collection.

Deletes the specified element from the Collection by calling its remove method. If no value is specified,
Expand All @@ -112,6 +112,7 @@
# For example, let's remove all occurrences of the value instead of just the first one
if value is None:
super().remove(*args, **kwargs)
Editable.remove(self, *args, **kwargs)
else:
value.remove(*args, **kwargs)

Expand Down Expand Up @@ -241,7 +242,7 @@
Returns:
None
"""
return Editable.edit(self, *args, **kwargs) # HACK: keep start/end brackets

Check failure on line 245 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: Value of type variable "Parent" of "edit" of "Editable" cannot be "Parent" [type-var]

@property
@reader
Expand Down Expand Up @@ -277,4 +278,4 @@
self._reversed.clear()

def _smart_remove(self, child, *args, **kwargs) -> bool:
return self.parent._smart_remove(self, child, *args, **kwargs)

Check failure on line 281 in src/codegen/sdk/core/symbol_groups/collection.py

View workflow job for this annotation

GitHub Actions / mypy

error: "Parent" has no attribute "_smart_remove" [attr-defined]
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,32 @@ def test_reduce_ternary_condition_with_dict_trailing_comma(tmpdir):
}
"""
)


def test_reduce_ternary_condition_with_empty_arrays(tmpdir):
# language=typescript
content = """
function foo(): string[] {
let result = condition ? [] : ['value'];
let result2 = condition ? ['value'] : [];
return result.concat(result2);
}
"""
with get_codebase_session(tmpdir=tmpdir, files={"dir/file1.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase:
file: TSFile = codebase.get_file("dir/file1.ts")
foo = file.get_function("foo")
ternary1 = foo.code_block.statements[0].value
ternary2 = foo.code_block.statements[1].value
ternary1.reduce_condition(True)
ternary2.reduce_condition(False)
# language=typescript
assert (
file.content
== """
function foo(): string[] {
let result = [];
let result2 = [];
return result.concat(result2);
}
"""
)
Loading