Skip to content

Commit 3693009

Browse files
committed
more cov
1 parent 41f6a33 commit 3693009

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/idom/_console/update_html_usages.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ def generate_rewrite(file: Path, source: str) -> None:
6363

6464
changed: list[Sequence[ast.AST]] = []
6565
for parents, node in walk_with_parent(tree):
66-
if isinstance(node, ast.Call):
66+
if isinstance(node, ast.Call) and node.args:
6767
func = node.func
6868
if isinstance(func, ast.Attribute):
6969
name = func.attr
7070
elif isinstance(func, ast.Name):
7171
name = func.id
7272
else:
73-
name = ""
73+
continue
7474
if hasattr(html, name) or name == "vdom":
7575
if name == "vdom":
7676
maybe_attr_dict_node = node.args[1]
@@ -112,7 +112,7 @@ def generate_rewrite(file: Path, source: str) -> None:
112112
):
113113
nodes_to_unparse.append(current_node)
114114
break
115-
else:
115+
else: # pragma: no cover
116116
raise RuntimeError("Failed to change code")
117117

118118
# check if an nodes to rewrite contain eachother, pick outermost nodes
@@ -134,12 +134,11 @@ def generate_rewrite(file: Path, source: str) -> None:
134134
# there may be some content just before and after the content we're re-writing
135135
before_replacement = lines[node.lineno - 1][: node.col_offset].lstrip()
136136

137-
if node.end_lineno is not None and node.end_col_offset is not None:
138-
after_replacement = lines[node.end_lineno - 1][
139-
node.end_col_offset :
140-
].strip()
141-
else:
142-
after_replacement = ""
137+
after_replacement = (
138+
lines[node.end_lineno - 1][node.end_col_offset :].strip()
139+
if node.end_lineno is not None and node.end_col_offset is not None
140+
else ""
141+
)
143142

144143
replacement = indent(
145144
before_replacement
@@ -148,10 +147,7 @@ def generate_rewrite(file: Path, source: str) -> None:
148147
" " * (node.col_offset - len(before_replacement)),
149148
)
150149

151-
if node.end_lineno:
152-
lines[node.lineno - 1 : node.end_lineno] = [replacement]
153-
else:
154-
lines[node.lineno - 1] = replacement
150+
lines[node.lineno - 1 : node.end_lineno or node.lineno] = [replacement]
155151

156152
if comments:
157153
moved_comment_lines_from_end.append(len(lines) - node.lineno)

tests/test__console/test_update_html_usages.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def test_update_html_usages(tmp_path):
3838
"html.div(**{variable: 'test', **other, 'key': value})",
3939
),
4040
(
41-
'html.div(dict(other, className="test"))',
42-
"html.div(**other, class_name='test')",
41+
'html.div(dict(other, className="test", **another))',
42+
"html.div(**other, class_name='test', **another)",
4343
),
4444
(
4545
'html.div({"className": "outer"}, html.div({"className": "inner"}))',
@@ -76,6 +76,23 @@ def test_update_html_usages(tmp_path):
7676
'html.div({"tagName": "test"})',
7777
None,
7878
),
79+
(
80+
'html.div(dict(tagName="test"))',
81+
None,
82+
),
83+
(
84+
'html.not_an_html_tag({"className": "test"})',
85+
None,
86+
),
87+
(
88+
'html.div(class_name="test")',
89+
None,
90+
),
91+
(
92+
# we don't try to interpret the logic here
93+
'(div or button)({"className": "test"})',
94+
None,
95+
),
7996
# avoid unnecessary changes
8097
(
8198
"""
@@ -192,6 +209,9 @@ def func():
192209
""",
193210
),
194211
],
212+
ids=lambda item: " ".join(map(str.strip, item.split()))
213+
if isinstance(item, str)
214+
else item,
195215
)
196216
def test_generate_rewrite(source, expected):
197217
actual = generate_rewrite(Path("test.py"), dedent(source).strip())

0 commit comments

Comments
 (0)