Documentation
¶
Overview ¶
Package preproc extracts TypeScript-ish code blocks from preprocessor-language files (Svelte, Astro) into a "virtual source" buffer that can be fed to a tree-sitter TypeScript/TSX parser. The companion Builder type and per-extractor functions (ExtractSvelte, ExtractAstro) live in the same package.
Import cycle note: this package does NOT import internal/parser. The RemapSymbolLines helper that rewrites parse results lives in internal/parser/preproc_remap.go (parser package) to avoid a cycle.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractAstroWithRefs ¶
func ExtractAstroWithRefs(src []byte) (*VirtualSource, []TemplateRef)
ExtractAstroWithRefs is the primary extractor. It returns both the virtual TypeScript/JS source (frontmatter + <script> blocks) and a list of capitalised JSX-style component tag references found in the template body.
TemplateRefs are emitted for each opening tag whose name starts with an uppercase ASCII letter. Closing tags, HTML-lowercase tags, and namespace-prefixed tags (<astro:fragment>, <svelte:head>) are skipped. Contents of <!-- comments -->, <script>, and <style> blocks are also skipped. Each usage occurrence is recorded (no deduplication — callers may deduplicate per their own requirements).
Resolution of tag names to file paths is NOT performed here — callers should join TemplateRefs against ParseResult.Imports for that purpose.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder accumulates virtual code and a line-map from multiple source-block spans.
func NewBuilder ¶
NewBuilder creates a Builder for the given language label.
func (*Builder) AppendBlankLine ¶
func (b *Builder) AppendBlankLine()
AppendBlankLine appends a single "\n" with LineMap entry 0 (padding — maps to no original line). Use between blocks to keep virtual symbols separate.
func (*Builder) AppendBlock ¶
AppendBlock copies fullSrc[blockStart:blockEnd] into the virtual buffer and records line offsets so every virtual line in this block maps to the corresponding original 1-based line number in the full source.
Parameters:
- fullSrc: the entire original source file bytes
- blockStart: byte offset in fullSrc where the content begins (inclusive)
- blockEnd: byte offset in fullSrc where the content ends (exclusive)
Panics if blockEnd < blockStart or offsets are out of range.
func (*Builder) Build ¶
func (b *Builder) Build() *VirtualSource
Build finalises and returns the VirtualSource. The Builder must not be used after Build is called.
type TemplateRef ¶
TemplateRef records a single usage of a capitalised JSX-style component tag found in an Astro (or similar) template body.
Name is the bare tag name (e.g. "Breadcrumbs"), Line and Col are 1-based positions in the original source file. Closing tags (</Foo>) are not recorded — only opening tags.
type VirtualSource ¶
type VirtualSource struct {
// Code is the synthetic TypeScript buffer passed to the tree-sitter parser.
Code []byte
// Lang is the effective language label for Symbol.Language ("svelte", "astro", …).
Lang string
// LineMap[i] is the 1-based line number in the ORIGINAL source that corresponds
// to line (i+1) in Code. Length == number of \n in Code + 1. Lines that exist
// only in Code (blank padding between script blocks) map to 0.
LineMap []uint32
}
VirtualSource is TypeScript code extracted from a preprocessor-language file (Svelte, Astro, Vue) together with a per-virtual-line mapping back to the original source line numbers.
func ExtractAstro ¶
func ExtractAstro(src []byte) *VirtualSource
ExtractAstro extracts the Astro frontmatter block (between leading ---) and all <script> blocks (page-level <script> tags). Returns VirtualSource with lang="astro".
Handles:
- Leading frontmatter: starts with optional whitespace then ---\n, ends at next line starting with ---
- Frontmatter may be absent — just <script> blocks processed
- Multiple <script> blocks with blank-line padding
Does NOT handle:
- Template expressions {foo} in the HTML body
- Style tags
- Template literal strings containing <script> (backtick-quoted JS strings with embedded <script> markers may be misidentified as real script blocks)
func ExtractSvelte ¶
func ExtractSvelte(src []byte) *VirtualSource
ExtractSvelte extracts all <script> and <script context="module"> / <script module> blocks from a Svelte component and returns a VirtualSource with lang="svelte".
Handles:
- <script> … </script>
- <script lang="ts"> … </script>
- <script context="module"> … </script> (Svelte 4)
- <script module> … </script> (Svelte 5)
- Multiple script tags — concatenated with blank-line padding
- CRLF and LF line endings
- Missing closing tag — treat as extending to EOF (best-effort)
Does NOT handle:
- Script content inside HTML comments
- <style> blocks
- Template markup
- Escaped <script> in string literals
- Template literal strings containing <script> (backtick-quoted JS strings with embedded <script> markers may be misidentified as real script blocks)