Skip to content

Commit

Permalink
Allow comments in declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsca committed Jul 5, 2024
1 parent cff8666 commit c98adc3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/jinjax/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*")
Expand Down Expand Up @@ -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:
Expand Down
71 changes: 71 additions & 0 deletions tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit c98adc3

Please sign in to comment.