js_ast

package
v0.20.2 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

This file was automatically generated by gen-unicode-table.js. Do not edit.

Index

Constants

View Source
const NSExportPartIndex = uint32(0)

This is the index to the automatically-generated part containing code that calls "__export(exports, { ... getters ... })". This is used to generate getters on an exports object for ES6 export statements, and is both for ES6 star imports and CommonJS-style modules. All files have one of these, although it may contain no statements if there is nothing to export.

Variables

View Source
var BMissingShared = &BMissing{}

These help reduce unnecessary memory allocations

View Source
var EMissingShared = &EMissing{}
View Source
var ENullShared = &ENull{}
View Source
var ESuperShared = &ESuper{}
View Source
var EThisShared = &EThis{}
View Source
var EUndefinedShared = &EUndefined{}
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},
	{"??=", LAssign, false},
	{"||=", LAssign, false},
	{"&&=", LAssign, false},
}
View Source
var SDebuggerShared = &SDebugger{}
View Source
var SEmptyShared = &SEmpty{}
View Source
var STypeScriptShared = &STypeScript{}
View Source
var STypeScriptSharedWasDeclareClass = &STypeScript{WasDeclareClass: true}

Functions

func CanChangeStrictToLoose added in v0.16.5

func CanChangeStrictToLoose(a Expr, b Expr) bool

func CheckEqualityBigInt added in v0.19.6

func CheckEqualityBigInt(a string, b string) (equal bool, ok bool)

func CheckEqualityIfNoSideEffects added in v0.14.25

func CheckEqualityIfNoSideEffects(left E, right E, kind EqualityKind) (equal bool, ok bool)

Returns "equal, ok". If "ok" is false, then nothing is known about the two values. If "ok" is true, the equality or inequality of the two values is stored in "equal".

func EnsureValidIdentifier added in v0.8.27

func EnsureValidIdentifier(base string) string

func ForEachIdentifierBinding added in v0.18.7

func ForEachIdentifierBinding(binding Binding, callback func(loc logger.Loc, b *BIdentifier))

func ForEachIdentifierBindingInDecls added in v0.18.7

func ForEachIdentifierBindingInDecls(decls []Decl, callback func(loc logger.Loc, b *BIdentifier))

func ForceValidIdentifier added in v0.16.5

func ForceValidIdentifier(prefix string, text string) string

func GenerateNonUniqueNameFromPath

func GenerateNonUniqueNameFromPath(path string) string

For readability, the names of certain automatically-generated symbols are derived from the file name. For example, instead of the CommonJS wrapper for a file being called something like "require273" it can be called something like "require_react" instead. This function generates the part of these identifiers that's specific to the file path. It can take both an absolute path (OS-specific) and a path in the source code (OS-independent).

Note that these generated names do not at all relate to the correctness of the code as far as avoiding symbol name collisions. These names still go through the renaming logic that all other symbols go through to avoid name collisions.

func IsBinaryNullAndUndefined added in v0.16.5

func IsBinaryNullAndUndefined(left Expr, right Expr, op OpCode) (Expr, Expr, bool)

func IsIdentifier added in v0.16.5

func IsIdentifier(text string) bool

func IsIdentifierContinue added in v0.16.5

func IsIdentifierContinue(codePoint rune) bool

func IsIdentifierContinueES5AndESNext added in v0.16.5

func IsIdentifierContinueES5AndESNext(codePoint rune) bool

func IsIdentifierES5AndESNext added in v0.16.5

func IsIdentifierES5AndESNext(text string) bool

func IsIdentifierES5AndESNextUTF16 added in v0.16.5

func IsIdentifierES5AndESNextUTF16(text []uint16) bool

This does "IsIdentifierES5AndESNext(UTF16ToString(text))" without any allocations

func IsIdentifierStart added in v0.16.5

func IsIdentifierStart(codePoint rune) bool

func IsIdentifierStartES5AndESNext added in v0.16.5

func IsIdentifierStartES5AndESNext(codePoint rune) bool

func IsIdentifierUTF16 added in v0.16.5

func IsIdentifierUTF16(text []uint16) bool

This does "IsIdentifier(UTF16ToString(text))" without any allocations

func IsOptionalChain added in v0.8.48

func IsOptionalChain(value Expr) bool

func IsPrimitiveLiteral added in v0.16.5

func IsPrimitiveLiteral(data E) bool

func IsPropertyAccess added in v0.16.5

func IsPropertyAccess(expr Expr) bool

If this returns true, then calling this expression captures the target of the property access as "this" when calling the function in the property.

func IsSymbolInstance added in v0.19.11

func IsSymbolInstance(data E) bool

func IsWhitespace added in v0.16.5

func IsWhitespace(codePoint rune) bool

See the "White Space Code Points" table in the ECMAScript standard

func ShouldFoldBinaryArithmeticWhenMinifying added in v0.16.5

func ShouldFoldBinaryArithmeticWhenMinifying(binary *EBinary) bool

func StringToEquivalentNumberValue added in v0.16.5

func StringToEquivalentNumberValue(value []uint16) (float64, bool)

func ToInt32 added in v0.14.19

func ToInt32(f float64) int32

func ToNumberWithoutSideEffects added in v0.14.19

func ToNumberWithoutSideEffects(data E) (float64, bool)

func ToStringWithoutSideEffects added in v0.19.6

func ToStringWithoutSideEffects(data E) (string, bool)

func ToUint32 added in v0.14.19

func ToUint32(f float64) uint32

func TryToInsertOptionalChain added in v0.14.25

func TryToInsertOptionalChain(test Expr, expr Expr) bool

func TryToStringOnNumberSafely added in v0.19.6

func TryToStringOnNumberSafely(n float64, radix int) (string, bool)

String concatenation with numbers is required by the TypeScript compiler for "constant expression" handling in enums. However, we don't want to introduce correctness bugs by accidentally stringifying a number differently than how a real JavaScript VM would do it. So we are conservative and we only do this when we know it'll be the same result.

func TypeofWithoutSideEffects added in v0.16.5

func TypeofWithoutSideEffects(data E) (string, bool)

Returns true if the result of the "typeof" operator on this expression is statically determined and this expression has no side effects (i.e. can be removed without consequence).

func ValuesLookTheSame added in v0.14.25

func ValuesLookTheSame(left E, right E) bool

Types

type AST

type AST struct {
	ModuleTypeData ModuleTypeData
	Parts          []Part
	Symbols        []ast.Symbol
	ExprComments   map[logger.Loc][]string
	ModuleScope    *Scope
	CharFreq       *ast.CharFreq

	// This is internal-only data used for the implementation of Yarn PnP
	ManifestForYarnPnP Expr

	Hashbang   string
	Directives []string
	URLForCSS  string

	// Note: If you're in the linker, do not use this map directly. This map is
	// filled in by the parser and is considered immutable. For performance reasons,
	// the linker doesn't mutate this map (cloning a map is slow in Go). Instead the
	// linker super-imposes relevant information on top in a method call. You should
	// call "TopLevelSymbolToParts" instead.
	TopLevelSymbolToPartsFromParser map[ast.Ref][]uint32

	// This contains all top-level exported TypeScript enum constants. It exists
	// to enable cross-module inlining of constant enums.
	TSEnums map[ast.Ref]map[string]TSEnumValue

	// This contains the values of all detected inlinable constants. It exists
	// to enable cross-module inlining of these constants.
	ConstValues map[ast.Ref]ConstValue

	// Properties in here are represented as symbols instead of strings, which
	// allows them to be renamed to smaller names.
	MangledProps map[string]ast.Ref

	// Properties in here are existing non-mangled properties in the source code
	// and must not be used when generating mangled names to avoid a collision.
	ReservedProps map[string]bool

	// These are stored at the AST level instead of on individual AST nodes so
	// they can be manipulated efficiently without a full AST traversal
	ImportRecords []ast.ImportRecord

	// These are used when bundling. They are filled in during the parser pass
	// since we already have to traverse the AST then anyway and the parser pass
	// is conveniently fully parallelized.
	NamedImports            map[ast.Ref]NamedImport
	NamedExports            map[string]NamedExport
	ExportStarImportRecords []uint32

	SourceMapComment logger.Span

	// This is a list of ES6 features. They are ranges instead of booleans so
	// that they can be used in log messages. Check to see if "Len > 0".
	ExportKeyword            logger.Range // Does not include TypeScript-specific syntax
	TopLevelAwaitKeyword     logger.Range
	LiveTopLevelAwaitKeyword logger.Range // Excludes top-level await in dead branches

	ExportsRef ast.Ref
	ModuleRef  ast.Ref
	WrapperRef ast.Ref

	ApproximateLineCount  int32
	NestedScopeSlotCounts ast.SlotCounts
	HasLazyExport         bool

	// This is a list of CommonJS features. When a file uses CommonJS features,
	// it's not a candidate for "flat bundling" and must be wrapped in its own
	// closure. Note that this also includes top-level "return" but these aren't
	// here because only the parser checks those.
	UsesExportsRef bool
	UsesModuleRef  bool
	ExportsKind    ExportsKind
}

type AnnotationFlags added in v0.18.2

type AnnotationFlags uint8
const (
	// This is sort of like an IIFE with a "/* @__PURE__ */" comment except it's an
	// inline annotation on an expression itself without the nested scope. Sometimes
	// we can't easily introduce a new scope (e.g. if the expression uses "await").
	CanBeRemovedIfUnusedFlag AnnotationFlags = 1 << iota
)

func (AnnotationFlags) Has added in v0.18.2

func (flags AnnotationFlags) Has(flag AnnotationFlags) bool

type Arg

type Arg struct {
	Binding      Binding
	DefaultOrNil Expr
	Decorators   []Decorator

	// "constructor(public x: boolean) {}"
	IsTypeScriptCtorField bool
}

type ArrayBinding

type ArrayBinding struct {
	Binding           Binding
	DefaultValueOrNil Expr
	Loc               logger.Loc
}

type AssignTarget

type AssignTarget uint8
const (
	AssignTargetNone    AssignTarget = iota
	AssignTargetReplace              // "a = b"
	AssignTargetUpdate               // "a += b"
)

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
	CloseBracketLoc logger.Loc
	HasSpread       bool
	IsSingleLine    bool
}

type BIdentifier

type BIdentifier struct{ Ref ast.Ref }

type BMissing

type BMissing struct{}

type BObject

type BObject struct {
	Properties    []PropertyBinding
	CloseBraceLoc logger.Loc
	IsSingleLine  bool
}

type Binding

type Binding struct {
	Data B
	Loc  logger.Loc
}

type CallKind added in v0.16.5

type CallKind uint8
const (
	NormalCall CallKind = iota
	DirectEval
	TargetWasOriginallyPropertyAccess
)

type Case

type Case struct {
	ValueOrNil Expr // If this is nil, this is "default" instead of "case"
	Body       []Stmt
	Loc        logger.Loc
}

type Catch

type Catch struct {
	BindingOrNil Binding
	Block        SBlock
	Loc          logger.Loc
	BlockLoc     logger.Loc
}

type Class

type Class struct {
	Decorators    []Decorator
	Name          *ast.LocRef
	ExtendsOrNil  Expr
	Properties    []Property
	ClassKeyword  logger.Range
	BodyLoc       logger.Loc
	CloseBraceLoc logger.Loc

	// If true, property field initializers cannot be assumed to have no side
	// effects. For example:
	//
	//   class Foo {
	//     static set foo(x) { importantSideEffect(x) }
	//   }
	//   class Bar extends Foo {
	//     foo = 1
	//   }
	//
	// This happens in TypeScript when "useDefineForClassFields" is disabled
	// because TypeScript (and esbuild) transforms the above class into this:
	//
	//   class Foo {
	//     static set foo(x) { importantSideEffect(x); }
	//   }
	//   class Bar extends Foo {
	//   }
	//   Bar.foo = 1;
	//
	UseDefineForClassFields bool
}

type ClassStaticBlock added in v0.13.11

type ClassStaticBlock struct {
	Block SBlock
	Loc   logger.Loc
}

type ClauseItem

type ClauseItem struct {
	Alias string

	// This is the original name of the symbol stored in "Name". It's needed for
	// "SExportClause" statements such as this:
	//
	//   export {foo as bar} from 'path'
	//
	// In this case both "foo" and "bar" are aliases because it's a re-export.
	// We need to preserve both aliases in case the symbol is renamed. In this
	// example, "foo" is "OriginalName" and "bar" is "Alias".
	OriginalName string

	AliasLoc logger.Loc
	Name     ast.LocRef
}

type ConstValue added in v0.14.19

type ConstValue struct {
	Number float64 // Use this for "ConstValueNumber"
	Kind   ConstValueKind
}

func ExprToConstValue added in v0.14.19

func ExprToConstValue(expr Expr) ConstValue

type ConstValueKind added in v0.14.19

type ConstValueKind uint8
const (
	ConstValueNone ConstValueKind = iota
	ConstValueNull
	ConstValueUndefined
	ConstValueTrue
	ConstValueFalse
	ConstValueNumber
)

type Decl

type Decl struct {
	Binding    Binding
	ValueOrNil Expr
}

type DeclaredSymbol

type DeclaredSymbol struct {
	Ref        ast.Ref
	IsTopLevel bool
}

type Decorator added in v0.18.5

type Decorator struct {
	Value            Expr
	AtLoc            logger.Loc
	OmitNewlineAfter bool
}

type Dependency added in v0.11.3

type Dependency struct {
	SourceIndex uint32
	PartIndex   uint32
}

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 EAnnotation added in v0.18.2

type EAnnotation struct {
	Value Expr
	Flags AnnotationFlags
}

type EArray

type EArray struct {
	Items            []Expr
	CommaAfterSpread logger.Loc
	CloseBracketLoc  logger.Loc
	IsSingleLine     bool
	IsParenthesized  bool
}

type EArrow

type EArrow struct {
	Args []Arg
	Body FnBody

	IsAsync    bool
	HasRestArg bool
	PreferExpr bool // Use shorthand if true and "Body" is a single return statement

	// See: https://github.com/rollup/rollup/pull/5024
	HasNoSideEffectsComment bool
}

type EAwait

type EAwait struct {
	Value Expr
}

type EBigInt

type EBigInt struct{ Value string }

type EBinary

type EBinary struct {
	Left  Expr
	Right Expr
	Op    OpCode
}

type EBoolean

type EBoolean struct{ Value bool }

type ECall

type ECall struct {
	Target        Expr
	Args          []Expr
	CloseParenLoc logger.Loc
	OptionalChain OptionalChain
	Kind          CallKind
	IsMultiLine   bool

	// True if there is a comment containing "@__PURE__" or "#__PURE__" preceding
	// this call expression. This is an annotation used for tree shaking, and
	// means that the call can be removed if it's unused. It does not mean the
	// call is pure (e.g. it may still return something different if called twice).
	//
	// Note that the arguments are not considered to be part of the call. If the
	// call itself is removed due to this annotation, the arguments must remain
	// if they have side effects.
	CanBeUnwrappedIfUnused bool
}

func (*ECall) HasSameFlagsAs

func (a *ECall) HasSameFlagsAs(b *ECall) bool

type EClass

type EClass struct{ Class Class }

type EDot

type EDot struct {
	Target        Expr
	Name          string
	NameLoc       logger.Loc
	OptionalChain OptionalChain

	// If true, this property access is known to be free of side-effects. That
	// means it can be removed if the resulting value isn't used.
	CanBeRemovedIfUnused bool

	// If true, this property access is a function that, when called, can be
	// unwrapped if the resulting value is unused. Unwrapping means discarding
	// the call target but keeping any arguments with side effects.
	CallCanBeUnwrappedIfUnused bool

	// Symbol values are known to not have side effects when used as property
	// names in class declarations and object literals.
	IsSymbolInstance bool
}

func (*EDot) HasSameFlagsAs

func (a *EDot) HasSameFlagsAs(b *EDot) bool

type EFunction

type EFunction struct{ Fn Fn }

type EIdentifier

type EIdentifier struct {
	Ref ast.Ref

	// If we're inside a "with" statement, this identifier may be a property
	// access. In that case it would be incorrect to remove this identifier since
	// the property access may be a getter or setter with side effects.
	MustKeepDueToWithStmt bool

	// If true, this identifier is known to not have a side effect (i.e. to not
	// throw an exception) when referenced. If false, this identifier may or may
	// not have side effects when referenced. This is used to allow the removal
	// of known globals such as "Object" if they aren't used.
	CanBeRemovedIfUnused bool

	// If true, this identifier represents a function that, when called, can be
	// unwrapped if the resulting value is unused. Unwrapping means discarding
	// the call target but keeping any arguments with side effects.
	CallCanBeUnwrappedIfUnused bool
}

type EIf

type EIf struct {
	Test Expr
	Yes  Expr
	No   Expr
}

type EImportCall added in v0.11.22

type EImportCall struct {
	Expr          Expr
	OptionsOrNil  Expr
	CloseParenLoc logger.Loc
}

type EImportIdentifier

type EImportIdentifier struct {
	Ref             ast.Ref
	PreferQuotedKey bool

	// If true, this was originally an identifier expression such as "foo". If
	// false, this could potentially have been a member access expression such
	// as "ns.foo" off of an imported namespace object.
	WasOriginallyIdentifier bool
}

This is similar to an EIdentifier but it represents a reference to an ES6 import item.

Depending on how the code is linked, the file containing this EImportIdentifier may or may not be in the same module group as the file it was imported from.

If it's the same module group than we can just merge the import item symbol with the corresponding symbol that was imported, effectively renaming them to be the same thing and statically binding them together.

But if it's a different module group, then the import must be dynamically evaluated using a property access off the corresponding namespace symbol, which represents the result of a require() call.

It's stored as a separate type so it's not easy to confuse with a plain identifier. For example, it'd be bad if code trying to convert "{x: x}" into "{x}" shorthand syntax wasn't aware that the "x" in this case is actually "{x: importedNamespace.x}". This separate type forces code to opt-in to doing this instead of opt-out.

type EImportMeta

type EImportMeta struct {
	RangeLen int32
}

type EImportString added in v0.11.22

type EImportString struct {
	ImportRecordIndex uint32
	CloseParenLoc     logger.Loc
}

type EIndex

type EIndex struct {
	Target          Expr
	Index           Expr
	CloseBracketLoc logger.Loc
	OptionalChain   OptionalChain

	// If true, this property access is known to be free of side-effects. That
	// means it can be removed if the resulting value isn't used.
	CanBeRemovedIfUnused bool

	// If true, this property access is a function that, when called, can be
	// unwrapped if the resulting value is unused. Unwrapping means discarding
	// the call target but keeping any arguments with side effects.
	CallCanBeUnwrappedIfUnused bool

	// Symbol values are known to not have side effects when used as property
	// names in class declarations and object literals.
	IsSymbolInstance bool
}

func (*EIndex) HasSameFlagsAs

func (a *EIndex) HasSameFlagsAs(b *EIndex) bool

type EInlinedEnum added in v0.14.1

type EInlinedEnum struct {
	Value   Expr
	Comment string
}

type EJSXElement

type EJSXElement struct {
	TagOrNil   Expr
	Properties []Property

	// Note: This array may contain nil entries. Be careful about nil entries
	// when iterating over this array.
	//
	// Each nil entry corresponds to the "JSXChildExpression_opt" part of the
	// grammar (https://facebook.github.io/jsx/#prod-JSXChild):
	//
	//   JSXChild :
	//       JSXText
	//       JSXElement
	//       JSXFragment
	//       { JSXChildExpression_opt }
	//
	// This is the "{}" part in "<a>{}</a>". We allow this because some people
	// put comments there and then expect to be able to process them from
	// esbuild's output. These absent AST nodes are completely omitted when
	// JSX is transformed to JS. They are only present when JSX preservation is
	// enabled.
	NullableChildren []Expr

	CloseLoc        logger.Loc
	IsTagSingleLine bool
}

type EJSXText added in v0.19.12

type EJSXText struct {
	Raw string
}

The JSX specification doesn't say how JSX text is supposed to be interpreted so our "preserve" JSX transform should reproduce the original source code verbatim. One reason why this matters is because there is no canonical way to interpret JSX text (Babel and TypeScript differ in what newlines mean). Another reason is that some people want to do custom things such as this: https://github.com/evanw/esbuild/issues/3605

type EMissing

type EMissing struct{}

type ENameOfSymbol added in v0.18.14

type ENameOfSymbol struct {
	Ref                   ast.Ref
	HasPropertyKeyComment bool // If true, a preceding comment contains "@__KEY__"
}

This represents an internal property name that can be mangled. The symbol referenced by this expression should be a "SymbolMangledProp" symbol.

type ENew

type ENew struct {
	Target Expr
	Args   []Expr

	CloseParenLoc logger.Loc
	IsMultiLine   bool

	// True if there is a comment containing "@__PURE__" or "#__PURE__" preceding
	// this call expression. See the comment inside ECall for more details.
	CanBeUnwrappedIfUnused bool
}

type ENewTarget

type ENewTarget struct {
	Range logger.Range
}

type ENull

type ENull struct{}

type ENumber

type ENumber struct{ Value float64 }

type EObject

type EObject struct {
	Properties       []Property
	CommaAfterSpread logger.Loc
	CloseBraceLoc    logger.Loc
	IsSingleLine     bool
	IsParenthesized  bool
}

type EPrivateIdentifier

type EPrivateIdentifier struct {
	Ref ast.Ref
}

This is similar to EIdentifier but it represents class-private fields and methods. It can be used where computed properties can be used, such as EIndex and Property.

type ERegExp

type ERegExp struct{ Value string }

type ERequireResolveString added in v0.11.22

type ERequireResolveString struct {
	ImportRecordIndex uint32
	CloseParenLoc     logger.Loc
}

type ERequireString added in v0.11.22

type ERequireString struct {
	ImportRecordIndex uint32
	CloseParenLoc     logger.Loc
}

type ESpread

type ESpread struct{ Value Expr }

type EString

type EString struct {
	Value                 []uint16
	LegacyOctalLoc        logger.Loc
	PreferTemplate        bool
	HasPropertyKeyComment bool // If true, a preceding comment contains "@__KEY__"
	ContainsUniqueKey     bool // If true, this string must not be wrapped
}

This is used for both strings and no-substitution template literals to reduce the number of cases that need to be checked for string optimization code

type ESuper

type ESuper struct{}

type ETemplate

type ETemplate struct {
	TagOrNil       Expr
	HeadRaw        string   // Only use when "TagOrNil" is not nil
	HeadCooked     []uint16 // Only use when "TagOrNil" is nil
	Parts          []TemplatePart
	HeadLoc        logger.Loc
	LegacyOctalLoc logger.Loc

	// True if this is a tagged template literal with a comment that indicates
	// this function call can be removed if the result is unused. Note that the
	// arguments are not considered to be part of the call. If the call itself
	// is removed due to this annotation, the arguments must remain if they have
	// side effects (including the string conversions).
	CanBeUnwrappedIfUnused bool

	// If the tag is present, it is expected to be a function and is called. If
	// the tag is a syntactic property access, then the value for "this" in the
	// function call is the object whose property was accessed (e.g. in "a.b“"
	// the value for "this" in "a.b" is "a"). We need to ensure that if "a“"
	// ever becomes "b.c“" later on due to optimizations, it is written as
	// "(0, b.c)“" to avoid a behavior change.
	TagWasOriginallyPropertyAccess bool
}

type EThis

type EThis struct{}

type EUnary

type EUnary struct {
	Value Expr
	Op    OpCode

	// The expression "typeof (0, x)" must not become "typeof x" if "x"
	// is unbound because that could suppress a ReferenceError from "x".
	//
	// Also if we know a typeof operator was originally an identifier, then
	// we know that this typeof operator always has no side effects (even if
	// we consider the identifier by itself to have a side effect).
	//
	// Note that there *is* actually a case where "typeof x" can throw an error:
	// when "x" is being referenced inside of its TDZ (temporal dead zone). TDZ
	// checks are not yet handled correctly by esbuild, so this possibility is
	// currently ignored.
	WasOriginallyTypeofIdentifier bool

	// Similarly the expression "delete (0, x)" must not become "delete x"
	// because that syntax is invalid in strict mode. We also need to make sure
	// we don't accidentally change the return value:
	//
	//   Returns false:
	//     "var a; delete (a)"
	//     "var a = Object.freeze({b: 1}); delete (a.b)"
	//     "var a = Object.freeze({b: 1}); delete (a?.b)"
	//     "var a = Object.freeze({b: 1}); delete (a['b'])"
	//     "var a = Object.freeze({b: 1}); delete (a?.['b'])"
	//
	//   Returns true:
	//     "var a; delete (0, a)"
	//     "var a = Object.freeze({b: 1}); delete (true && a.b)"
	//     "var a = Object.freeze({b: 1}); delete (false || a?.b)"
	//     "var a = Object.freeze({b: 1}); delete (null ?? a?.['b'])"
	//     "var a = Object.freeze({b: 1}); delete (true ? a['b'] : a['b'])"
	//
	WasOriginallyDeleteOfIdentifierOrPropertyAccess bool
}

type EUndefined

type EUndefined struct{}

type EYield

type EYield struct {
	ValueOrNil Expr
	IsStar     bool
}

type EnumValue

type EnumValue struct {
	ValueOrNil Expr
	Name       []uint16
	Ref        ast.Ref
	Loc        logger.Loc
}

type EqualityKind added in v0.17.7

type EqualityKind uint8
const (
	LooseEquality EqualityKind = iota
	StrictEquality
)

type ExportStarAlias

type ExportStarAlias struct {
	// Although this alias name starts off as being the same as the statement's
	// namespace symbol, it may diverge if the namespace symbol name is minified.
	// The original alias name is preserved here to avoid this scenario.
	OriginalName string

	Loc logger.Loc
}

type ExportsKind added in v0.10.0

type ExportsKind uint8
const (
	// This file doesn't have any kind of export, so it's impossible to say what
	// kind of file this is. An empty file is in this category, for example.
	ExportsNone ExportsKind = iota

	// The exports are stored on "module" and/or "exports". Calling "require()"
	// on this module returns "module.exports". All imports to this module are
	// allowed but may return undefined.
	ExportsCommonJS

	// All export names are known explicitly. Calling "require()" on this module
	// generates an exports object (stored in "exports") with getters for the
	// export names. Named imports to this module are only allowed if they are
	// in the set of export names.
	ExportsESM

	// Some export names are known explicitly, but others fall back to a dynamic
	// run-time object. This is necessary when using the "export * from" syntax
	// with either a CommonJS module or an external module (i.e. a module whose
	// export names are not known at compile-time).
	//
	// Calling "require()" on this module generates an exports object (stored in
	// "exports") with getters for the export names. All named imports to this
	// module are allowed. Direct named imports reference the corresponding export
	// directly. Other imports go through property accesses on "exports".
	ExportsESMWithDynamicFallback
)

func (ExportsKind) IsDynamic added in v0.10.0

func (kind ExportsKind) IsDynamic() bool

type Expr

type Expr struct {
	Data E
	Loc  logger.Loc
}

func Assign

func Assign(a Expr, b Expr) Expr

func ConstValueToExpr added in v0.14.19

func ConstValueToExpr(loc logger.Loc, value ConstValue) Expr

func ConvertBindingToExpr added in v0.11.0

func ConvertBindingToExpr(binding Binding, wrapIdentifier func(logger.Loc, ast.Ref) Expr) Expr

func FoldBinaryArithmetic added in v0.16.5

func FoldBinaryArithmetic(loc logger.Loc, e *EBinary) Expr

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func FoldStringAddition added in v0.16.5

func FoldStringAddition(left Expr, right Expr, kind StringAdditionKind) Expr

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func InlinePrimitivesIntoTemplate added in v0.19.11

func InlinePrimitivesIntoTemplate(loc logger.Loc, e *ETemplate) Expr

"`a${'b'}c`" => "`abc`"

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func InlineSpreadsOfArrayLiterals added in v0.16.5

func InlineSpreadsOfArrayLiterals(values []Expr) (results []Expr)

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func JoinAllWithComma

func JoinAllWithComma(all []Expr) (result Expr)

func JoinWithComma

func JoinWithComma(a Expr, b Expr) Expr

func JoinWithLeftAssociativeOp added in v0.8.30

func JoinWithLeftAssociativeOp(op OpCode, a Expr, b Expr) Expr

The goal of this function is to "rotate" the AST if it's possible to use the left-associative property of the operator to avoid unnecessary parentheses.

When using this, make absolutely sure that the operator is actually associative. For example, the "+" operator is not associative for floating-point numbers.

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func MaybeSimplifyEqualityComparison added in v0.16.5

func MaybeSimplifyEqualityComparison(loc logger.Loc, e *EBinary, unsupportedFeatures compat.JSFeature) (Expr, bool)

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func MaybeSimplifyNot added in v0.8.29

func MaybeSimplifyNot(expr Expr) (Expr, bool)

The given "expr" argument should be the operand of a "!" prefix operator (i.e. the "x" in "!x"). This returns a simplified expression for the whole operator (i.e. the "!x") if it can be simplified, or false if not. It's separate from "Not()" above to avoid allocation on failure in case that is undesired.

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func Not

func Not(expr Expr) Expr

Wraps the provided expression in the "!" prefix operator. The expression will potentially be simplified to avoid generating unnecessary extra "!" operators. For example, calling this with "!!x" will return "!x" instead of returning "!!!x".

type Finally

type Finally struct {
	Block SBlock
	Loc   logger.Loc
}

type Fn

type Fn struct {
	Name         *ast.LocRef
	Args         []Arg
	Body         FnBody
	ArgumentsRef ast.Ref
	OpenParenLoc logger.Loc

	IsAsync     bool
	IsGenerator bool
	HasRestArg  bool
	HasIfScope  bool

	// See: https://github.com/rollup/rollup/pull/5024
	HasNoSideEffectsComment bool

	// This is true if the function is a method
	IsUniqueFormalParameters bool
}

type FnBody

type FnBody struct {
	Block SBlock
	Loc   logger.Loc
}

type HelperContext added in v0.19.7

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

func MakeHelperContext added in v0.19.7

func MakeHelperContext(isUnbound func(ast.Ref) bool) HelperContext

func (HelperContext) ClassCanBeRemovedIfUnused added in v0.19.7

func (ctx HelperContext) ClassCanBeRemovedIfUnused(class Class) bool

func (HelperContext) ExprCanBeRemovedIfUnused added in v0.19.7

func (ctx HelperContext) ExprCanBeRemovedIfUnused(expr Expr) bool

func (HelperContext) MangleIfExpr added in v0.19.7

func (ctx HelperContext) MangleIfExpr(loc logger.Loc, e *EIf, unsupportedFeatures compat.JSFeature) Expr

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func (HelperContext) SimplifyBooleanExpr added in v0.19.7

func (ctx HelperContext) SimplifyBooleanExpr(expr Expr) Expr

Simplify syntax when we know it's used inside a boolean context

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func (HelperContext) SimplifyUnusedExpr added in v0.19.7

func (ctx HelperContext) SimplifyUnusedExpr(expr Expr, unsupportedFeatures compat.JSFeature) Expr

This will return a nil expression if the expression can be totally removed.

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

func (HelperContext) StmtsCanBeRemovedIfUnused added in v0.19.7

func (ctx HelperContext) StmtsCanBeRemovedIfUnused(stmts []Stmt, flags StmtsCanBeRemovedIfUnusedFlags) bool

type L

type L uint8
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
	LMember
)

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

type LocalKind

type LocalKind uint8
const (
	LocalVar LocalKind = iota
	LocalLet
	LocalConst
	LocalUsing
	LocalAwaitUsing
)

func (LocalKind) IsUsing added in v0.18.7

func (kind LocalKind) IsUsing() bool

type ModuleType added in v0.14.4

type ModuleType uint8
const (
	ModuleUnknown ModuleType = iota

	// ".cjs" or ".cts" or "type: commonjs" in package.json
	ModuleCommonJS_CJS
	ModuleCommonJS_CTS
	ModuleCommonJS_PackageJSON

	// ".mjs" or ".mts" or "type: module" in package.json
	ModuleESM_MJS
	ModuleESM_MTS
	ModuleESM_PackageJSON
)

func (ModuleType) IsCommonJS added in v0.14.9

func (mt ModuleType) IsCommonJS() bool

func (ModuleType) IsESM added in v0.14.9

func (mt ModuleType) IsESM() bool

type ModuleTypeData added in v0.14.9

type ModuleTypeData struct {
	Source *logger.Source
	Range  logger.Range
	Type   ModuleType
}

type NamedExport added in v0.8.8

type NamedExport struct {
	Ref      ast.Ref
	AliasLoc logger.Loc
}

type NamedImport

type NamedImport struct {
	Alias string

	// Parts within this file that use this import
	LocalPartsWithUses []uint32

	AliasLoc          logger.Loc
	NamespaceRef      ast.Ref
	ImportRecordIndex uint32

	// If true, the alias refers to the entire export namespace object of a
	// module. This is no longer represented as an alias called "*" because of
	// the upcoming "Arbitrary module namespace identifier names" feature:
	// https://github.com/tc39/ecma262/pull/2154
	AliasIsStar bool

	// It's useful to flag exported imports because if they are in a TypeScript
	// file, we can't tell if they are a type or a value.
	IsExported bool
}

type OpCode

type OpCode uint8
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
	BinOpNullishCoalescingAssign
	BinOpLogicalOrAssign
	BinOpLogicalAndAssign
)

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

func (OpCode) BinaryAssignTarget

func (op OpCode) BinaryAssignTarget() AssignTarget

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) IsShortCircuit added in v0.8.29

func (op OpCode) IsShortCircuit() bool

func (OpCode) UnaryAssignTarget

func (op OpCode) UnaryAssignTarget() AssignTarget

type OpTableEntry added in v0.17.8

type OpTableEntry struct {
	Text      string
	Level     L
	IsKeyword bool
}

type OptionalChain

type OptionalChain uint8
const (
	// "a.b"
	OptionalChainNone OptionalChain = iota

	// "a?.b"
	OptionalChainStart

	// "a?.b.c" => ".c" is OptionalChainContinue
	// "(a?.b).c" => ".c" is OptionalChainNone
	OptionalChainContinue
)

type Part

type Part struct {
	Stmts  []Stmt
	Scopes []*Scope

	// Each is an index into the file-level import record list
	ImportRecordIndices []uint32

	// All symbols that are declared in this part. Note that a given symbol may
	// have multiple declarations, and so may end up being declared in multiple
	// parts (e.g. multiple "var" declarations with the same name). Also note
	// that this list isn't deduplicated and may contain duplicates.
	DeclaredSymbols []DeclaredSymbol

	// An estimate of the number of uses of all symbols used within this part.
	SymbolUses map[ast.Ref]SymbolUse

	// An estimate of the number of uses of all symbols used as the target of
	// function calls within this part.
	SymbolCallUses map[ast.Ref]SymbolCallUse

	// This tracks property accesses off of imported symbols. We don't know
	// during parsing if an imported symbol is going to be an inlined enum
	// value or not. This is only known during linking. So we defer adding
	// a dependency on these imported symbols until we know whether the
	// property access is an inlined enum value or not.
	ImportSymbolPropertyUses map[ast.Ref]map[string]SymbolUse

	// The indices of the other parts in this file that are needed if this part
	// is needed.
	Dependencies []Dependency

	// If true, this part can be removed if none of the declared symbols are
	// used. If the file containing this part is imported, then all parts that
	// don't have this flag enabled must be included.
	CanBeRemovedIfUnused bool

	// This is used for generated parts that we don't want to be present if they
	// aren't needed. This enables tree shaking for these parts even if global
	// tree shaking isn't enabled.
	ForceTreeShaking bool

	// This is true if this file has been marked as live by the tree shaking
	// algorithm.
	IsLive bool
}

Each file is made up of multiple parts, and each part consists of one or more top-level statements. Parts are used for tree shaking and code splitting analysis. Individual parts of a file can be discarded by tree shaking and can be assigned to separate chunks (i.e. output files) by code splitting.

type PrimitiveType added in v0.14.6

type PrimitiveType uint8
const (
	PrimitiveUnknown PrimitiveType = iota
	PrimitiveMixed
	PrimitiveNull
	PrimitiveUndefined
	PrimitiveBoolean
	PrimitiveNumber
	PrimitiveString
	PrimitiveBigInt
)

func KnownPrimitiveType added in v0.14.6

func KnownPrimitiveType(expr E) PrimitiveType

Note: This function does not say whether the expression is side-effect free or not. For example, the expression "++x" always returns a primitive.

func MergedKnownPrimitiveTypes added in v0.14.6

func MergedKnownPrimitiveTypes(a Expr, b Expr) PrimitiveType

This can be used when the returned type is either one or the other

type Property

type Property struct {
	ClassStaticBlock *ClassStaticBlock

	Key Expr

	// This is omitted for class fields
	ValueOrNil 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 }
	//
	InitializerOrNil Expr

	Decorators []Decorator

	Loc             logger.Loc
	CloseBracketLoc logger.Loc
	Kind            PropertyKind
	Flags           PropertyFlags
}

func MangleObjectSpread added in v0.16.5

func MangleObjectSpread(properties []Property) []Property

This function intentionally avoids mutating the input AST so it can be called after the AST has been frozen (i.e. after parsing ends).

type PropertyBinding

type PropertyBinding struct {
	Key               Expr
	Value             Binding
	DefaultValueOrNil Expr
	Loc               logger.Loc
	CloseBracketLoc   logger.Loc
	IsComputed        bool
	IsSpread          bool
	PreferQuotedKey   bool
}

type PropertyFlags added in v0.14.40

type PropertyFlags uint8
const (
	PropertyIsComputed PropertyFlags = 1 << iota
	PropertyIsMethod
	PropertyIsStatic
	PropertyWasShorthand
	PropertyPreferQuotedKey
)

func (PropertyFlags) Has added in v0.14.40

func (flags PropertyFlags) Has(flag PropertyFlags) bool

type PropertyKind

type PropertyKind uint8
const (
	PropertyNormal PropertyKind = iota
	PropertyGet
	PropertySet
	PropertyAutoAccessor
	PropertySpread
	PropertyDeclareOrAbstract
	PropertyClassStaticBlock
)

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
	CloseBraceLoc logger.Loc
}

type SBreak

type SBreak struct {
	Label *ast.LocRef
}

type SClass

type SClass struct {
	Class    Class
	IsExport bool
}

type SComment

type SComment struct {
	Text           string
	IsLegalComment bool
}

type SContinue

type SContinue struct {
	Label *ast.LocRef
}

type SDebugger

type SDebugger struct{}

type SDirective

type SDirective struct {
	Value          []uint16
	LegacyOctalLoc logger.Loc
}

type SDoWhile

type SDoWhile struct {
	Body Stmt
	Test Expr
}

type SEmpty

type SEmpty struct{}

type SEnum

type SEnum struct {
	Values   []EnumValue
	Name     ast.LocRef
	Arg      ast.Ref
	IsExport bool
}

type SExportClause

type SExportClause struct {
	Items        []ClauseItem
	IsSingleLine bool
}

type SExportDefault

type SExportDefault struct {
	Value       Stmt // May be a SExpr or SFunction or SClass
	DefaultName ast.LocRef
}

type SExportEquals

type SExportEquals struct {
	Value Expr
}

This is an "export = value;" statement in TypeScript

type SExportFrom

type SExportFrom struct {
	Items             []ClauseItem
	NamespaceRef      ast.Ref
	ImportRecordIndex uint32
	IsSingleLine      bool
}

type SExportStar

type SExportStar struct {
	Alias             *ExportStarAlias
	NamespaceRef      ast.Ref
	ImportRecordIndex uint32
}

type SExpr

type SExpr struct {
	Value Expr

	// This is set to true for automatically-generated expressions that are part
	// of class syntax lowering. A single class declaration may end up with many
	// generated expressions after it (e.g. class field initializations, a call
	// to keep the original value of the "name" property). When this happens we
	// can't tell that the class is side-effect free anymore because all of these
	// methods mutate the class. We use this annotation for that instead.
	IsFromClassOrFnThatCanBeRemovedIfUnused bool
}

type SFor

type SFor struct {
	InitOrNil   Stmt // May be a SConst, SLet, SVar, or SExpr
	TestOrNil   Expr
	UpdateOrNil 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 {
	Init  Stmt // May be a SConst, SLet, SVar, or SExpr
	Value Expr
	Body  Stmt
	Await logger.Range
}

type SFunction

type SFunction struct {
	Fn       Fn
	IsExport bool
}

type SIf

type SIf struct {
	Test    Expr
	Yes     Stmt
	NoOrNil Stmt
}

type SImport

type SImport struct {
	DefaultName *ast.LocRef
	Items       *[]ClauseItem
	StarNameLoc *logger.Loc

	// 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 ast.Ref

	ImportRecordIndex uint32
	IsSingleLine      bool
}

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 {
	Stmt Stmt
	Name ast.LocRef
}

type SLazyExport

type SLazyExport struct {
	Value Expr
}

The decision of whether to export an expression using "module.exports" or "export default" is deferred until linking using this statement kind

type SLocal

type SLocal struct {
	Decls    []Decl
	Kind     LocalKind
	IsExport bool

	// The TypeScript compiler doesn't generate code for "import foo = bar"
	// statements where the import is never used.
	WasTSImportEquals bool
}

type SNamespace

type SNamespace struct {
	Stmts    []Stmt
	Name     ast.LocRef
	Arg      ast.Ref
	IsExport bool
}

type SReturn

type SReturn struct {
	ValueOrNil Expr
}

type SSwitch

type SSwitch struct {
	Test          Expr
	Cases         []Case
	BodyLoc       logger.Loc
	CloseBraceLoc logger.Loc
}

type SThrow

type SThrow struct {
	Value Expr
}

type STry

type STry struct {
	Catch    *Catch
	Finally  *Finally
	Block    SBlock
	BlockLoc logger.Loc
}

type STypeScript

type STypeScript struct {
	WasDeclareClass bool
}

This is a stand-in for a TypeScript type declaration

type SWhile

type SWhile struct {
	Test Expr
	Body Stmt
}

type SWith

type SWith struct {
	Value   Expr
	Body    Stmt
	BodyLoc logger.Loc
}

type Scope

type Scope struct {
	// This will be non-nil if this is a TypeScript "namespace" or "enum"
	TSNamespace *TSNamespaceScope

	Parent    *Scope
	Children  []*Scope
	Members   map[string]ScopeMember
	Replaced  []ScopeMember
	Generated []ast.Ref

	// The location of the "use strict" directive for ExplicitStrictMode
	UseStrictLoc logger.Loc

	// This is used to store the ref of the label symbol for ScopeLabel scopes.
	Label           ast.LocRef
	LabelStmtIsLoop bool

	// If a scope contains a direct eval() expression, then none of the symbols
	// inside that scope can be renamed. We conservatively assume that the
	// evaluated code might reference anything that it has access to.
	ContainsDirectEval bool

	// This is to help forbid "arguments" inside class body scopes
	ForbidArguments bool

	// As a special case, we enable constant propagation for any chain of "const"
	// declarations at the start of a statement list. This special case doesn't
	// have any TDZ considerations because no other statements come before it.
	IsAfterConstLocalPrefix bool

	StrictMode StrictModeKind
	Kind       ScopeKind
}

func (*Scope) RecursiveSetStrictMode added in v0.8.41

func (s *Scope) RecursiveSetStrictMode(kind StrictModeKind)

type ScopeKind

type ScopeKind uint8
const (
	ScopeBlock ScopeKind = iota
	ScopeWith
	ScopeLabel
	ScopeClassName
	ScopeClassBody
	ScopeCatchBinding

	// The scopes below stop hoisted variables from extending into parent scopes
	ScopeEntry // This is a module, TypeScript enum, or TypeScript namespace
	ScopeFunctionArgs
	ScopeFunctionBody
	ScopeClassStaticInit
)

func (ScopeKind) StopsHoisting

func (kind ScopeKind) StopsHoisting() bool

type ScopeMember

type ScopeMember struct {
	Ref ast.Ref
	Loc logger.Loc
}

type SideEffects added in v0.14.25

type SideEffects uint8
const (
	CouldHaveSideEffects SideEffects = iota
	NoSideEffects
)

func ToBooleanWithSideEffects added in v0.14.25

func ToBooleanWithSideEffects(data E) (boolean bool, sideEffects SideEffects, ok bool)

func ToNullOrUndefinedWithSideEffects added in v0.14.25

func ToNullOrUndefinedWithSideEffects(data E) (isNullOrUndefined bool, sideEffects SideEffects, ok bool)

type Stmt

type Stmt struct {
	Data S
	Loc  logger.Loc
}

func AssignStmt

func AssignStmt(a Expr, b Expr) Stmt

type StmtsCanBeRemovedIfUnusedFlags added in v0.16.5

type StmtsCanBeRemovedIfUnusedFlags uint8
const (
	KeepExportClauses StmtsCanBeRemovedIfUnusedFlags = 1 << iota
	ReturnCanBeRemovedIfUnused
)

type StrictModeKind added in v0.8.41

type StrictModeKind uint8
const (
	SloppyMode StrictModeKind = iota
	ExplicitStrictMode
	ImplicitStrictModeClass
	ImplicitStrictModeESM
	ImplicitStrictModeTSAlwaysStrict
	ImplicitStrictModeJSXAutomaticRuntime
)

type StringAdditionKind added in v0.17.5

type StringAdditionKind uint8
const (
	StringAdditionNormal StringAdditionKind = iota
	StringAdditionWithNestedLeft
)

type SymbolCallUse added in v0.14.10

type SymbolCallUse struct {
	CallCountEstimate                   uint32
	SingleArgNonSpreadCallCountEstimate uint32
}

type SymbolUse

type SymbolUse struct {
	CountEstimate uint32
}

type TSEnumValue added in v0.14.7

type TSEnumValue struct {
	String []uint16 // Use this if it's not nil
	Number float64  // Use this if "String" is nil
}

type TSNamespaceMember added in v0.14.0

type TSNamespaceMember struct {
	Data        TSNamespaceMemberData
	Loc         logger.Loc
	IsEnumValue bool
}

type TSNamespaceMemberData added in v0.14.0

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

type TSNamespaceMemberEnumNumber added in v0.14.0

type TSNamespaceMemberEnumNumber struct {
	Value float64
}

"enum ns { it }"

type TSNamespaceMemberEnumString added in v0.14.0

type TSNamespaceMemberEnumString struct {
	Value []uint16
}

"enum ns { it = 'it' }"

type TSNamespaceMemberNamespace added in v0.14.0

type TSNamespaceMemberNamespace struct {
	ExportedMembers TSNamespaceMembers
}

"namespace ns { export namespace it {} }"

type TSNamespaceMemberProperty added in v0.14.0

type TSNamespaceMemberProperty struct{}

"namespace ns { export let it }"

type TSNamespaceMembers added in v0.14.0

type TSNamespaceMembers map[string]TSNamespaceMember

type TSNamespaceScope added in v0.14.0

type TSNamespaceScope struct {
	// This is shared between all sibling namespace blocks
	ExportedMembers TSNamespaceMembers

	// This is a lazily-generated map of identifiers that actually represent
	// property accesses to this namespace's properties. For example:
	//
	//   namespace x {
	//     export let y = 123
	//   }
	//   namespace x {
	//     export let z = y
	//   }
	//
	// This should be compiled into the following code:
	//
	//   var x;
	//   (function(x2) {
	//     x2.y = 123;
	//   })(x || (x = {}));
	//   (function(x3) {
	//     x3.z = x3.y;
	//   })(x || (x = {}));
	//
	// When we try to find the symbol "y", we instead return one of these lazily
	// generated proxy symbols that represent the property access "x3.y". This
	// map is unique per namespace block because "x3" is the argument symbol that
	// is specific to that particular namespace block.
	LazilyGeneratedProperyAccesses map[string]ast.Ref

	// This is specific to this namespace block. It's the argument of the
	// immediately-invoked function expression that the namespace block is
	// compiled into:
	//
	//   var ns;
	//   (function (ns2) {
	//     ns2.x = 123;
	//   })(ns || (ns = {}));
	//
	// This variable is "ns2" in the above example. It's the symbol to use when
	// generating property accesses off of this namespace when it's in scope.
	ArgRef ast.Ref

	// Even though enums are like namespaces and both enums and namespaces allow
	// implicit references to properties of sibling scopes, they behave like
	// separate, er, namespaces. Implicit references only work namespace-to-
	// namespace and enum-to-enum. They do not work enum-to-namespace. And I'm
	// not sure what's supposed to happen for the namespace-to-enum case because
	// the compiler crashes: https://github.com/microsoft/TypeScript/issues/46891.
	// So basically these both work:
	//
	//   enum a { b = 1 }
	//   enum a { c = b }
	//
	//   namespace x { export let y = 1 }
	//   namespace x { export let z = y }
	//
	// This doesn't work:
	//
	//   enum a { b = 1 }
	//   namespace a { export let c = b }
	//
	// And this crashes the TypeScript compiler:
	//
	//   namespace a { export let b = 1 }
	//   enum a { c = b }
	//
	// Therefore we only allow enum/enum and namespace/namespace interactions.
	IsEnumScope bool
}

This is for TypeScript "enum" and "namespace" blocks. Each block can potentially be instantiated multiple times. The exported members of each block are merged into a single namespace while the non-exported code is still scoped to just within that block:

let x = 1;
namespace Foo {
  let x = 2;
  export let y = 3;
}
namespace Foo {
  console.log(x); // 1
  console.log(y); // 3
}

Doing this also works inside an enum:

enum Foo {
  A = 3,
  B = A + 1,
}
enum Foo {
  C = A + 2,
}
console.log(Foo.B) // 4
console.log(Foo.C) // 5

This is a form of identifier lookup that works differently than the hierarchical scope-based identifier lookup in JavaScript. Lookup now needs to search sibling scopes in addition to parent scopes. This is accomplished by sharing the map of exported members between all matching sibling scopes.

type TemplatePart

type TemplatePart struct {
	Value      Expr
	TailRaw    string   // Only use when "TagOrNil" is not nil
	TailCooked []uint16 // Only use when "TagOrNil" is nil
	TailLoc    logger.Loc
}

Jump to

Keyboard shortcuts

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