Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

64 lines
2.5 KiB
Go

package languages
import (
"path/filepath"
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
)
// SvelteExtractor extracts Svelte components: a file node plus one always-exported
// component node, with every <script> / <script context="module"> block carved
// out and delegated to the TypeScript or JavaScript extractor so the component's
// logic lands in the graph at its real source lines.
type SvelteExtractor struct {
ts *TypeScriptExtractor
js *JavaScriptExtractor
}
// NewSvelteExtractor constructs a Svelte component extractor.
func NewSvelteExtractor() *SvelteExtractor {
return &SvelteExtractor{ts: NewTypeScriptExtractor(), js: NewJavaScriptExtractor()}
}
func (e *SvelteExtractor) Language() string { return "svelte" }
func (e *SvelteExtractor) Extensions() []string { return []string{".svelte"} }
func (e *SvelteExtractor) Extract(filePath string, src []byte) (*parser.ExtractionResult, error) {
result := &parser.ExtractionResult{}
lineCount := 1 + strings.Count(string(src), "\n")
fileNode := &graph.Node{
ID: filePath, Kind: graph.KindFile, Name: filePath,
FilePath: filePath, StartLine: 1, EndLine: lineCount, Language: "svelte",
}
result.Nodes = append(result.Nodes, fileNode)
componentName := markupComponentName(filePath)
componentID := filePath + "::" + componentName
result.Nodes = append(result.Nodes, &graph.Node{
ID: componentID, Kind: graph.KindType, Name: componentName,
FilePath: filePath, StartLine: 1, EndLine: lineCount, Language: "svelte",
Meta: map[string]any{"component": true, "exported": true, "ui_component": "svelte", "type_flavor": "component"},
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: componentID, Kind: graph.EdgeDefines, FilePath: filePath, Line: 1,
})
carveAndDelegateScripts(src, filePath, fileNode.ID, "svelte", e.ts, e.js, result)
mineTemplateComponentUsages(src, filePath, componentID, "svelte", result)
applyFrameworkTemplatePasses(src, filePath, componentID, "svelte", result)
// `$store` auto-subscriptions in markup resolve to the imported store.
mineSvelteStoreSubscriptions(src, filePath, componentID, result)
return result, nil
}
// markupComponentName derives a component name from a single-file-component path
// (Counter.svelte -> Counter, pages/index.astro -> index). Shared by the
// Svelte/Astro extractors.
func markupComponentName(filePath string) string {
base := filepath.Base(filePath)
return strings.TrimSuffix(base, filepath.Ext(base))
}