From 6bec3da18d04e39a3191825e1385e12c29df0ac6 Mon Sep 17 00:00:00 2001 From: rmorshea Date: Tue, 21 Feb 2023 11:06:06 -0800 Subject: [PATCH] fix rewrite camelCase --- src/idom/_console/ast_utils.py | 25 +++++++++++++++---- src/idom/_console/rewrite_camel_case_props.py | 2 +- src/idom/_console/rewrite_keys.py | 2 +- .../test_rewrite_camel_case_props.py | 24 ++++++++++++++++-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/idom/_console/ast_utils.py b/src/idom/_console/ast_utils.py index ace429010..f2419b70b 100644 --- a/src/idom/_console/ast_utils.py +++ b/src/idom/_console/ast_utils.py @@ -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)): @@ -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] diff --git a/src/idom/_console/rewrite_camel_case_props.py b/src/idom/_console/rewrite_camel_case_props.py index ff1e361bd..7335b2d7a 100644 --- a/src/idom/_console/rewrite_camel_case_props.py +++ b/src/idom/_console/rewrite_camel_case_props.py @@ -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 diff --git a/src/idom/_console/rewrite_keys.py b/src/idom/_console/rewrite_keys.py index ad2b10e72..f5821b4db 100644 --- a/src/idom/_console/rewrite_keys.py +++ b/src/idom/_console/rewrite_keys.py @@ -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 diff --git a/tests/test__console/test_rewrite_camel_case_props.py b/tests/test__console/test_rewrite_camel_case_props.py index 29bb83c64..d460ca6f8 100644 --- a/tests/test__console/test_rewrite_camel_case_props.py +++ b/tests/test__console/test_rewrite_camel_case_props.py @@ -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})", @@ -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)