Documentation ¶
Index ¶
- func Sprint(x any) string
- type Change
- type Changelog
- type FileMatcher
- type FileReplacer
- type ForDotsMatcher
- type ForDotsReplacer
- type GenericNodeMatcher
- type ImportMatcher
- type ImportReplacer
- type ImportsMatcher
- type ImportsReplacer
- type InterfaceMatcher
- type InterfaceReplacer
- type Interval
- type Matcher
- type Meta
- type MetavarMatcher
- type MetavarReplacer
- type MetavarType
- type PosMatcher
- type PosReplacer
- type Program
- type PtrMatcher
- type PtrReplacer
- type Region
- type Replacer
- type SearchMatcher
- type SearchNode
- type SearchReplacer
- type SearchResult
- type Searcher
- type SliceDotsMatcher
- type SliceDotsReplacer
- type SliceMatcher
- type SliceReplacer
- type StructMatcher
- type StructReplacer
- type ValueMatcher
- type ValueReplacer
- type ZeroReplacer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Change ¶
type Change struct { Name string Meta *Meta Comments []string // contains filtered or unexported fields }
Change is a single Change in a program.
type Changelog ¶
type Changelog struct {
// contains filtered or unexported fields
}
Changelog records the ranges of positions changed over the course of successive patches.
func (Changelog) Changed ¶
Changed records the portion of code altered in this match to the Changelog.
func (Changelog) ChangedIntervals ¶
ChangedIntervals returns the sum of the Intervals recorded to the Changelog.
type FileMatcher ¶
type FileMatcher struct { // Matches the package name, if any. Package string // Imports in the file. Imports ImportsMatcher // Matches nodes in the file. NodeMatcher Matcher }
FileMatcher matches Go files.
type FileReplacer ¶
type FileReplacer struct { Fset *token.FileSet // Package name to change to, if any. Package string // Imports in the file. Imports ImportsReplacer // Replaces matched nodes in the file. NodeReplacer Replacer }
FileReplacer replaces an ast.File.
type ForDotsMatcher ¶
ForDotsMatcher represents a "for ..." stmt, matching both, for and range statements.
type ForDotsReplacer ¶
type ForDotsReplacer struct { Dots token.Pos Body Replacer // contains filtered or unexported fields }
ForDotsReplacer replaces a "for ...".
type GenericNodeMatcher ¶
type GenericNodeMatcher struct {
Matcher // underlying matcher
}
GenericNodeMatcher is the top-level matcher for ast.Node objects.
type ImportMatcher ¶
type ImportMatcher struct { NameS string // TODO: better naming Name Matcher // named import, if any NameIsMetavar bool // records wehther the named import was a metavariable Path string // import path as a string }
ImportMatcher matches a single import.
type ImportReplacer ¶
type ImportReplacer struct { Name Replacer // named import, if any NameS string NameIsMetavar bool Path string // import path as a string Fset *token.FileSet }
ImportReplacer replaces imports in a file.
type ImportsMatcher ¶
type ImportsMatcher struct {
Imports []ImportMatcher
}
ImportsMatcher matches multiple imports in a Go file.
type ImportsReplacer ¶
type ImportsReplacer struct { Imports []ImportReplacer Fset *token.FileSet }
ImportsReplacer replaces a block of imports.
type InterfaceMatcher ¶
type InterfaceMatcher struct {
Matcher // underlying matcher
}
InterfaceMatcher matches an interface value.
type InterfaceReplacer ¶
type InterfaceReplacer struct { Replacer // underlying replacer Type reflect.Type // type of the interface }
InterfaceReplacer replaces an interface value.
type Matcher ¶
type Matcher interface { // Match is called with the a value from the AST being compared with the // match data captured so far. // // Match reports whether the match succeeded and if so, returns the // original or a different Data object containing additional match data. Match(reflect.Value, data.Data, Region) (_ data.Data, ok bool) }
Matcher matches values in a Go AST. It is built from the "-" portion of a patch.
type Meta ¶
type Meta struct { // Variables defined in this Meta section and their types. Vars map[string]MetavarType }
Meta is the compiled representaton of a Meta section.
func (*Meta) LookupVar ¶
func (m *Meta) LookupVar(name string) MetavarType
LookupVar returns the type of the given metavariable or zero value if it wasn't found.
type MetavarMatcher ¶
type MetavarMatcher struct { Fset *token.FileSet // Name of the metavariable. Name string // Reports whether the provided type matches the metavariable declaration. TypeMatches func(reflect.Type) bool }
MetavarMatcher is compiled from a metavarible occurring in the minus section of the patch.
@@ var x expression @@ -foo(x, x)
The first time a metavariable occurs in the patch, it matches and captures a value of the requested type. For any consecutive appearances of the metavariable, the previously captured value is expected to match.
For example, the patch above will match any expression for the first "x" and the second occurrence will require the previously captured expression to match.
type MetavarReplacer ¶
type MetavarReplacer struct {
Name string
}
MetavarReplacer is compiled from a metavarible occurring in the plus section of the patch.
@@ var x expression @@ -foo(x) +bar(x)
A metavariable cannot be referenced in the plus secton of the patch if it wasn't in the minus section. For example, the following is invalid.
@@ var x expression @@ -foo() +foo(x)
Each occurrence of the metavariable in the plus section of the patch is replaced with the value originally captured for it by the Matcher.
type MetavarType ¶
type MetavarType int
MetavarType defines the different types of metavariables accepted in a '@@' section.
const ( ExprMetavarType MetavarType = iota + 1 // expression IdentMetavarType // identifier )
Supported metavariable types.
type PosMatcher ¶
PosMatcher matches token.Pos fields in an AST.
token.Pos fields are not matched by value, but by validity. A token.Pos in the patch matches a token.Pos in the source file if they're both valid (non-zero) or they're both invalid (zero).
PosMatcher records the matched position from the source file so that it can be reproduced by the PosReplacer later.
type PosReplacer ¶
PosReplacer replaces token.Pos fields.
For valid positions, the replacer will use the previously recorded position for the field, if any. This ensures that whitespace between tokens from the original file are preserved as much as possible.
type Program ¶
type Program struct {
Changes []*Change
}
Program is a collection of compiled changes.
type PtrMatcher ¶
type PtrMatcher struct {
Matcher // underlying matcher
}
PtrMatcher matches a non-nil pointer in the AST.
type PtrReplacer ¶
type PtrReplacer struct { Replacer // underlying replacer Type reflect.Type // type of the pointer }
PtrReplacer replaces a pointer type.
type Region ¶
Region denotes the portion of the code being matched, i.e. the start and end position of the given Node.
type Replacer ¶
type Replacer interface { // Replace generates values for a Go AST provided prior match data. Replace(d data.Data, cl Changelog, pos token.Pos) (v reflect.Value, err error) }
Replacer generates portions of the Go AST meant to replace sections matched by a Matcher. A Replacer is built from the "+" portion of a patch.
type SearchMatcher ¶
SearchMatcher runs a Matcher on descendants of an AST, producing SearchResults into Data.
The corresponding replacer applies a Replacer to these matched descendants.
type SearchNode ¶
SearchNode provides access to an AST node, its parent, and its positional information during a traversal.
type SearchReplacer ¶
type SearchReplacer struct {
Replacer Replacer
}
SearchReplacer replaces nodes found by a SearchMatcher.
type SearchResult ¶
type SearchResult struct {
// contains filtered or unexported fields
}
SearchResult contains information about search results found by a SearchMatcher.
type Searcher ¶
type Searcher func(SearchNode, Matcher, data.Data) *SearchResult
Searcher inspects the given Node using the given Matcher and returns a non-nil SearchResult if it matched.
type SliceDotsMatcher ¶
type SliceDotsMatcher struct { // List of contiguous sections to match against. Sections [][]Matcher // inv: len > 0 // Positions at which dots were found. Dots []token.Pos // inv: len(dots) = len(sections) - 1 }
SliceDotsMatcher implements support for "..." in portions of the AST where slices of values are expected.
type SliceDotsReplacer ¶
type SliceDotsReplacer struct { Type reflect.Type Sections [][]Replacer // inv: len > 0 // Positions at which dots were found. Dots []token.Pos // inv: len(dots) = len(sections) - 1 // contains filtered or unexported fields }
SliceDotsReplacer replaces target nodes and reproduces the values captured by "..." in places which the AST expects a slice.
type SliceMatcher ¶
type SliceMatcher struct { // Matchers for individual fields of the slice. Items []Matcher }
SliceMatcher matches a slice of values exactly.
This matcher does not support eliding values with "...".
type SliceReplacer ¶
type SliceReplacer struct { Type reflect.Type // type of slice // Replacers for individual items in the slice. Items []Replacer }
SliceReplacer replaces a slice of values.
This replacer does not support generating values elided with "...".
type StructMatcher ¶
type StructMatcher struct { Type reflect.Type // type of the struct // Matchers for individual fields of the struct. Fields []Matcher }
StructMatcher matches a struct.
type StructReplacer ¶
type StructReplacer struct { Type reflect.Type // type of the struct // Replacers for individual fields of the struct. Fields []Replacer }
StructReplacer replaces a struct.
type ValueMatcher ¶
ValueMatcher matches a value as-is.
type ValueReplacer ¶
ValueReplacer replace a value as-is.