Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/codegen/sdk/codebase/node_classes/ts_node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from codegen.sdk.typescript.detached_symbols.code_block import TSCodeBlock
from codegen.sdk.typescript.detached_symbols.jsx.element import JSXElement
from codegen.sdk.typescript.detached_symbols.jsx.expression import JSXExpression
from codegen.sdk.typescript.detached_symbols.jsx.prop import JSXProp
from codegen.sdk.typescript.detached_symbols.parameter import TSParameter
from codegen.sdk.typescript.enum_definition import TSEnum
from codegen.sdk.typescript.enums import TSFunctionTypeNames
Expand Down Expand Up @@ -91,6 +92,7 @@ def parse_new(node: TSNode, *args):
"jsx_closing_element": JSXElement,
"jsx_opening_element": JSXElement,
"jsx_self_closing_element": JSXElement,
"jsx_attribute": JSXProp,
"spread_element": Unpack,
"subscript_expression": SubscriptExpression,
"type_parameters": TypeParameters,
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/sdk/typescript/detached_symbols/jsx/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
super().__init__(ts_node, file_node_id, G, parent)
open_tag = self.ts_node.child_by_field_name("open_tag") or self.ts_node
name_node = open_tag.child_by_field_name("name")
self._name_node = self._parse_expression(name_node, default=Name)

Check failure on line 37 in src/codegen/sdk/typescript/detached_symbols/jsx/element.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Expression[JSXElement[Parent]] | None", variable has type "Name[Any] | None") [assignment]
self.children # Force parse children of this JSX element

@cached_property
Expand All @@ -55,7 +55,7 @@
for node in self.extended_nodes:
jsx_element_nodes = find_all_descendants(node.ts_node, {"jsx_element", "jsx_self_closing_element"})
jsx_elements.extend([self._parse_expression(x) for x in jsx_element_nodes if x != self.ts_node])
return jsx_elements

Check failure on line 58 in src/codegen/sdk/typescript/detached_symbols/jsx/element.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "list[Expression[JSXElement[Parent]]]", expected "list[JSXElement[Any]]") [return-value]

@cached_property
@reader
Expand All @@ -71,7 +71,7 @@
for node in self.extended_nodes:
jsx_expressions_nodes = find_all_descendants(node.ts_node, {"jsx_expression"})
jsx_expressions.extend([self._parse_expression(x) for x in jsx_expressions_nodes if x != self.ts_node])
return jsx_expressions

Check failure on line 74 in src/codegen/sdk/typescript/detached_symbols/jsx/element.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "list[Expression[JSXElement[Parent]]]", expected "list[JSXExpression]") [return-value]

@property
@noapidoc
Expand All @@ -95,7 +95,7 @@
Returns:
list[JSXProp]: A list of JSXProp objects representing each attribute on the element.
"""
return [JSXProp(x.ts_node, self) for x in self._attribute_nodes]
return [self._parse_expression(x.ts_node, default=JSXProp) for x in self._attribute_nodes]

Check failure on line 98 in src/codegen/sdk/typescript/detached_symbols/jsx/element.py

View workflow job for this annotation

GitHub Actions / mypy

error: List comprehension has incompatible type List[Expression[JSXElement[Parent]]]; expected List[JSXProp] [misc]

@reader
def get_prop(self, name: str) -> JSXProp | None:
Expand Down Expand Up @@ -124,7 +124,7 @@
Returns:
list[JSXProp]: A list of JSXProp objects representing each attribute/prop on the JSXElement.
"""
return [JSXProp(x.ts_node, self) for x in self._attribute_nodes]
return [self._parse_expression(x.ts_node, default=JSXProp) for x in self._attribute_nodes]

Check failure on line 127 in src/codegen/sdk/typescript/detached_symbols/jsx/element.py

View workflow job for this annotation

GitHub Actions / mypy

error: List comprehension has incompatible type List[Expression[JSXElement[Parent]]]; expected List[JSXProp] [misc]

@writer
def set_name(self, name: str) -> None:
Expand All @@ -141,7 +141,7 @@
# This should correctly set the name of both the opening and closing tags
if open_tag := self.ts_node.child_by_field_name("open_tag"):
name_node = self._parse_expression(open_tag.child_by_field_name("name"), default=Name)
name_node.edit(name)

Check failure on line 144 in src/codegen/sdk/typescript/detached_symbols/jsx/element.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "None" of "Expression[JSXElement[Parent]] | None" has no attribute "edit" [union-attr]
if close_tag := self.ts_node.child_by_field_name("close_tag"):
name_node = self._parse_expression(close_tag.child_by_field_name("name"), default=Name)
name_node.edit(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
self.statement._compute_dependencies(usage_type, dest=dest)

@writer
def reduce_condition(self, bool_condition: bool, node: Editable) -> None:

Check failure on line 48 in src/codegen/sdk/typescript/detached_symbols/jsx/expression.py

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "reduce_condition" incompatible with supertype "Editable" [override]
"""Simplifies a JSX expression by reducing it based on a boolean condition.


Expand All @@ -53,7 +53,7 @@
bool_condition (bool): The boolean value to reduce the condition to.

"""
if self.ts_node.parent.type == "jsx_attribute" and not bool_condition:

Check failure on line 56 in src/codegen/sdk/typescript/detached_symbols/jsx/expression.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "None" of "Node | None" has no attribute "type" [union-attr]
node.edit(self.G.node_classes.bool_conversion[bool_condition])
else:
self.remove()
Expand All @@ -72,6 +72,9 @@

if node is None:
node = self
print(self.parent.ts_node_type)
if isinstance(self.parent, JSXProp):
return
if isinstance(node, JSXExpression | JSXElement | JSXProp):
for child in self._anonymous_children:
child.remove()
6 changes: 4 additions & 2 deletions src/codegen/sdk/typescript/detached_symbols/jsx/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from tree_sitter import Node as TSNode

from codegen.sdk.codebase.codebase_graph import CodebaseGraph
from codegen.sdk.core.autocommit import reader, writer
from codegen.sdk.core.dataclasses.usage import UsageKind
from codegen.sdk.core.expressions import Expression
from codegen.sdk.core.expressions.name import Name
from codegen.sdk.core.interfaces.has_name import HasName
from codegen.sdk.core.interfaces.has_value import HasValue
from codegen.sdk.core.node_id_factory import NodeId
from codegen.sdk.extensions.autocommit import commiter
from codegen.sdk.typescript.detached_symbols.jsx.expression import JSXExpression
from codegen.shared.decorators.docs import noapidoc, ts_apidoc
Expand All @@ -24,13 +26,13 @@
_name_node: Name | None
_expression_node: JSXExpression | None

def __init__(self, ts_node: TSNode, parent: "Function | JSXElement | JSXProp") -> None:
super().__init__(ts_node, parent.file_node_id, parent.G, parent)
def __init__(self, ts_node: TSNode, file_node_id: NodeId, G: CodebaseGraph, parent: "Function | JSXElement | JSXProp") -> None:
super().__init__(ts_node, file_node_id, G, parent)
self._name_node = self._parse_expression(self.ts_node.children[0], default=Name)

Check failure on line 31 in src/codegen/sdk/typescript/detached_symbols/jsx/prop.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Expression[JSXProp]", variable has type "Name[Any] | None") [assignment]
if len(self.ts_node.children) > 2:
self._value_node = self._parse_expression(self.ts_node.children[2])
if self._value_node.ts_node.type == "jsx_expression":
self._expression_node = self._parse_expression(self._value_node.ts_node)

Check failure on line 35 in src/codegen/sdk/typescript/detached_symbols/jsx/prop.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Expression[JSXProp]", variable has type "JSXExpression | None") [assignment]
else:
self._expression_node = None
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,34 @@ def test_reduce_ternary_condition_with_empty_arrays(tmpdir):
}
"""
)


def test_reduce_ternary_condition_with_jsx_prop(tmpdir):
# language=typescript jsx
content = """
function foo(): JSX.Element {
return (
<Container
content={condition ? <ComponentA title="hello" /> : undefined}
/>
);
}
"""
with get_codebase_session(tmpdir=tmpdir, files={"dir/file1.ts": content}, programming_language=ProgrammingLanguage.TYPESCRIPT) as codebase:
file: TSFile = codebase.get_file("dir/file1.ts")
foo = file.get_function("foo")
ternary = foo.code_block.statements[0].value.value.props[0].value.statement
ternary.reduce_condition(True)
# language=typescript jsx
assert (
file.content
== """
function foo(): JSX.Element {
return (
<Container
content={<ComponentA title="hello" />}
/>
);
}
"""
)