Skip to content

Commit 18a73f2

Browse files
committed
Polish test case
1 parent fbfcb5c commit 18a73f2

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

rewrite/rewrite/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def random_id() -> UUID:
1010

1111
# Define a type that allows both single and two-argument callables
1212
FnType = Union[Callable[[T], Union[T, None]], Callable[[T, int], Union[T, None]]]
13-
FlatMapFnType = Union[Callable[[T], List[T]], Callable[[T, int], List[T]]]
13+
FlatMapFnType = Union[Callable[[T], Union[T, List[T]]], Callable[[T, int], Union[T, List[T]]]]
1414

1515
def list_find(lst: List[T], t: T) -> int:
1616
for i, x in enumerate(lst):
@@ -52,9 +52,12 @@ def list_flat_map(fn: FlatMapFnType[T], lst: List[T]) -> List[T]:
5252
changed = True
5353
continue
5454

55-
if len(new_items) != 1 or new_items[0] is not item:
55+
if isinstance(new_items, list) and (len(new_items) != 1 or new_items[0] is not item):
5656
changed = True
57-
result.extend(new_items)
57+
result.extend(new_items)
58+
elif not isinstance(new_items, list) and new_items is not item:
59+
changed = True
60+
result.append(new_items)
5861

5962
return result if changed else lst
6063

rewrite/tests/python/all/templating/template_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,19 @@ def test_add_statement_last():
6363
"""\
6464
def f():
6565
pass
66+
pass
6667
""",
6768
"""\
6869
def f():
70+
pass
6971
pass
7072
return
7173
"""
7274
),
7375
spec=RecipeSpec()
7476
.with_recipe(from_visitor(
7577
GenericTemplatingVisitor(
76-
lambda j: isinstance(j, MethodDeclaration) and len(j.body.statements) == 1,
78+
lambda j: isinstance(j, MethodDeclaration) and len(j.body.statements) == 2,
7779
'return',
7880
coordinate_provider=lambda m: cast(MethodDeclaration, m).body.get_coordinates().last_statement())
7981
))

0 commit comments

Comments
 (0)