Skip to main content
The module 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.
Requires Go 1.25.2 or later.

Design goals

  • Shared component model and matcher. The sightmap and match packages give consumers the same types and matching logic as the CLI.
  • Tree-level matching. Matching operates on a component tree whose nodes carry SelectorPart values. This supports offline analysis of saved trees and synthetic selectors for native-mobile nodes.
  • Embedded browser probe. The probe package exposes the browser-side probe.js source as probe.ProbeJS, including its bounds, visibility, and interactivity logic.

Package map

All import paths are rooted at github.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

The Validate and Lint functions run the same checks as sightmap validate and sightmap lint:
Each 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.json file containing the raw ComponentNode tree. sightmap snapshot --tree-out FILE writes 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 a browser.Page to browser.ExtractComponents — or use the higher-level observe.Page, which connects, extracts, matches the corpus, and computes coverage in one call.
  • Custom extraction. Call extract.BuildTree with probe, accessibility, and DOM data. Native consumers can create SelectorPart values 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.