ast

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package ast provides utilities for working with Go's Abstract Syntax Tree

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTypeNotFound is returned when a type isn't in the cache and can't be
	// loaded
	ErrTypeNotFound = errors.New("type not found")
	// ErrInvalidType is returned when the type doesn't have the expected
	// structure
	ErrInvalidType = errors.New("type did not have expected format")
)

Functions

func Break

func Break() *dst.BranchStmt

Break creates a break dst.BranchStmt

func Continue

func Continue() *dst.BranchStmt

Continue creates a continue dst.BranchStmt

func Ellipsis added in v0.11.0

func Ellipsis(elt dst.Expr) *dst.Ellipsis

Ellipsis translates an expression to a dst.Ellipsis

func FieldList

func FieldList(fields ...*dst.Field) *dst.FieldList

FieldList translates to a dst.FieldList

func Id

func Id(name string) *dst.Ident

Id returns a named dst.Ident

func IdPath

func IdPath(name, path string) *dst.Ident

IdPath returns a dst.Ident with a name and path

func Idf

func Idf(format string, a ...interface{}) *dst.Ident

Idf returns a formatted dst.Ident

func IncStmt

func IncStmt(x dst.Expr) *dst.IncDecStmt

IncStmt creates a dst.IncDecStmt for incrementing an expression

func LitInt

func LitInt(value int) *dst.BasicLit

LitInt returns a dst.BasicLit with a literal int value

func LitString

func LitString(value string) *dst.BasicLit

LitString returns a dst.BasicLit with a literal string value

func LitStringf

func LitStringf(format string, a ...interface{}) *dst.BasicLit

LitStringf returns a formatted dst.BasicLit with a literal string value

func NodeDecsf added in v0.22.0

func NodeDecsf(format string, a ...interface{}) dst.NodeDecs

NodeDecsf formats a string into a standard node decoration, chopping down the line as necessary

func Paren

func Paren(x dst.Expr) *dst.ParenExpr

Paren translates to a dst.ParenExpr

func Return

func Return(results ...dst.Expr) *dst.ReturnStmt

Return returns a dst.ReturnStmt

func SliceType

func SliceType(elt dst.Expr) *dst.ArrayType

SliceType returns a dst.ArrayType representing a slice

func Star

func Star(x dst.Expr) *dst.StarExpr

Star returns a dst.StarExpr

func Struct

func Struct(fields ...*dst.Field) *dst.StructType

Struct returns a dst.StructType

func StructFromList

func StructFromList(fieldList *dst.FieldList) *dst.StructType

StructFromList returns a dst.StructType given a dst.FieldList

func Un

func Un(op token.Token, x dst.Expr) *dst.UnaryExpr

Un returns a dst.UnaryExpr

func Var

func Var(specs ...dst.Spec) *dst.DeclStmt

Var returns a var dst.DeclStmt

Types

type AssignDSL

type AssignDSL struct{ Obj *dst.AssignStmt }

AssignDSL translates to a dst.AssignStmt

func Assign

func Assign(lhs ...dst.Expr) AssignDSL

Assign creates a new AssignDSL

func (AssignDSL) Decs

Decs adds decorations to an AssignDSL

func (AssignDSL) Rhs

func (d AssignDSL) Rhs(rhs ...dst.Expr) AssignDSL

Rhs specifies the right-hand expressions in the assignment

func (AssignDSL) Tok

func (d AssignDSL) Tok(tok token.Token) AssignDSL

Tok specifies the token used in an assignment

type AssignDecsDSL

type AssignDecsDSL struct{ Obj dst.AssignStmtDecorations }

AssignDecsDSL translates to a dst.AssignStmtDecorations

func AssignDecs

func AssignDecs(before dst.SpaceType) AssignDecsDSL

AssignDecs creates a new AssignDecsDSL

func (AssignDecsDSL) After

func (d AssignDecsDSL) After(after dst.SpaceType) AssignDecsDSL

After adds whitespace after an assign statement

type BinDSL

type BinDSL struct{ Obj *dst.BinaryExpr }

BinDSL translates to a dst.BinaryExpr

func Bin

func Bin(x dst.Expr) BinDSL

Bin creates a new BinDSL

func (BinDSL) Op

func (d BinDSL) Op(op token.Token) BinDSL

Op specifies the operator

func (BinDSL) Y

func (d BinDSL) Y(y dst.Expr) BinDSL

Y specifies the right side expression

type BlockDSL

type BlockDSL struct{ Obj *dst.BlockStmt }

BlockDSL translates to a dst.BlockStmt

func Block

func Block(list ...dst.Stmt) BlockDSL

Block creates a new BlockDSL for a list of statements

type Cache

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

Cache loads packages from the AST and caches the results

func NewCache

func NewCache(load LoadFn, metrics metrics.Metrics) *Cache

NewCache returns a new empty Caches

func (*Cache) FindPackage added in v0.17.0

func (c *Cache) FindPackage(dir string) (string, error)

FindPackage finds the package for a given directory

func (*Cache) IsComparable

func (c *Cache) IsComparable(expr dst.Expr) (bool, error)

IsComparable determines if an expression is comparable

func (*Cache) IsDefaultComparable added in v0.15.0

func (c *Cache) IsDefaultComparable(expr dst.Expr) (bool, error)

IsDefaultComparable determines if an expression is comparable. Returns the same results as IsComparable but pointers and interfaces are not comparable by default (interface implementations that are not comparable and put into a map key will panic at runtime and by default pointers use a deep hash to be comparable).

func (*Cache) LoadPackage added in v0.22.0

func (c *Cache) LoadPackage(pkgPattern string) error

LoadPackage loads the specified pattern of package(s) and returns a list of mock-able types

func (*Cache) MockableTypes added in v0.22.0

func (c *Cache) MockableTypes(onlyExported bool) []dst.Ident

MockableTypes returns all the mockable types loaded so far

func (*Cache) Type

func (c *Cache) Type(id dst.Ident, contextPkg string, testImport bool) (TypeInfo, error)

Type returns the requested TypeSpec or an error if the type can't be found

type CallDSL

type CallDSL struct{ Obj *dst.CallExpr }

CallDSL translates to a dst.CallExpr

func Call

func Call(fun dst.Expr) CallDSL

Call creates a new CallDSL

func (CallDSL) Args

func (d CallDSL) Args(args ...dst.Expr) CallDSL

Args specifies the arguments to a call

func (CallDSL) Ellipsis

func (d CallDSL) Ellipsis(ellipsis bool) CallDSL

Ellipsis specifies if the last argument is variadic

type CompDSL

type CompDSL struct{ Obj *dst.CompositeLit }

CompDSL translates to a dst.CompositeLit

func Comp

func Comp(typ dst.Expr) CompDSL

Comp creates a new CompDSL

func (CompDSL) Decs

Decs adds decorations to a CompDSL

func (CompDSL) Elts

func (d CompDSL) Elts(elts ...dst.Expr) CompDSL

Elts defines the elements of a CompDSL

type ExprDSL

type ExprDSL struct{ Obj *dst.ExprStmt }

ExprDSL translates to a dst.ExprStmt

func Expr

func Expr(x dst.Expr) ExprDSL

Expr returns a new ExprDSL

func (ExprDSL) Decs

func (d ExprDSL) Decs(decs dst.ExprStmtDecorations) ExprDSL

Decs adds decorations to a ExprDSL

type ExprDecsDSL

type ExprDecsDSL struct{ Obj dst.ExprStmtDecorations }

ExprDecsDSL translates to a dst.ExprStmtDecorations

func ExprDecs

func ExprDecs(after dst.SpaceType) ExprDecsDSL

ExprDecs creates a new ExprDecsDSL

type FieldDSL

type FieldDSL struct{ Obj *dst.Field }

FieldDSL translates to a dst.Field

func Field

func Field(typ dst.Expr) FieldDSL

Field creates a new FieldDSL

func (FieldDSL) Decs added in v0.15.0

func (d FieldDSL) Decs(decs dst.FieldDecorations) FieldDSL

Decs adds decorations to a FieldDSL

func (FieldDSL) Names

func (d FieldDSL) Names(names ...*dst.Ident) FieldDSL

Names sets the names of a field

type FieldDecsDSL added in v0.15.0

type FieldDecsDSL struct{ Obj dst.FieldDecorations }

FieldDecsDSL translates to a dst.FieldDecorations

func FieldDecs added in v0.15.0

func FieldDecs(before, after dst.SpaceType) FieldDecsDSL

FieldDecs creates a new FieldDecsDSL

type FnDSL added in v0.18.0

type FnDSL struct{ Obj *dst.FuncDecl }

FnDSL translates to a dst.GenDecl containing a function type

func Fn

func Fn(name string) FnDSL

Fn creates a new FnDSL

func (FnDSL) Body added in v0.18.0

func (d FnDSL) Body(list ...dst.Stmt) FnDSL

Body specifies the body for a function

func (FnDSL) Decs added in v0.18.0

func (d FnDSL) Decs(decs dst.FuncDeclDecorations) FnDSL

Decs adds decorations to a FnDSL

func (FnDSL) ParamList added in v0.18.0

func (d FnDSL) ParamList(fieldList *dst.FieldList) FnDSL

ParamList specifies a parameter FieldList for a function

func (FnDSL) Params added in v0.18.0

func (d FnDSL) Params(fields ...*dst.Field) FnDSL

Params specifies parameters for a function

func (FnDSL) Recv added in v0.18.0

func (d FnDSL) Recv(fields ...*dst.Field) FnDSL

Recv specifies the receiver for a function

func (FnDSL) ResultList added in v0.18.0

func (d FnDSL) ResultList(fieldList *dst.FieldList) FnDSL

ResultList specifies a result FieldList for a function

func (FnDSL) Results added in v0.18.0

func (d FnDSL) Results(fields ...*dst.Field) FnDSL

Results specifies results for a function

type FnLitDSL

type FnLitDSL struct{ Obj *dst.FuncLit }

FnLitDSL translates to a dst.FuncLit

func FnLit

func FnLit(typ *dst.FuncType) FnLitDSL

FnLit creates a new FnLitDSL

func (FnLitDSL) Body

func (d FnLitDSL) Body(list ...dst.Stmt) FnLitDSL

Body specifies a body

type FnTypeDSL

type FnTypeDSL struct{ Obj *dst.FuncType }

FnTypeDSL translates to a dst.FuncType

func FnType

func FnType(paramFieldList *dst.FieldList) FnTypeDSL

FnType creates a new FnTypeDSL

func (FnTypeDSL) Results

func (d FnTypeDSL) Results(resultFieldList *dst.FieldList) FnTypeDSL

Results adds a result field list

type ForDSL

type ForDSL struct{ Obj *dst.ForStmt }

ForDSL translatesto a dst.ForStmt

func For

func For(init dst.Stmt) ForDSL

For returns a new ForDSL

func (ForDSL) Body

func (d ForDSL) Body(list ...dst.Stmt) ForDSL

Body defines the body of a for statement

func (ForDSL) Cond

func (d ForDSL) Cond(cond dst.Expr) ForDSL

Cond specifies the condition of a for statement

func (ForDSL) Post

func (d ForDSL) Post(post dst.Stmt) ForDSL

Post specifies the post statement of a for statement

type IfDSL

type IfDSL struct{ Obj *dst.IfStmt }

IfDSL translates to a dst.IfStmt

func If

func If(cond dst.Expr) IfDSL

If creates a new If

func (IfDSL) Body

func (d IfDSL) Body(list ...dst.Stmt) IfDSL

Body specifies the body of the If

func (IfDSL) Decs

func (d IfDSL) Decs(decs dst.IfStmtDecorations) IfDSL

Decs adds decorations to a IfDSL

func (IfDSL) Else added in v0.15.0

func (d IfDSL) Else(els ...dst.Stmt) IfDSL

Else specifies an optional else branch of the If

type IfDecsDSL

type IfDecsDSL struct{ Obj dst.IfStmtDecorations }

IfDecsDSL translates to a dst.IfStmtDecorations

func IfDecs

func IfDecs(after dst.SpaceType) IfDecsDSL

IfDecs creates a new IfDecsDSL

type IndexDSL

type IndexDSL struct{ Obj *dst.IndexExpr }

IndexDSL translates to a dst.IndexExpr

func Index

func Index(x dst.Expr) IndexDSL

Index creates a new IndexDSL

func (IndexDSL) Sub

func (d IndexDSL) Sub(index dst.Expr) IndexDSL

Sub specifies the sub-expression

type KeyValueDSL

type KeyValueDSL struct{ Obj *dst.KeyValueExpr }

KeyValueDSL translates to a dst.KeyValueExpr

func Key

func Key(key dst.Expr) KeyValueDSL

Key creates a new KeyValueDSL

func (KeyValueDSL) Decs

Decs adds decorations to a KeyValueDSL

func (KeyValueDSL) Value

func (d KeyValueDSL) Value(value dst.Expr) KeyValueDSL

Value specifies the value

type KeyValueDecsDSL

type KeyValueDecsDSL struct{ Obj dst.KeyValueExprDecorations }

KeyValueDecsDSL translates to a dst.KeyValueExprDecorations

func KeyValueDecs

func KeyValueDecs(before dst.SpaceType) KeyValueDecsDSL

KeyValueDecs creates a new KeyValueDecsDSL

func (KeyValueDecsDSL) After

After adds decorations after the KeyValueDSL

type LoadFn added in v0.17.0

type LoadFn func(cfg *packages.Config, patterns ...string) ([]*packages.Package, error)

LoadFn is the function type of packages.Load

type MapTypeDSL

type MapTypeDSL struct{ Obj *dst.MapType }

MapTypeDSL translates to a dst.MapType

func MapType

func MapType(key dst.Expr) MapTypeDSL

MapType returns a new MapTypeDSL

func (MapTypeDSL) Value

func (d MapTypeDSL) Value(value dst.Expr) MapTypeDSL

Value specifies the value expression of a dst.MapType

type RangeDSL

type RangeDSL struct{ Obj *dst.RangeStmt }

RangeDSL translates to a dst.RangeStmt

func Range

func Range(x dst.Expr) RangeDSL

Range returns a new RangeDSL

func (RangeDSL) Body

func (d RangeDSL) Body(list ...dst.Stmt) RangeDSL

Body defines the body of a range statement

func (RangeDSL) Key

func (d RangeDSL) Key(key dst.Expr) RangeDSL

Key sets the key of a range statement

func (RangeDSL) Tok

func (d RangeDSL) Tok(tok token.Token) RangeDSL

Tok sets the token of a range statement

func (RangeDSL) Value

func (d RangeDSL) Value(value dst.Expr) RangeDSL

Value sets the value of a range statement

type SelDSL

type SelDSL struct{ Obj *dst.SelectorExpr }

SelDSL translates to a dst.SelectorExpr

func Sel

func Sel(x dst.Expr) SelDSL

Sel creates a new SelDSL

func (SelDSL) Dot

func (d SelDSL) Dot(sel *dst.Ident) SelDSL

Dot specifies what is selected

type SliceExprDSL

type SliceExprDSL struct{ Obj *dst.SliceExpr }

SliceExprDSL translates to a dst.SliceExpr

func SliceExpr

func SliceExpr(x dst.Expr) SliceExprDSL

SliceExpr creates a new slice expression

func (SliceExprDSL) High

func (d SliceExprDSL) High(high dst.Expr) SliceExprDSL

High specifies the high expression of a slice expression

func (SliceExprDSL) Low

func (d SliceExprDSL) Low(low dst.Expr) SliceExprDSL

Low specifies the low expression of a slice expression

type TypeDeclDSL

type TypeDeclDSL struct{ Obj *dst.GenDecl }

TypeDeclDSL translates various types into a dst.GenDecl

func TypeDecl

func TypeDecl(typeSpec *dst.TypeSpec) TypeDeclDSL

TypeDecl creates a new TypeDeclDSL

func (TypeDeclDSL) Decs

Decs adds decorations to a TypeDeclDSL

type TypeInfo added in v0.22.0

type TypeInfo struct {
	Type       *dst.TypeSpec
	PkgPath    string
	Exported   bool
	Fabricated bool
}

TypeInfo returns all the information the cache holds for a type

type TypeSpecDSL

type TypeSpecDSL struct{ Obj *dst.TypeSpec }

TypeSpecDSL translates to a dst.TypeSpec

func TypeSpec

func TypeSpec(name string) TypeSpecDSL

TypeSpec creates a new TypeSpecDSL

func (TypeSpecDSL) Type

func (d TypeSpecDSL) Type(typ dst.Expr) TypeSpecDSL

Type creates a new TypeSpecDSL

type ValueDSL

type ValueDSL struct{ Obj *dst.ValueSpec }

ValueDSL translates to a dst.ValueSpec

func Value

func Value(typ dst.Expr) ValueDSL

Value creates a new ValueDSL

func (ValueDSL) Names

func (d ValueDSL) Names(names ...*dst.Ident) ValueDSL

Names sets the names of a value

func (ValueDSL) Values added in v0.22.0

func (d ValueDSL) Values(values ...dst.Expr) ValueDSL

Values sets the values of the spec

type VarDeclDSL added in v0.22.0

type VarDeclDSL struct{ Obj *dst.GenDecl }

VarDeclDSL translates variable declaration into a dst.GenDecl

func VarDecl added in v0.22.0

func VarDecl(specs ...dst.Spec) VarDeclDSL

VarDecl creates a new VarDeclDSL

func (VarDeclDSL) Decs added in v0.22.0

Decs adds decorations to a VarDeclDSL

Directories

Path Synopsis
testpkgs

Jump to

Keyboard shortcuts

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