ast.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  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. w.Write(n.Value)
  385. w.Write([]byte("\n"))
  386. }
  387. // BlockStmt is a block statement.
  388. type BlockStmt struct {
  389. List []IStmt
  390. Scope
  391. }
  392. func (n BlockStmt) String() string {
  393. s := "Stmt({"
  394. for _, item := range n.List {
  395. s += " " + item.String()
  396. }
  397. return s + " })"
  398. }
  399. // JS writes JavaScript to writer.
  400. func (n BlockStmt) JS(w io.Writer) {
  401. if len(n.List) == 0 {
  402. w.Write([]byte("{}"))
  403. return
  404. }
  405. w.Write([]byte("{"))
  406. wi := NewIndenter(w, 4)
  407. for _, item := range n.List {
  408. wi.Write([]byte("\n"))
  409. item.JS(wi)
  410. if _, ok := item.(*VarDecl); ok {
  411. w.Write([]byte(";"))
  412. }
  413. }
  414. w.Write([]byte("\n}"))
  415. }
  416. // EmptyStmt is an empty statement.
  417. type EmptyStmt struct{}
  418. func (n EmptyStmt) String() string {
  419. return "Stmt()"
  420. }
  421. // JS writes JavaScript to writer.
  422. func (n EmptyStmt) JS(w io.Writer) {
  423. w.Write([]byte(";"))
  424. }
  425. // ExprStmt is an expression statement.
  426. type ExprStmt struct {
  427. Value IExpr
  428. }
  429. func (n ExprStmt) String() string {
  430. val := n.Value.String()
  431. if val[0] == '(' && val[len(val)-1] == ')' {
  432. return "Stmt" + n.Value.String()
  433. }
  434. return "Stmt(" + n.Value.String() + ")"
  435. }
  436. // JS writes JavaScript to writer.
  437. func (n ExprStmt) JS(w io.Writer) {
  438. n.Value.JS(w)
  439. w.Write([]byte(";"))
  440. }
  441. // IfStmt is an if statement.
  442. type IfStmt struct {
  443. Cond IExpr
  444. Body IStmt
  445. Else IStmt // can be nil
  446. }
  447. func (n IfStmt) String() string {
  448. s := "Stmt(if " + n.Cond.String() + " " + n.Body.String()
  449. if n.Else != nil {
  450. s += " else " + n.Else.String()
  451. }
  452. return s + ")"
  453. }
  454. // JS writes JavaScript to writer.
  455. func (n IfStmt) JS(w io.Writer) {
  456. w.Write([]byte("if ("))
  457. n.Cond.JS(w)
  458. w.Write([]byte(")"))
  459. if _, ok := n.Body.(*EmptyStmt); !ok {
  460. w.Write([]byte(" "))
  461. }
  462. n.Body.JS(w)
  463. if _, ok := n.Body.(*VarDecl); ok {
  464. w.Write([]byte(";"))
  465. }
  466. if n.Else != nil {
  467. w.Write([]byte(" else"))
  468. if _, ok := n.Else.(*EmptyStmt); !ok {
  469. w.Write([]byte(" "))
  470. }
  471. n.Else.JS(w)
  472. if _, ok := n.Else.(*VarDecl); ok {
  473. w.Write([]byte(";"))
  474. }
  475. }
  476. }
  477. // DoWhileStmt is a do-while iteration statement.
  478. type DoWhileStmt struct {
  479. Cond IExpr
  480. Body IStmt
  481. }
  482. func (n DoWhileStmt) String() string {
  483. return "Stmt(do " + n.Body.String() + " while " + n.Cond.String() + ")"
  484. }
  485. // JS writes JavaScript to writer.
  486. func (n DoWhileStmt) JS(w io.Writer) {
  487. w.Write([]byte("do"))
  488. if _, ok := n.Body.(*EmptyStmt); !ok {
  489. w.Write([]byte(" "))
  490. }
  491. n.Body.JS(w)
  492. if _, ok := n.Body.(*VarDecl); ok {
  493. w.Write([]byte(";"))
  494. }
  495. w.Write([]byte(" while ("))
  496. n.Cond.JS(w)
  497. w.Write([]byte(");"))
  498. }
  499. // WhileStmt is a while iteration statement.
  500. type WhileStmt struct {
  501. Cond IExpr
  502. Body IStmt
  503. }
  504. func (n WhileStmt) String() string {
  505. return "Stmt(while " + n.Cond.String() + " " + n.Body.String() + ")"
  506. }
  507. // JS writes JavaScript to writer.
  508. func (n WhileStmt) JS(w io.Writer) {
  509. w.Write([]byte("while ("))
  510. n.Cond.JS(w)
  511. w.Write([]byte(")"))
  512. if _, ok := n.Body.(*EmptyStmt); !ok {
  513. w.Write([]byte(" "))
  514. }
  515. n.Body.JS(w)
  516. if _, ok := n.Body.(*VarDecl); ok {
  517. w.Write([]byte(";"))
  518. }
  519. }
  520. // ForStmt is a regular for iteration statement.
  521. type ForStmt struct {
  522. Init IExpr // can be nil
  523. Cond IExpr // can be nil
  524. Post IExpr // can be nil
  525. Body *BlockStmt
  526. }
  527. func (n ForStmt) String() string {
  528. s := "Stmt(for"
  529. if v, ok := n.Init.(*VarDecl); !ok && n.Init != nil || ok && len(v.List) != 0 {
  530. s += " " + n.Init.String()
  531. }
  532. s += " ;"
  533. if n.Cond != nil {
  534. s += " " + n.Cond.String()
  535. }
  536. s += " ;"
  537. if n.Post != nil {
  538. s += " " + n.Post.String()
  539. }
  540. return s + " " + n.Body.String() + ")"
  541. }
  542. // JS writes JavaScript to writer.
  543. func (n ForStmt) JS(w io.Writer) {
  544. w.Write([]byte("for ("))
  545. if v, ok := n.Init.(*VarDecl); !ok && n.Init != nil || ok && len(v.List) != 0 {
  546. n.Init.JS(w)
  547. } else {
  548. w.Write([]byte(" "))
  549. }
  550. w.Write([]byte("; "))
  551. if n.Cond != nil {
  552. n.Cond.JS(w)
  553. }
  554. w.Write([]byte("; "))
  555. if n.Post != nil {
  556. n.Post.JS(w)
  557. }
  558. w.Write([]byte(") "))
  559. n.Body.JS(w)
  560. }
  561. // ForInStmt is a for-in iteration statement.
  562. type ForInStmt struct {
  563. Init IExpr
  564. Value IExpr
  565. Body *BlockStmt
  566. }
  567. func (n ForInStmt) String() string {
  568. return "Stmt(for " + n.Init.String() + " in " + n.Value.String() + " " + n.Body.String() + ")"
  569. }
  570. // JS writes JavaScript to writer.
  571. func (n ForInStmt) JS(w io.Writer) {
  572. w.Write([]byte("for ("))
  573. n.Init.JS(w)
  574. w.Write([]byte(" in "))
  575. n.Value.JS(w)
  576. w.Write([]byte(") "))
  577. n.Body.JS(w)
  578. }
  579. // ForOfStmt is a for-of iteration statement.
  580. type ForOfStmt struct {
  581. Await bool
  582. Init IExpr
  583. Value IExpr
  584. Body *BlockStmt
  585. }
  586. func (n ForOfStmt) String() string {
  587. s := "Stmt(for"
  588. if n.Await {
  589. s += " await"
  590. }
  591. return s + " " + n.Init.String() + " of " + n.Value.String() + " " + n.Body.String() + ")"
  592. }
  593. // JS writes JavaScript to writer.
  594. func (n ForOfStmt) JS(w io.Writer) {
  595. w.Write([]byte("for"))
  596. if n.Await {
  597. w.Write([]byte(" await"))
  598. }
  599. w.Write([]byte(" ("))
  600. n.Init.JS(w)
  601. w.Write([]byte(" of "))
  602. n.Value.JS(w)
  603. w.Write([]byte(") "))
  604. n.Body.JS(w)
  605. }
  606. // CaseClause is a case clause or default clause for a switch statement.
  607. type CaseClause struct {
  608. TokenType
  609. Cond IExpr // can be nil
  610. List []IStmt
  611. }
  612. func (n CaseClause) String() string {
  613. s := " Clause(" + n.TokenType.String()
  614. if n.Cond != nil {
  615. s += " " + n.Cond.String()
  616. }
  617. for _, item := range n.List {
  618. s += " " + item.String()
  619. }
  620. return s + ")"
  621. }
  622. // JS writes JavaScript to writer.
  623. func (n CaseClause) JS(w io.Writer) {
  624. if n.Cond != nil {
  625. w.Write([]byte("case "))
  626. n.Cond.JS(w)
  627. } else {
  628. w.Write([]byte("default"))
  629. }
  630. w.Write([]byte(":"))
  631. wi := NewIndenter(w, 4)
  632. for _, item := range n.List {
  633. wi.Write([]byte("\n"))
  634. item.JS(wi)
  635. if _, ok := item.(*VarDecl); ok {
  636. w.Write([]byte(";"))
  637. }
  638. }
  639. }
  640. // SwitchStmt is a switch statement.
  641. type SwitchStmt struct {
  642. Init IExpr
  643. List []CaseClause
  644. Scope
  645. }
  646. func (n SwitchStmt) String() string {
  647. s := "Stmt(switch " + n.Init.String()
  648. for _, clause := range n.List {
  649. s += clause.String()
  650. }
  651. return s + ")"
  652. }
  653. // JS writes JavaScript to writer.
  654. func (n SwitchStmt) JS(w io.Writer) {
  655. w.Write([]byte("switch ("))
  656. n.Init.JS(w)
  657. if len(n.List) == 0 {
  658. w.Write([]byte(") {}"))
  659. return
  660. }
  661. w.Write([]byte(") {"))
  662. for _, clause := range n.List {
  663. w.Write([]byte("\n"))
  664. clause.JS(w)
  665. }
  666. w.Write([]byte("\n}"))
  667. }
  668. // BranchStmt is a continue or break statement.
  669. type BranchStmt struct {
  670. Type TokenType
  671. Label []byte // can be nil
  672. }
  673. func (n BranchStmt) String() string {
  674. s := "Stmt(" + n.Type.String()
  675. if n.Label != nil {
  676. s += " " + string(n.Label)
  677. }
  678. return s + ")"
  679. }
  680. // JS writes JavaScript to writer.
  681. func (n BranchStmt) JS(w io.Writer) {
  682. w.Write(n.Type.Bytes())
  683. if n.Label != nil {
  684. w.Write([]byte(" "))
  685. w.Write(n.Label)
  686. }
  687. w.Write([]byte(";"))
  688. }
  689. // ReturnStmt is a return statement.
  690. type ReturnStmt struct {
  691. Value IExpr // can be nil
  692. }
  693. func (n ReturnStmt) String() string {
  694. s := "Stmt(return"
  695. if n.Value != nil {
  696. s += " " + n.Value.String()
  697. }
  698. return s + ")"
  699. }
  700. // JS writes JavaScript to writer.
  701. func (n ReturnStmt) JS(w io.Writer) {
  702. w.Write([]byte("return"))
  703. if n.Value != nil {
  704. w.Write([]byte(" "))
  705. n.Value.JS(w)
  706. }
  707. w.Write([]byte(";"))
  708. }
  709. // WithStmt is a with statement.
  710. type WithStmt struct {
  711. Cond IExpr
  712. Body IStmt
  713. }
  714. func (n WithStmt) String() string {
  715. return "Stmt(with " + n.Cond.String() + " " + n.Body.String() + ")"
  716. }
  717. // JS writes JavaScript to writer.
  718. func (n WithStmt) JS(w io.Writer) {
  719. w.Write([]byte("with ("))
  720. n.Cond.JS(w)
  721. w.Write([]byte(")"))
  722. if _, ok := n.Body.(*EmptyStmt); !ok {
  723. w.Write([]byte(" "))
  724. }
  725. n.Body.JS(w)
  726. if _, ok := n.Body.(*VarDecl); ok {
  727. w.Write([]byte(";"))
  728. }
  729. }
  730. // LabelledStmt is a labelled statement.
  731. type LabelledStmt struct {
  732. Label []byte
  733. Value IStmt
  734. }
  735. func (n LabelledStmt) String() string {
  736. return "Stmt(" + string(n.Label) + " : " + n.Value.String() + ")"
  737. }
  738. // JS writes JavaScript to writer.
  739. func (n LabelledStmt) JS(w io.Writer) {
  740. w.Write(n.Label)
  741. w.Write([]byte(":"))
  742. if _, ok := n.Value.(*EmptyStmt); !ok {
  743. w.Write([]byte(" "))
  744. }
  745. n.Value.JS(w)
  746. if _, ok := n.Value.(*VarDecl); ok {
  747. w.Write([]byte(";"))
  748. }
  749. }
  750. // ThrowStmt is a throw statement.
  751. type ThrowStmt struct {
  752. Value IExpr
  753. }
  754. func (n ThrowStmt) String() string {
  755. return "Stmt(throw " + n.Value.String() + ")"
  756. }
  757. // JS writes JavaScript to writer.
  758. func (n ThrowStmt) JS(w io.Writer) {
  759. w.Write([]byte("throw "))
  760. n.Value.JS(w)
  761. w.Write([]byte(";"))
  762. }
  763. // TryStmt is a try statement.
  764. type TryStmt struct {
  765. Body *BlockStmt
  766. Binding IBinding // can be nil
  767. Catch *BlockStmt // can be nil
  768. Finally *BlockStmt // can be nil
  769. }
  770. func (n TryStmt) String() string {
  771. s := "Stmt(try " + n.Body.String()
  772. if n.Catch != nil {
  773. s += " catch"
  774. if n.Binding != nil {
  775. s += " Binding(" + n.Binding.String() + ")"
  776. }
  777. s += " " + n.Catch.String()
  778. }
  779. if n.Finally != nil {
  780. s += " finally " + n.Finally.String()
  781. }
  782. return s + ")"
  783. }
  784. // JS writes JavaScript to writer.
  785. func (n TryStmt) JS(w io.Writer) {
  786. w.Write([]byte("try "))
  787. n.Body.JS(w)
  788. if n.Catch != nil {
  789. w.Write([]byte(" catch"))
  790. if n.Binding != nil {
  791. w.Write([]byte("("))
  792. n.Binding.JS(w)
  793. w.Write([]byte(")"))
  794. }
  795. w.Write([]byte(" "))
  796. n.Catch.JS(w)
  797. }
  798. if n.Finally != nil {
  799. w.Write([]byte(" finally "))
  800. n.Finally.JS(w)
  801. }
  802. }
  803. // DebuggerStmt is a debugger statement.
  804. type DebuggerStmt struct{}
  805. func (n DebuggerStmt) String() string {
  806. return "Stmt(debugger)"
  807. }
  808. // JS writes JavaScript to writer.
  809. func (n DebuggerStmt) JS(w io.Writer) {
  810. w.Write([]byte("debugger;"))
  811. }
  812. // Alias is a name space import or import/export specifier for import/export statements.
  813. type Alias struct {
  814. Name []byte // can be nil
  815. Binding []byte // can be nil
  816. }
  817. func (alias Alias) String() string {
  818. s := ""
  819. if alias.Name != nil {
  820. s += string(alias.Name) + " as "
  821. }
  822. return s + string(alias.Binding)
  823. }
  824. // JS writes JavaScript to writer.
  825. func (alias Alias) JS(w io.Writer) {
  826. if alias.Name != nil {
  827. w.Write(alias.Name)
  828. w.Write([]byte(" as "))
  829. }
  830. w.Write(alias.Binding)
  831. }
  832. // ImportStmt is an import statement.
  833. type ImportStmt struct {
  834. List []Alias
  835. Default []byte // can be nil
  836. Module []byte
  837. }
  838. func (n ImportStmt) String() string {
  839. s := "Stmt(import"
  840. if n.Default != nil {
  841. s += " " + string(n.Default)
  842. if n.List != nil {
  843. s += " ,"
  844. }
  845. }
  846. if len(n.List) == 1 && len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' {
  847. s += " " + n.List[0].String()
  848. } else if n.List != nil {
  849. s += " {"
  850. for i, item := range n.List {
  851. if i != 0 {
  852. s += " ,"
  853. }
  854. if item.Binding != nil {
  855. s += " " + item.String()
  856. }
  857. }
  858. s += " }"
  859. }
  860. if n.Default != nil || n.List != nil {
  861. s += " from"
  862. }
  863. return s + " " + string(n.Module) + ")"
  864. }
  865. // JS writes JavaScript to writer.
  866. func (n ImportStmt) JS(w io.Writer) {
  867. w.Write([]byte("import"))
  868. if n.Default != nil {
  869. w.Write([]byte(" "))
  870. w.Write(n.Default)
  871. if n.List != nil {
  872. w.Write([]byte(","))
  873. }
  874. }
  875. if len(n.List) == 1 && len(n.List[0].Name) == 1 && n.List[0].Name[0] == '*' {
  876. w.Write([]byte(" "))
  877. n.List[0].JS(w)
  878. } else if n.List != nil {
  879. if len(n.List) == 0 {
  880. w.Write([]byte(" {}"))
  881. } else {
  882. w.Write([]byte(" {"))
  883. for j, item := range n.List {
  884. if j != 0 {
  885. w.Write([]byte(","))
  886. }
  887. if item.Binding != nil {
  888. w.Write([]byte(" "))
  889. item.JS(w)
  890. }
  891. }
  892. w.Write([]byte(" }"))
  893. }
  894. }
  895. if n.Default != nil || n.List != nil {
  896. w.Write([]byte(" from"))
  897. }
  898. w.Write([]byte(" "))
  899. w.Write(n.Module)
  900. w.Write([]byte(";"))
  901. }
  902. // ExportStmt is an export statement.
  903. type ExportStmt struct {
  904. List []Alias
  905. Module []byte // can be nil
  906. Default bool
  907. Decl IExpr
  908. }
  909. func (n ExportStmt) String() string {
  910. s := "Stmt(export"
  911. if n.Decl != nil {
  912. if n.Default {
  913. s += " default"
  914. }
  915. return s + " " + n.Decl.String() + ")"
  916. } 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] == '*') {
  917. s += " " + n.List[0].String()
  918. } else if 0 < len(n.List) {
  919. s += " {"
  920. for i, item := range n.List {
  921. if i != 0 {
  922. s += " ,"
  923. }
  924. if item.Binding != nil {
  925. s += " " + item.String()
  926. }
  927. }
  928. s += " }"
  929. }
  930. if n.Module != nil {
  931. s += " from " + string(n.Module)
  932. }
  933. return s + ")"
  934. }
  935. // JS writes JavaScript to writer.
  936. func (n ExportStmt) JS(w io.Writer) {
  937. w.Write([]byte("export"))
  938. if n.Decl != nil {
  939. if n.Default {
  940. w.Write([]byte(" default"))
  941. }
  942. w.Write([]byte(" "))
  943. n.Decl.JS(w)
  944. w.Write([]byte(";"))
  945. return
  946. } 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] == '*') {
  947. w.Write([]byte(" "))
  948. n.List[0].JS(w)
  949. } else if len(n.List) == 0 {
  950. w.Write([]byte(" {}"))
  951. } else {
  952. w.Write([]byte(" {"))
  953. for j, item := range n.List {
  954. if j != 0 {
  955. w.Write([]byte(","))
  956. }
  957. if item.Binding != nil {
  958. w.Write([]byte(" "))
  959. item.JS(w)
  960. }
  961. }
  962. w.Write([]byte(" }"))
  963. }
  964. if n.Module != nil {
  965. w.Write([]byte(" from "))
  966. w.Write(n.Module)
  967. }
  968. w.Write([]byte(";"))
  969. }
  970. // DirectivePrologueStmt is a string literal at the beginning of a function or module (usually "use strict").
  971. type DirectivePrologueStmt struct {
  972. Value []byte
  973. }
  974. func (n DirectivePrologueStmt) String() string {
  975. return "Stmt(" + string(n.Value) + ")"
  976. }
  977. // JS writes JavaScript to writer.
  978. func (n DirectivePrologueStmt) JS(w io.Writer) {
  979. w.Write(n.Value)
  980. w.Write([]byte(";"))
  981. }
  982. func (n Comment) stmtNode() {}
  983. func (n BlockStmt) stmtNode() {}
  984. func (n EmptyStmt) stmtNode() {}
  985. func (n ExprStmt) stmtNode() {}
  986. func (n IfStmt) stmtNode() {}
  987. func (n DoWhileStmt) stmtNode() {}
  988. func (n WhileStmt) stmtNode() {}
  989. func (n ForStmt) stmtNode() {}
  990. func (n ForInStmt) stmtNode() {}
  991. func (n ForOfStmt) stmtNode() {}
  992. func (n SwitchStmt) stmtNode() {}
  993. func (n BranchStmt) stmtNode() {}
  994. func (n ReturnStmt) stmtNode() {}
  995. func (n WithStmt) stmtNode() {}
  996. func (n LabelledStmt) stmtNode() {}
  997. func (n ThrowStmt) stmtNode() {}
  998. func (n TryStmt) stmtNode() {}
  999. func (n DebuggerStmt) stmtNode() {}
  1000. func (n ImportStmt) stmtNode() {}
  1001. func (n ExportStmt) stmtNode() {}
  1002. func (n DirectivePrologueStmt) stmtNode() {}
  1003. ////////////////////////////////////////////////////////////////
  1004. // PropertyName is a property name for binding properties, method names, and in object literals.
  1005. type PropertyName struct {
  1006. Literal LiteralExpr
  1007. Computed IExpr // can be nil
  1008. }
  1009. // IsSet returns true is PropertyName is not nil.
  1010. func (n PropertyName) IsSet() bool {
  1011. return n.IsComputed() || n.Literal.TokenType != ErrorToken
  1012. }
  1013. // IsComputed returns true if PropertyName is computed.
  1014. func (n PropertyName) IsComputed() bool {
  1015. return n.Computed != nil
  1016. }
  1017. // IsIdent returns true if PropertyName equals the given identifier name.
  1018. func (n PropertyName) IsIdent(data []byte) bool {
  1019. return !n.IsComputed() && n.Literal.TokenType == IdentifierToken && bytes.Equal(data, n.Literal.Data)
  1020. }
  1021. func (n PropertyName) String() string {
  1022. if n.Computed != nil {
  1023. val := n.Computed.String()
  1024. if val[0] == '(' {
  1025. return "[" + val[1:len(val)-1] + "]"
  1026. }
  1027. return "[" + val + "]"
  1028. }
  1029. return string(n.Literal.Data)
  1030. }
  1031. // JS writes JavaScript to writer.
  1032. func (n PropertyName) JS(w io.Writer) {
  1033. if n.Computed != nil {
  1034. w.Write([]byte("["))
  1035. n.Computed.JS(w)
  1036. w.Write([]byte("]"))
  1037. return
  1038. }
  1039. w.Write(n.Literal.Data)
  1040. }
  1041. // BindingArray is an array binding pattern.
  1042. type BindingArray struct {
  1043. List []BindingElement
  1044. Rest IBinding // can be nil
  1045. }
  1046. func (n BindingArray) String() string {
  1047. s := "["
  1048. for i, item := range n.List {
  1049. if i != 0 {
  1050. s += ","
  1051. }
  1052. s += " " + item.String()
  1053. }
  1054. if n.Rest != nil {
  1055. if len(n.List) != 0 {
  1056. s += ","
  1057. }
  1058. s += " ...Binding(" + n.Rest.String() + ")"
  1059. }
  1060. return s + " ]"
  1061. }
  1062. // JS writes JavaScript to writer.
  1063. func (n BindingArray) JS(w io.Writer) {
  1064. w.Write([]byte("["))
  1065. for j, item := range n.List {
  1066. if j != 0 {
  1067. w.Write([]byte(", "))
  1068. }
  1069. item.JS(w)
  1070. }
  1071. if n.Rest != nil {
  1072. if len(n.List) != 0 {
  1073. w.Write([]byte(", "))
  1074. }
  1075. w.Write([]byte("..."))
  1076. n.Rest.JS(w)
  1077. }
  1078. w.Write([]byte("]"))
  1079. }
  1080. // BindingObjectItem is a binding property.
  1081. type BindingObjectItem struct {
  1082. Key *PropertyName // can be nil
  1083. Value BindingElement
  1084. }
  1085. func (n BindingObjectItem) String() string {
  1086. s := ""
  1087. if n.Key != nil {
  1088. if v, ok := n.Value.Binding.(*Var); !ok || !n.Key.IsIdent(v.Data) {
  1089. s += " " + n.Key.String() + ":"
  1090. }
  1091. }
  1092. return s + " " + n.Value.String()
  1093. }
  1094. // JS writes JavaScript to writer.
  1095. func (n BindingObjectItem) JS(w io.Writer) {
  1096. if n.Key != nil {
  1097. if v, ok := n.Value.Binding.(*Var); !ok || !n.Key.IsIdent(v.Data) {
  1098. n.Key.JS(w)
  1099. w.Write([]byte(": "))
  1100. }
  1101. }
  1102. n.Value.JS(w)
  1103. }
  1104. // BindingObject is an object binding pattern.
  1105. type BindingObject struct {
  1106. List []BindingObjectItem
  1107. Rest *Var // can be nil
  1108. }
  1109. func (n BindingObject) String() string {
  1110. s := "{"
  1111. for i, item := range n.List {
  1112. if i != 0 {
  1113. s += ","
  1114. }
  1115. s += item.String()
  1116. }
  1117. if n.Rest != nil {
  1118. if len(n.List) != 0 {
  1119. s += ","
  1120. }
  1121. s += " ...Binding(" + string(n.Rest.Data) + ")"
  1122. }
  1123. return s + " }"
  1124. }
  1125. // JS writes JavaScript to writer.
  1126. func (n BindingObject) JS(w io.Writer) {
  1127. w.Write([]byte("{"))
  1128. for j, item := range n.List {
  1129. if j != 0 {
  1130. w.Write([]byte(", "))
  1131. }
  1132. item.JS(w)
  1133. }
  1134. if n.Rest != nil {
  1135. if len(n.List) != 0 {
  1136. w.Write([]byte(", "))
  1137. }
  1138. w.Write([]byte("..."))
  1139. w.Write(n.Rest.Data)
  1140. }
  1141. w.Write([]byte("}"))
  1142. }
  1143. // BindingElement is a binding element.
  1144. type BindingElement struct {
  1145. Binding IBinding // can be nil (in case of ellision)
  1146. Default IExpr // can be nil
  1147. }
  1148. func (n BindingElement) String() string {
  1149. if n.Binding == nil {
  1150. return "Binding()"
  1151. }
  1152. s := "Binding(" + n.Binding.String()
  1153. if n.Default != nil {
  1154. s += " = " + n.Default.String()
  1155. }
  1156. return s + ")"
  1157. }
  1158. // JS writes JavaScript to writer.
  1159. func (n BindingElement) JS(w io.Writer) {
  1160. if n.Binding == nil {
  1161. return
  1162. }
  1163. n.Binding.JS(w)
  1164. if n.Default != nil {
  1165. w.Write([]byte(" = "))
  1166. n.Default.JS(w)
  1167. }
  1168. }
  1169. func (v *Var) bindingNode() {}
  1170. func (n BindingArray) bindingNode() {}
  1171. func (n BindingObject) bindingNode() {}
  1172. ////////////////////////////////////////////////////////////////
  1173. // VarDecl is a variable statement or lexical declaration.
  1174. type VarDecl struct {
  1175. TokenType
  1176. List []BindingElement
  1177. Scope *Scope
  1178. InFor, InForInOf bool
  1179. }
  1180. func (n VarDecl) String() string {
  1181. s := "Decl(" + n.TokenType.String()
  1182. for _, item := range n.List {
  1183. s += " " + item.String()
  1184. }
  1185. return s + ")"
  1186. }
  1187. // JS writes JavaScript to writer.
  1188. func (n VarDecl) JS(w io.Writer) {
  1189. w.Write(n.TokenType.Bytes())
  1190. for j, item := range n.List {
  1191. if j != 0 {
  1192. w.Write([]byte(","))
  1193. }
  1194. w.Write([]byte(" "))
  1195. item.JS(w)
  1196. }
  1197. }
  1198. // Params is a list of parameters for functions, methods, and arrow function.
  1199. type Params struct {
  1200. List []BindingElement
  1201. Rest IBinding // can be nil
  1202. }
  1203. func (n Params) String() string {
  1204. s := "Params("
  1205. for i, item := range n.List {
  1206. if i != 0 {
  1207. s += ", "
  1208. }
  1209. s += item.String()
  1210. }
  1211. if n.Rest != nil {
  1212. if len(n.List) != 0 {
  1213. s += ", "
  1214. }
  1215. s += "...Binding(" + n.Rest.String() + ")"
  1216. }
  1217. return s + ")"
  1218. }
  1219. // JS writes JavaScript to writer.
  1220. func (n Params) JS(w io.Writer) {
  1221. w.Write([]byte("("))
  1222. for j, item := range n.List {
  1223. if j != 0 {
  1224. w.Write([]byte(", "))
  1225. }
  1226. item.JS(w)
  1227. }
  1228. if n.Rest != nil {
  1229. if len(n.List) != 0 {
  1230. w.Write([]byte(", "))
  1231. }
  1232. w.Write([]byte("..."))
  1233. n.Rest.JS(w)
  1234. }
  1235. w.Write([]byte(")"))
  1236. }
  1237. // FuncDecl is an (async) (generator) function declaration or expression.
  1238. type FuncDecl struct {
  1239. Async bool
  1240. Generator bool
  1241. Name *Var // can be nil
  1242. Params Params
  1243. Body BlockStmt
  1244. }
  1245. func (n FuncDecl) String() string {
  1246. s := "Decl("
  1247. if n.Async {
  1248. s += "async function"
  1249. } else {
  1250. s += "function"
  1251. }
  1252. if n.Generator {
  1253. s += "*"
  1254. }
  1255. if n.Name != nil {
  1256. s += " " + string(n.Name.Data)
  1257. }
  1258. return s + " " + n.Params.String() + " " + n.Body.String() + ")"
  1259. }
  1260. // JS writes JavaScript to writer.
  1261. func (n FuncDecl) JS(w io.Writer) {
  1262. if n.Async {
  1263. w.Write([]byte("async function"))
  1264. } else {
  1265. w.Write([]byte("function"))
  1266. }
  1267. if n.Generator {
  1268. w.Write([]byte("*"))
  1269. }
  1270. if n.Name != nil {
  1271. w.Write([]byte(" "))
  1272. w.Write(n.Name.Data)
  1273. }
  1274. n.Params.JS(w)
  1275. w.Write([]byte(" "))
  1276. n.Body.JS(w)
  1277. }
  1278. // MethodDecl is a method definition in a class declaration.
  1279. type MethodDecl struct {
  1280. Static bool
  1281. Async bool
  1282. Generator bool
  1283. Get bool
  1284. Set bool
  1285. Name PropertyName
  1286. Params Params
  1287. Body BlockStmt
  1288. }
  1289. func (n MethodDecl) String() string {
  1290. s := ""
  1291. if n.Static {
  1292. s += " static"
  1293. }
  1294. if n.Async {
  1295. s += " async"
  1296. }
  1297. if n.Generator {
  1298. s += " *"
  1299. }
  1300. if n.Get {
  1301. s += " get"
  1302. }
  1303. if n.Set {
  1304. s += " set"
  1305. }
  1306. s += " " + n.Name.String() + " " + n.Params.String() + " " + n.Body.String()
  1307. return "Method(" + s[1:] + ")"
  1308. }
  1309. // JS writes JavaScript to writer.
  1310. func (n MethodDecl) JS(w io.Writer) {
  1311. writen := false
  1312. if n.Static {
  1313. w.Write([]byte("static"))
  1314. writen = true
  1315. }
  1316. if n.Async {
  1317. if writen {
  1318. w.Write([]byte(" "))
  1319. }
  1320. w.Write([]byte("async"))
  1321. writen = true
  1322. }
  1323. if n.Generator {
  1324. if writen {
  1325. w.Write([]byte(" "))
  1326. }
  1327. w.Write([]byte("*"))
  1328. writen = true
  1329. }
  1330. if n.Get {
  1331. if writen {
  1332. w.Write([]byte(" "))
  1333. }
  1334. w.Write([]byte("get"))
  1335. writen = true
  1336. }
  1337. if n.Set {
  1338. if writen {
  1339. w.Write([]byte(" "))
  1340. }
  1341. w.Write([]byte("set"))
  1342. writen = true
  1343. }
  1344. if writen {
  1345. w.Write([]byte(" "))
  1346. }
  1347. n.Name.JS(w)
  1348. w.Write([]byte(" "))
  1349. n.Params.JS(w)
  1350. w.Write([]byte(" "))
  1351. n.Body.JS(w)
  1352. }
  1353. // Field is a field definition in a class declaration.
  1354. type Field struct {
  1355. Static bool
  1356. Name PropertyName
  1357. Init IExpr
  1358. }
  1359. func (n Field) String() string {
  1360. s := "Field("
  1361. if n.Static {
  1362. s += "static "
  1363. }
  1364. s += n.Name.String()
  1365. if n.Init != nil {
  1366. s += " = " + n.Init.String()
  1367. }
  1368. return s + ")"
  1369. }
  1370. // JS writes JavaScript to writer.
  1371. func (n Field) JS(w io.Writer) {
  1372. if n.Static {
  1373. w.Write([]byte("static "))
  1374. }
  1375. n.Name.JS(w)
  1376. if n.Init != nil {
  1377. w.Write([]byte(" = "))
  1378. n.Init.JS(w)
  1379. }
  1380. }
  1381. // ClassElement is a class element that is either a static block, a field definition, or a class method
  1382. type ClassElement struct {
  1383. StaticBlock *BlockStmt // can be nil
  1384. Method *MethodDecl // can be nil
  1385. Field
  1386. }
  1387. func (n ClassElement) String() string {
  1388. if n.StaticBlock != nil {
  1389. return "Static(" + n.StaticBlock.String() + ")"
  1390. } else if n.Method != nil {
  1391. return n.Method.String()
  1392. }
  1393. return n.Field.String()
  1394. }
  1395. // JS writes JavaScript to writer.
  1396. func (n ClassElement) JS(w io.Writer) {
  1397. if n.StaticBlock != nil {
  1398. w.Write([]byte("static "))
  1399. n.StaticBlock.JS(w)
  1400. return
  1401. } else if n.Method != nil {
  1402. n.Method.JS(w)
  1403. return
  1404. }
  1405. n.Field.JS(w)
  1406. w.Write([]byte(";"))
  1407. }
  1408. // ClassDecl is a class declaration.
  1409. type ClassDecl struct {
  1410. Name *Var // can be nil
  1411. Extends IExpr // can be nil
  1412. List []ClassElement
  1413. }
  1414. func (n ClassDecl) String() string {
  1415. s := "Decl(class"
  1416. if n.Name != nil {
  1417. s += " " + string(n.Name.Data)
  1418. }
  1419. if n.Extends != nil {
  1420. s += " extends " + n.Extends.String()
  1421. }
  1422. for _, item := range n.List {
  1423. s += " " + item.String()
  1424. }
  1425. return s + ")"
  1426. }
  1427. // JS writes JavaScript to writer.
  1428. func (n ClassDecl) JS(w io.Writer) {
  1429. w.Write([]byte("class"))
  1430. if n.Name != nil {
  1431. w.Write([]byte(" "))
  1432. w.Write(n.Name.Data)
  1433. }
  1434. if n.Extends != nil {
  1435. w.Write([]byte(" extends "))
  1436. n.Extends.JS(w)
  1437. }
  1438. if len(n.List) == 0 {
  1439. w.Write([]byte(" {}"))
  1440. return
  1441. }
  1442. w.Write([]byte(" {"))
  1443. wi := NewIndenter(w, 4)
  1444. for _, item := range n.List {
  1445. wi.Write([]byte("\n"))
  1446. item.JS(wi)
  1447. }
  1448. w.Write([]byte("\n}"))
  1449. }
  1450. func (n VarDecl) stmtNode() {}
  1451. func (n FuncDecl) stmtNode() {}
  1452. func (n ClassDecl) stmtNode() {}
  1453. func (n VarDecl) exprNode() {} // not a real IExpr, used for ForInit and ExportDecl
  1454. func (n FuncDecl) exprNode() {}
  1455. func (n ClassDecl) exprNode() {}
  1456. func (n MethodDecl) exprNode() {} // not a real IExpr, used for ObjectExpression PropertyName
  1457. ////////////////////////////////////////////////////////////////
  1458. // LiteralExpr can be this, null, boolean, numeric, string, or regular expression literals.
  1459. type LiteralExpr struct {
  1460. TokenType
  1461. Data []byte
  1462. }
  1463. func (n LiteralExpr) String() string {
  1464. return string(n.Data)
  1465. }
  1466. // JS writes JavaScript to writer.
  1467. func (n LiteralExpr) JS(w io.Writer) {
  1468. w.Write(n.Data)
  1469. }
  1470. // JSON writes JSON to writer.
  1471. func (n LiteralExpr) JSON(w io.Writer) error {
  1472. if n.TokenType == TrueToken || n.TokenType == FalseToken || n.TokenType == NullToken || n.TokenType == DecimalToken {
  1473. w.Write(n.Data)
  1474. return nil
  1475. } else if n.TokenType == StringToken {
  1476. data := n.Data
  1477. if n.Data[0] == '\'' {
  1478. data = parse.Copy(data)
  1479. data = bytes.ReplaceAll(data, []byte(`\'`), []byte(`'`))
  1480. data = bytes.ReplaceAll(data, []byte(`"`), []byte(`\"`))
  1481. data[0] = '"'
  1482. data[len(data)-1] = '"'
  1483. }
  1484. w.Write(data)
  1485. return nil
  1486. }
  1487. return ErrInvalidJSON
  1488. }
  1489. // Element is an array literal element.
  1490. type Element struct {
  1491. Value IExpr // can be nil
  1492. Spread bool
  1493. }
  1494. func (n Element) String() string {
  1495. s := ""
  1496. if n.Value != nil {
  1497. if n.Spread {
  1498. s += "..."
  1499. }
  1500. s += n.Value.String()
  1501. }
  1502. return s
  1503. }
  1504. // JS writes JavaScript to writer.
  1505. func (n Element) JS(w io.Writer) {
  1506. if n.Value != nil {
  1507. if n.Spread {
  1508. w.Write([]byte("..."))
  1509. }
  1510. n.Value.JS(w)
  1511. }
  1512. }
  1513. // ArrayExpr is an array literal.
  1514. type ArrayExpr struct {
  1515. List []Element
  1516. }
  1517. func (n ArrayExpr) String() string {
  1518. s := "["
  1519. for i, item := range n.List {
  1520. if i != 0 {
  1521. s += ", "
  1522. }
  1523. if item.Value != nil {
  1524. if item.Spread {
  1525. s += "..."
  1526. }
  1527. s += item.Value.String()
  1528. }
  1529. }
  1530. if 0 < len(n.List) && n.List[len(n.List)-1].Value == nil {
  1531. s += ","
  1532. }
  1533. return s + "]"
  1534. }
  1535. // JS writes JavaScript to writer.
  1536. func (n ArrayExpr) JS(w io.Writer) {
  1537. w.Write([]byte("["))
  1538. for j, item := range n.List {
  1539. if j != 0 {
  1540. w.Write([]byte(", "))
  1541. }
  1542. if item.Value != nil {
  1543. if item.Spread {
  1544. w.Write([]byte("..."))
  1545. }
  1546. item.Value.JS(w)
  1547. }
  1548. }
  1549. if 0 < len(n.List) && n.List[len(n.List)-1].Value == nil {
  1550. w.Write([]byte(","))
  1551. }
  1552. w.Write([]byte("]"))
  1553. }
  1554. // JSON writes JSON to writer.
  1555. func (n ArrayExpr) JSON(w io.Writer) error {
  1556. w.Write([]byte("["))
  1557. for i, item := range n.List {
  1558. if i != 0 {
  1559. w.Write([]byte(", "))
  1560. }
  1561. if item.Value == nil || item.Spread {
  1562. return ErrInvalidJSON
  1563. }
  1564. if val, ok := item.Value.(JSONer); !ok {
  1565. return ErrInvalidJSON
  1566. } else if err := val.JSON(w); err != nil {
  1567. return err
  1568. }
  1569. }
  1570. w.Write([]byte("]"))
  1571. return nil
  1572. }
  1573. // Property is a property definition in an object literal.
  1574. type Property struct {
  1575. // either Name or Spread are set. When Spread is set then Value is AssignmentExpression
  1576. // if Init is set then Value is IdentifierReference, otherwise it can also be MethodDefinition
  1577. Name *PropertyName // can be nil
  1578. Spread bool
  1579. Value IExpr
  1580. Init IExpr // can be nil
  1581. }
  1582. func (n Property) String() string {
  1583. s := ""
  1584. if n.Name != nil {
  1585. if v, ok := n.Value.(*Var); !ok || !n.Name.IsIdent(v.Data) {
  1586. s += n.Name.String() + ": "
  1587. }
  1588. } else if n.Spread {
  1589. s += "..."
  1590. }
  1591. s += n.Value.String()
  1592. if n.Init != nil {
  1593. s += " = " + n.Init.String()
  1594. }
  1595. return s
  1596. }
  1597. // JS writes JavaScript to writer.
  1598. func (n Property) JS(w io.Writer) {
  1599. if n.Name != nil {
  1600. if v, ok := n.Value.(*Var); !ok || !n.Name.IsIdent(v.Data) {
  1601. n.Name.JS(w)
  1602. w.Write([]byte(": "))
  1603. }
  1604. } else if n.Spread {
  1605. w.Write([]byte("..."))
  1606. }
  1607. n.Value.JS(w)
  1608. if n.Init != nil {
  1609. w.Write([]byte(" = "))
  1610. n.Init.JS(w)
  1611. }
  1612. }
  1613. // JSON writes JSON to writer.
  1614. func (n Property) JSON(w io.Writer) error {
  1615. if n.Name == nil || n.Name.Literal.TokenType != StringToken && n.Name.Literal.TokenType != IdentifierToken || n.Spread || n.Init != nil {
  1616. return ErrInvalidJSON
  1617. } else if n.Name.Literal.TokenType == IdentifierToken {
  1618. w.Write([]byte(`"`))
  1619. w.Write(n.Name.Literal.Data)
  1620. w.Write([]byte(`"`))
  1621. } else {
  1622. _ = n.Name.Literal.JSON(w)
  1623. }
  1624. w.Write([]byte(": "))
  1625. if val, ok := n.Value.(JSONer); !ok {
  1626. return ErrInvalidJSON
  1627. } else if err := val.JSON(w); err != nil {
  1628. return err
  1629. }
  1630. return nil
  1631. }
  1632. // ObjectExpr is an object literal.
  1633. type ObjectExpr struct {
  1634. List []Property
  1635. }
  1636. func (n ObjectExpr) String() string {
  1637. s := "{"
  1638. for i, item := range n.List {
  1639. if i != 0 {
  1640. s += ", "
  1641. }
  1642. s += item.String()
  1643. }
  1644. return s + "}"
  1645. }
  1646. // JS writes JavaScript to writer.
  1647. func (n ObjectExpr) JS(w io.Writer) {
  1648. w.Write([]byte("{"))
  1649. for j, item := range n.List {
  1650. if j != 0 {
  1651. w.Write([]byte(", "))
  1652. }
  1653. item.JS(w)
  1654. }
  1655. w.Write([]byte("}"))
  1656. }
  1657. // JSON writes JSON to writer.
  1658. func (n ObjectExpr) JSON(w io.Writer) error {
  1659. w.Write([]byte("{"))
  1660. for i, item := range n.List {
  1661. if i != 0 {
  1662. w.Write([]byte(", "))
  1663. }
  1664. if err := item.JSON(w); err != nil {
  1665. return err
  1666. }
  1667. }
  1668. w.Write([]byte("}"))
  1669. return nil
  1670. }
  1671. // TemplatePart is a template head or middle.
  1672. type TemplatePart struct {
  1673. Value []byte
  1674. Expr IExpr
  1675. }
  1676. func (n TemplatePart) String() string {
  1677. return string(n.Value) + n.Expr.String()
  1678. }
  1679. // JS writes JavaScript to writer.
  1680. func (n TemplatePart) JS(w io.Writer) {
  1681. w.Write(n.Value)
  1682. n.Expr.JS(w)
  1683. }
  1684. // TemplateExpr is a template literal or member/call expression, super property, or optional chain with template literal.
  1685. type TemplateExpr struct {
  1686. Tag IExpr // can be nil
  1687. List []TemplatePart
  1688. Tail []byte
  1689. Prec OpPrec
  1690. Optional bool
  1691. }
  1692. func (n TemplateExpr) String() string {
  1693. s := ""
  1694. if n.Tag != nil {
  1695. s += n.Tag.String()
  1696. if n.Optional {
  1697. s += "?."
  1698. }
  1699. }
  1700. for _, item := range n.List {
  1701. s += item.String()
  1702. }
  1703. return s + string(n.Tail)
  1704. }
  1705. // JS writes JavaScript to writer.
  1706. func (n TemplateExpr) JS(w io.Writer) {
  1707. if n.Tag != nil {
  1708. n.Tag.JS(w)
  1709. if n.Optional {
  1710. w.Write([]byte("?."))
  1711. }
  1712. }
  1713. for _, item := range n.List {
  1714. item.JS(w)
  1715. }
  1716. w.Write(n.Tail)
  1717. }
  1718. // GroupExpr is a parenthesized expression.
  1719. type GroupExpr struct {
  1720. X IExpr
  1721. }
  1722. func (n GroupExpr) String() string {
  1723. return "(" + n.X.String() + ")"
  1724. }
  1725. // JS writes JavaScript to writer.
  1726. func (n GroupExpr) JS(w io.Writer) {
  1727. w.Write([]byte("("))
  1728. n.X.JS(w)
  1729. w.Write([]byte(")"))
  1730. }
  1731. // IndexExpr is a member/call expression, super property, or optional chain with an index expression.
  1732. type IndexExpr struct {
  1733. X IExpr
  1734. Y IExpr
  1735. Prec OpPrec
  1736. Optional bool
  1737. }
  1738. func (n IndexExpr) String() string {
  1739. if n.Optional {
  1740. return "(" + n.X.String() + "?.[" + n.Y.String() + "])"
  1741. }
  1742. return "(" + n.X.String() + "[" + n.Y.String() + "])"
  1743. }
  1744. // JS writes JavaScript to writer.
  1745. func (n IndexExpr) JS(w io.Writer) {
  1746. n.X.JS(w)
  1747. if n.Optional {
  1748. w.Write([]byte("?.["))
  1749. } else {
  1750. w.Write([]byte("["))
  1751. }
  1752. n.Y.JS(w)
  1753. w.Write([]byte("]"))
  1754. }
  1755. // DotExpr is a member/call expression, super property, or optional chain with a dot expression.
  1756. type DotExpr struct {
  1757. X IExpr
  1758. Y LiteralExpr
  1759. Prec OpPrec
  1760. Optional bool
  1761. }
  1762. func (n DotExpr) String() string {
  1763. if n.Optional {
  1764. return "(" + n.X.String() + "?." + n.Y.String() + ")"
  1765. }
  1766. return "(" + n.X.String() + "." + n.Y.String() + ")"
  1767. }
  1768. // JS writes JavaScript to writer.
  1769. func (n DotExpr) JS(w io.Writer) {
  1770. lit, ok := n.X.(*LiteralExpr)
  1771. group := ok && !n.Optional && lit.TokenType == DecimalToken
  1772. if group {
  1773. w.Write([]byte("("))
  1774. }
  1775. n.X.JS(w)
  1776. if n.Optional {
  1777. w.Write([]byte("?."))
  1778. } else {
  1779. if group {
  1780. w.Write([]byte(")"))
  1781. }
  1782. w.Write([]byte("."))
  1783. }
  1784. n.Y.JS(w)
  1785. }
  1786. // NewTargetExpr is a new target meta property.
  1787. type NewTargetExpr struct{}
  1788. func (n NewTargetExpr) String() string {
  1789. return "(new.target)"
  1790. }
  1791. // JS writes JavaScript to writer.
  1792. func (n NewTargetExpr) JS(w io.Writer) {
  1793. w.Write([]byte("new.target"))
  1794. }
  1795. // ImportMetaExpr is a import meta meta property.
  1796. type ImportMetaExpr struct{}
  1797. func (n ImportMetaExpr) String() string {
  1798. return "(import.meta)"
  1799. }
  1800. // JS writes JavaScript to writer.
  1801. func (n ImportMetaExpr) JS(w io.Writer) {
  1802. w.Write([]byte("import.meta"))
  1803. }
  1804. type Arg struct {
  1805. Value IExpr
  1806. Rest bool
  1807. }
  1808. func (n Arg) String() string {
  1809. s := ""
  1810. if n.Rest {
  1811. s += "..."
  1812. }
  1813. return s + n.Value.String()
  1814. }
  1815. // JS writes JavaScript to writer.
  1816. func (n Arg) JS(w io.Writer) {
  1817. if n.Rest {
  1818. w.Write([]byte("..."))
  1819. }
  1820. n.Value.JS(w)
  1821. }
  1822. // Args is a list of arguments as used by new and call expressions.
  1823. type Args struct {
  1824. List []Arg
  1825. }
  1826. func (n Args) String() string {
  1827. s := "("
  1828. for i, item := range n.List {
  1829. if i != 0 {
  1830. s += ", "
  1831. }
  1832. s += item.String()
  1833. }
  1834. return s + ")"
  1835. }
  1836. // JS writes JavaScript to writer.
  1837. func (n Args) JS(w io.Writer) {
  1838. for j, item := range n.List {
  1839. if j != 0 {
  1840. w.Write([]byte(", "))
  1841. }
  1842. item.JS(w)
  1843. }
  1844. }
  1845. // NewExpr is a new expression or new member expression.
  1846. type NewExpr struct {
  1847. X IExpr
  1848. Args *Args // can be nil
  1849. }
  1850. func (n NewExpr) String() string {
  1851. if n.Args != nil {
  1852. return "(new " + n.X.String() + n.Args.String() + ")"
  1853. }
  1854. return "(new " + n.X.String() + ")"
  1855. }
  1856. // JS writes JavaScript to writer.
  1857. func (n NewExpr) JS(w io.Writer) {
  1858. w.Write([]byte("new "))
  1859. n.X.JS(w)
  1860. if n.Args != nil {
  1861. w.Write([]byte("("))
  1862. n.Args.JS(w)
  1863. w.Write([]byte(")"))
  1864. } else {
  1865. w.Write([]byte("()"))
  1866. }
  1867. }
  1868. // CallExpr is a call expression.
  1869. type CallExpr struct {
  1870. X IExpr
  1871. Args Args
  1872. Optional bool
  1873. }
  1874. func (n CallExpr) String() string {
  1875. if n.Optional {
  1876. return "(" + n.X.String() + "?." + n.Args.String() + ")"
  1877. }
  1878. return "(" + n.X.String() + n.Args.String() + ")"
  1879. }
  1880. // JS writes JavaScript to writer.
  1881. func (n CallExpr) JS(w io.Writer) {
  1882. n.X.JS(w)
  1883. if n.Optional {
  1884. w.Write([]byte("?.("))
  1885. } else {
  1886. w.Write([]byte("("))
  1887. }
  1888. n.Args.JS(w)
  1889. w.Write([]byte(")"))
  1890. }
  1891. // UnaryExpr is an update or unary expression.
  1892. type UnaryExpr struct {
  1893. Op TokenType
  1894. X IExpr
  1895. }
  1896. func (n UnaryExpr) String() string {
  1897. if n.Op == PostIncrToken || n.Op == PostDecrToken {
  1898. return "(" + n.X.String() + n.Op.String() + ")"
  1899. } else if IsIdentifierName(n.Op) {
  1900. return "(" + n.Op.String() + " " + n.X.String() + ")"
  1901. }
  1902. return "(" + n.Op.String() + n.X.String() + ")"
  1903. }
  1904. // JS writes JavaScript to writer.
  1905. func (n UnaryExpr) JS(w io.Writer) {
  1906. if n.Op == PostIncrToken || n.Op == PostDecrToken {
  1907. n.X.JS(w)
  1908. w.Write(n.Op.Bytes())
  1909. return
  1910. } 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) {
  1911. w.Write(n.Op.Bytes())
  1912. w.Write([]byte(" "))
  1913. n.X.JS(w)
  1914. return
  1915. }
  1916. w.Write(n.Op.Bytes())
  1917. n.X.JS(w)
  1918. }
  1919. // JSON writes JSON to writer.
  1920. func (n UnaryExpr) JSON(w io.Writer) error {
  1921. if lit, ok := n.X.(*LiteralExpr); ok && n.Op == NegToken && lit.TokenType == DecimalToken {
  1922. w.Write([]byte("-"))
  1923. w.Write(lit.Data)
  1924. return nil
  1925. }
  1926. return ErrInvalidJSON
  1927. }
  1928. // BinaryExpr is a binary expression.
  1929. type BinaryExpr struct {
  1930. Op TokenType
  1931. X, Y IExpr
  1932. }
  1933. func (n BinaryExpr) String() string {
  1934. if IsIdentifierName(n.Op) {
  1935. return "(" + n.X.String() + " " + n.Op.String() + " " + n.Y.String() + ")"
  1936. }
  1937. return "(" + n.X.String() + n.Op.String() + n.Y.String() + ")"
  1938. }
  1939. // JS writes JavaScript to writer.
  1940. func (n BinaryExpr) JS(w io.Writer) {
  1941. n.X.JS(w)
  1942. w.Write([]byte(" "))
  1943. w.Write(n.Op.Bytes())
  1944. w.Write([]byte(" "))
  1945. n.Y.JS(w)
  1946. }
  1947. // CondExpr is a conditional expression.
  1948. type CondExpr struct {
  1949. Cond, X, Y IExpr
  1950. }
  1951. func (n CondExpr) String() string {
  1952. return "(" + n.Cond.String() + " ? " + n.X.String() + " : " + n.Y.String() + ")"
  1953. }
  1954. // JS writes JavaScript to writer.
  1955. func (n CondExpr) JS(w io.Writer) {
  1956. n.Cond.JS(w)
  1957. w.Write([]byte(" ? "))
  1958. n.X.JS(w)
  1959. w.Write([]byte(" : "))
  1960. n.Y.JS(w)
  1961. }
  1962. // YieldExpr is a yield expression.
  1963. type YieldExpr struct {
  1964. Generator bool
  1965. X IExpr // can be nil
  1966. }
  1967. func (n YieldExpr) String() string {
  1968. if n.X == nil {
  1969. return "(yield)"
  1970. }
  1971. s := "(yield"
  1972. if n.Generator {
  1973. s += "*"
  1974. }
  1975. return s + " " + n.X.String() + ")"
  1976. }
  1977. // JS writes JavaScript to writer.
  1978. func (n YieldExpr) JS(w io.Writer) {
  1979. w.Write([]byte("yield"))
  1980. if n.X == nil {
  1981. return
  1982. }
  1983. if n.Generator {
  1984. w.Write([]byte("*"))
  1985. }
  1986. w.Write([]byte(" "))
  1987. n.X.JS(w)
  1988. }
  1989. // ArrowFunc is an (async) arrow function.
  1990. type ArrowFunc struct {
  1991. Async bool
  1992. Params Params
  1993. Body BlockStmt
  1994. }
  1995. func (n ArrowFunc) String() string {
  1996. s := "("
  1997. if n.Async {
  1998. s += "async "
  1999. }
  2000. return s + n.Params.String() + " => " + n.Body.String() + ")"
  2001. }
  2002. // JS writes JavaScript to writer.
  2003. func (n ArrowFunc) JS(w io.Writer) {
  2004. if n.Async {
  2005. w.Write([]byte("async "))
  2006. }
  2007. n.Params.JS(w)
  2008. w.Write([]byte(" => "))
  2009. n.Body.JS(w)
  2010. }
  2011. // CommaExpr is a series of comma expressions.
  2012. type CommaExpr struct {
  2013. List []IExpr
  2014. }
  2015. func (n CommaExpr) String() string {
  2016. s := "("
  2017. for i, item := range n.List {
  2018. if i != 0 {
  2019. s += ","
  2020. }
  2021. s += item.String()
  2022. }
  2023. return s + ")"
  2024. }
  2025. // JS writes JavaScript to writer.
  2026. func (n CommaExpr) JS(w io.Writer) {
  2027. for j, item := range n.List {
  2028. if j != 0 {
  2029. w.Write([]byte(","))
  2030. }
  2031. item.JS(w)
  2032. }
  2033. }
  2034. func (v *Var) exprNode() {}
  2035. func (n LiteralExpr) exprNode() {}
  2036. func (n ArrayExpr) exprNode() {}
  2037. func (n ObjectExpr) exprNode() {}
  2038. func (n TemplateExpr) exprNode() {}
  2039. func (n GroupExpr) exprNode() {}
  2040. func (n DotExpr) exprNode() {}
  2041. func (n IndexExpr) exprNode() {}
  2042. func (n NewTargetExpr) exprNode() {}
  2043. func (n ImportMetaExpr) exprNode() {}
  2044. func (n NewExpr) exprNode() {}
  2045. func (n CallExpr) exprNode() {}
  2046. func (n UnaryExpr) exprNode() {}
  2047. func (n BinaryExpr) exprNode() {}
  2048. func (n CondExpr) exprNode() {}
  2049. func (n YieldExpr) exprNode() {}
  2050. func (n ArrowFunc) exprNode() {}
  2051. func (n CommaExpr) exprNode() {}