diff --git a/src/jinjax/component.py b/src/jinjax/component.py index 2d6ecff..c83637e 100644 --- a/src/jinjax/component.py +++ b/src/jinjax/component.py @@ -20,6 +20,7 @@ RX_PROPS_START = re.compile(r"{#-?\s*def\s+") +RX_COMMENTS = re.compile(r"\n\s*#[^\n]*") RX_CSS_START = re.compile(r"{#-?\s*css\s+") RX_JS_START = re.compile(r"{#-?\s*js\s+") RX_COMMA = re.compile(r"\s*,\s*") @@ -156,13 +157,13 @@ def load_metadata(self, source: str) -> None: if not match: return + header = RX_COMMENTS.sub("", match.group(0)).replace("\n", " ") # Reversed because I will use `header.pop()` - header = match.group(0).replace("\n", " ").split("#}")[::-1] + header = header.split("#}")[::-1] def_found = False - while line := header.pop(): - line = line.strip(" -") - + while header: + line = header.pop().strip(" -") expr = self.read_metadata_line(line, RX_PROPS_START) if expr: if def_found: diff --git a/tests/test_component.py b/tests/test_component.py index 8352529..f4fabd5 100644 --- a/tests/test_component.py +++ b/tests/test_component.py @@ -212,3 +212,74 @@ def test_global_assets(): ) assert com.css == ["a.css", "/static/shared/b.css", "http://example.com/cdn.css"] assert com.js == ["http://example.com/cdn.js", "a.js", "/static/shared/b.js"] + + +def test_types_in_args_decl(): + com = Component( + name="Test.jinja", + source="""{# def + ring_class: str = "ring-1 ring-black", + rounded_class: str = "rounded-2xl md:rounded-3xl", + + image: str | None = None, + + title: str = "", + p_class: str = "px-5 md:px-6 py-5 md:py-6", + gap_class: str = "gap-4", + content_class: str = "", + + layer_class: str | None = None, + layer_height: int = 4, +#}""" + ) + assert com.required == [] + print(com.optional) + assert com.optional == { + "ring_class": "ring-1 ring-black", + "rounded_class": "rounded-2xl md:rounded-3xl", + "image": None, + "title": "", + "p_class": "px-5 md:px-6 py-5 md:py-6", + "gap_class": "gap-4", + "content_class": "", + "layer_class": None, + "layer_height": 4, + } + + +def test_comments_in_args_decl(): + com = Component( + name="Test.jinja", + source="""{# def + # + # Card style + ring_class: str = "ring-1 ring-black", + rounded_class: str = "rounded-2xl md:rounded-3xl", + # + # Image + image: str | None = None, + # + # Content + title: str = "", + p_class: str = "px-5 md:px-6 py-5 md:py-6", + gap_class: str = "gap-4", + content_class: str = "", + # + # Decorative layer + layer_class: str | None = None, + layer_height: int = 4, +#}""" + ) + assert com.required == [] + print(com.optional) + assert com.optional == { + "ring_class": "ring-1 ring-black", + "rounded_class": "rounded-2xl md:rounded-3xl", + "image": None, + "title": "", + "p_class": "px-5 md:px-6 py-5 md:py-6", + "gap_class": "gap-4", + "content_class": "", + "layer_class": None, + "layer_height": 4, + }