From f9b42e8d23ad93f790be76d960e7dbe941de4c33 Mon Sep 17 00:00:00 2001 From: Agnik47 <140933190+Agnik47@users.noreply.github.com> Date: Fri, 2 Oct 2026 10:35:31 +0530 Subject: [PATCH] fix(extract): parse only Svelte script blocks in the AST pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit extract_svelte fed the whole .svelte file to the JS grammar. The template is not JS, so every component reported a syntax error at line 1 and no symbol declared inside + +""", + ) + result = extract_svelte(component) + assert result.get("parse_errors") is None + labels = _labels(result) + assert "bump()" in labels + # Masking keeps offsets, so locations still point at the original lines. + assert labels["bump()"] == "L4" + + +def test_extract_svelte_parses_module_and_instance_scripts(tmp_path): + """Svelte 5 ` + +

hi

+""", + ) + result = extract_svelte(component) + assert result.get("parse_errors") is None + labels = _labels(result) + assert "shared()" in labels and "local()" in labels + assert labels["shared()"] == "L2" + assert labels["local()"] == "L5" + + +def test_extract_svelte_lang_ts_parses_type_syntax(tmp_path): + """` + +""", + ) + result = extract_svelte(component) + assert result.get("parse_errors") is None + labels = _labels(result) + assert "Props" in labels + assert "bump()" in labels + + +def test_extract_svelte_non_js_script_type_is_not_parsed_as_code(tmp_path): + """` + +

hi

+""", + ) + result = extract_svelte(component) + assert result.get("parse_errors") is None + assert "go()" in _labels(result) + + +def test_extract_svelte_static_import_still_resolves(tmp_path): + """Masking must not cost the imports the regex rescue already recovered.""" + component = _write( + tmp_path / "src/Imports.svelte", + """ +

hi

+""", + ) + helper = _write(tmp_path / "src/helper.ts", "export function helper(){}\n") + + result = extract_svelte(component) + assert _make_id(str(helper)) in _import_targets(result, relation="imports_from") + + +def test_extract_svelte_dynamic_import_in_template(tmp_path): + """`{#await import('./X.svelte')}` lives in markup, so the regex pass owns it.""" + component = _write( + tmp_path / "src/Lazy.svelte", + """ +{#await import('./Other.svelte') then Mod} + +{/await} +""", + ) + other = _write(tmp_path / "src/Other.svelte", "

o

\n") + + result = extract_svelte(component) + assert _make_id(str(other)) in _import_targets(result, relation="dynamic_import") + + +def test_extract_svelte_scripts_on_one_line_do_not_merge(tmp_path): + """Two script bodies on one line must not parse as a single statement.""" + component = _write( + tmp_path / "src/Inline.svelte", + "\n", + ) + result = extract_svelte(component) + assert result.get("parse_errors") is None + assert "b()" in _labels(result) + + +def test_extract_svelte_markup_only_component_does_not_crash(tmp_path): + """A `.svelte` file need not have a `