Skip to content

Commit b8af14c

Browse files
authored
fix rewrite camelCase (#934)
1 parent 6e9b99f commit b8af14c

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

src/idom/_console/ast_utils.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ class ChangedNode:
9292
parents: Sequence[ast.AST]
9393

9494

95-
def find_element_constructor_usages(tree: ast.AST) -> Iterator[ElementConstructorInfo]:
95+
def find_element_constructor_usages(
96+
tree: ast.AST, add_props: bool = False
97+
) -> Iterator[ElementConstructorInfo]:
9698
changed: list[Sequence[ast.AST]] = []
9799
for parents, node in _walk_with_parent(tree):
98100
if not (isinstance(node, ast.Call)):
@@ -111,24 +113,37 @@ def find_element_constructor_usages(tree: ast.AST) -> Iterator[ElementConstructo
111113
continue
112114

113115
maybe_attr_dict_node: Any | None = None
116+
114117
if name == "vdom":
115118
if len(node.args) == 0:
116119
continue
117120
elif len(node.args) == 1:
118121
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
119-
node.args.append(maybe_attr_dict_node)
122+
if add_props:
123+
node.args.append(maybe_attr_dict_node)
124+
else:
125+
continue
120126
elif isinstance(node.args[1], (ast.Constant, ast.JoinedStr)):
121127
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
122-
node.args.insert(1, maybe_attr_dict_node)
128+
if add_props:
129+
node.args.insert(1, maybe_attr_dict_node)
130+
else:
131+
continue
123132
elif len(node.args) >= 2:
124133
maybe_attr_dict_node = node.args[1]
125134
elif hasattr(html, name):
126135
if len(node.args) == 0:
127136
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
128-
node.args.append(maybe_attr_dict_node)
137+
if add_props:
138+
node.args.append(maybe_attr_dict_node)
139+
else:
140+
continue
129141
elif isinstance(node.args[0], (ast.Constant, ast.JoinedStr)):
130142
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
131-
node.args.insert(0, maybe_attr_dict_node)
143+
if add_props:
144+
node.args.insert(0, maybe_attr_dict_node)
145+
else:
146+
continue
132147
else:
133148
maybe_attr_dict_node = node.args[0]
134149

src/idom/_console/rewrite_camel_case_props.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ def find_nodes_to_change(tree: ast.AST) -> list[ChangedNode]:
8484

8585

8686
def conv_attr_name(name: str) -> str:
87-
new_name = CAMEL_CASE_SUB_PATTERN.sub("_", name).replace("-", "_").lower()
87+
new_name = CAMEL_CASE_SUB_PATTERN.sub("_", name).lower()
8888
return f"{new_name}_" if new_name in kwlist else new_name

src/idom/_console/rewrite_keys.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def generate_rewrite(file: Path, source: str) -> str | None:
7171

7272
def find_nodes_to_change(tree: ast.AST) -> list[ChangedNode]:
7373
changed: list[ChangedNode] = []
74-
for el_info in find_element_constructor_usages(tree):
74+
for el_info in find_element_constructor_usages(tree, add_props=True):
7575
for kw in list(el_info.call.keywords):
7676
if kw.arg == "key":
7777
break

tests/test__console/test_rewrite_camel_case_props.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def test_rewrite_camel_case_props_declarations_no_files():
5858
"vdom('tag', dict(camel_case='test', **props))",
5959
),
6060
(
61-
"html.div({'camelCase': test})",
62-
"html.div({'camel_case': test})",
61+
"html.div({'camelCase': test, 'data-thing': test})",
62+
"html.div({'camel_case': test, 'data-thing': test})",
6363
),
6464
(
6565
"html.div({'camelCase': test, ignore: this})",
@@ -70,10 +70,30 @@ def test_rewrite_camel_case_props_declarations_no_files():
7070
"html.div({'snake_case': test})",
7171
None,
7272
),
73+
(
74+
"html.div({'data-case': test})",
75+
None,
76+
),
7377
(
7478
"html.div(dict(snake_case='test'))",
7579
None,
7680
),
81+
(
82+
"html.div()",
83+
None,
84+
),
85+
(
86+
"vdom('tag')",
87+
None,
88+
),
89+
(
90+
"html.div('child')",
91+
None,
92+
),
93+
(
94+
"vdom('tag', 'child')",
95+
None,
96+
),
7797
],
7898
ids=lambda item: " ".join(map(str.strip, item.split()))
7999
if isinstance(item, str)

0 commit comments

Comments
 (0)