From c0048d0a61ccfc8431094101d0527ca1f7850204 Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Wed, 13 May 2026 10:07:50 -0700 Subject: [PATCH] feat(extract): add .astro support (#850) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Astro files have a `---...---` TypeScript frontmatter block at the top containing nearly all imports, followed by an HTML-with-expressions template body, and optionally ` +""", + ) + layout = _write(tmp_path / "src/layouts/Layout.astro", "---\n---\n\n") + hydrate = _write(tmp_path / "src/client/hydrate.ts", "export function hydrate(){}\n") + + result = extract_astro(page) + targets = _import_targets(result, relation="imports_from") + assert _make_id(str(layout)) in targets + assert _make_id(str(hydrate)) in targets + + +def test_extract_astro_no_frontmatter_does_not_crash(tmp_path): + """Astro permits frontmatter-less files (pure-HTML pages). Must not raise.""" + page = _write( + tmp_path / "src/pages/plain.astro", + "

no frontmatter here

\n", + ) + result = extract_astro(page) + # Empty/no-imports result is acceptable; the extractor must just not crash. + assert isinstance(result, dict) + assert _import_targets(result, relation="imports_from") == set() + + +def test_extract_astro_handles_tsconfig_path_alias(tmp_path): + _write( + tmp_path / "tsconfig.json", + """{ + "compilerOptions": { + "baseUrl": ".", + "paths": { "@components/*": ["src/components/*"] } + } +} +""", + ) + page = _write( + tmp_path / "src/pages/alias.astro", + """--- +import Hero from '@components/Hero.astro'; +--- + + +""", + ) + hero = _write(tmp_path / "src/components/Hero.astro", "---\n---\n

h

\n") + + result = extract_astro(page) + targets = _import_targets(result, relation="imports_from") + assert _make_id(str(hero)) in targets