Skip to content
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
25 changes: 20 additions & 5 deletions src/idom/_console/ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class ChangedNode:
parents: Sequence[ast.AST]


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

maybe_attr_dict_node: Any | None = None

if name == "vdom":
if len(node.args) == 0:
continue
elif len(node.args) == 1:
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
node.args.append(maybe_attr_dict_node)
if add_props:
node.args.append(maybe_attr_dict_node)
else:
continue
elif isinstance(node.args[1], (ast.Constant, ast.JoinedStr)):
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
node.args.insert(1, maybe_attr_dict_node)
if add_props:
node.args.insert(1, maybe_attr_dict_node)
else:
continue
elif len(node.args) >= 2:
maybe_attr_dict_node = node.args[1]
elif hasattr(html, name):
if len(node.args) == 0:
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
node.args.append(maybe_attr_dict_node)
if add_props:
node.args.append(maybe_attr_dict_node)
else:
continue
elif isinstance(node.args[0], (ast.Constant, ast.JoinedStr)):
maybe_attr_dict_node = ast.Dict(keys=[], values=[])
node.args.insert(0, maybe_attr_dict_node)
if add_props:
node.args.insert(0, maybe_attr_dict_node)
else:
continue
else:
maybe_attr_dict_node = node.args[0]

Expand Down
2 changes: 1 addition & 1 deletion src/idom/_console/rewrite_camel_case_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ def find_nodes_to_change(tree: ast.AST) -> list[ChangedNode]:


def conv_attr_name(name: str) -> str:
new_name = CAMEL_CASE_SUB_PATTERN.sub("_", name).replace("-", "_").lower()
new_name = CAMEL_CASE_SUB_PATTERN.sub("_", name).lower()
return f"{new_name}_" if new_name in kwlist else new_name
2 changes: 1 addition & 1 deletion src/idom/_console/rewrite_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def generate_rewrite(file: Path, source: str) -> str | None:

def find_nodes_to_change(tree: ast.AST) -> list[ChangedNode]:
changed: list[ChangedNode] = []
for el_info in find_element_constructor_usages(tree):
for el_info in find_element_constructor_usages(tree, add_props=True):
for kw in list(el_info.call.keywords):
if kw.arg == "key":
break
Expand Down
24 changes: 22 additions & 2 deletions tests/test__console/test_rewrite_camel_case_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def test_rewrite_camel_case_props_declarations_no_files():
"vdom('tag', dict(camel_case='test', **props))",
),
(
"html.div({'camelCase': test})",
"html.div({'camel_case': test})",
"html.div({'camelCase': test, 'data-thing': test})",
"html.div({'camel_case': test, 'data-thing': test})",
),
(
"html.div({'camelCase': test, ignore: this})",
Expand All @@ -70,10 +70,30 @@ def test_rewrite_camel_case_props_declarations_no_files():
"html.div({'snake_case': test})",
None,
),
(
"html.div({'data-case': test})",
None,
),
(
"html.div(dict(snake_case='test'))",
None,
),
(
"html.div()",
None,
),
(
"vdom('tag')",
None,
),
(
"html.div('child')",
None,
),
(
"vdom('tag', 'child')",
None,
),
],
ids=lambda item: " ".join(map(str.strip, item.split()))
if isinstance(item, str)
Expand Down