ast

package
v0.0.0-...-fae14fb Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2020 License: MIT Imports: 2 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OpTable = []opTableEntry{

	{"+", LPrefix, false},
	{"-", LPrefix, false},
	{"~", LPrefix, false},
	{"!", LPrefix, false},
	{"void", LPrefix, true},
	{"typeof", LPrefix, true},
	{"delete", LPrefix, true},

	{"--", LPrefix, false},
	{"++", LPrefix, false},

	{"--", LPostfix, false},
	{"++", LPostfix, false},

	{"+", LAdd, false},
	{"-", LAdd, false},
	{"*", LMultiply, false},
	{"/", LMultiply, false},
	{"%", LMultiply, false},
	{"**", LExponentiation, false},
	{"<", LCompare, false},
	{"<=", LCompare, false},
	{">", LCompare, false},
	{">=", LCompare, false},
	{"in", LCompare, true},
	{"instanceof", LCompare, true},
	{"<<", LShift, false},
	{">>", LShift, false},
	{">>>", LShift, false},
	{"==", LEquals, false},
	{"!=", LEquals, false},
	{"===", LEquals, false},
	{"!==", LEquals, false},
	{"??", LNullishCoalescing, false},
	{"||", LLogicalOr, false},
	{"&&", LLogicalAnd, false},
	{"|", LBitwiseOr, false},
	{"&", LBitwiseAnd, false},
	{"^", LBitwiseXor, false},

	{",", LComma, false},

	{"=", LAssign, false},
	{"+=", LAssign, false},
	{"-=", LAssign, false},
	{"*=", LAssign, false},
	{"/=", LAssign, false},
	{"%=", LAssign, false},
	{"**=", LAssign, false},
	{"<<=", LAssign, false},
	{">>=", LAssign, false},
	{">>>=", LAssign, false},
	{"|=", LAssign, false},
	{"&=", LAssign, false},
	{"^=", LAssign, false},
}

Functions

func FollowAllSymbols

func FollowAllSymbols(symbols *SymbolMap)

Use this before calling "FollowSymbols" from separate threads to avoid concurrent map update hazards. In Go, mutating a map is not threadsafe but reading from a map is. Calling "FollowAllSymbols" first ensures that all mutation is done up front.

func GenerateNonUniqueNameFromPath

func GenerateNonUniqueNameFromPath(text string) string

Types

type AST

type AST struct {
	ImportPaths []ImportPath

	// ENamespaceImport items in this map are printed as an indirect access off
	// of the namespace. This is a way for the bundler to pass this information
	// to the printer. This is necessary when using a namespace import or when
	// an import item must be converted to a property access off a require() call.
	IndirectImportItems map[Ref]bool

	// This is true if something used the "exports" or "module" variables, which
	// means they could have exported something. This is used to silence errors
	// about mismatched exports.
	HasCommonJsExports bool

	Stmts       []Stmt
	Symbols     *SymbolMap
	ModuleScope *Scope
	ExportsRef  Ref
	RequireRef  Ref
	ModuleRef   Ref
}

type Arg

type Arg struct {
	Binding Binding
	Default *Expr
}

type ArrayBinding

type ArrayBinding struct {
	Binding      Binding
	DefaultValue *Expr
}

type B

type B interface {
	// contains filtered or unexported methods
}

This interface is never called. Its purpose is to encode a variant type in Go's type system.

type BArray

type BArray struct {
	Items     []ArrayBinding
	HasSpread bool
}

type BIdentifier

type BIdentifier struct{ Ref Ref }

type BMissing

type BMissing struct{}

type BObject

type BObject struct{ Properties []PropertyBinding }

type Binding

type Binding struct {
	Loc  Loc
	Data B
}

type Case

type Case struct {
	Value *Expr
	Body  []Stmt
}

type Catch

type Catch struct {
	Binding *Binding
	Body    []Stmt
}

type Class

type Class struct {
	Name       *LocRef
	Extends    *Expr
	Properties []Property
}

type ClauseItem

type ClauseItem struct {
	Alias    string
	AliasLoc Loc
	Name     LocRef
}

type Decl

type Decl struct {
	Binding Binding
	Value   *Expr
}

type E

type E interface {
	// contains filtered or unexported methods
}

This interface is never called. Its purpose is to encode a variant type in Go's type system.

type EArray

type EArray struct{ Items []Expr }

type EArrow

type EArrow struct {
	IsAsync    bool
	Args       []Arg
	HasRestArg bool
	Stmts      []Stmt
	Expr       *Expr
}

type EAwait

type EAwait struct {
	Value Expr
}

type EBigInt

type EBigInt struct{ Value string }

type EBinary

type EBinary struct {
	Op    OpCode
	Left  Expr
	Right Expr
}

type EBoolean

type EBoolean struct{ Value bool }

type ECall

type ECall struct {
	Target          Expr
	Args            []Expr
	IsOptionalChain bool
}

type EClass

type EClass struct{ Class Class }

type EDot

type EDot struct {
	Target          Expr
	Name            string
	NameLoc         Loc
	IsOptionalChain bool
}

type EFunction

type EFunction struct{ Fn Fn }

type EIdentifier

type EIdentifier struct{ Ref Ref }

type EIf

type EIf struct {
	Test Expr
	Yes  Expr
	No   Expr
}

type EImport

type EImport struct {
	Expr Expr
}

type EImportMeta

type EImportMeta struct{}

type EIndex

type EIndex struct {
	Target          Expr
	Index           Expr
	IsOptionalChain bool
}

type EJSXElement

type EJSXElement struct {
	Tag        *Expr
	Properties []Property
	Children   []Expr
}

type EMissing

type EMissing struct{}

type ENamespaceImport

type ENamespaceImport struct {
	NamespaceRef Ref
	ItemRef      Ref
	Alias        string
}

type ENew

type ENew struct {
	Target Expr
	Args   []Expr
}

type ENewTarget

type ENewTarget struct{}

type ENull

type ENull struct{}

type ENumber

type ENumber struct{ Value float64 }

type EObject

type EObject struct{ Properties []Property }

type ERegExp

type ERegExp struct{ Value string }

type ERequire

type ERequire struct {
	Path        Path
	IsES6Import bool
}

type ESpread

type ESpread struct{ Value Expr }

type EString

type EString struct{ Value []uint16 }

type ESuper

type ESuper struct{}

type ETemplate

type ETemplate struct {
	Tag     *Expr
	Head    []uint16
	HeadRaw string // This is only filled out for tagged template literals
	Parts   []TemplatePart
}

type EThis

type EThis struct{}

type EUnary

type EUnary struct {
	Op    OpCode
	Value Expr
}

type EUndefined

type EUndefined struct{}

type EYield

type EYield struct {
	Value  *Expr
	IsStar bool
}

type Expr

type Expr struct {
	Loc  Loc
	Data E
}

type ExprOrStmt

type ExprOrStmt struct {
	Expr *Expr
	Stmt *Stmt
}

type Fn

type Fn struct {
	Name        *LocRef
	Args        []Arg
	IsAsync     bool
	IsGenerator bool
	HasRestArg  bool
	Stmts       []Stmt
}

type ImportKind

type ImportKind uint8
const (
	ImportStmt ImportKind = iota
	ImportRequire
	ImportDynamic
)

type ImportPath

type ImportPath struct {
	Path Path
	Kind ImportKind
}

type L

type L int
const (
	LLowest L = iota
	LComma
	LSpread
	LYield
	LAssign
	LConditional
	LNullishCoalescing
	LLogicalOr
	LLogicalAnd
	LBitwiseOr
	LBitwiseXor
	LBitwiseAnd
	LEquals
	LCompare
	LShift
	LAdd
	LMultiply
	LExponentiation
	LPrefix
	LPostfix
	LNew
	LCall
)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

type Loc

type Loc struct {
	// This is the 0-based index of this location from the start of the file
	Start int32
}

type LocRef

type LocRef struct {
	Loc Loc
	Ref Ref
}

type OpCode

type OpCode int
const (
	// Prefix
	UnOpPos OpCode = iota
	UnOpNeg
	UnOpCpl
	UnOpNot
	UnOpVoid
	UnOpTypeOf
	UnOpDelete

	// Prefix update
	UnOpPreDec
	UnOpPreInc

	// Postfix update
	UnOpPostDec
	UnOpPostInc

	// Left-associative
	BinOpAdd
	BinOpSub
	BinOpMul
	BinOpDiv
	BinOpRem
	BinOpPow
	BinOpLt
	BinOpLe
	BinOpGt
	BinOpGe
	BinOpIn
	BinOpInstanceOf
	BinOpShl
	BinOpShr
	BinOpUShr
	BinOpLooseEq
	BinOpLooseNe
	BinOpStrictEq
	BinOpStrictNe
	BinOpNullishCoalescing
	BinOpLogicalOr
	BinOpLogicalAnd
	BinOpBitwiseOr
	BinOpBitwiseAnd
	BinOpBitwiseXor

	// Non-associative
	BinOpComma

	// Right-associative
	BinOpAssign
	BinOpAddAssign
	BinOpSubAssign
	BinOpMulAssign
	BinOpDivAssign
	BinOpRemAssign
	BinOpPowAssign
	BinOpShlAssign
	BinOpShrAssign
	BinOpUShrAssign
	BinOpBitwiseOrAssign
	BinOpBitwiseAndAssign
	BinOpBitwiseXorAssign
)

If you add a new token, remember to add it to "OpTable" too

func (OpCode) IsLeftAssociative

func (op OpCode) IsLeftAssociative() bool

func (OpCode) IsPrefix

func (op OpCode) IsPrefix() bool

func (OpCode) IsRightAssociative

func (op OpCode) IsRightAssociative() bool

func (OpCode) IsUnaryUpdate

func (op OpCode) IsUnaryUpdate() bool

type Path

type Path struct {
	Loc  Loc
	Text string
}

type Property

type Property struct {
	Kind       PropertyKind
	IsComputed bool
	IsMethod   bool
	IsStatic   bool
	Key        Expr

	// This is omitted for class fields
	Value *Expr

	// This is used when parsing a pattern that uses default values:
	//
	//   [a = 1] = [];
	//   ({a = 1} = {});
	//
	// It's also used for class fields:
	//
	//   class Foo { a = 1 }
	//
	Initializer *Expr
}

type PropertyBinding

type PropertyBinding struct {
	IsComputed   bool
	IsSpread     bool
	Key          Expr
	Value        Binding
	DefaultValue *Expr
}

type PropertyKind

type PropertyKind int
const (
	PropertyNormal PropertyKind = iota
	PropertyGet
	PropertySet
	PropertySpread
)

type Range

type Range struct {
	Loc Loc
	Len int32
}

type Ref

type Ref struct {
	OuterIndex uint32
	InnerIndex uint32
}

Files are parsed in parallel for speed. We want to allow each parser to generate symbol IDs that won't conflict with each other. We also want to be able to quickly merge symbol tables from all files into one giant symbol table.

We can accomplish both goals by giving each symbol ID two parts: an outer index that is unique to the parser goroutine, and an inner index that increments as the parser generates new symbol IDs. Then a symbol map can be an array of arrays indexed first by outer index, then by inner index. The maps can be merged quickly by creating a single outer array containing all inner arrays from all parsed files.

var InvalidRef Ref = Ref{^uint32(0), ^uint32(0)}

func FollowSymbols

func FollowSymbols(symbols *SymbolMap, ref Ref) Ref

Returns the canonical ref that represents the ref for the provided symbol. This may not be the provided ref if the symbol has been merged with another symbol.

func MergeSymbols

func MergeSymbols(symbols *SymbolMap, old Ref, new Ref) Ref

Makes "old" point to "new" by joining the linked lists for the two symbols together. That way "FollowSymbols" on both "old" and "new" will result in the same ref.

type S

type S interface {
	// contains filtered or unexported methods
}

This interface is never called. Its purpose is to encode a variant type in Go's type system.

type SBlock

type SBlock struct {
	Stmts []Stmt
}

type SBreak

type SBreak struct {
	Name *LocRef
}

type SClass

type SClass struct {
	Class    Class
	IsExport bool
}

type SConst

type SConst struct {
	Decls    []Decl
	IsExport bool
}

type SContinue

type SContinue struct {
	Name *LocRef
}

type SDebugger

type SDebugger struct{}

type SDirective

type SDirective struct {
	Value []uint16
}

type SDoWhile

type SDoWhile struct {
	Body Stmt
	Test Expr
}

type SEmpty

type SEmpty struct{}

type SExportClause

type SExportClause struct {
	Items []ClauseItem
}

type SExportDefault

type SExportDefault struct {
	DefaultName LocRef
	Value       ExprOrStmt // May be a SFunction or SClass
}

type SExportFrom

type SExportFrom struct {
	Items        []ClauseItem
	NamespaceRef Ref
	Path         Path
}

type SExportStar

type SExportStar struct {
	Item *ClauseItem
	Path Path
}

type SExpr

type SExpr struct {
	Value Expr
}

type SFor

type SFor struct {
	Init   *Stmt // May be a SConst, SLet, SVar, or SExpr
	Test   *Expr
	Update *Expr
	Body   Stmt
}

type SForIn

type SForIn struct {
	Init  Stmt // May be a SConst, SLet, SVar, or SExpr
	Value Expr
	Body  Stmt
}

type SForOf

type SForOf struct {
	IsAwait bool
	Init    Stmt // May be a SConst, SLet, SVar, or SExpr
	Value   Expr
	Body    Stmt
}

type SFunction

type SFunction struct {
	Fn       Fn
	IsExport bool
}

type SIf

type SIf struct {
	Test Expr
	Yes  Stmt
	No   *Stmt
}

type SImport

type SImport struct {
	// If this is a star import: This is a Ref for the namespace symbol. The Loc
	// for the symbol is StarLoc.
	//
	// Otherwise: This is an auto-generated Ref for the namespace representing
	// the imported file. In this case StarLoc is nil. The NamespaceRef is used
	// when converting this module to a CommonJS module.
	NamespaceRef Ref

	DefaultName *LocRef
	Items       *[]ClauseItem
	StarLoc     *Loc
	Path        Path
}

This object represents all of these types of import statements:

import 'path'
import {item1, item2} from 'path'
import * as ns from 'path'
import defaultItem, {item1, item2} from 'path'
import defaultItem, * as ns from 'path'

Many parts are optional and can be combined in different ways. The only restriction is that you cannot have both a clause and a star namespace.

type SLabel

type SLabel struct {
	Name LocRef
	Stmt Stmt
}

type SLet

type SLet struct {
	Decls    []Decl
	IsExport bool
}

type SReturn

type SReturn struct {
	Value *Expr
}

type SSwitch

type SSwitch struct {
	Test  Expr
	Cases []Case
}

type SThrow

type SThrow struct {
	Value Expr
}

type STry

type STry struct {
	Body    []Stmt
	Catch   *Catch
	Finally *[]Stmt
}

type SVar

type SVar struct {
	Decls    []Decl
	IsExport bool
}

type SWhile

type SWhile struct {
	Test Expr
	Body Stmt
}

type SWith

type SWith struct {
	Value Expr
	Body  Stmt
}

type Scope

type Scope struct {
	Kind      ScopeKind
	Parent    *Scope
	Children  []*Scope
	Members   map[string]Ref
	Generated []Ref

	// This is used to store the ref of the label symbol for ScopeLabel scopes.
	LabelRef Ref
}

type ScopeKind

type ScopeKind int
const (
	ScopeBlock ScopeKind = iota
	ScopeLabel
	ScopeFunction
	ScopeFunctionName
	ScopeClassName
	ScopeModule
)

type Stmt

type Stmt struct {
	Loc  Loc
	Data S
}

type Symbol

type Symbol struct {
	Kind SymbolKind

	// An estimate of the number of uses of this symbol. This is used for
	// minification (to prefer shorter names for more frequently used symbols).
	// The reason why this is an estimate instead of an accurate count is that
	// it's not updated during dead code elimination for speed. I figure that
	// even without updating after parsing it's still a pretty good heuristic.
	UseCountEstimate uint32

	Name string

	// Used by the parser for single pass parsing. Symbols that have been merged
	// form a linked-list where the last link is the symbol to use. This link is
	// an invalid ref if it's the last link. If this isn't invalid, you need to
	// FollowSymbols to get the real one.
	Link Ref
}

type SymbolKind

type SymbolKind uint8
const (
	// An unbound symbol is one that isn't declared in the file it's referenced
	// in. For example, using "window" without declaring it will be unbound.
	SymbolUnbound SymbolKind = iota

	// This has special merging behavior. You're allowed to re-declare these
	// symbols more than once in the same scope. These symbols are also hoisted
	// out of the scope they are declared in to the closest containing function
	// or module scope. These are the symbols with this kind:
	//
	// - Function arguments
	// - Function statements
	// - Variables declared using "var"
	//
	SymbolHoisted

	// There's a weird special case where catch variables declared using a simple
	// identifier (i.e. not a binding pattern) block hoisted variables instead of
	// becoming an error:
	//
	//   var e = 0;
	//   try { throw 1 } catch (e) {
	//     print(e) // 1
	//     var e = 2
	//     print(e) // 2
	//   }
	//   print(e) // 0 (since the hoisting stops at the catch block boundary)
	//
	// However, other forms are still a syntax error:
	//
	//   try {} catch (e) { let e }
	//   try {} catch ({e}) { var e }
	//
	// This symbol is for handling this weird special case.
	SymbolCatchIdentifier

	// This annotates all other symbols that don't have special behavior.
	SymbolOther
)

type SymbolMap

type SymbolMap struct {
	// This could be represented as a "map[Ref]Symbol" but a two-level array was
	// more efficient in profiles. This appears to be because it doesn't involve
	// a hash. This representation also makes it trivial to quickly merge symbol
	// maps from multiple files together. Each file only generates symbols in a
	// single inner array, so you can join the maps together by just make a
	// single outer array containing all of the inner arrays. See the comment on
	// "Ref" for more detail.
	Outer [][]Symbol
}

func NewSymbolMap

func NewSymbolMap(maxSourceIndex int) *SymbolMap

func (*SymbolMap) Get

func (sm *SymbolMap) Get(ref Ref) Symbol

func (*SymbolMap) IncrementUseCountEstimate

func (sm *SymbolMap) IncrementUseCountEstimate(ref Ref)

func (*SymbolMap) Set

func (sm *SymbolMap) Set(ref Ref, symbol Symbol)

The symbol must already exist to call this

func (*SymbolMap) SetNew

func (sm *SymbolMap) SetNew(ref Ref, symbol Symbol)

The symbol may not already exist when you call this

type TemplatePart

type TemplatePart struct {
	Value   Expr
	Tail    []uint16
	TailRaw string // This is only filled out for tagged template literals
}

Jump to

Keyboard shortcuts

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