github.com/sightmap/sightmap/go contains the packages used by the sightmap CLI. Snapshot enrichers, analysis programs, and other Go tools can import the same component model and matcher.
Design goals
- Shared component model and matcher. The
sightmapandmatchpackages give consumers the same types and matching logic as the CLI. - Tree-level matching. Matching operates on a component tree whose nodes carry
SelectorPartvalues. This supports offline analysis of saved trees and synthetic selectors for native-mobile nodes. - Embedded browser probe. The
probepackage exposes the browser-sideprobe.jssource asprobe.ProbeJS, including its bounds, visibility, and interactivity logic.
Package map
All import paths are rooted atgithub.com/sightmap/sightmap/go/.
Load a corpus and match a tree
sightmap.Load parses a .sightmap/ directory into a Corpus, which is plain data. To match against it, wrap it in a match.Matcher, which compiles the component definitions per page URL and caches them.
main.go
Match returns map[*sightmap.ComponentNode]*sightmap.ComponentMatch. Each match carries the component Name, Memory, resolved Tags (a union across matching ancestors), the node’s effective Privacy (nearest enclosing declaration), the component’s own Watch, and extracted Properties. A property read from a block or mask node is already withheld. The library selects the most specific view whose route matches the URL pathname, using declaration order only to break equal-specificity ties. Trailing slashes are normalized away before matching. If no view matches, global components still apply.
Matcher is safe for concurrent use. Create one per Corpus and reuse it so the compiled-query cache is shared. Matcher.Conflicts(root, url) returns nodes claimed by more than one component definition; each sightmap.Conflict carries Names and the index-aligned Defs, since two definitions can share a name.
The Corpus
sightmap.Corpus holds the loaded definitions. Hold it for the life of a run; to pick up on-disk edits in a long-lived process, call Load again and swap it (and its Matcher). Its json.Marshal output is the wire sightmap export prints.
Package-level helpers include
RequestEnvironments(view, request), which intersects a view-scoped request’s environments with its view’s, and SplitFieldPath, which splits a body path with \. and \\ escapes. Every property definition (ComponentPropertyDef, RequestPropertyDef, MessagePropertyDef, URLPropertyDef) carries one Extract value.
Load(dir) is shorthand for DirLoader(dir).Load(). For an existing in-memory corpus, StaticLoader(c *Corpus) Loader wraps it as a Loader; the LoaderFunc type, func() (*Corpus, error), also implements Loader.
Per-site tooling defaults (.sightmap/config.yaml) load separately via sightmap.LoadConfig(dir) SiteConfig. This is tooling configuration, not part of the spec.
Validate and lint from Go
TheValidate and Lint functions run the same checks as sightmap validate and sightmap lint:
ValidationError has a Code and a Severity (error or warning; empty means error). LintWithCounts(c *Corpus, counts map[string]int) []LintWarning adds per-component match counts to lint results. The CLI uses it for sightmap lint --snapshot and sightmap lint --all-snapshots.
Where component trees come from
- Saved captures. Auto-organized captures under
.sightmap/snapshots/{view}/include a sibling.snap.tree.jsonfile containing the rawComponentNodetree.sightmap snapshot --tree-out FILEwrites the tree to an explicit path. Unmarshal either form into*sightmap.ComponentNode, as in the example above. - Live pages. Call
browser.Connect(addr, tabID)to attach to a running session’s tab, then pass abrowser.Pagetobrowser.ExtractComponents— or use the higher-levelobserve.Page, which connects, extracts, matches the corpus, and computes coverage in one call. - Custom extraction. Call
extract.BuildTreewith probe, accessibility, and DOM data. Native consumers can createSelectorPartvalues for non-DOM trees.
The
conformance directory includes component-tree fixtures, flat sightmap definitions, and expected matches. Use them to compare another implementation with the Go matcher. See Conformance fixtures.Related
- CLI overview: the commands built on these packages
- Curator vs. consumer: where a library consumer fits
- Schema reference: the YAML format