Blank everything outside inline `` `` bodies, keeping line numbers. tree-sitter-php treats non-PHP spans (including inline `` `` blocks) as opaque text, so JS declared there is invisible to the PHP grammar (#2320). Mirrors :func:`_vue_mask_non_script`'s approach: replace e
(src: str)
| 4847 | |
| 4848 | |
| 4849 | def _php_mask_to_script_blocks(src: str) -> tuple[str, bool]: |
| 4850 | """Blank everything outside inline ``<script>`` bodies, keeping line numbers. |
| 4851 | |
| 4852 | tree-sitter-php treats non-PHP spans (including inline ``<script>`` blocks) |
| 4853 | as opaque text, so JS declared there is invisible to the PHP grammar (#2320). |
| 4854 | Mirrors :func:`_vue_mask_non_script`'s approach: replace every character |
| 4855 | outside a script body with a space (preserving ``\\r``/``\\n`` so source |
| 4856 | locations still line up), leaving only the script content for a JS parse. |
| 4857 | A ``<script src="…"></script>`` with no body masks to an empty region, |
| 4858 | which parses as empty JS and contributes nothing — no special-casing needed. |
| 4859 | Returns ``(masked_source, had_script)``; the caller skips the JS pass |
| 4860 | entirely when ``had_script`` is False. |
| 4861 | """ |
| 4862 | def _blank(s: str) -> str: |
| 4863 | return re.sub(r"[^\r\n]", " ", s) |
| 4864 | |
| 4865 | out: list[str] = [] |
| 4866 | pos = 0 |
| 4867 | had_script = False |
| 4868 | for m in _PHP_SCRIPT_RE.finditer(src): |
| 4869 | had_script = True |
| 4870 | out.append(_blank(src[pos:m.start(1)])) # everything up to and including <script ...> |
| 4871 | out.append(m.group(1)) # script body, verbatim |
| 4872 | out.append(_blank(src[m.end(1):m.end()])) # </script> and any trailing part of the match |
| 4873 | pos = m.end() |
| 4874 | if not had_script: |
| 4875 | return src, False |
| 4876 | out.append(_blank(src[pos:])) |
| 4877 | return "".join(out), True |
| 4878 | |
| 4879 | |
| 4880 | # tree-sitter-php (the pinned <0.25 and 0.25.1 alike) has no PHP 8.5 `(void)` |
no test coverage detected