mirror of
https://github.com/safishamsi/graphify.git
synced 2026-10-13 15:40:05 +00:00
fix(extract): terminate trailing Astro script comments
Preserve subsequent script blocks and byte offsets when a script ends in a line comment. Fixes #4072. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
1 parent
35adf432b9
commit
fc48f659c4
2 files changed
+34
-3
No files matched your search
+5
-3
@@ -2527,10 +2527,12 @@ def _astro_mask_non_script(src: str) -> str:
|
||||
chars = [c if c in "\r\n" else " " for c in src]
|
||||
for start, end in keep:
|
||||
chars[start:end] = src[start:end]
|
||||
# Terminate the region in place of the following `<`, so two scripts on
|
||||
# one line don't run together into a single statement.
|
||||
# End a trailing // comment before terminating the statement, as in
|
||||
# the Svelte masker. U+2028 keeps source line numbers unchanged; its
|
||||
# three UTF-8 bytes replace three ASCII bytes from the closing tag.
|
||||
# Frontmatter ends at a preserved newline and needs no terminator.
|
||||
if end < len(chars) and chars[end] == " ":
|
||||
chars[end] = ";"
|
||||
chars[end:end + 4] = ["\u2028", ";", "", ""]
|
||||
return "".join(chars)
|
||||
|
||||
|
||||
|
||||
@@ -197,3 +197,32 @@ def test_extract_astro_scripts_on_one_line_do_not_merge(tmp_path):
|
||||
result = extract_astro(page)
|
||||
assert result.get("parse_errors") is None
|
||||
assert "b()" in _labels(result)
|
||||
|
||||
|
||||
def test_extract_astro_trailing_comment_does_not_hide_next_script(tmp_path):
|
||||
"""A comment at the closing tag must end before the next script (#4072)."""
|
||||
page = _write(
|
||||
tmp_path / "inline-comment.astro",
|
||||
"<script>const a = 1 // trailing comment</script>"
|
||||
"<script>function visible() {}</script>\n",
|
||||
)
|
||||
result = extract_astro(page)
|
||||
assert result.get("parse_errors") is None
|
||||
assert _labels(result)["visible()"] == "L1"
|
||||
|
||||
|
||||
def test_astro_mask_preserves_bytes_and_newlines_after_comment():
|
||||
from graphify.extract import _astro_mask_non_script
|
||||
|
||||
source = (
|
||||
"---\nconst title = 'hello';\n---\n"
|
||||
"<script>const a = 1 // comment</script>"
|
||||
"<script>function visible() {}</script>\r\n"
|
||||
)
|
||||
masked = _astro_mask_non_script(source).encode("utf-8")
|
||||
original = source.encode("utf-8")
|
||||
assert len(masked) == len(original)
|
||||
assert masked.index(b"function visible") == original.index(b"function visible")
|
||||
assert [(i, c) for i, c in enumerate(masked) if c in (10, 13)] == [
|
||||
(i, c) for i, c in enumerate(original) if c in (10, 13)
|
||||
]
|
||||
Reference in new issue
Block a user