diff --git a/graphify/extract.py b/graphify/extract.py index b463ae388..2ff478adb 100644 --- a/graphify/extract.py +++ b/graphify/extract.py @@ -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) diff --git a/tests/test_astro_extraction.py b/tests/test_astro_extraction.py index 2b4fbb344..1772f0545 100644 --- a/tests/test_astro_extraction.py +++ b/tests/test_astro_extraction.py @@ -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", + "" + "\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" + "" + "\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) + ]