sourcemap

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package sourcemap provides utilities for working with source code locations, snippet extraction, and line-based operations.

This package bridges BuildKit's AST positions with our output requirements (snippets for diagnostics, comment extraction for inline directives).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment

type Comment struct {
	// Line is the 0-based line number where the comment appears.
	Line int

	// Text is the comment text including the # prefix.
	// Leading whitespace before # is trimmed.
	Text string

	// IsDirective indicates if this looks like a directive comment.
	// True if the comment matches patterns like:
	//   # tally ignore=...
	//   # hadolint ignore=...
	//   # check=skip=...
	//   # syntax=...
	//   # escape=...
	IsDirective bool
}

Comment represents a comment extracted from source. Comments in Dockerfiles start with # and extend to end of line.

type SourceMap

type SourceMap struct {
	// contains filtered or unexported fields
}

SourceMap provides efficient access to source code by line. It precomputes line boundaries for fast snippet extraction.

All line numbers are 0-based (matching BuildKit/LSP conventions).

func New

func New(source []byte) *SourceMap

New creates a SourceMap from source content. Lines are split on \n (handles both \n and \r\n).

func (*SourceMap) Comments

func (sm *SourceMap) Comments() []Comment

Comments extracts all comments from the source. This includes both standalone comment lines and comments associated with AST nodes. Comments are returned in line order.

Note: This only extracts top-level comments (lines starting with #). Comments within instruction arguments are not extracted.

func (*SourceMap) CommentsForLine

func (sm *SourceMap) CommentsForLine(line int) []Comment

CommentsForLine returns all comments that appear immediately before a line. This matches BuildKit's PrevComment behavior where comments are associated with the following instruction.

Example: For line 5, this returns comments from lines 3-4 if:

  • Line 3: # comment one
  • Line 4: # comment two
  • Line 5: FROM alpine

func (*SourceMap) Line

func (sm *SourceMap) Line(line int) string

Line returns the text of a specific line (0-based). Returns empty string if line is out of range.

func (*SourceMap) LineCount

func (sm *SourceMap) LineCount() int

LineCount returns the total number of lines.

func (*SourceMap) LineOffset

func (sm *SourceMap) LineOffset(line int) int

LineOffset returns the byte offset where a line starts (0-based). Returns -1 if line is out of range.

func (*SourceMap) Lines

func (sm *SourceMap) Lines() []string

Lines returns all lines (without line endings). The returned slice should not be modified.

func (*SourceMap) Snippet

func (sm *SourceMap) Snippet(startLine, endLine int) string

Snippet extracts a range of lines as a single string. Both startLine and endLine are 0-based and inclusive. Returns empty string if range is invalid.

Example:

sm.Snippet(2, 4) // Returns lines 2, 3, and 4 joined with newlines

func (*SourceMap) SnippetAround

func (sm *SourceMap) SnippetAround(line, before, after int) string

SnippetAround extracts context lines around a target line. Returns (contextBefore + target + contextAfter) lines as a single string. The before/after counts are clamped to available lines.

Example:

sm.SnippetAround(5, 2, 2) // Returns lines 3-7 (5 ± 2)

func (*SourceMap) Source

func (sm *SourceMap) Source() []byte

Source returns the raw source content. The returned slice should not be modified.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL