ast.go 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317
  1. package js
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "strconv"
  7. "strings"
  8. "github.com/tdewolff/parse/v2"
  9. )
  10. var ErrInvalidJSON = fmt.Errorf("invalid JSON")
  11. type JSONer interface {
  12. JSON(io.Writer) error
  13. }
  14. // AST is the full ECMAScript abstract syntax tree.
  15. type AST struct {
  16. BlockStmt // module
  17. }
  18. func (ast AST) String() string {
  19. s := ""
  20. for i, item := range ast.BlockStmt.List {
  21. if i != 0 {
  22. s += " "
  23. }
  24. s += item.String()
  25. }
  26. return s
  27. }
  28. // JS writes JavaScript to writer.
  29. func (ast AST) JS(w io.Writer) {
  30. for i, item := range ast.List {
  31. if i != 0 {
  32. w.Write([]byte("\n"))
  33. }
  34. item.JS(w)
  35. if _, ok := item.(*VarDecl); ok {
  36. w.Write([]byte(";"))
  37. }
  38. }
  39. }
  40. // JSONString returns a string of JavaScript.
  41. func (ast AST) JSString() string {
  42. sb := strings.Builder{}
  43. ast.JS(&sb)
  44. return sb.String()
  45. }
  46. // JSON writes JSON to writer.
  47. func (ast AST) JSON(w io.Writer) error {
  48. if 1 < len(ast.List) {
  49. return ErrInvalidJSON
  50. } else if len(ast.List) == 0 {
  51. return nil
  52. } else if expr, ok := ast.List[0].(*ExprStmt); !ok {
  53. return ErrInvalidJSON
  54. } else if val, ok := expr.Value.(JSONer); !ok {
  55. return ErrInvalidJSON
  56. } else {
  57. return val.JSON(w)
  58. }
  59. return nil
  60. }
  61. // JSONString returns a string of JSON if valid.
  62. func (ast AST) JSONString() (string, error) {
  63. sb := strings.Builder{}
  64. err := ast.JSON(&sb)
  65. return sb.String(), err
  66. }
  67. ////////////////////////////////////////////////////////////////
  68. // DeclType specifies the kind of declaration.
  69. type DeclType uint16
  70. // DeclType values.
  71. const (
  72. NoDecl DeclType = iota // undeclared variables
  73. VariableDecl // var
  74. FunctionDecl // function
  75. ArgumentDecl // function and method arguments
  76. LexicalDecl // let, const, class
  77. CatchDecl // catch statement argument
  78. ExprDecl // function expression name or class expression name
  79. )
  80. func (decl DeclType) String() string {
  81. switch decl {
  82. case NoDecl:
  83. return "NoDecl"
  84. case VariableDecl:
  85. return "VariableDecl"
  86. case FunctionDecl:
  87. return "FunctionDecl"
  88. case ArgumentDecl:
  89. return "ArgumentDecl"
  90. case LexicalDecl:
  91. return "LexicalDecl"
  92. case CatchDecl:
  93. return "CatchDecl"
  94. case ExprDecl:
  95. return "ExprDecl"
  96. }
  97. return "Invalid(" + strconv.Itoa(int(decl)) + ")"
  98. }
  99. // Var is a variable, where Decl is the type of declaration and can be var|function for function scoped variables, let|const|class for block scoped variables.
  100. type Var struct {
  101. Data []byte
  102. Link *Var // is set when merging variable uses, as in: {a} {var a} where the first links to the second, only used for undeclared variables
  103. Uses uint16
  104. Decl DeclType
  105. }
  106. // Name returns the variable name.
  107. func (v *Var) Name() []byte {
  108. for v.Link != nil {
  109. v = v.Link
  110. }
  111. return v.Data
  112. }
  113. func (v *Var) Info() string {
  114. s := fmt.Sprintf("%p type=%s name='%s' uses=%d", v, v.Decl, string(v.Data), v.Uses)
  115. links := 0
  116. for v.Link != nil {
  117. v = v.Link
  118. links++
  119. }
  120. if 0 < links {
  121. s += fmt.Sprintf(" links=%d => %p", links, v)
  122. }
  123. return s
  124. }
  125. func (v Var) String() string {
  126. return string(v.Name())
  127. }
  128. // JS writes JavaScript to writer.
  129. func (v Var) JS(w io.Writer) {
  130. w.Write(v.Name())
  131. }
  132. // VarsByUses is sortable by uses in descending order.
  133. type VarsByUses VarArray
  134. func (vs VarsByUses) Len() int {
  135. return len(vs)
  136. }
  137. func (vs VarsByUses) Swap(i, j int) {
  138. vs[i], vs[j] = vs[j], vs[i]
  139. }
  140. func (vs VarsByUses) Less(i, j int) bool {
  141. return vs[i].Uses > vs[j].Uses
  142. }
  143. ////////////////////////////////////////////////////////////////
  144. // VarArray is a set of variables in scopes.
  145. type VarArray []*Var
  146. func (vs VarArray) String() string {
  147. s := "["
  148. for i, v := range vs {
  149. if i != 0 {
  150. s += ", "
  151. }
  152. links := 0
  153. for v.Link != nil {
  154. v = v.Link
  155. links++
  156. }
  157. s += fmt.Sprintf("Var{%v %s %v %v}", v.Decl, string(v.Data), links, v.Uses)
  158. }
  159. return s + "]"
  160. }
  161. // Scope is a function or block scope with a list of variables declared and used.
  162. type Scope struct {
  163. Parent, Func *Scope // Parent is nil for global scope
  164. Declared VarArray // Link in Var are always nil
  165. Undeclared VarArray
  166. VarDecls []*VarDecl
  167. NumForDecls uint16 // offset into Declared to mark variables used in for statements
  168. NumFuncArgs uint16 // offset into Declared to mark variables used in function arguments
  169. NumArgUses uint16 // offset into Undeclared to mark variables used in arguments
  170. IsGlobalOrFunc bool
  171. HasWith bool
  172. }
  173. func (s Scope) String() string {
  174. return "Scope{Declared: " + s.Declared.String() + ", Undeclared: " + s.Undeclared.String() + "}"
  175. }
  176. // Declare declares a new variable.
  177. func (s *Scope) Declare(decl DeclType, name []byte) (*Var, bool) {
  178. // refer to new variable for previously undeclared symbols in the current and lower scopes
  179. // this happens in `{ a = 5; } var a` where both a's refer to the same variable
  180. curScope := s
  181. if decl == VariableDecl || decl == FunctionDecl {
  182. // find function scope for var and function declarations
  183. for s != s.Func {
  184. // make sure that `{let i;{var i}}` is an error
  185. if v := s.findDeclared(name, false); v != nil && v.Decl != decl && v.Decl != CatchDecl {
  186. return nil, false
  187. }
  188. s = s.Parent
  189. }
  190. }
  191. if v := s.findDeclared(name, true); v != nil {
  192. // variable already declared, might be an error or a duplicate declaration
  193. if (ArgumentDecl < v.Decl || FunctionDecl < decl) && v.Decl != ExprDecl {
  194. // only allow (v.Decl,decl) of: (var|function|argument,var|function), (expr,*), any other combination is a syntax error
  195. return nil, false
  196. }
  197. if v.Decl == ExprDecl {
  198. v.Decl = decl
  199. }
  200. v.Uses++
  201. for s != curScope {
  202. curScope.AddUndeclared(v) // add variable declaration as used variable to the current scope
  203. curScope = curScope.Parent
  204. }
  205. return v, true
  206. }
  207. var v *Var
  208. // reuse variable if previously used, as in: a;var a
  209. if decl != ArgumentDecl { // in case of function f(a=b,b), where the first b is different from the second
  210. for i, uv := range s.Undeclared[s.NumArgUses:] {
  211. // no need to evaluate v.Link as v.Data stays the same and Link is nil in the active scope
  212. if 0 < uv.Uses && uv.Decl == NoDecl && bytes.Equal(name, uv.Data) {
  213. // must be NoDecl so that it can't be a var declaration that has been added
  214. v = uv
  215. s.Undeclared = append(s.Undeclared[:int(s.NumArgUses)+i], s.Undeclared[int(s.NumArgUses)+i+1:]...)
  216. break
  217. }
  218. }
  219. }
  220. if v == nil {
  221. // add variable to the context list and to the scope
  222. v = &Var{name, nil, 0, decl}
  223. } else {
  224. v.Decl = decl
  225. }
  226. v.Uses++
  227. s.Declared = append(s.Declared, v)
  228. for s != curScope {
  229. curScope.AddUndeclared(v) // add variable declaration as used variable to the current scope
  230. curScope = curScope.Parent
  231. }
  232. return v, true
  233. }
  234. // Use increments the usage of a variable.
  235. func (s *Scope) Use(name []byte) *Var {
  236. // check if variable is declared in the current scope
  237. v := s.findDeclared(name, false)
  238. if v == nil {
  239. // check if variable is already used before in the current or lower scopes
  240. v = s.findUndeclared(name)
  241. if v == nil {
  242. // add variable to the context list and to the scope's undeclared
  243. v = &Var{name, nil, 0, NoDecl}
  244. s.Undeclared = append(s.Undeclared, v)
  245. }
  246. }
  247. v.Uses++
  248. return v
  249. }
  250. // findDeclared finds a declared variable in the current scope.
  251. func (s *Scope) findDeclared(name []byte, skipForDeclared bool) *Var {
  252. start := 0
  253. if skipForDeclared {
  254. // we skip the for initializer for declarations (only has effect for let/const)
  255. start = int(s.NumForDecls)
  256. }
  257. // reverse order to find the inner let first in `for(let a in []){let a; {a}}`
  258. for i := len(s.Declared) - 1; start <= i; i-- {
  259. v := s.Declared[i]
  260. // no need to evaluate v.Link as v.Data stays the same, and Link is always nil in Declared
  261. if bytes.Equal(name, v.Data) {
  262. return v
  263. }
  264. }
  265. return nil
  266. }
  267. // findUndeclared finds an undeclared variable in the current and contained scopes.
  268. func (s *Scope) findUndeclared(name []byte) *Var {
  269. for _, v := range s.Undeclared {
  270. // no need to evaluate v.Link as v.Data stays the same and Link is nil in the active scope
  271. if 0 < v.Uses && bytes.Equal(name, v.Data) {
  272. return v
  273. }
  274. }
  275. return nil
  276. }
  277. // add undeclared variable to scope, this is called for the block scope when declaring a var in it
  278. func (s *Scope) AddUndeclared(v *Var) {
  279. // don't add undeclared symbol if it's already there
  280. for _, vorig := range s.Undeclared {
  281. if v == vorig {
  282. return
  283. }
  284. }
  285. s.Undeclared = append(s.Undeclared, v) // add variable declaration as used variable to the current scope
  286. }
  287. // MarkForStmt marks the declared variables in current scope as for statement initializer to distinguish from declarations in body.
  288. func (s *Scope) MarkForStmt() {
  289. s.NumForDecls = uint16(len(s.Declared))
  290. s.NumArgUses = uint16(len(s.Undeclared)) // ensures for different b's in for(var a in b){let b}
  291. }
  292. // MarkFuncArgs marks the declared/undeclared variables in the current scope as function arguments.
  293. func (s *Scope) MarkFuncArgs() {
  294. s.NumFuncArgs = uint16(len(s.Declared))
  295. s.NumArgUses = uint16(len(s.Undeclared)) // ensures different b's in `function f(a=b){var b}`.
  296. }
  297. // HoistUndeclared copies all undeclared variables of the current scope to the parent scope.
  298. func (s *Scope) HoistUndeclared() {
  299. for i, vorig := range s.Undeclared {
  300. // no need to evaluate vorig.Link as vorig.Data stays the same
  301. if 0 < vorig.Uses && vorig.Decl == NoDecl {
  302. if v := s.Parent.findDeclared(vorig.Data, false); v != nil {
  303. // check if variable is declared in parent scope
  304. v.Uses += vorig.Uses
  305. vorig.Link = v
  306. s.Undeclared[i] = v // point reference to existing var (to avoid many Link chains)
  307. } else if v := s.Parent.findUndeclared(vorig.Data); v != nil {
  308. // check if variable is already used before in parent scope
  309. v.Uses += vorig.Uses
  310. vorig.Link = v
  311. s.Undeclared[i] = v // point reference to existing var (to avoid many Link chains)
  312. } else {
  313. // add variable to the context list and to the scope's undeclared
  314. s.Parent.Undeclared = append(s.Parent.Undeclared, vorig)
  315. }
  316. }
  317. }
  318. }
  319. // UndeclareScope undeclares all declared variables in the current scope and adds them to the parent scope.
  320. // Called when possible arrow func ends up being a parenthesized expression, scope is not further used.
  321. func (s *Scope) UndeclareScope() {
  322. // look if the variable already exists in the parent scope, if so replace the Var pointer in original use
  323. for _, vorig := range s.Declared {
  324. // no need to evaluate vorig.Link as vorig.Data stays the same, and Link is always nil in Declared
  325. // vorig.Uses will be atleast 1
  326. if v := s.Parent.findDeclared(vorig.Data, false); v != nil {
  327. // check if variable has been declared in this scope
  328. v.Uses += vorig.Uses
  329. vorig.Link = v
  330. } else if v := s.Parent.findUndeclared(vorig.Data); v != nil {
  331. // check if variable is already used before in the current or lower scopes
  332. v.Uses += vorig.Uses
  333. vorig.Link = v
  334. } else {
  335. // add variable to the context list and to the scope's undeclared
  336. vorig.Decl = NoDecl
  337. s.Parent.Undeclared = append(s.Parent.Undeclared, vorig)
  338. }
  339. }
  340. s.Declared = s.Declared[:0]
  341. s.Undeclared = s.Undeclared[:0]
  342. }
  343. // Unscope moves all declared variables of the current scope to the parent scope. Undeclared variables are already in the parent scope.
  344. func (s *Scope) Unscope() {
  345. for _, vorig := range s.Declared {
  346. // no need to evaluate vorig.Link as vorig.Data stays the same, and Link is always nil in Declared
  347. // vorig.Uses will be atleast 1
  348. s.Parent.Declared = append(s.Parent.Declared, vorig)
  349. }
  350. s.Declared = s.Declared[:0]
  351. s.Undeclared = s.Undeclared[:0]
  352. }
  353. ////////////////////////////////////////////////////////////////
  354. // INode is an interface for AST nodes
  355. type INode interface {
  356. String() string
  357. JS(io.Writer)
  358. }
  359. // IStmt is a dummy interface for statements.
  360. type IStmt interface {
  361. INode
  362. stmtNode()
  363. }
  364. // IBinding is a dummy interface for bindings.
  365. type IBinding interface {
  366. INode
  367. bindingNode()
  368. }
  369. // IExpr is a dummy interface for expressions.
  370. type IExpr interface {
  371. INode
  372. exprNode()
  373. }
  374. ////////////////////////////////////////////////////////////////
  375. // Comment block or line, usually a bang comment.
  376. type Comment struct {
  377. Value []byte
  378. }
  379. func (n Comment) String() string {
  380. return "Stmt(" + string(n.Value) + ")"
  381. }
  382. // JS writes JavaScript to writer.
  383. func (n Comment) JS(w io.Writer) {
  384. if wi, ok := w.(Indenter); ok {
  385. w = wi.w
  386. }
  387. w.Write(n.Value)
  388. }
  389. // BlockStmt is a block statement.
  390. type BlockStmt struct {
  391. List []IStmt
  392. Scope
  393. }
  394. func (n BlockStmt) String() string {
  395. s := "Stmt({"
  396. for _, item := range n.List {
  397. s += " " + item.String()
  398. }
  399. return s + " })"
  400. }
  401. // JS writes JavaScript to writer.
  402. func (n BlockStmt) JS(w io.Writer) {
  403. if len(n.List) == 0 {
  404. w.Write([]byte("{}"))
  405. return
  406. }
  407. w.Write([]byte("{"))
  408. wi := NewIndenter(w, 4)
  409. for _, item := range n.List {
  410. wi.Write([]byte("\n"))
  411. item.JS(wi)
  412. if _, ok := item.(*VarDecl); ok {
  413. w.Write([]byte(";"))
  414. }
  415. }
  416. w.Write([]byte("\n}"))
  417. }
  418. // EmptyStmt is an empty statement.
  419. type EmptyStmt struct{}
  420. func (n EmptyStmt) String() string {
  421. return "Stmt()"
  422. }
  423. // JS writes JavaScript to writer.
  424. func (n EmptyStmt) JS(w io.Writer) {
  425. w.Write([]byte(";"))
  426. }
  427. // ExprStmt is an expression statement.
  428. type ExprStmt struct {
  429. Value IExpr
  430. }
  431. func (n ExprStmt) String() string {
  432. val := n.Value.String()
  433. if val[0] == '(' && val[len(val)-1] == ')' {
  434. return "Stmt" + n.Value.String()
  435. }
  436. return "Stmt(" + n.Value.String() + ")"
  437. }
  438. // JS writes JavaScript to writer.
  439. func (n ExprStmt) JS(w io.Writer) {
  440. n.Value.JS(w)
  441. w.Write([]byte(";"))
  442. }
  443. // IfStmt is an if statement.
  444. type IfStmt struct {
  445. Cond IExpr
  446. Body IStmt
  447. Else IStmt // can be nil
  448. }
  449. func (n IfStmt) String() string {
  450. s := "Stmt(if " + n.Cond.String() + " " + n.Body.String()
  451. if n.Else != nil {
  452. s += " else " + n.Else.String()
  453. }
  454. return s + ")"
  455. }
  456. // JS writes JavaScript to writer.
  457. func (n IfStmt) JS(w io.Writer) {
  458. w.Write([]byte("if ("))
  459. n.Cond.JS(w)
  460. w.Write([]byte(")"))
  461. if _, ok := n.Body.(*EmptyStmt); !ok {
  462. w.Write([]byte(" "))
  463. }
  464. n.Body.JS(w)
  465. if _, ok := n.Body.(*VarDecl); ok {
  466. w.Write([]byte(";"))
  467. }
  468. if n.Else != nil {
  469. w.Write([]byte(" else"))
  470. if _, ok := n.Else.(*EmptyStmt); !ok {
  471. w.Write([]byte(" "))
  472. }
  473. n.Else.JS(w)
  474. if _, ok := n.Else.(*VarDecl); ok {
  475. w.Write([]byte(";"))
  476. }
  477. }
  478. }
  479. // DoWhileStmt is a do-while iteration statement.
  480. type DoWhileStmt struct {
  481. Cond IExpr
  482. Body IStmt
  483. }
  484. func (n DoWhileStmt) String() string {
  485. return "Stmt(do " + n.Body.String() + " while " + n.Cond.String() + ")"
  486. }
  487. // JS writes JavaScript to writer.
  488. func (n DoWhileStmt) JS(w io.Writer) {
  489. w.Write([]byte("do"))
  490. if _, ok := n.Body.(*EmptyStmt); !ok {
  491. w.Write([]byte(" "))
  492. }
  493. n.Body.JS(w)
  494. if _, ok := n.Body.(*VarDecl); ok {
  495. w.Write([]byte(";"))
  496. }
  497. w.Write([]byte(" while ("))
  498. n.Cond.JS(w)
  499. w.Write([]byte(");"))
  500. }
  501. // WhileStmt is a while iteration statement.
  502. type WhileStmt struct {
  503. Cond IExpr
  504. Body IStmt
  505. }
  506. func (n WhileStmt) String() string {
  507. return "Stmt(while " + n.Cond.String() + " " + n.Body.String() + ")"
  508. }
  509. // JS writes JavaScript to writer.
  510. func (n WhileStmt) JS(w io.Writer) {
  511. w.Write([]byte("while ("))
  512. n.Cond.JS(w)
  513. w.Write([]byte(")"))
  514. if _, ok := n.Body.(*EmptyStmt); !ok {
  515. w.Write([]byte(" "))
  516. }
  517. n.Body.JS(w)
  518. if _, ok := n.Body.(*VarDecl); ok {
  519. w.Write([]byte(";"))
  520. }
  521. }
  522. // ForStmt is a regular for iteration statement.
  523. type ForStmt struct {
  524. Init IExpr // can be nil
  525. Cond IExpr // can be nil
  526. Post IExpr // can be nil
  527. Body *BlockStmt
  528. }
  529. func (n ForStmt) String() string {
  530. s := "Stmt(for"
  531. if v, ok := n.Init.(*VarDecl); !ok && n.Init != nil || ok && len(v.List) != 0 {
  532. s += " " + n.Init.String()
  533. }
  534. s += " ;"
  535. if n.Cond != nil {
  536. s += " " + n.Cond.String()
  537. }
  538. s += " ;"
  539. if n.Post != nil {
  540. s += " " + n.Post.String()
  541. }
  542. return s + " " + n.Body.String() + ")"
  543. }
  544. // JS writes JavaScript to writer.
  545. func (n ForStmt) JS(w io.Writer) {
  546. w.Write([]byte("for ("))
  547. if v, ok := n.Init.(*VarDecl); !ok && n.Init != nil || ok && len(v.List) != 0 {
  548. n.Init.JS(w)
  549. } else {
  550. w.Write([]byte(" "))
  551. }
  552. w.Write([]byte("; "))
  553. if n.Cond != nil {
  554. n.Cond.JS(w)
  555. }
  556. w.Write([]byte("; "))
  557. if n.Post != nil {
  558. n.Post.JS(w)
  559. }
  560. w.Write([]byte(") "))
  561. n.Body.JS(w)
  562. }
  563. // ForInStmt is a for-in iteration statement.
  564. type ForInStmt struct {
  565. Init IExpr
  566. Value IExpr
  567. Body *BlockStmt
  568. }
  569. func (n ForInStmt) String() string {
  570. return "Stmt(for " + n.Init.String() + " in " + n.Value.String() + " " + n.Body.String() + ")"
  571. }
  572. // JS writes JavaScript to writer.
  573. func (n ForInStmt) JS(w io.Writer) {
  574. w.Write([]byte("for ("))
  575. n.Init.JS(w)
  576. w.Write([]byte(" in "))
  577. n.Value.JS(w)
  578. w.Write([]byte(") "))
  579. n.Body.JS(w)
  580. }
  581. // ForOfStmt is a for-of iteration statement.
  582. type ForOfStmt struct {
  583. Await bool
  584. Init IExpr
  585. Value IExpr
  586. Body *BlockStmt
  587. }
  588. func (n ForOfStmt) String() string {
  589. s := "Stmt(for"
  590. if n.Await {
  591. s += " await"
  592. }
  593. return s + " " + n.Init.String() + " of " + n.Value.String() + " " + n.Body.String() + ")"
  594. }
  595. // JS writes JavaScript to writer.
  596. func (n ForOfStmt) JS(w io.Writer) {
  597. w.Write([]byte("for"))
  598. if n.Await {
  599. w.Write([]byte(" await"))
  600. }
  601. w.Write([]byte(" ("))
  602. n.Init.JS(w)
  603. w.Write([]byte(" of "))
  604. n.Value.JS(w)
  605. w.Write([]byte(") "))
  606. n.Body.JS(w)
  607. }
  608. // CaseClause is a case clause or default clause for a switch statement.
  609. type CaseClause struct {
  610. TokenType
  611. Cond IExpr // can be nil
  612. List []IStmt
  613. }
  614. func (n CaseClause) String() string {
  615. s := " Clause(" + n.TokenType.String()
  616. if n.Cond != nil {
  617. s += " " + n.Cond.String()
  618. }
  619. for _, item := range n.List {
  620. s += " " + item.String()
  621. }
  622. return s + ")"
  623. }
  624. // JS writes JavaScript to writer.
  625. func (n CaseClause) JS(w io.Writer) {
  626. if n.Cond != nil {
  627. w.Write([]byte("case "))
  628. n.Cond.JS(w)
  629. } else {
  630. w.Write([]byte("default"))
  631. }
  632. w.Write([]byte(":"))
  633. wi := NewIndenter(w, 4)
  634. for _, item := range n.List {
  635. wi.Write([]byte("\n"))
  636. item.JS(wi)
  637. if _, ok := item.(*VarDecl); ok {
  638. w.Write([]byte(";"))
  639. }
  640. }
  641. }
  642. // SwitchStmt is a switch statement.
  643. type SwitchStmt struct {
  644. Init IExpr
  645. List []CaseClause
  646. Scope
  647. }
  648. func (n SwitchStmt) String() string {
  649. s := "Stmt(switch " + n.Init.String()
  650. for _, clause := range n.List {
  651. s += clause.String()
  652. }
  653. return s + ")"
  654. }
  655. // JS writes JavaScript to writer.
  656. func (n SwitchStmt) JS(w io.Writer) {
  657. w.Write([]byte("switch ("))
  658. n.Init.JS(w)
  659. if len(n.List) == 0 {
  660. w.Write([]byte(") {}"))
  661. return
  662. }
  663. w.Write([]byte(") {"))
  664. for _, clause := range n.List {
  665. w.Write([]byte("\n"))
  666. clause.JS(w)
  667. }
  668. w.Write([]byte("\n}"))
  669. }
  670. // BranchStmt is a continue or break statement.
  671. type BranchStmt struct {
  672. Type TokenType
  673. Label []byte // can be nil
  674. }
  675. func (n BranchStmt) String() string {
  676. s := "Stmt(" + n.Type.String()
  677. if n.Label != nil {
  678. s += " " + string(n.Label)
  679. }
  680. return s + ")"
  681. }
  682. // JS writes JavaScript to writer.
  683. func (n BranchStmt) JS(w io.Writer) {
  684. w.Write(n.Type.Bytes())
  685. if n.Label != nil {
  686. w.Write([]byte(" "))
  687. w.Write(n.Label)
  688. }
  689. w.Write([]byte(";"))
  690. }
  691. // ReturnStmt is a return statement.
  692. type ReturnStmt struct {
  693. Value IExpr // can be nil
  694. }
  695. func (n ReturnStmt) String() string {
  696. s := "Stmt(return"
  697. if n.Value != nil {
  698. s += " " + n.Value.String()
  699. }
  700. return s + ")"
  701. }
  702. // JS writes JavaScript to writer.
  703. func (n ReturnStmt) JS(w io.Writer) {
  704. w.Write([]byte("return"))
  705. if n.Value != nil {
  706. w.Write([]byte(" "))
  707. n.Value.JS(w)
  708. }
  709. w.Write([]byte(";"))
  710. }
  711. // WithStmt is a with statement.
  712. type WithStmt struct {
  713. Cond IExpr
  714. Body IStmt
  715. }
  716. func (n WithStmt) String() string {
  717. return "Stmt(with " + n.Cond.String() + " " + n.Body.String() + ")"
  718. }
  719. // JS writes JavaScript to writer.
  720. func (n WithStmt) JS(w io.Writer) {
  721. w.Write([]byte("with ("))
  722. n.Cond.JS(w)
  723. w.Write([]byte(")"))
  724. if _, ok := n.Body.(*EmptyStmt); !ok {
  725. w.Write([]byte(" "))
  726. }
  727. n.Body.JS(w)
  728. if _, ok := n.Body.(*VarDecl); ok {
  729. w.Write([]byte(";"))
  730. }
  731. }
  732. // LabelledStmt is a labelled statement.
  733. type LabelledStmt struct {
  734. Label []byte
  735. Value IStmt
  736. }
  737. func (n LabelledStmt) String() string {
  738. return "Stmt(" + string(n.Label) + " : " + n.Value.String() + ")"
  739. }
  740. // JS writes JavaScript to writer.
  741. func (n LabelledStmt) JS(w io.Writer) {
  742. w.Write(n.Label)
  743. w.Write([]byte(":"))
  744. if _, ok := n.Value.(*EmptyStmt); !ok {
  745. w.Write([]byte(" "))
  746. }
  747. n.Value.JS(w)
  748. if _, ok := n.Value.(*VarDecl); ok {
  749. w.Write([]byte(";"))
  750. }
  751. }
  752. // ThrowStmt is a throw statement.
  753. type ThrowStmt struct {
  754. Value IExpr
  755. }
  756. func (n ThrowStmt) String() string {
  757. return "Stmt(throw " + n.Value.String() + ")"
  758. }
  759. // JS writes JavaScript to writer.
  760. func (n ThrowStmt) JS(w io.Writer) {
  761. w.Write([]byte("throw "))
  762. n.Value.JS(w)
  763. w.Write([]byte(";"))
  764. }
  765. // TryStmt is a try statement.
  766. type TryStmt struct {
  767. Body *BlockStmt
  768. Binding IBinding // can be nil
  769. Catch *BlockStmt // can be nil
  770. Finally *BlockStmt // can be nil
  771. }
  772. func (n TryStmt) String() string {
  773. s := "Stmt(try " + n.Body.String()
  774. if n.Catch != nil {
  775. s += " catch"
  776. if n.Binding != nil {
  777. s += " Binding(" + n.Binding.String() + ")"
  778. }
  779. s += " " + n.Catch.String()
  780. }
  781. if n.Finally != nil {
  782. s += " finally " + n.Finally.String()
  783. }
  784. return s + ")"
  785. }
  786. // JS writes JavaScript to writer.
  787. func (n TryStmt) JS(w io.Writer) {
  788. w.Write([]byte("try "))
  789. n.Body.JS(w)
  790. if n.Catch != nil {
  791. w.Write([]byte(" catch"))
  792. if n.Binding != nil {
  793. w.Write([]byte("("))
  794. n.Binding.JS(w)
  795. w.Write([]byte(")"))
  796. }
  797. w.Write([]byte(" "))
  798. n.Catch.JS(w)
  799. }
  800. if n.Finally != nil {
  801. w.Write([]byte(" finally "))
  802. n.Finally.JS(w)
  803. }
  804. }
  805. // DebuggerStmt is a debugger statement.
  806. type DebuggerStmt struct{}
  807. func (n DebuggerStmt) String() string {
  808. return "Stmt(debugger)"
  809. }
  810. // JS writes JavaScript to writer.
  811. func (n DebuggerStmt) JS(w io.Writer) {
  812. w.Write([]byte("debugger;"))
  813. }
  814. // Alias is a name space import or import/export specifier for import/export statements.
  815. type Alias struct {
  816. Name []byte // can be nil
  817. Binding []byte // can be nil
  818. }
  819. func (alias Alias) String() string {
  820. s := ""
  821. if alias.Name != nil {
  822. s += string(alias.Name) + " as "
  823. }
  824. return s + string(alias.Binding)
  825. }
  826. // JS writes JavaScript to writer.
  827. func (alias Alias) JS(w io.Writer) {
  828. if alias.Name != nil {
  829. w.Write(alias.Name)
  830. w.Write([]byte(" as "))
  831. }
  832. w.Write(alias.Binding)
  833. }
  834. // ImportStmt is an import statement.
  835. type ImportStmt struct {
  836. List []Alias
  837. Default []byte // can be nil
  838. Module []byte
  839. }
  840. func (n ImportStmt) String() string {
  841. s := "Stmt(import"
  842. if n.Default != nil {
  843. s += " " + string(n.Default)
  844. if n.List != nil {
  845. s += " ,"
  846. }
  847. }
  848. if len(n.List) == 1 && len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' {
  849. s += " " + n.List[0].String()
  850. } else if n.List != nil {
  851. s += " {"
  852. for i, item := range n.List {
  853. if i != 0 {
  854. s += " ,"
  855. }
  856. if item.Binding != nil {
  857. s += " " + item.String()
  858. }
  859. }
  860. s += " }"
  861. }
  862. if n.Default != nil || n.List != nil {
  863. s += " from"
  864. }
  865. return s + " " + string(n.Module) + ")"
  866. }
  867. // JS writes JavaScript to writer.
  868. func (n ImportStmt) JS(w io.Writer) {
  869. if wi, ok := w.(Indenter); ok {
  870. w = wi.w
  871. }
  872. w.Write([]byte("import"))
  873. if n.Default != nil {
  874. w.Write([]byte(" "))
  875. w.Write(n.Default)
  876. if n.List != nil {
  877. w.Write([]byte(","))
  878. }
  879. }
  880. if len(n.List) == 1 && len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' {
  881. w.Write([]byte(" "))
  882. n.List[0].JS(w)
  883. } else if n.List != nil {
  884. if len(n.List) == 0 {
  885. w.Write([]byte(" {}"))
  886. } else {
  887. w.Write([]byte(" {"))
  888. for j, item := range n.List {
  889. if j != 0 {
  890. w.Write([]byte(","))
  891. }
  892. if item.Binding != nil {
  893. w.Write([]byte(" "))
  894. item.JS(w)
  895. }
  896. }
  897. w.Write([]byte(" }"))
  898. }
  899. }
  900. if n.Default != nil || n.List != nil {
  901. w.Write([]byte(" from"))
  902. }
  903. w.Write([]byte(" "))
  904. w.Write(n.Module)
  905. w.Write([]byte(";"))
  906. }
  907. // ExportStmt is an export statement.
  908. type ExportStmt struct {
  909. List []Alias
  910. Module []byte // can be nil
  911. Default bool
  912. Decl IExpr
  913. }
  914. func (n ExportStmt) String() string {
  915. s := "Stmt(export"
  916. if n.Decl != nil {
  917. if n.Default {
  918. s += " default"
  919. }
  920. return s + " " + n.Decl.String() + ")"
  921. } else if len(n.List) == 1 && (len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' || n.List[0].Name == nil && len(n.List[0].Binding) == 1 && n.List[0].Binding[0] == '*') {
  922. s += " " + n.List[0].String()
  923. } else if 0 < len(n.List) {
  924. s += " {"
  925. for i, item := range n.List {
  926. if i != 0 {
  927. s += " ,"
  928. }
  929. if item.Binding != nil {
  930. s += " " + item.String()
  931. }
  932. }
  933. s += " }"
  934. }
  935. if n.Module != nil {
  936. s += " from " + string(n.Module)
  937. }
  938. return s + ")"
  939. }
  940. // JS writes JavaScript to writer.
  941. func (n ExportStmt) JS(w io.Writer) {
  942. if wi, ok := w.(Indenter); ok {
  943. w = wi.w
  944. }
  945. w.Write([]byte("export"))
  946. if n.Decl != nil {
  947. if n.Default {
  948. w.Write([]byte(" default"))
  949. }
  950. w.Write([]byte(" "))
  951. n.Decl.JS(w)
  952. w.Write([]byte(";"))
  953. return
  954. } else if len(n.List) == 1 && (len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' || n.List[0].Name == nil && len(n.List[0].Binding) == 1 && n.List[0].Binding[0] == '*') {
  955. w.Write([]byte(" "))
  956. n.List[0].JS(w)
  957. } else if len(n.List) == 0 {
  958. w.Write([]byte(" {}"))
  959. } else {
  960. w.Write([]byte(" {"))
  961. for j, item := range n.List {
  962. if j != 0 {
  963. w.Write([]byte(","))
  964. }
  965. if item.Binding != nil {
  966. w.Write([]byte(" "))
  967. item.JS(w)
  968. }
  969. }
  970. w.Write([]byte(" }"))
  971. }
  972. if n.Module != nil {
  973. w.Write([]byte(" from "))
  974. w.Write(n.Module)
  975. }
  976. w.Write([]byte(";"))
  977. }
  978. // DirectivePrologueStmt is a string literal at the beginning of a function or module (usually "use strict").
  979. type DirectivePrologueStmt struct {
  980. Value []byte
  981. }
  982. func (n DirectivePrologueStmt) String() string {
  983. return "Stmt(" + string(n.Value) + ")"
  984. }
  985. // JS writes JavaScript to writer.
  986. func (n DirectivePrologueStmt) JS(w io.Writer) {
  987. if wi, ok := w.(Indenter); ok {
  988. w = wi.w
  989. }
  990. w.Write(n.Value)
  991. w.Write([]byte(";"))
  992. }
  993. func (n Comment) stmtNode() {}
  994. func (n BlockStmt) stmtNode() {}
  995. func (n EmptyStmt) stmtNode() {}
  996. func (n ExprStmt) stmtNode() {}
  997. func (n IfStmt) stmtNode() {}
  998. func (n DoWhileStmt) stmtNode() {}
  999. func (n WhileStmt) stmtNode() {}
  1000. func (n ForStmt) stmtNode() {}
  1001. func (n ForInStmt) stmtNode() {}
  1002. func (n ForOfStmt) stmtNode() {}
  1003. func (n SwitchStmt) stmtNode() {}
  1004. func (n BranchStmt) stmtNode() {}
  1005. func (n ReturnStmt) stmtNode() {}
  1006. func (n WithStmt) stmtNode() {}
  1007. func (n LabelledStmt) stmtNode() {}
  1008. func (n ThrowStmt) stmtNode() {}
  1009. func (n TryStmt) stmtNode() {}
  1010. func (n DebuggerStmt) stmtNode() {}
  1011. func (n ImportStmt) stmtNode() {}
  1012. func (n ExportStmt) stmtNode() {}
  1013. func (n DirectivePrologueStmt) stmtNode() {}
  1014. ////////////////////////////////////////////////////////////////
  1015. // PropertyName is a property name for binding properties, method names, and in object literals.
  1016. type PropertyName struct {
  1017. Literal LiteralExpr
  1018. Computed IExpr // can be nil
  1019. }
  1020. // IsSet returns true is PropertyName is not nil.
  1021. func (n PropertyName) IsSet() bool {
  1022. return n.IsComputed() || n.Literal.TokenType != ErrorToken
  1023. }
  1024. // IsComputed returns true if PropertyName is computed.
  1025. func (n PropertyName) IsComputed() bool {
  1026. return n.Computed != nil
  1027. }
  1028. // IsIdent returns true if PropertyName equals the given identifier name.
  1029. func (n PropertyName) IsIdent(data []byte) bool {
  1030. return !n.IsComputed() && n.Literal.TokenType == IdentifierToken && bytes.Equal(data, n.Literal.Data)
  1031. }
  1032. func (n PropertyName) String() string {
  1033. if n.Computed != nil {
  1034. val := n.Computed.String()
  1035. if val[0] == '(' {
  1036. return "[" + val[1:len(val)-1] + "]"
  1037. }
  1038. return "[" + val + "]"
  1039. }
  1040. return string(n.Literal.Data)
  1041. }
  1042. // JS writes JavaScript to writer.
  1043. func (n PropertyName) JS(w io.Writer) {
  1044. if n.Computed != nil {
  1045. w.Write([]byte("["))
  1046. n.Computed.JS(w)
  1047. w.Write([]byte("]"))
  1048. return
  1049. }
  1050. if wi, ok := w.(Indenter); ok {
  1051. w = wi.w
  1052. }
  1053. w.Write(n.Literal.Data)
  1054. }
  1055. // BindingArray is an array binding pattern.
  1056. type BindingArray struct {
  1057. List []BindingElement
  1058. Rest IBinding // can be nil
  1059. }
  1060. func (n BindingArray) String() string {
  1061. s := "["
  1062. for i, item := range n.List {
  1063. if i != 0 {
  1064. s += ","
  1065. }
  1066. s += " " + item.String()
  1067. }
  1068. if n.Rest != nil {
  1069. if len(n.List) != 0 {
  1070. s += ","
  1071. }
  1072. s += " ...Binding(" + n.Rest.String() + ")"
  1073. } else if 0 < len(n.List) && n.List[len(n.List)-1].Binding == nil {
  1074. s += ","
  1075. }
  1076. return s + " ]"
  1077. }
  1078. // JS writes JavaScript to writer.
  1079. func (n BindingArray) JS(w io.Writer) {
  1080. w.Write([]byte("["))
  1081. for j, item := range n.List {
  1082. if j != 0 {
  1083. w.Write([]byte(","))
  1084. }
  1085. if item.Binding != nil {
  1086. if j != 0 {
  1087. w.Write([]byte(" "))
  1088. }
  1089. item.JS(w)
  1090. }
  1091. }
  1092. if n.Rest != nil {
  1093. if len(n.List) != 0 {
  1094. w.Write([]byte(", "))
  1095. }
  1096. w.Write([]byte("..."))
  1097. n.Rest.JS(w)
  1098. } else if 0 < len(n.List) && n.List[len(n.List)-1].Binding == nil {
  1099. w.Write([]byte(","))
  1100. }
  1101. w.Write([]byte("]"))
  1102. }
  1103. // BindingObjectItem is a binding property.
  1104. type BindingObjectItem struct {
  1105. Key *PropertyName // can be nil
  1106. Value BindingElement
  1107. }
  1108. func (n BindingObjectItem) String() string {
  1109. s := ""
  1110. if n.Key != nil {
  1111. if v, ok := n.Value.Binding.(*Var); !ok || !n.Key.IsIdent(v.Data) {
  1112. s += " " + n.Key.String() + ":"
  1113. }
  1114. }
  1115. return s + " " + n.Value.String()
  1116. }
  1117. // JS writes JavaScript to writer.
  1118. func (n BindingObjectItem) JS(w io.Writer) {
  1119. if n.Key != nil {
  1120. if v, ok := n.Value.Binding.(*Var); !ok || !n.Key.IsIdent(v.Data) {
  1121. n.Key.JS(w)
  1122. w.Write([]byte(": "))
  1123. }
  1124. }
  1125. n.Value.JS(w)
  1126. }
  1127. // BindingObject is an object binding pattern.
  1128. type BindingObject struct {
  1129. List []BindingObjectItem
  1130. Rest *Var // can be nil
  1131. }
  1132. func (n BindingObject) String() string {
  1133. s := "{"
  1134. for i, item := range n.List {
  1135. if i != 0 {
  1136. s += ","
  1137. }
  1138. s += item.String()
  1139. }
  1140. if n.Rest != nil {
  1141. if len(n.List) != 0 {
  1142. s += ","
  1143. }
  1144. s += " ...Binding(" + string(n.Rest.Data) + ")"
  1145. }
  1146. return s + " }"
  1147. }
  1148. // JS writes JavaScript to writer.
  1149. func (n BindingObject) JS(w io.Writer) {
  1150. w.Write([]byte("{"))
  1151. for j, item := range n.List {
  1152. if j != 0 {
  1153. w.Write([]byte(", "))
  1154. }
  1155. item.JS(w)
  1156. }
  1157. if n.Rest != nil {
  1158. if len(n.List) != 0 {
  1159. w.Write([]byte(", "))
  1160. }
  1161. w.Write([]byte("..."))
  1162. w.Write(n.Rest.Data)
  1163. }
  1164. w.Write([]byte("}"))
  1165. }
  1166. // BindingElement is a binding element.
  1167. type BindingElement struct {
  1168. Binding IBinding // can be nil (in case of ellision)
  1169. Default IExpr // can be nil
  1170. }
  1171. func (n BindingElement) String() string {
  1172. if n.Binding == nil {
  1173. return "Binding()"
  1174. }
  1175. s := "Binding(" + n.Binding.String()
  1176. if n.Default != nil {
  1177. s += " = " + n.Default.String()
  1178. }
  1179. return s + ")"
  1180. }
  1181. // JS writes JavaScript to writer.
  1182. func (n BindingElement) JS(w io.Writer) {
  1183. if n.Binding == nil {
  1184. return
  1185. }
  1186. n.Binding.JS(w)
  1187. if n.Default != nil {
  1188. w.Write([]byte(" = "))
  1189. n.Default.JS(w)
  1190. }
  1191. }
  1192. func (v *Var) bindingNode() {}
  1193. func (n BindingArray) bindingNode() {}
  1194. func (n BindingObject) bindingNode() {}
  1195. ////////////////////////////////////////////////////////////////
  1196. // VarDecl is a variable statement or lexical declaration.
  1197. type VarDecl struct {
  1198. TokenType
  1199. List []BindingElement
  1200. Scope *Scope
  1201. InFor, InForInOf bool
  1202. }
  1203. func (n VarDecl) String() string {
  1204. s := "Decl(" + n.TokenType.String()
  1205. for _, item := range n.List {
  1206. s += " " + item.String()
  1207. }
  1208. return s + ")"
  1209. }
  1210. // JS writes JavaScript to writer.
  1211. func (n VarDecl) JS(w io.Writer) {
  1212. w.Write(n.TokenType.Bytes())
  1213. for j, item := range n.List {
  1214. if j != 0 {
  1215. w.Write([]byte(","))
  1216. }
  1217. w.Write([]byte(" "))
  1218. item.JS(w)
  1219. }
  1220. }
  1221. // Params is a list of parameters for functions, methods, and arrow function.
  1222. type Params struct {
  1223. List []BindingElement
  1224. Rest IBinding // can be nil
  1225. }
  1226. func (n Params) String() string {
  1227. s := "Params("
  1228. for i, item := range n.List {
  1229. if i != 0 {
  1230. s += ", "
  1231. }
  1232. s += item.String()
  1233. }
  1234. if n.Rest != nil {
  1235. if len(n.List) != 0 {
  1236. s += ", "
  1237. }
  1238. s += "...Binding(" + n.Rest.String() + ")"
  1239. }
  1240. return s + ")"
  1241. }
  1242. // JS writes JavaScript to writer.
  1243. func (n Params) JS(w io.Writer) {
  1244. w.Write([]byte("("))
  1245. for j, item := range n.List {
  1246. if j != 0 {
  1247. w.Write([]byte(", "))
  1248. }
  1249. item.JS(w)
  1250. }
  1251. if n.Rest != nil {
  1252. if len(n.List) != 0 {
  1253. w.Write([]byte(", "))
  1254. }
  1255. w.Write([]byte("..."))
  1256. n.Rest.JS(w)
  1257. }
  1258. w.Write([]byte(")"))
  1259. }
  1260. // FuncDecl is an (async) (generator) function declaration or expression.
  1261. type FuncDecl struct {
  1262. Async bool
  1263. Generator bool
  1264. Name *Var // can be nil
  1265. Params Params
  1266. Body BlockStmt
  1267. }
  1268. func (n FuncDecl) String() string {
  1269. s := "Decl("
  1270. if n.Async {
  1271. s += "async function"
  1272. } else {
  1273. s += "function"
  1274. }
  1275. if n.Generator {
  1276. s += "*"
  1277. }
  1278. if n.Name != nil {
  1279. s += " " + string(n.Name.Data)
  1280. }
  1281. return s + " " + n.Params.String() + " " + n.Body.String() + ")"
  1282. }
  1283. // JS writes JavaScript to writer.
  1284. func (n FuncDecl) JS(w io.Writer) {
  1285. if n.Async {
  1286. w.Write([]byte("async function"))
  1287. } else {
  1288. w.Write([]byte("function"))
  1289. }
  1290. if n.Generator {
  1291. w.Write([]byte("*"))
  1292. }
  1293. if n.Name != nil {
  1294. w.Write([]byte(" "))
  1295. w.Write(n.Name.Data)
  1296. }
  1297. n.Params.JS(w)
  1298. w.Write([]byte(" "))
  1299. n.Body.JS(w)
  1300. }
  1301. // MethodDecl is a method definition in a class declaration.
  1302. type MethodDecl struct {
  1303. Static bool
  1304. Async bool
  1305. Generator bool
  1306. Get bool
  1307. Set bool
  1308. Name PropertyName
  1309. Params Params
  1310. Body BlockStmt
  1311. }
  1312. func (n MethodDecl) String() string {
  1313. s := ""
  1314. if n.Static {
  1315. s += " static"
  1316. }
  1317. if n.Async {
  1318. s += " async"
  1319. }
  1320. if n.Generator {
  1321. s += " *"
  1322. }
  1323. if n.Get {
  1324. s += " get"
  1325. }
  1326. if n.Set {
  1327. s += " set"
  1328. }
  1329. s += " " + n.Name.String() + " " + n.Params.String() + " " + n.Body.String()
  1330. return "Method(" + s[1:] + ")"
  1331. }
  1332. // JS writes JavaScript to writer.
  1333. func (n MethodDecl) JS(w io.Writer) {
  1334. writen := false
  1335. if n.Static {
  1336. w.Write([]byte("static"))
  1337. writen = true
  1338. }
  1339. if n.Async {
  1340. if writen {
  1341. w.Write([]byte(" "))
  1342. }
  1343. w.Write([]byte("async"))
  1344. writen = true
  1345. }
  1346. if n.Generator {
  1347. if writen {
  1348. w.Write([]byte(" "))
  1349. }
  1350. w.Write([]byte("*"))
  1351. writen = true
  1352. }
  1353. if n.Get {
  1354. if writen {
  1355. w.Write([]byte(" "))
  1356. }
  1357. w.Write([]byte("get"))
  1358. writen = true
  1359. }
  1360. if n.Set {
  1361. if writen {
  1362. w.Write([]byte(" "))
  1363. }
  1364. w.Write([]byte("set"))
  1365. writen = true
  1366. }
  1367. if writen {
  1368. w.Write([]byte(" "))
  1369. }
  1370. n.Name.JS(w)
  1371. w.Write([]byte(" "))
  1372. n.Params.JS(w)
  1373. w.Write([]byte(" "))
  1374. n.Body.JS(w)
  1375. }
  1376. // Field is a field definition in a class declaration.
  1377. type Field struct {
  1378. Static bool
  1379. Name PropertyName
  1380. Init IExpr
  1381. }
  1382. func (n Field) String() string {
  1383. s := "Field("
  1384. if n.Static {
  1385. s += "static "
  1386. }
  1387. s += n.Name.String()
  1388. if n.Init != nil {
  1389. s += " = " + n.Init.String()
  1390. }
  1391. return s + ")"
  1392. }
  1393. // JS writes JavaScript to writer.
  1394. func (n Field) JS(w io.Writer) {
  1395. if n.Static {
  1396. w.Write([]byte("static "))
  1397. }
  1398. n.Name.JS(w)
  1399. if n.Init != nil {
  1400. w.Write([]byte(" = "))
  1401. n.Init.JS(w)
  1402. }
  1403. }
  1404. // ClassElement is a class element that is either a static block, a field definition, or a class method
  1405. type ClassElement struct {
  1406. StaticBlock *BlockStmt // can be nil
  1407. Method *MethodDecl // can be nil
  1408. Field
  1409. }
  1410. func (n ClassElement) String() string {
  1411. if n.StaticBlock != nil {
  1412. return "Static(" + n.StaticBlock.String() + ")"
  1413. } else if n.Method != nil {
  1414. return n.Method.String()
  1415. }
  1416. return n.Field.String()
  1417. }
  1418. // JS writes JavaScript to writer.
  1419. func (n ClassElement) JS(w io.Writer) {
  1420. if n.StaticBlock != nil {
  1421. w.Write([]byte("static "))
  1422. n.StaticBlock.JS(w)
  1423. return
  1424. } else if n.Method != nil {
  1425. n.Method.JS(w)
  1426. return
  1427. }
  1428. n.Field.JS(w)
  1429. w.Write([]byte(";"))
  1430. }
  1431. // ClassDecl is a class declaration.
  1432. type ClassDecl struct {
  1433. Name *Var // can be nil
  1434. Extends IExpr // can be nil
  1435. List []ClassElement
  1436. }
  1437. func (n ClassDecl) String() string {
  1438. s := "Decl(class"
  1439. if n.Name != nil {
  1440. s += " " + string(n.Name.Data)
  1441. }
  1442. if n.Extends != nil {
  1443. s += " extends " + n.Extends.String()
  1444. }
  1445. for _, item := range n.List {
  1446. s += " " + item.String()
  1447. }
  1448. return s + ")"
  1449. }
  1450. // JS writes JavaScript to writer.
  1451. func (n ClassDecl) JS(w io.Writer) {
  1452. w.Write([]byte("class"))
  1453. if n.Name != nil {
  1454. w.Write([]byte(" "))
  1455. w.Write(n.Name.Data)
  1456. }
  1457. if n.Extends != nil {
  1458. w.Write([]byte(" extends "))
  1459. n.Extends.JS(w)
  1460. }
  1461. if len(n.List) == 0 {
  1462. w.Write([]byte(" {}"))
  1463. return
  1464. }
  1465. w.Write([]byte(" {"))
  1466. wi := NewIndenter(w, 4)
  1467. for _, item := range n.List {
  1468. wi.Write([]byte("\n"))
  1469. item.JS(wi)
  1470. }
  1471. w.Write([]byte("\n}"))
  1472. }
  1473. func (n VarDecl) stmtNode() {}
  1474. func (n FuncDecl) stmtNode() {}
  1475. func (n ClassDecl) stmtNode() {}
  1476. func (n VarDecl) exprNode() {} // not a real IExpr, used for ForInit and ExportDecl
  1477. func (n FuncDecl) exprNode() {}
  1478. func (n ClassDecl) exprNode() {}
  1479. func (n MethodDecl) exprNode() {} // not a real IExpr, used for ObjectExpression PropertyName
  1480. ////////////////////////////////////////////////////////////////
  1481. // LiteralExpr can be this, null, boolean, numeric, string, or regular expression literals.
  1482. type LiteralExpr struct {
  1483. TokenType
  1484. Data []byte
  1485. }
  1486. func (n LiteralExpr) String() string {
  1487. return string(n.Data)
  1488. }
  1489. // JS writes JavaScript to writer.
  1490. func (n LiteralExpr) JS(w io.Writer) {
  1491. if wi, ok := w.(Indenter); ok {
  1492. w = wi.w
  1493. }
  1494. w.Write(n.Data)
  1495. }
  1496. // JSON writes JSON to writer.
  1497. func (n LiteralExpr) JSON(w io.Writer) error {
  1498. if n.TokenType == TrueToken || n.TokenType == FalseToken || n.TokenType == NullToken || n.TokenType == DecimalToken {
  1499. w.Write(n.Data)
  1500. return nil
  1501. } else if n.TokenType == StringToken {
  1502. data := n.Data
  1503. if n.Data[0] == '\'' {
  1504. data = parse.Copy(data)
  1505. data = bytes.ReplaceAll(data, []byte(`\'`), []byte(`'`))
  1506. data = bytes.ReplaceAll(data, []byte(`"`), []byte(`\"`))
  1507. data[0] = '"'
  1508. data[len(data)-1] = '"'
  1509. }
  1510. w.Write(data)
  1511. return nil
  1512. }
  1513. return ErrInvalidJSON
  1514. }
  1515. // Element is an array literal element.
  1516. type Element struct {
  1517. Value IExpr // can be nil
  1518. Spread bool
  1519. }
  1520. func (n Element) String() string {
  1521. s := ""
  1522. if n.Value != nil {
  1523. if n.Spread {
  1524. s += "..."
  1525. }
  1526. s += n.Value.String()
  1527. }
  1528. return s
  1529. }
  1530. // JS writes JavaScript to writer.
  1531. func (n Element) JS(w io.Writer) {
  1532. if n.Value != nil {
  1533. if n.Spread {
  1534. w.Write([]byte("..."))
  1535. }
  1536. n.Value.JS(w)
  1537. }
  1538. }
  1539. // ArrayExpr is an array literal.
  1540. type ArrayExpr struct {
  1541. List []Element
  1542. }
  1543. func (n ArrayExpr) String() string {
  1544. s := "["
  1545. for i, item := range n.List {
  1546. if i != 0 {
  1547. s += ", "
  1548. }
  1549. if item.Value != nil {
  1550. if item.Spread {
  1551. s += "..."
  1552. }
  1553. s += item.Value.String()
  1554. }
  1555. }
  1556. if 0 < len(n.List) && n.List[len(n.List)-1].Value == nil {
  1557. s += ","
  1558. }
  1559. return s + "]"
  1560. }
  1561. // JS writes JavaScript to writer.
  1562. func (n ArrayExpr) JS(w io.Writer) {
  1563. w.Write([]byte("["))
  1564. for j, item := range n.List {
  1565. if j != 0 {
  1566. w.Write([]byte(", "))
  1567. }
  1568. if item.Value != nil {
  1569. if item.Spread {
  1570. w.Write([]byte("..."))
  1571. }
  1572. item.Value.JS(w)
  1573. }
  1574. }
  1575. if 0 < len(n.List) && n.List[len(n.List)-1].Value == nil {
  1576. w.Write([]byte(","))
  1577. }
  1578. w.Write([]byte("]"))
  1579. }
  1580. // JSON writes JSON to writer.
  1581. func (n ArrayExpr) JSON(w io.Writer) error {
  1582. w.Write([]byte("["))
  1583. for i, item := range n.List {
  1584. if i != 0 {
  1585. w.Write([]byte(", "))
  1586. }
  1587. if item.Value == nil || item.Spread {
  1588. return ErrInvalidJSON
  1589. }
  1590. if val, ok := item.Value.(JSONer); !ok {
  1591. return ErrInvalidJSON
  1592. } else if err := val.JSON(w); err != nil {
  1593. return err
  1594. }
  1595. }
  1596. w.Write([]byte("]"))
  1597. return nil
  1598. }
  1599. // Property is a property definition in an object literal.
  1600. type Property struct {
  1601. // either Name or Spread are set. When Spread is set then Value is AssignmentExpression
  1602. // if Init is set then Value is IdentifierReference, otherwise it can also be MethodDefinition
  1603. Name *PropertyName // can be nil
  1604. Spread bool
  1605. Value IExpr
  1606. Init IExpr // can be nil
  1607. }
  1608. func (n Property) String() string {
  1609. s := ""
  1610. if n.Name != nil {
  1611. if v, ok := n.Value.(*Var); !ok || !n.Name.IsIdent(v.Data) {
  1612. s += n.Name.String() + ": "
  1613. }
  1614. } else if n.Spread {
  1615. s += "..."
  1616. }
  1617. s += n.Value.String()
  1618. if n.Init != nil {
  1619. s += " = " + n.Init.String()
  1620. }
  1621. return s
  1622. }
  1623. // JS writes JavaScript to writer.
  1624. func (n Property) JS(w io.Writer) {
  1625. if n.Name != nil {
  1626. if v, ok := n.Value.(*Var); !ok || !n.Name.IsIdent(v.Data) {
  1627. n.Name.JS(w)
  1628. w.Write([]byte(": "))
  1629. }
  1630. } else if n.Spread {
  1631. w.Write([]byte("..."))
  1632. }
  1633. n.Value.JS(w)
  1634. if n.Init != nil {
  1635. w.Write([]byte(" = "))
  1636. n.Init.JS(w)
  1637. }
  1638. }
  1639. // JSON writes JSON to writer.
  1640. func (n Property) JSON(w io.Writer) error {
  1641. if n.Name == nil || n.Name.Literal.TokenType != StringToken && n.Name.Literal.TokenType != IdentifierToken || n.Spread || n.Init != nil {
  1642. return ErrInvalidJSON
  1643. } else if n.Name.Literal.TokenType == IdentifierToken {
  1644. w.Write([]byte(`"`))
  1645. w.Write(n.Name.Literal.Data)
  1646. w.Write([]byte(`"`))
  1647. } else {
  1648. _ = n.Name.Literal.JSON(w)
  1649. }
  1650. w.Write([]byte(": "))
  1651. if val, ok := n.Value.(JSONer); !ok {
  1652. return ErrInvalidJSON
  1653. } else if err := val.JSON(w); err != nil {
  1654. return err
  1655. }
  1656. return nil
  1657. }
  1658. // ObjectExpr is an object literal.
  1659. type ObjectExpr struct {
  1660. List []Property
  1661. }
  1662. func (n ObjectExpr) String() string {
  1663. s := "{"
  1664. for i, item := range n.List {
  1665. if i != 0 {
  1666. s += ", "
  1667. }
  1668. s += item.String()
  1669. }
  1670. return s + "}"
  1671. }
  1672. // JS writes JavaScript to writer.
  1673. func (n ObjectExpr) JS(w io.Writer) {
  1674. w.Write([]byte("{"))
  1675. for j, item := range n.List {
  1676. if j != 0 {
  1677. w.Write([]byte(", "))
  1678. }
  1679. item.JS(w)
  1680. }
  1681. w.Write([]byte("}"))
  1682. }
  1683. // JSON writes JSON to writer.
  1684. func (n ObjectExpr) JSON(w io.Writer) error {
  1685. w.Write([]byte("{"))
  1686. for i, item := range n.List {
  1687. if i != 0 {
  1688. w.Write([]byte(", "))
  1689. }
  1690. if err := item.JSON(w); err != nil {
  1691. return err
  1692. }
  1693. }
  1694. w.Write([]byte("}"))
  1695. return nil
  1696. }
  1697. // TemplatePart is a template head or middle.
  1698. type TemplatePart struct {
  1699. Value []byte
  1700. Expr IExpr
  1701. }
  1702. func (n TemplatePart) String() string {
  1703. return string(n.Value) + n.Expr.String()
  1704. }
  1705. // JS writes JavaScript to writer.
  1706. func (n TemplatePart) JS(w io.Writer) {
  1707. w.Write(n.Value)
  1708. n.Expr.JS(w)
  1709. }
  1710. // TemplateExpr is a template literal or member/call expression, super property, or optional chain with template literal.
  1711. type TemplateExpr struct {
  1712. Tag IExpr // can be nil
  1713. List []TemplatePart
  1714. Tail []byte
  1715. Prec OpPrec
  1716. Optional bool
  1717. }
  1718. func (n TemplateExpr) String() string {
  1719. s := ""
  1720. if n.Tag != nil {
  1721. s += n.Tag.String()
  1722. if n.Optional {
  1723. s += "?."
  1724. }
  1725. }
  1726. for _, item := range n.List {
  1727. s += item.String()
  1728. }
  1729. return s + string(n.Tail)
  1730. }
  1731. // JS writes JavaScript to writer.
  1732. func (n TemplateExpr) JS(w io.Writer) {
  1733. if wi, ok := w.(Indenter); ok {
  1734. w = wi.w
  1735. }
  1736. if n.Tag != nil {
  1737. n.Tag.JS(w)
  1738. if n.Optional {
  1739. w.Write([]byte("?."))
  1740. }
  1741. }
  1742. for _, item := range n.List {
  1743. item.JS(w)
  1744. }
  1745. w.Write(n.Tail)
  1746. }
  1747. // GroupExpr is a parenthesized expression.
  1748. type GroupExpr struct {
  1749. X IExpr
  1750. }
  1751. func (n GroupExpr) String() string {
  1752. return "(" + n.X.String() + ")"
  1753. }
  1754. // JS writes JavaScript to writer.
  1755. func (n GroupExpr) JS(w io.Writer) {
  1756. w.Write([]byte("("))
  1757. n.X.JS(w)
  1758. w.Write([]byte(")"))
  1759. }
  1760. // IndexExpr is a member/call expression, super property, or optional chain with an index expression.
  1761. type IndexExpr struct {
  1762. X IExpr
  1763. Y IExpr
  1764. Prec OpPrec
  1765. Optional bool
  1766. }
  1767. func (n IndexExpr) String() string {
  1768. if n.Optional {
  1769. return "(" + n.X.String() + "?.[" + n.Y.String() + "])"
  1770. }
  1771. return "(" + n.X.String() + "[" + n.Y.String() + "])"
  1772. }
  1773. // JS writes JavaScript to writer.
  1774. func (n IndexExpr) JS(w io.Writer) {
  1775. n.X.JS(w)
  1776. if n.Optional {
  1777. w.Write([]byte("?.["))
  1778. } else {
  1779. w.Write([]byte("["))
  1780. }
  1781. n.Y.JS(w)
  1782. w.Write([]byte("]"))
  1783. }
  1784. // DotExpr is a member/call expression, super property, or optional chain with a dot expression.
  1785. type DotExpr struct {
  1786. X IExpr
  1787. Y LiteralExpr
  1788. Prec OpPrec
  1789. Optional bool
  1790. }
  1791. func (n DotExpr) String() string {
  1792. if n.Optional {
  1793. return "(" + n.X.String() + "?." + n.Y.String() + ")"
  1794. }
  1795. return "(" + n.X.String() + "." + n.Y.String() + ")"
  1796. }
  1797. // JS writes JavaScript to writer.
  1798. func (n DotExpr) JS(w io.Writer) {
  1799. lit, ok := n.X.(*LiteralExpr)
  1800. group := ok && !n.Optional && lit.TokenType == DecimalToken
  1801. if group {
  1802. w.Write([]byte("("))
  1803. }
  1804. n.X.JS(w)
  1805. if n.Optional {
  1806. w.Write([]byte("?."))
  1807. } else {
  1808. if group {
  1809. w.Write([]byte(")"))
  1810. }
  1811. w.Write([]byte("."))
  1812. }
  1813. n.Y.JS(w)
  1814. }
  1815. // NewTargetExpr is a new target meta property.
  1816. type NewTargetExpr struct{}
  1817. func (n NewTargetExpr) String() string {
  1818. return "(new.target)"
  1819. }
  1820. // JS writes JavaScript to writer.
  1821. func (n NewTargetExpr) JS(w io.Writer) {
  1822. w.Write([]byte("new.target"))
  1823. }
  1824. // ImportMetaExpr is a import meta meta property.
  1825. type ImportMetaExpr struct{}
  1826. func (n ImportMetaExpr) String() string {
  1827. return "(import.meta)"
  1828. }
  1829. // JS writes JavaScript to writer.
  1830. func (n ImportMetaExpr) JS(w io.Writer) {
  1831. w.Write([]byte("import.meta"))
  1832. }
  1833. type Arg struct {
  1834. Value IExpr
  1835. Rest bool
  1836. }
  1837. func (n Arg) String() string {
  1838. s := ""
  1839. if n.Rest {
  1840. s += "..."
  1841. }
  1842. return s + n.Value.String()
  1843. }
  1844. // JS writes JavaScript to writer.
  1845. func (n Arg) JS(w io.Writer) {
  1846. if n.Rest {
  1847. w.Write([]byte("..."))
  1848. }
  1849. n.Value.JS(w)
  1850. }
  1851. // Args is a list of arguments as used by new and call expressions.
  1852. type Args struct {
  1853. List []Arg
  1854. }
  1855. func (n Args) String() string {
  1856. s := "("
  1857. for i, item := range n.List {
  1858. if i != 0 {
  1859. s += ", "
  1860. }
  1861. s += item.String()
  1862. }
  1863. return s + ")"
  1864. }
  1865. // JS writes JavaScript to writer.
  1866. func (n Args) JS(w io.Writer) {
  1867. for j, item := range n.List {
  1868. if j != 0 {
  1869. w.Write([]byte(", "))
  1870. }
  1871. item.JS(w)
  1872. }
  1873. }
  1874. // NewExpr is a new expression or new member expression.
  1875. type NewExpr struct {
  1876. X IExpr
  1877. Args *Args // can be nil
  1878. }
  1879. func (n NewExpr) String() string {
  1880. if n.Args != nil {
  1881. return "(new " + n.X.String() + n.Args.String() + ")"
  1882. }
  1883. return "(new " + n.X.String() + ")"
  1884. }
  1885. // JS writes JavaScript to writer.
  1886. func (n NewExpr) JS(w io.Writer) {
  1887. w.Write([]byte("new "))
  1888. n.X.JS(w)
  1889. if n.Args != nil {
  1890. w.Write([]byte("("))
  1891. n.Args.JS(w)
  1892. w.Write([]byte(")"))
  1893. } else {
  1894. w.Write([]byte("()"))
  1895. }
  1896. }
  1897. // CallExpr is a call expression.
  1898. type CallExpr struct {
  1899. X IExpr
  1900. Args Args
  1901. Optional bool
  1902. }
  1903. func (n CallExpr) String() string {
  1904. if n.Optional {
  1905. return "(" + n.X.String() + "?." + n.Args.String() + ")"
  1906. }
  1907. return "(" + n.X.String() + n.Args.String() + ")"
  1908. }
  1909. // JS writes JavaScript to writer.
  1910. func (n CallExpr) JS(w io.Writer) {
  1911. n.X.JS(w)
  1912. if n.Optional {
  1913. w.Write([]byte("?.("))
  1914. } else {
  1915. w.Write([]byte("("))
  1916. }
  1917. n.Args.JS(w)
  1918. w.Write([]byte(")"))
  1919. }
  1920. // UnaryExpr is an update or unary expression.
  1921. type UnaryExpr struct {
  1922. Op TokenType
  1923. X IExpr
  1924. }
  1925. func (n UnaryExpr) String() string {
  1926. if n.Op == PostIncrToken || n.Op == PostDecrToken {
  1927. return "(" + n.X.String() + n.Op.String() + ")"
  1928. } else if IsIdentifierName(n.Op) {
  1929. return "(" + n.Op.String() + " " + n.X.String() + ")"
  1930. }
  1931. return "(" + n.Op.String() + n.X.String() + ")"
  1932. }
  1933. // JS writes JavaScript to writer.
  1934. func (n UnaryExpr) JS(w io.Writer) {
  1935. if n.Op == PostIncrToken || n.Op == PostDecrToken {
  1936. n.X.JS(w)
  1937. w.Write(n.Op.Bytes())
  1938. return
  1939. } else if unary, ok := n.X.(*UnaryExpr); ok && (n.Op == PosToken && (unary.Op == PreIncrToken || unary.Op == PosToken) || n.Op == NegToken && (unary.Op == PreDecrToken || unary.Op == NegToken)) || IsIdentifierName(n.Op) {
  1940. w.Write(n.Op.Bytes())
  1941. w.Write([]byte(" "))
  1942. n.X.JS(w)
  1943. return
  1944. }
  1945. w.Write(n.Op.Bytes())
  1946. n.X.JS(w)
  1947. }
  1948. // JSON writes JSON to writer.
  1949. func (n UnaryExpr) JSON(w io.Writer) error {
  1950. if lit, ok := n.X.(*LiteralExpr); ok && n.Op == NegToken && lit.TokenType == DecimalToken {
  1951. w.Write([]byte("-"))
  1952. w.Write(lit.Data)
  1953. return nil
  1954. }
  1955. return ErrInvalidJSON
  1956. }
  1957. // BinaryExpr is a binary expression.
  1958. type BinaryExpr struct {
  1959. Op TokenType
  1960. X, Y IExpr
  1961. }
  1962. func (n BinaryExpr) String() string {
  1963. if IsIdentifierName(n.Op) {
  1964. return "(" + n.X.String() + " " + n.Op.String() + " " + n.Y.String() + ")"
  1965. }
  1966. return "(" + n.X.String() + n.Op.String() + n.Y.String() + ")"
  1967. }
  1968. // JS writes JavaScript to writer.
  1969. func (n BinaryExpr) JS(w io.Writer) {
  1970. n.X.JS(w)
  1971. w.Write([]byte(" "))
  1972. w.Write(n.Op.Bytes())
  1973. w.Write([]byte(" "))
  1974. n.Y.JS(w)
  1975. }
  1976. // CondExpr is a conditional expression.
  1977. type CondExpr struct {
  1978. Cond, X, Y IExpr
  1979. }
  1980. func (n CondExpr) String() string {
  1981. return "(" + n.Cond.String() + " ? " + n.X.String() + " : " + n.Y.String() + ")"
  1982. }
  1983. // JS writes JavaScript to writer.
  1984. func (n CondExpr) JS(w io.Writer) {
  1985. n.Cond.JS(w)
  1986. w.Write([]byte(" ? "))
  1987. n.X.JS(w)
  1988. w.Write([]byte(" : "))
  1989. n.Y.JS(w)
  1990. }
  1991. // YieldExpr is a yield expression.
  1992. type YieldExpr struct {
  1993. Generator bool
  1994. X IExpr // can be nil
  1995. }
  1996. func (n YieldExpr) String() string {
  1997. if n.X == nil {
  1998. return "(yield)"
  1999. }
  2000. s := "(yield"
  2001. if n.Generator {
  2002. s += "*"
  2003. }
  2004. return s + " " + n.X.String() + ")"
  2005. }
  2006. // JS writes JavaScript to writer.
  2007. func (n YieldExpr) JS(w io.Writer) {
  2008. w.Write([]byte("yield"))
  2009. if n.X == nil {
  2010. return
  2011. }
  2012. if n.Generator {
  2013. w.Write([]byte("*"))
  2014. }
  2015. w.Write([]byte(" "))
  2016. n.X.JS(w)
  2017. }
  2018. // ArrowFunc is an (async) arrow function.
  2019. type ArrowFunc struct {
  2020. Async bool
  2021. Params Params
  2022. Body BlockStmt
  2023. }
  2024. func (n ArrowFunc) String() string {
  2025. s := "("
  2026. if n.Async {
  2027. s += "async "
  2028. }
  2029. return s + n.Params.String() + " => " + n.Body.String() + ")"
  2030. }
  2031. // JS writes JavaScript to writer.
  2032. func (n ArrowFunc) JS(w io.Writer) {
  2033. if n.Async {
  2034. w.Write([]byte("async "))
  2035. }
  2036. n.Params.JS(w)
  2037. w.Write([]byte(" => "))
  2038. n.Body.JS(w)
  2039. }
  2040. // CommaExpr is a series of comma expressions.
  2041. type CommaExpr struct {
  2042. List []IExpr
  2043. }
  2044. func (n CommaExpr) String() string {
  2045. s := "("
  2046. for i, item := range n.List {
  2047. if i != 0 {
  2048. s += ","
  2049. }
  2050. s += item.String()
  2051. }
  2052. return s + ")"
  2053. }
  2054. // JS writes JavaScript to writer.
  2055. func (n CommaExpr) JS(w io.Writer) {
  2056. for j, item := range n.List {
  2057. if j != 0 {
  2058. w.Write([]byte(","))
  2059. }
  2060. item.JS(w)
  2061. }
  2062. }
  2063. func (v *Var) exprNode() {}
  2064. func (n LiteralExpr) exprNode() {}
  2065. func (n ArrayExpr) exprNode() {}
  2066. func (n ObjectExpr) exprNode() {}
  2067. func (n TemplateExpr) exprNode() {}
  2068. func (n GroupExpr) exprNode() {}
  2069. func (n DotExpr) exprNode() {}
  2070. func (n IndexExpr) exprNode() {}
  2071. func (n NewTargetExpr) exprNode() {}
  2072. func (n ImportMetaExpr) exprNode() {}
  2073. func (n NewExpr) exprNode() {}
  2074. func (n CallExpr) exprNode() {}
  2075. func (n UnaryExpr) exprNode() {}
  2076. func (n BinaryExpr) exprNode() {}
  2077. func (n CondExpr) exprNode() {}
  2078. func (n YieldExpr) exprNode() {}
  2079. func (n ArrowFunc) exprNode() {}
  2080. func (n CommaExpr) exprNode() {}