ast.js 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. import {
  34. HOP,
  35. MAP,
  36. noop
  37. } from "./utils/index.js";
  38. import { parse } from "./parse.js";
  39. function DEFNODE(type, props, ctor, methods, base = AST_Node) {
  40. if (!props) props = [];
  41. else props = props.split(/\s+/);
  42. var self_props = props;
  43. if (base && base.PROPS)
  44. props = props.concat(base.PROPS);
  45. const proto = base && Object.create(base.prototype);
  46. if (proto) {
  47. ctor.prototype = proto;
  48. ctor.BASE = base;
  49. }
  50. if (base) base.SUBCLASSES.push(ctor);
  51. ctor.prototype.CTOR = ctor;
  52. ctor.prototype.constructor = ctor;
  53. ctor.PROPS = props || null;
  54. ctor.SELF_PROPS = self_props;
  55. ctor.SUBCLASSES = [];
  56. if (type) {
  57. ctor.prototype.TYPE = ctor.TYPE = type;
  58. }
  59. if (methods) for (let i in methods) if (HOP(methods, i)) {
  60. if (i[0] === "$") {
  61. ctor[i.substr(1)] = methods[i];
  62. } else {
  63. ctor.prototype[i] = methods[i];
  64. }
  65. }
  66. ctor.DEFMETHOD = function(name, method) {
  67. this.prototype[name] = method;
  68. };
  69. return ctor;
  70. }
  71. const has_tok_flag = (tok, flag) => Boolean(tok.flags & flag);
  72. const set_tok_flag = (tok, flag, truth) => {
  73. if (truth) {
  74. tok.flags |= flag;
  75. } else {
  76. tok.flags &= ~flag;
  77. }
  78. };
  79. const TOK_FLAG_NLB = 0b0001;
  80. const TOK_FLAG_QUOTE_SINGLE = 0b0010;
  81. const TOK_FLAG_QUOTE_EXISTS = 0b0100;
  82. const TOK_FLAG_TEMPLATE_END = 0b1000;
  83. class AST_Token {
  84. constructor(type, value, line, col, pos, nlb, comments_before, comments_after, file) {
  85. this.flags = (nlb ? 1 : 0);
  86. this.type = type;
  87. this.value = value;
  88. this.line = line;
  89. this.col = col;
  90. this.pos = pos;
  91. this.comments_before = comments_before;
  92. this.comments_after = comments_after;
  93. this.file = file;
  94. Object.seal(this);
  95. }
  96. get nlb() {
  97. return has_tok_flag(this, TOK_FLAG_NLB);
  98. }
  99. set nlb(new_nlb) {
  100. set_tok_flag(this, TOK_FLAG_NLB, new_nlb);
  101. }
  102. get quote() {
  103. return !has_tok_flag(this, TOK_FLAG_QUOTE_EXISTS)
  104. ? ""
  105. : (has_tok_flag(this, TOK_FLAG_QUOTE_SINGLE) ? "'" : '"');
  106. }
  107. set quote(quote_type) {
  108. set_tok_flag(this, TOK_FLAG_QUOTE_SINGLE, quote_type === "'");
  109. set_tok_flag(this, TOK_FLAG_QUOTE_EXISTS, !!quote_type);
  110. }
  111. get template_end() {
  112. return has_tok_flag(this, TOK_FLAG_TEMPLATE_END);
  113. }
  114. set template_end(new_template_end) {
  115. set_tok_flag(this, TOK_FLAG_TEMPLATE_END, new_template_end);
  116. }
  117. }
  118. var AST_Node = DEFNODE("Node", "start end", function AST_Node(props) {
  119. if (props) {
  120. this.start = props.start;
  121. this.end = props.end;
  122. }
  123. this.flags = 0;
  124. }, {
  125. _clone: function(deep) {
  126. if (deep) {
  127. var self = this.clone();
  128. return self.transform(new TreeTransformer(function(node) {
  129. if (node !== self) {
  130. return node.clone(true);
  131. }
  132. }));
  133. }
  134. return new this.CTOR(this);
  135. },
  136. clone: function(deep) {
  137. return this._clone(deep);
  138. },
  139. $documentation: "Base class of all AST nodes",
  140. $propdoc: {
  141. start: "[AST_Token] The first token of this node",
  142. end: "[AST_Token] The last token of this node"
  143. },
  144. _walk: function(visitor) {
  145. return visitor._visit(this);
  146. },
  147. walk: function(visitor) {
  148. return this._walk(visitor); // not sure the indirection will be any help
  149. },
  150. _children_backwards: () => {}
  151. }, null);
  152. /* -----[ statements ]----- */
  153. var AST_Statement = DEFNODE("Statement", null, function AST_Statement(props) {
  154. if (props) {
  155. this.start = props.start;
  156. this.end = props.end;
  157. }
  158. this.flags = 0;
  159. }, {
  160. $documentation: "Base class of all statements",
  161. });
  162. var AST_Debugger = DEFNODE("Debugger", null, function AST_Debugger(props) {
  163. if (props) {
  164. this.start = props.start;
  165. this.end = props.end;
  166. }
  167. this.flags = 0;
  168. }, {
  169. $documentation: "Represents a debugger statement",
  170. }, AST_Statement);
  171. var AST_Directive = DEFNODE("Directive", "value quote", function AST_Directive(props) {
  172. if (props) {
  173. this.value = props.value;
  174. this.quote = props.quote;
  175. this.start = props.start;
  176. this.end = props.end;
  177. }
  178. this.flags = 0;
  179. }, {
  180. $documentation: "Represents a directive, like \"use strict\";",
  181. $propdoc: {
  182. value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
  183. quote: "[string] the original quote character"
  184. },
  185. }, AST_Statement);
  186. var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", function AST_SimpleStatement(props) {
  187. if (props) {
  188. this.body = props.body;
  189. this.start = props.start;
  190. this.end = props.end;
  191. }
  192. this.flags = 0;
  193. }, {
  194. $documentation: "A statement consisting of an expression, i.e. a = 1 + 2",
  195. $propdoc: {
  196. body: "[AST_Node] an expression node (should not be instanceof AST_Statement)"
  197. },
  198. _walk: function(visitor) {
  199. return visitor._visit(this, function() {
  200. this.body._walk(visitor);
  201. });
  202. },
  203. _children_backwards(push) {
  204. push(this.body);
  205. }
  206. }, AST_Statement);
  207. function walk_body(node, visitor) {
  208. const body = node.body;
  209. for (var i = 0, len = body.length; i < len; i++) {
  210. body[i]._walk(visitor);
  211. }
  212. }
  213. function clone_block_scope(deep) {
  214. var clone = this._clone(deep);
  215. if (this.block_scope) {
  216. clone.block_scope = this.block_scope.clone();
  217. }
  218. return clone;
  219. }
  220. var AST_Block = DEFNODE("Block", "body block_scope", function AST_Block(props) {
  221. if (props) {
  222. this.body = props.body;
  223. this.block_scope = props.block_scope;
  224. this.start = props.start;
  225. this.end = props.end;
  226. }
  227. this.flags = 0;
  228. }, {
  229. $documentation: "A body of statements (usually braced)",
  230. $propdoc: {
  231. body: "[AST_Statement*] an array of statements",
  232. block_scope: "[AST_Scope] the block scope"
  233. },
  234. _walk: function(visitor) {
  235. return visitor._visit(this, function() {
  236. walk_body(this, visitor);
  237. });
  238. },
  239. _children_backwards(push) {
  240. let i = this.body.length;
  241. while (i--) push(this.body[i]);
  242. },
  243. clone: clone_block_scope
  244. }, AST_Statement);
  245. var AST_BlockStatement = DEFNODE("BlockStatement", null, function AST_BlockStatement(props) {
  246. if (props) {
  247. this.body = props.body;
  248. this.block_scope = props.block_scope;
  249. this.start = props.start;
  250. this.end = props.end;
  251. }
  252. this.flags = 0;
  253. }, {
  254. $documentation: "A block statement",
  255. }, AST_Block);
  256. var AST_EmptyStatement = DEFNODE("EmptyStatement", null, function AST_EmptyStatement(props) {
  257. if (props) {
  258. this.start = props.start;
  259. this.end = props.end;
  260. }
  261. this.flags = 0;
  262. }, {
  263. $documentation: "The empty statement (empty block or simply a semicolon)"
  264. }, AST_Statement);
  265. var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", function AST_StatementWithBody(props) {
  266. if (props) {
  267. this.body = props.body;
  268. this.start = props.start;
  269. this.end = props.end;
  270. }
  271. this.flags = 0;
  272. }, {
  273. $documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`",
  274. $propdoc: {
  275. body: "[AST_Statement] the body; this should always be present, even if it's an AST_EmptyStatement"
  276. }
  277. }, AST_Statement);
  278. var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", function AST_LabeledStatement(props) {
  279. if (props) {
  280. this.label = props.label;
  281. this.body = props.body;
  282. this.start = props.start;
  283. this.end = props.end;
  284. }
  285. this.flags = 0;
  286. }, {
  287. $documentation: "Statement with a label",
  288. $propdoc: {
  289. label: "[AST_Label] a label definition"
  290. },
  291. _walk: function(visitor) {
  292. return visitor._visit(this, function() {
  293. this.label._walk(visitor);
  294. this.body._walk(visitor);
  295. });
  296. },
  297. _children_backwards(push) {
  298. push(this.body);
  299. push(this.label);
  300. },
  301. clone: function(deep) {
  302. var node = this._clone(deep);
  303. if (deep) {
  304. var label = node.label;
  305. var def = this.label;
  306. node.walk(new TreeWalker(function(node) {
  307. if (node instanceof AST_LoopControl
  308. && node.label && node.label.thedef === def) {
  309. node.label.thedef = label;
  310. label.references.push(node);
  311. }
  312. }));
  313. }
  314. return node;
  315. }
  316. }, AST_StatementWithBody);
  317. var AST_IterationStatement = DEFNODE(
  318. "IterationStatement",
  319. "block_scope",
  320. function AST_IterationStatement(props) {
  321. if (props) {
  322. this.block_scope = props.block_scope;
  323. this.body = props.body;
  324. this.start = props.start;
  325. this.end = props.end;
  326. }
  327. this.flags = 0;
  328. },
  329. {
  330. $documentation: "Internal class. All loops inherit from it.",
  331. $propdoc: {
  332. block_scope: "[AST_Scope] the block scope for this iteration statement."
  333. },
  334. clone: clone_block_scope
  335. },
  336. AST_StatementWithBody
  337. );
  338. var AST_DWLoop = DEFNODE("DWLoop", "condition", function AST_DWLoop(props) {
  339. if (props) {
  340. this.condition = props.condition;
  341. this.block_scope = props.block_scope;
  342. this.body = props.body;
  343. this.start = props.start;
  344. this.end = props.end;
  345. }
  346. this.flags = 0;
  347. }, {
  348. $documentation: "Base class for do/while statements",
  349. $propdoc: {
  350. condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement"
  351. }
  352. }, AST_IterationStatement);
  353. var AST_Do = DEFNODE("Do", null, function AST_Do(props) {
  354. if (props) {
  355. this.condition = props.condition;
  356. this.block_scope = props.block_scope;
  357. this.body = props.body;
  358. this.start = props.start;
  359. this.end = props.end;
  360. }
  361. this.flags = 0;
  362. }, {
  363. $documentation: "A `do` statement",
  364. _walk: function(visitor) {
  365. return visitor._visit(this, function() {
  366. this.body._walk(visitor);
  367. this.condition._walk(visitor);
  368. });
  369. },
  370. _children_backwards(push) {
  371. push(this.condition);
  372. push(this.body);
  373. }
  374. }, AST_DWLoop);
  375. var AST_While = DEFNODE("While", null, function AST_While(props) {
  376. if (props) {
  377. this.condition = props.condition;
  378. this.block_scope = props.block_scope;
  379. this.body = props.body;
  380. this.start = props.start;
  381. this.end = props.end;
  382. }
  383. this.flags = 0;
  384. }, {
  385. $documentation: "A `while` statement",
  386. _walk: function(visitor) {
  387. return visitor._visit(this, function() {
  388. this.condition._walk(visitor);
  389. this.body._walk(visitor);
  390. });
  391. },
  392. _children_backwards(push) {
  393. push(this.body);
  394. push(this.condition);
  395. },
  396. }, AST_DWLoop);
  397. var AST_For = DEFNODE("For", "init condition step", function AST_For(props) {
  398. if (props) {
  399. this.init = props.init;
  400. this.condition = props.condition;
  401. this.step = props.step;
  402. this.block_scope = props.block_scope;
  403. this.body = props.body;
  404. this.start = props.start;
  405. this.end = props.end;
  406. }
  407. this.flags = 0;
  408. }, {
  409. $documentation: "A `for` statement",
  410. $propdoc: {
  411. init: "[AST_Node?] the `for` initialization code, or null if empty",
  412. condition: "[AST_Node?] the `for` termination clause, or null if empty",
  413. step: "[AST_Node?] the `for` update clause, or null if empty"
  414. },
  415. _walk: function(visitor) {
  416. return visitor._visit(this, function() {
  417. if (this.init) this.init._walk(visitor);
  418. if (this.condition) this.condition._walk(visitor);
  419. if (this.step) this.step._walk(visitor);
  420. this.body._walk(visitor);
  421. });
  422. },
  423. _children_backwards(push) {
  424. push(this.body);
  425. if (this.step) push(this.step);
  426. if (this.condition) push(this.condition);
  427. if (this.init) push(this.init);
  428. },
  429. }, AST_IterationStatement);
  430. var AST_ForIn = DEFNODE("ForIn", "init object", function AST_ForIn(props) {
  431. if (props) {
  432. this.init = props.init;
  433. this.object = props.object;
  434. this.block_scope = props.block_scope;
  435. this.body = props.body;
  436. this.start = props.start;
  437. this.end = props.end;
  438. }
  439. this.flags = 0;
  440. }, {
  441. $documentation: "A `for ... in` statement",
  442. $propdoc: {
  443. init: "[AST_Node] the `for/in` initialization code",
  444. object: "[AST_Node] the object that we're looping through"
  445. },
  446. _walk: function(visitor) {
  447. return visitor._visit(this, function() {
  448. this.init._walk(visitor);
  449. this.object._walk(visitor);
  450. this.body._walk(visitor);
  451. });
  452. },
  453. _children_backwards(push) {
  454. push(this.body);
  455. if (this.object) push(this.object);
  456. if (this.init) push(this.init);
  457. },
  458. }, AST_IterationStatement);
  459. var AST_ForOf = DEFNODE("ForOf", "await", function AST_ForOf(props) {
  460. if (props) {
  461. this.await = props.await;
  462. this.init = props.init;
  463. this.object = props.object;
  464. this.block_scope = props.block_scope;
  465. this.body = props.body;
  466. this.start = props.start;
  467. this.end = props.end;
  468. }
  469. this.flags = 0;
  470. }, {
  471. $documentation: "A `for ... of` statement",
  472. }, AST_ForIn);
  473. var AST_With = DEFNODE("With", "expression", function AST_With(props) {
  474. if (props) {
  475. this.expression = props.expression;
  476. this.body = props.body;
  477. this.start = props.start;
  478. this.end = props.end;
  479. }
  480. this.flags = 0;
  481. }, {
  482. $documentation: "A `with` statement",
  483. $propdoc: {
  484. expression: "[AST_Node] the `with` expression"
  485. },
  486. _walk: function(visitor) {
  487. return visitor._visit(this, function() {
  488. this.expression._walk(visitor);
  489. this.body._walk(visitor);
  490. });
  491. },
  492. _children_backwards(push) {
  493. push(this.body);
  494. push(this.expression);
  495. },
  496. }, AST_StatementWithBody);
  497. /* -----[ scope and functions ]----- */
  498. var AST_Scope = DEFNODE(
  499. "Scope",
  500. "variables uses_with uses_eval parent_scope enclosed cname",
  501. function AST_Scope(props) {
  502. if (props) {
  503. this.variables = props.variables;
  504. this.uses_with = props.uses_with;
  505. this.uses_eval = props.uses_eval;
  506. this.parent_scope = props.parent_scope;
  507. this.enclosed = props.enclosed;
  508. this.cname = props.cname;
  509. this.body = props.body;
  510. this.block_scope = props.block_scope;
  511. this.start = props.start;
  512. this.end = props.end;
  513. }
  514. this.flags = 0;
  515. },
  516. {
  517. $documentation: "Base class for all statements introducing a lexical scope",
  518. $propdoc: {
  519. variables: "[Map/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
  520. uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
  521. uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
  522. parent_scope: "[AST_Scope?/S] link to the parent scope",
  523. enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
  524. cname: "[integer/S] current index for mangling variables (used internally by the mangler)",
  525. },
  526. get_defun_scope: function() {
  527. var self = this;
  528. while (self.is_block_scope()) {
  529. self = self.parent_scope;
  530. }
  531. return self;
  532. },
  533. clone: function(deep, toplevel) {
  534. var node = this._clone(deep);
  535. if (deep && this.variables && toplevel && !this._block_scope) {
  536. node.figure_out_scope({}, {
  537. toplevel: toplevel,
  538. parent_scope: this.parent_scope
  539. });
  540. } else {
  541. if (this.variables) node.variables = new Map(this.variables);
  542. if (this.enclosed) node.enclosed = this.enclosed.slice();
  543. if (this._block_scope) node._block_scope = this._block_scope;
  544. }
  545. return node;
  546. },
  547. pinned: function() {
  548. return this.uses_eval || this.uses_with;
  549. }
  550. },
  551. AST_Block
  552. );
  553. var AST_Toplevel = DEFNODE("Toplevel", "globals", function AST_Toplevel(props) {
  554. if (props) {
  555. this.globals = props.globals;
  556. this.variables = props.variables;
  557. this.uses_with = props.uses_with;
  558. this.uses_eval = props.uses_eval;
  559. this.parent_scope = props.parent_scope;
  560. this.enclosed = props.enclosed;
  561. this.cname = props.cname;
  562. this.body = props.body;
  563. this.block_scope = props.block_scope;
  564. this.start = props.start;
  565. this.end = props.end;
  566. }
  567. this.flags = 0;
  568. }, {
  569. $documentation: "The toplevel scope",
  570. $propdoc: {
  571. globals: "[Map/S] a map of name -> SymbolDef for all undeclared names",
  572. },
  573. wrap_commonjs: function(name) {
  574. var body = this.body;
  575. var wrapped_tl = "(function(exports){'$ORIG';})(typeof " + name + "=='undefined'?(" + name + "={}):" + name + ");";
  576. wrapped_tl = parse(wrapped_tl);
  577. wrapped_tl = wrapped_tl.transform(new TreeTransformer(function(node) {
  578. if (node instanceof AST_Directive && node.value == "$ORIG") {
  579. return MAP.splice(body);
  580. }
  581. }));
  582. return wrapped_tl;
  583. },
  584. wrap_enclose: function(args_values) {
  585. if (typeof args_values != "string") args_values = "";
  586. var index = args_values.indexOf(":");
  587. if (index < 0) index = args_values.length;
  588. var body = this.body;
  589. return parse([
  590. "(function(",
  591. args_values.slice(0, index),
  592. '){"$ORIG"})(',
  593. args_values.slice(index + 1),
  594. ")"
  595. ].join("")).transform(new TreeTransformer(function(node) {
  596. if (node instanceof AST_Directive && node.value == "$ORIG") {
  597. return MAP.splice(body);
  598. }
  599. }));
  600. }
  601. }, AST_Scope);
  602. var AST_Expansion = DEFNODE("Expansion", "expression", function AST_Expansion(props) {
  603. if (props) {
  604. this.expression = props.expression;
  605. this.start = props.start;
  606. this.end = props.end;
  607. }
  608. this.flags = 0;
  609. }, {
  610. $documentation: "An expandible argument, such as ...rest, a splat, such as [1,2,...all], or an expansion in a variable declaration, such as var [first, ...rest] = list",
  611. $propdoc: {
  612. expression: "[AST_Node] the thing to be expanded"
  613. },
  614. _walk: function(visitor) {
  615. return visitor._visit(this, function() {
  616. this.expression.walk(visitor);
  617. });
  618. },
  619. _children_backwards(push) {
  620. push(this.expression);
  621. },
  622. });
  623. var AST_Lambda = DEFNODE(
  624. "Lambda",
  625. "name argnames uses_arguments is_generator async",
  626. function AST_Lambda(props) {
  627. if (props) {
  628. this.name = props.name;
  629. this.argnames = props.argnames;
  630. this.uses_arguments = props.uses_arguments;
  631. this.is_generator = props.is_generator;
  632. this.async = props.async;
  633. this.variables = props.variables;
  634. this.uses_with = props.uses_with;
  635. this.uses_eval = props.uses_eval;
  636. this.parent_scope = props.parent_scope;
  637. this.enclosed = props.enclosed;
  638. this.cname = props.cname;
  639. this.body = props.body;
  640. this.block_scope = props.block_scope;
  641. this.start = props.start;
  642. this.end = props.end;
  643. }
  644. this.flags = 0;
  645. },
  646. {
  647. $documentation: "Base class for functions",
  648. $propdoc: {
  649. name: "[AST_SymbolDeclaration?] the name of this function",
  650. argnames: "[AST_SymbolFunarg|AST_Destructuring|AST_Expansion|AST_DefaultAssign*] array of function arguments, destructurings, or expanding arguments",
  651. uses_arguments: "[boolean/S] tells whether this function accesses the arguments array",
  652. is_generator: "[boolean] is this a generator method",
  653. async: "[boolean] is this method async",
  654. },
  655. args_as_names: function () {
  656. var out = [];
  657. for (var i = 0; i < this.argnames.length; i++) {
  658. if (this.argnames[i] instanceof AST_Destructuring) {
  659. out.push(...this.argnames[i].all_symbols());
  660. } else {
  661. out.push(this.argnames[i]);
  662. }
  663. }
  664. return out;
  665. },
  666. _walk: function(visitor) {
  667. return visitor._visit(this, function() {
  668. if (this.name) this.name._walk(visitor);
  669. var argnames = this.argnames;
  670. for (var i = 0, len = argnames.length; i < len; i++) {
  671. argnames[i]._walk(visitor);
  672. }
  673. walk_body(this, visitor);
  674. });
  675. },
  676. _children_backwards(push) {
  677. let i = this.body.length;
  678. while (i--) push(this.body[i]);
  679. i = this.argnames.length;
  680. while (i--) push(this.argnames[i]);
  681. if (this.name) push(this.name);
  682. },
  683. is_braceless() {
  684. return this.body[0] instanceof AST_Return && this.body[0].value;
  685. },
  686. // Default args and expansion don't count, so .argnames.length doesn't cut it
  687. length_property() {
  688. let length = 0;
  689. for (const arg of this.argnames) {
  690. if (arg instanceof AST_SymbolFunarg || arg instanceof AST_Destructuring) {
  691. length++;
  692. }
  693. }
  694. return length;
  695. }
  696. },
  697. AST_Scope
  698. );
  699. var AST_Accessor = DEFNODE("Accessor", null, function AST_Accessor(props) {
  700. if (props) {
  701. this.name = props.name;
  702. this.argnames = props.argnames;
  703. this.uses_arguments = props.uses_arguments;
  704. this.is_generator = props.is_generator;
  705. this.async = props.async;
  706. this.variables = props.variables;
  707. this.uses_with = props.uses_with;
  708. this.uses_eval = props.uses_eval;
  709. this.parent_scope = props.parent_scope;
  710. this.enclosed = props.enclosed;
  711. this.cname = props.cname;
  712. this.body = props.body;
  713. this.block_scope = props.block_scope;
  714. this.start = props.start;
  715. this.end = props.end;
  716. }
  717. this.flags = 0;
  718. }, {
  719. $documentation: "A setter/getter function. The `name` property is always null."
  720. }, AST_Lambda);
  721. var AST_Function = DEFNODE("Function", null, function AST_Function(props) {
  722. if (props) {
  723. this.name = props.name;
  724. this.argnames = props.argnames;
  725. this.uses_arguments = props.uses_arguments;
  726. this.is_generator = props.is_generator;
  727. this.async = props.async;
  728. this.variables = props.variables;
  729. this.uses_with = props.uses_with;
  730. this.uses_eval = props.uses_eval;
  731. this.parent_scope = props.parent_scope;
  732. this.enclosed = props.enclosed;
  733. this.cname = props.cname;
  734. this.body = props.body;
  735. this.block_scope = props.block_scope;
  736. this.start = props.start;
  737. this.end = props.end;
  738. }
  739. this.flags = 0;
  740. }, {
  741. $documentation: "A function expression"
  742. }, AST_Lambda);
  743. var AST_Arrow = DEFNODE("Arrow", null, function AST_Arrow(props) {
  744. if (props) {
  745. this.name = props.name;
  746. this.argnames = props.argnames;
  747. this.uses_arguments = props.uses_arguments;
  748. this.is_generator = props.is_generator;
  749. this.async = props.async;
  750. this.variables = props.variables;
  751. this.uses_with = props.uses_with;
  752. this.uses_eval = props.uses_eval;
  753. this.parent_scope = props.parent_scope;
  754. this.enclosed = props.enclosed;
  755. this.cname = props.cname;
  756. this.body = props.body;
  757. this.block_scope = props.block_scope;
  758. this.start = props.start;
  759. this.end = props.end;
  760. }
  761. this.flags = 0;
  762. }, {
  763. $documentation: "An ES6 Arrow function ((a) => b)"
  764. }, AST_Lambda);
  765. var AST_Defun = DEFNODE("Defun", null, function AST_Defun(props) {
  766. if (props) {
  767. this.name = props.name;
  768. this.argnames = props.argnames;
  769. this.uses_arguments = props.uses_arguments;
  770. this.is_generator = props.is_generator;
  771. this.async = props.async;
  772. this.variables = props.variables;
  773. this.uses_with = props.uses_with;
  774. this.uses_eval = props.uses_eval;
  775. this.parent_scope = props.parent_scope;
  776. this.enclosed = props.enclosed;
  777. this.cname = props.cname;
  778. this.body = props.body;
  779. this.block_scope = props.block_scope;
  780. this.start = props.start;
  781. this.end = props.end;
  782. }
  783. this.flags = 0;
  784. }, {
  785. $documentation: "A function definition"
  786. }, AST_Lambda);
  787. /* -----[ DESTRUCTURING ]----- */
  788. var AST_Destructuring = DEFNODE("Destructuring", "names is_array", function AST_Destructuring(props) {
  789. if (props) {
  790. this.names = props.names;
  791. this.is_array = props.is_array;
  792. this.start = props.start;
  793. this.end = props.end;
  794. }
  795. this.flags = 0;
  796. }, {
  797. $documentation: "A destructuring of several names. Used in destructuring assignment and with destructuring function argument names",
  798. $propdoc: {
  799. "names": "[AST_Node*] Array of properties or elements",
  800. "is_array": "[Boolean] Whether the destructuring represents an object or array"
  801. },
  802. _walk: function(visitor) {
  803. return visitor._visit(this, function() {
  804. this.names.forEach(function(name) {
  805. name._walk(visitor);
  806. });
  807. });
  808. },
  809. _children_backwards(push) {
  810. let i = this.names.length;
  811. while (i--) push(this.names[i]);
  812. },
  813. all_symbols: function() {
  814. var out = [];
  815. this.walk(new TreeWalker(function (node) {
  816. if (node instanceof AST_Symbol) {
  817. out.push(node);
  818. }
  819. }));
  820. return out;
  821. }
  822. });
  823. var AST_PrefixedTemplateString = DEFNODE(
  824. "PrefixedTemplateString",
  825. "template_string prefix",
  826. function AST_PrefixedTemplateString(props) {
  827. if (props) {
  828. this.template_string = props.template_string;
  829. this.prefix = props.prefix;
  830. this.start = props.start;
  831. this.end = props.end;
  832. }
  833. this.flags = 0;
  834. },
  835. {
  836. $documentation: "A templatestring with a prefix, such as String.raw`foobarbaz`",
  837. $propdoc: {
  838. template_string: "[AST_TemplateString] The template string",
  839. prefix: "[AST_Node] The prefix, which will get called."
  840. },
  841. _walk: function(visitor) {
  842. return visitor._visit(this, function () {
  843. this.prefix._walk(visitor);
  844. this.template_string._walk(visitor);
  845. });
  846. },
  847. _children_backwards(push) {
  848. push(this.template_string);
  849. push(this.prefix);
  850. },
  851. }
  852. );
  853. var AST_TemplateString = DEFNODE("TemplateString", "segments", function AST_TemplateString(props) {
  854. if (props) {
  855. this.segments = props.segments;
  856. this.start = props.start;
  857. this.end = props.end;
  858. }
  859. this.flags = 0;
  860. }, {
  861. $documentation: "A template string literal",
  862. $propdoc: {
  863. segments: "[AST_Node*] One or more segments, starting with AST_TemplateSegment. AST_Node may follow AST_TemplateSegment, but each AST_Node must be followed by AST_TemplateSegment."
  864. },
  865. _walk: function(visitor) {
  866. return visitor._visit(this, function() {
  867. this.segments.forEach(function(seg) {
  868. seg._walk(visitor);
  869. });
  870. });
  871. },
  872. _children_backwards(push) {
  873. let i = this.segments.length;
  874. while (i--) push(this.segments[i]);
  875. }
  876. });
  877. var AST_TemplateSegment = DEFNODE("TemplateSegment", "value raw", function AST_TemplateSegment(props) {
  878. if (props) {
  879. this.value = props.value;
  880. this.raw = props.raw;
  881. this.start = props.start;
  882. this.end = props.end;
  883. }
  884. this.flags = 0;
  885. }, {
  886. $documentation: "A segment of a template string literal",
  887. $propdoc: {
  888. value: "Content of the segment",
  889. raw: "Raw source of the segment",
  890. }
  891. });
  892. /* -----[ JUMPS ]----- */
  893. var AST_Jump = DEFNODE("Jump", null, function AST_Jump(props) {
  894. if (props) {
  895. this.start = props.start;
  896. this.end = props.end;
  897. }
  898. this.flags = 0;
  899. }, {
  900. $documentation: "Base class for “jumps” (for now that's `return`, `throw`, `break` and `continue`)"
  901. }, AST_Statement);
  902. var AST_Exit = DEFNODE("Exit", "value", function AST_Exit(props) {
  903. if (props) {
  904. this.value = props.value;
  905. this.start = props.start;
  906. this.end = props.end;
  907. }
  908. this.flags = 0;
  909. }, {
  910. $documentation: "Base class for “exits” (`return` and `throw`)",
  911. $propdoc: {
  912. value: "[AST_Node?] the value returned or thrown by this statement; could be null for AST_Return"
  913. },
  914. _walk: function(visitor) {
  915. return visitor._visit(this, this.value && function() {
  916. this.value._walk(visitor);
  917. });
  918. },
  919. _children_backwards(push) {
  920. if (this.value) push(this.value);
  921. },
  922. }, AST_Jump);
  923. var AST_Return = DEFNODE("Return", null, function AST_Return(props) {
  924. if (props) {
  925. this.value = props.value;
  926. this.start = props.start;
  927. this.end = props.end;
  928. }
  929. this.flags = 0;
  930. }, {
  931. $documentation: "A `return` statement"
  932. }, AST_Exit);
  933. var AST_Throw = DEFNODE("Throw", null, function AST_Throw(props) {
  934. if (props) {
  935. this.value = props.value;
  936. this.start = props.start;
  937. this.end = props.end;
  938. }
  939. this.flags = 0;
  940. }, {
  941. $documentation: "A `throw` statement"
  942. }, AST_Exit);
  943. var AST_LoopControl = DEFNODE("LoopControl", "label", function AST_LoopControl(props) {
  944. if (props) {
  945. this.label = props.label;
  946. this.start = props.start;
  947. this.end = props.end;
  948. }
  949. this.flags = 0;
  950. }, {
  951. $documentation: "Base class for loop control statements (`break` and `continue`)",
  952. $propdoc: {
  953. label: "[AST_LabelRef?] the label, or null if none",
  954. },
  955. _walk: function(visitor) {
  956. return visitor._visit(this, this.label && function() {
  957. this.label._walk(visitor);
  958. });
  959. },
  960. _children_backwards(push) {
  961. if (this.label) push(this.label);
  962. },
  963. }, AST_Jump);
  964. var AST_Break = DEFNODE("Break", null, function AST_Break(props) {
  965. if (props) {
  966. this.label = props.label;
  967. this.start = props.start;
  968. this.end = props.end;
  969. }
  970. this.flags = 0;
  971. }, {
  972. $documentation: "A `break` statement"
  973. }, AST_LoopControl);
  974. var AST_Continue = DEFNODE("Continue", null, function AST_Continue(props) {
  975. if (props) {
  976. this.label = props.label;
  977. this.start = props.start;
  978. this.end = props.end;
  979. }
  980. this.flags = 0;
  981. }, {
  982. $documentation: "A `continue` statement"
  983. }, AST_LoopControl);
  984. var AST_Await = DEFNODE("Await", "expression", function AST_Await(props) {
  985. if (props) {
  986. this.expression = props.expression;
  987. this.start = props.start;
  988. this.end = props.end;
  989. }
  990. this.flags = 0;
  991. }, {
  992. $documentation: "An `await` statement",
  993. $propdoc: {
  994. expression: "[AST_Node] the mandatory expression being awaited",
  995. },
  996. _walk: function(visitor) {
  997. return visitor._visit(this, function() {
  998. this.expression._walk(visitor);
  999. });
  1000. },
  1001. _children_backwards(push) {
  1002. push(this.expression);
  1003. },
  1004. });
  1005. var AST_Yield = DEFNODE("Yield", "expression is_star", function AST_Yield(props) {
  1006. if (props) {
  1007. this.expression = props.expression;
  1008. this.is_star = props.is_star;
  1009. this.start = props.start;
  1010. this.end = props.end;
  1011. }
  1012. this.flags = 0;
  1013. }, {
  1014. $documentation: "A `yield` statement",
  1015. $propdoc: {
  1016. expression: "[AST_Node?] the value returned or thrown by this statement; could be null (representing undefined) but only when is_star is set to false",
  1017. is_star: "[Boolean] Whether this is a yield or yield* statement"
  1018. },
  1019. _walk: function(visitor) {
  1020. return visitor._visit(this, this.expression && function() {
  1021. this.expression._walk(visitor);
  1022. });
  1023. },
  1024. _children_backwards(push) {
  1025. if (this.expression) push(this.expression);
  1026. }
  1027. });
  1028. /* -----[ IF ]----- */
  1029. var AST_If = DEFNODE("If", "condition alternative", function AST_If(props) {
  1030. if (props) {
  1031. this.condition = props.condition;
  1032. this.alternative = props.alternative;
  1033. this.body = props.body;
  1034. this.start = props.start;
  1035. this.end = props.end;
  1036. }
  1037. this.flags = 0;
  1038. }, {
  1039. $documentation: "A `if` statement",
  1040. $propdoc: {
  1041. condition: "[AST_Node] the `if` condition",
  1042. alternative: "[AST_Statement?] the `else` part, or null if not present"
  1043. },
  1044. _walk: function(visitor) {
  1045. return visitor._visit(this, function() {
  1046. this.condition._walk(visitor);
  1047. this.body._walk(visitor);
  1048. if (this.alternative) this.alternative._walk(visitor);
  1049. });
  1050. },
  1051. _children_backwards(push) {
  1052. if (this.alternative) {
  1053. push(this.alternative);
  1054. }
  1055. push(this.body);
  1056. push(this.condition);
  1057. }
  1058. }, AST_StatementWithBody);
  1059. /* -----[ SWITCH ]----- */
  1060. var AST_Switch = DEFNODE("Switch", "expression", function AST_Switch(props) {
  1061. if (props) {
  1062. this.expression = props.expression;
  1063. this.body = props.body;
  1064. this.block_scope = props.block_scope;
  1065. this.start = props.start;
  1066. this.end = props.end;
  1067. }
  1068. this.flags = 0;
  1069. }, {
  1070. $documentation: "A `switch` statement",
  1071. $propdoc: {
  1072. expression: "[AST_Node] the `switch` “discriminant”"
  1073. },
  1074. _walk: function(visitor) {
  1075. return visitor._visit(this, function() {
  1076. this.expression._walk(visitor);
  1077. walk_body(this, visitor);
  1078. });
  1079. },
  1080. _children_backwards(push) {
  1081. let i = this.body.length;
  1082. while (i--) push(this.body[i]);
  1083. push(this.expression);
  1084. }
  1085. }, AST_Block);
  1086. var AST_SwitchBranch = DEFNODE("SwitchBranch", null, function AST_SwitchBranch(props) {
  1087. if (props) {
  1088. this.body = props.body;
  1089. this.block_scope = props.block_scope;
  1090. this.start = props.start;
  1091. this.end = props.end;
  1092. }
  1093. this.flags = 0;
  1094. }, {
  1095. $documentation: "Base class for `switch` branches",
  1096. }, AST_Block);
  1097. var AST_Default = DEFNODE("Default", null, function AST_Default(props) {
  1098. if (props) {
  1099. this.body = props.body;
  1100. this.block_scope = props.block_scope;
  1101. this.start = props.start;
  1102. this.end = props.end;
  1103. }
  1104. this.flags = 0;
  1105. }, {
  1106. $documentation: "A `default` switch branch",
  1107. }, AST_SwitchBranch);
  1108. var AST_Case = DEFNODE("Case", "expression", function AST_Case(props) {
  1109. if (props) {
  1110. this.expression = props.expression;
  1111. this.body = props.body;
  1112. this.block_scope = props.block_scope;
  1113. this.start = props.start;
  1114. this.end = props.end;
  1115. }
  1116. this.flags = 0;
  1117. }, {
  1118. $documentation: "A `case` switch branch",
  1119. $propdoc: {
  1120. expression: "[AST_Node] the `case` expression"
  1121. },
  1122. _walk: function(visitor) {
  1123. return visitor._visit(this, function() {
  1124. this.expression._walk(visitor);
  1125. walk_body(this, visitor);
  1126. });
  1127. },
  1128. _children_backwards(push) {
  1129. let i = this.body.length;
  1130. while (i--) push(this.body[i]);
  1131. push(this.expression);
  1132. },
  1133. }, AST_SwitchBranch);
  1134. /* -----[ EXCEPTIONS ]----- */
  1135. var AST_Try = DEFNODE("Try", "bcatch bfinally", function AST_Try(props) {
  1136. if (props) {
  1137. this.bcatch = props.bcatch;
  1138. this.bfinally = props.bfinally;
  1139. this.body = props.body;
  1140. this.block_scope = props.block_scope;
  1141. this.start = props.start;
  1142. this.end = props.end;
  1143. }
  1144. this.flags = 0;
  1145. }, {
  1146. $documentation: "A `try` statement",
  1147. $propdoc: {
  1148. bcatch: "[AST_Catch?] the catch block, or null if not present",
  1149. bfinally: "[AST_Finally?] the finally block, or null if not present"
  1150. },
  1151. _walk: function(visitor) {
  1152. return visitor._visit(this, function() {
  1153. walk_body(this, visitor);
  1154. if (this.bcatch) this.bcatch._walk(visitor);
  1155. if (this.bfinally) this.bfinally._walk(visitor);
  1156. });
  1157. },
  1158. _children_backwards(push) {
  1159. if (this.bfinally) push(this.bfinally);
  1160. if (this.bcatch) push(this.bcatch);
  1161. let i = this.body.length;
  1162. while (i--) push(this.body[i]);
  1163. },
  1164. }, AST_Block);
  1165. var AST_Catch = DEFNODE("Catch", "argname", function AST_Catch(props) {
  1166. if (props) {
  1167. this.argname = props.argname;
  1168. this.body = props.body;
  1169. this.block_scope = props.block_scope;
  1170. this.start = props.start;
  1171. this.end = props.end;
  1172. }
  1173. this.flags = 0;
  1174. }, {
  1175. $documentation: "A `catch` node; only makes sense as part of a `try` statement",
  1176. $propdoc: {
  1177. argname: "[AST_SymbolCatch|AST_Destructuring|AST_Expansion|AST_DefaultAssign] symbol for the exception"
  1178. },
  1179. _walk: function(visitor) {
  1180. return visitor._visit(this, function() {
  1181. if (this.argname) this.argname._walk(visitor);
  1182. walk_body(this, visitor);
  1183. });
  1184. },
  1185. _children_backwards(push) {
  1186. let i = this.body.length;
  1187. while (i--) push(this.body[i]);
  1188. if (this.argname) push(this.argname);
  1189. },
  1190. }, AST_Block);
  1191. var AST_Finally = DEFNODE("Finally", null, function AST_Finally(props) {
  1192. if (props) {
  1193. this.body = props.body;
  1194. this.block_scope = props.block_scope;
  1195. this.start = props.start;
  1196. this.end = props.end;
  1197. }
  1198. this.flags = 0;
  1199. }, {
  1200. $documentation: "A `finally` node; only makes sense as part of a `try` statement"
  1201. }, AST_Block);
  1202. /* -----[ VAR/CONST ]----- */
  1203. var AST_Definitions = DEFNODE("Definitions", "definitions", function AST_Definitions(props) {
  1204. if (props) {
  1205. this.definitions = props.definitions;
  1206. this.start = props.start;
  1207. this.end = props.end;
  1208. }
  1209. this.flags = 0;
  1210. }, {
  1211. $documentation: "Base class for `var` or `const` nodes (variable declarations/initializations)",
  1212. $propdoc: {
  1213. definitions: "[AST_VarDef*] array of variable definitions"
  1214. },
  1215. _walk: function(visitor) {
  1216. return visitor._visit(this, function() {
  1217. var definitions = this.definitions;
  1218. for (var i = 0, len = definitions.length; i < len; i++) {
  1219. definitions[i]._walk(visitor);
  1220. }
  1221. });
  1222. },
  1223. _children_backwards(push) {
  1224. let i = this.definitions.length;
  1225. while (i--) push(this.definitions[i]);
  1226. },
  1227. }, AST_Statement);
  1228. var AST_Var = DEFNODE("Var", null, function AST_Var(props) {
  1229. if (props) {
  1230. this.definitions = props.definitions;
  1231. this.start = props.start;
  1232. this.end = props.end;
  1233. }
  1234. this.flags = 0;
  1235. }, {
  1236. $documentation: "A `var` statement"
  1237. }, AST_Definitions);
  1238. var AST_Let = DEFNODE("Let", null, function AST_Let(props) {
  1239. if (props) {
  1240. this.definitions = props.definitions;
  1241. this.start = props.start;
  1242. this.end = props.end;
  1243. }
  1244. this.flags = 0;
  1245. }, {
  1246. $documentation: "A `let` statement"
  1247. }, AST_Definitions);
  1248. var AST_Const = DEFNODE("Const", null, function AST_Const(props) {
  1249. if (props) {
  1250. this.definitions = props.definitions;
  1251. this.start = props.start;
  1252. this.end = props.end;
  1253. }
  1254. this.flags = 0;
  1255. }, {
  1256. $documentation: "A `const` statement"
  1257. }, AST_Definitions);
  1258. var AST_VarDef = DEFNODE("VarDef", "name value", function AST_VarDef(props) {
  1259. if (props) {
  1260. this.name = props.name;
  1261. this.value = props.value;
  1262. this.start = props.start;
  1263. this.end = props.end;
  1264. }
  1265. this.flags = 0;
  1266. }, {
  1267. $documentation: "A variable declaration; only appears in a AST_Definitions node",
  1268. $propdoc: {
  1269. name: "[AST_Destructuring|AST_SymbolConst|AST_SymbolLet|AST_SymbolVar] name of the variable",
  1270. value: "[AST_Node?] initializer, or null of there's no initializer"
  1271. },
  1272. _walk: function(visitor) {
  1273. return visitor._visit(this, function() {
  1274. this.name._walk(visitor);
  1275. if (this.value) this.value._walk(visitor);
  1276. });
  1277. },
  1278. _children_backwards(push) {
  1279. if (this.value) push(this.value);
  1280. push(this.name);
  1281. },
  1282. });
  1283. var AST_NameMapping = DEFNODE("NameMapping", "foreign_name name", function AST_NameMapping(props) {
  1284. if (props) {
  1285. this.foreign_name = props.foreign_name;
  1286. this.name = props.name;
  1287. this.start = props.start;
  1288. this.end = props.end;
  1289. }
  1290. this.flags = 0;
  1291. }, {
  1292. $documentation: "The part of the export/import statement that declare names from a module.",
  1293. $propdoc: {
  1294. foreign_name: "[AST_SymbolExportForeign|AST_SymbolImportForeign] The name being exported/imported (as specified in the module)",
  1295. name: "[AST_SymbolExport|AST_SymbolImport] The name as it is visible to this module."
  1296. },
  1297. _walk: function (visitor) {
  1298. return visitor._visit(this, function() {
  1299. this.foreign_name._walk(visitor);
  1300. this.name._walk(visitor);
  1301. });
  1302. },
  1303. _children_backwards(push) {
  1304. push(this.name);
  1305. push(this.foreign_name);
  1306. },
  1307. });
  1308. var AST_Import = DEFNODE(
  1309. "Import",
  1310. "imported_name imported_names module_name assert_clause",
  1311. function AST_Import(props) {
  1312. if (props) {
  1313. this.imported_name = props.imported_name;
  1314. this.imported_names = props.imported_names;
  1315. this.module_name = props.module_name;
  1316. this.assert_clause = props.assert_clause;
  1317. this.start = props.start;
  1318. this.end = props.end;
  1319. }
  1320. this.flags = 0;
  1321. },
  1322. {
  1323. $documentation: "An `import` statement",
  1324. $propdoc: {
  1325. imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.",
  1326. imported_names: "[AST_NameMapping*] The names of non-default imported variables",
  1327. module_name: "[AST_String] String literal describing where this module came from",
  1328. assert_clause: "[AST_Object?] The import assertion"
  1329. },
  1330. _walk: function(visitor) {
  1331. return visitor._visit(this, function() {
  1332. if (this.imported_name) {
  1333. this.imported_name._walk(visitor);
  1334. }
  1335. if (this.imported_names) {
  1336. this.imported_names.forEach(function(name_import) {
  1337. name_import._walk(visitor);
  1338. });
  1339. }
  1340. this.module_name._walk(visitor);
  1341. });
  1342. },
  1343. _children_backwards(push) {
  1344. push(this.module_name);
  1345. if (this.imported_names) {
  1346. let i = this.imported_names.length;
  1347. while (i--) push(this.imported_names[i]);
  1348. }
  1349. if (this.imported_name) push(this.imported_name);
  1350. },
  1351. }
  1352. );
  1353. var AST_ImportMeta = DEFNODE("ImportMeta", null, function AST_ImportMeta(props) {
  1354. if (props) {
  1355. this.start = props.start;
  1356. this.end = props.end;
  1357. }
  1358. this.flags = 0;
  1359. }, {
  1360. $documentation: "A reference to import.meta",
  1361. });
  1362. var AST_Export = DEFNODE(
  1363. "Export",
  1364. "exported_definition exported_value is_default exported_names module_name assert_clause",
  1365. function AST_Export(props) {
  1366. if (props) {
  1367. this.exported_definition = props.exported_definition;
  1368. this.exported_value = props.exported_value;
  1369. this.is_default = props.is_default;
  1370. this.exported_names = props.exported_names;
  1371. this.module_name = props.module_name;
  1372. this.assert_clause = props.assert_clause;
  1373. this.start = props.start;
  1374. this.end = props.end;
  1375. }
  1376. this.flags = 0;
  1377. },
  1378. {
  1379. $documentation: "An `export` statement",
  1380. $propdoc: {
  1381. exported_definition: "[AST_Defun|AST_Definitions|AST_DefClass?] An exported definition",
  1382. exported_value: "[AST_Node?] An exported value",
  1383. exported_names: "[AST_NameMapping*?] List of exported names",
  1384. module_name: "[AST_String?] Name of the file to load exports from",
  1385. is_default: "[Boolean] Whether this is the default exported value of this module",
  1386. assert_clause: "[AST_Object?] The import assertion"
  1387. },
  1388. _walk: function (visitor) {
  1389. return visitor._visit(this, function () {
  1390. if (this.exported_definition) {
  1391. this.exported_definition._walk(visitor);
  1392. }
  1393. if (this.exported_value) {
  1394. this.exported_value._walk(visitor);
  1395. }
  1396. if (this.exported_names) {
  1397. this.exported_names.forEach(function(name_export) {
  1398. name_export._walk(visitor);
  1399. });
  1400. }
  1401. if (this.module_name) {
  1402. this.module_name._walk(visitor);
  1403. }
  1404. });
  1405. },
  1406. _children_backwards(push) {
  1407. if (this.module_name) push(this.module_name);
  1408. if (this.exported_names) {
  1409. let i = this.exported_names.length;
  1410. while (i--) push(this.exported_names[i]);
  1411. }
  1412. if (this.exported_value) push(this.exported_value);
  1413. if (this.exported_definition) push(this.exported_definition);
  1414. }
  1415. },
  1416. AST_Statement
  1417. );
  1418. /* -----[ OTHER ]----- */
  1419. var AST_Call = DEFNODE(
  1420. "Call",
  1421. "expression args optional _annotations",
  1422. function AST_Call(props) {
  1423. if (props) {
  1424. this.expression = props.expression;
  1425. this.args = props.args;
  1426. this.optional = props.optional;
  1427. this._annotations = props._annotations;
  1428. this.start = props.start;
  1429. this.end = props.end;
  1430. this.initialize();
  1431. }
  1432. this.flags = 0;
  1433. },
  1434. {
  1435. $documentation: "A function call expression",
  1436. $propdoc: {
  1437. expression: "[AST_Node] expression to invoke as function",
  1438. args: "[AST_Node*] array of arguments",
  1439. optional: "[boolean] whether this is an optional call (IE ?.() )",
  1440. _annotations: "[number] bitfield containing information about the call"
  1441. },
  1442. initialize() {
  1443. if (this._annotations == null) this._annotations = 0;
  1444. },
  1445. _walk(visitor) {
  1446. return visitor._visit(this, function() {
  1447. var args = this.args;
  1448. for (var i = 0, len = args.length; i < len; i++) {
  1449. args[i]._walk(visitor);
  1450. }
  1451. this.expression._walk(visitor); // TODO why do we need to crawl this last?
  1452. });
  1453. },
  1454. _children_backwards(push) {
  1455. let i = this.args.length;
  1456. while (i--) push(this.args[i]);
  1457. push(this.expression);
  1458. },
  1459. }
  1460. );
  1461. var AST_New = DEFNODE("New", null, function AST_New(props) {
  1462. if (props) {
  1463. this.expression = props.expression;
  1464. this.args = props.args;
  1465. this.optional = props.optional;
  1466. this._annotations = props._annotations;
  1467. this.start = props.start;
  1468. this.end = props.end;
  1469. this.initialize();
  1470. }
  1471. this.flags = 0;
  1472. }, {
  1473. $documentation: "An object instantiation. Derives from a function call since it has exactly the same properties"
  1474. }, AST_Call);
  1475. var AST_Sequence = DEFNODE("Sequence", "expressions", function AST_Sequence(props) {
  1476. if (props) {
  1477. this.expressions = props.expressions;
  1478. this.start = props.start;
  1479. this.end = props.end;
  1480. }
  1481. this.flags = 0;
  1482. }, {
  1483. $documentation: "A sequence expression (comma-separated expressions)",
  1484. $propdoc: {
  1485. expressions: "[AST_Node*] array of expressions (at least two)"
  1486. },
  1487. _walk: function(visitor) {
  1488. return visitor._visit(this, function() {
  1489. this.expressions.forEach(function(node) {
  1490. node._walk(visitor);
  1491. });
  1492. });
  1493. },
  1494. _children_backwards(push) {
  1495. let i = this.expressions.length;
  1496. while (i--) push(this.expressions[i]);
  1497. },
  1498. });
  1499. var AST_PropAccess = DEFNODE(
  1500. "PropAccess",
  1501. "expression property optional",
  1502. function AST_PropAccess(props) {
  1503. if (props) {
  1504. this.expression = props.expression;
  1505. this.property = props.property;
  1506. this.optional = props.optional;
  1507. this.start = props.start;
  1508. this.end = props.end;
  1509. }
  1510. this.flags = 0;
  1511. },
  1512. {
  1513. $documentation: "Base class for property access expressions, i.e. `a.foo` or `a[\"foo\"]`",
  1514. $propdoc: {
  1515. expression: "[AST_Node] the “container” expression",
  1516. property: "[AST_Node|string] the property to access. For AST_Dot & AST_DotHash this is always a plain string, while for AST_Sub it's an arbitrary AST_Node",
  1517. optional: "[boolean] whether this is an optional property access (IE ?.)"
  1518. }
  1519. }
  1520. );
  1521. var AST_Dot = DEFNODE("Dot", "quote", function AST_Dot(props) {
  1522. if (props) {
  1523. this.quote = props.quote;
  1524. this.expression = props.expression;
  1525. this.property = props.property;
  1526. this.optional = props.optional;
  1527. this.start = props.start;
  1528. this.end = props.end;
  1529. }
  1530. this.flags = 0;
  1531. }, {
  1532. $documentation: "A dotted property access expression",
  1533. $propdoc: {
  1534. quote: "[string] the original quote character when transformed from AST_Sub",
  1535. },
  1536. _walk: function(visitor) {
  1537. return visitor._visit(this, function() {
  1538. this.expression._walk(visitor);
  1539. });
  1540. },
  1541. _children_backwards(push) {
  1542. push(this.expression);
  1543. },
  1544. }, AST_PropAccess);
  1545. var AST_DotHash = DEFNODE("DotHash", "", function AST_DotHash(props) {
  1546. if (props) {
  1547. this.expression = props.expression;
  1548. this.property = props.property;
  1549. this.optional = props.optional;
  1550. this.start = props.start;
  1551. this.end = props.end;
  1552. }
  1553. this.flags = 0;
  1554. }, {
  1555. $documentation: "A dotted property access to a private property",
  1556. _walk: function(visitor) {
  1557. return visitor._visit(this, function() {
  1558. this.expression._walk(visitor);
  1559. });
  1560. },
  1561. _children_backwards(push) {
  1562. push(this.expression);
  1563. },
  1564. }, AST_PropAccess);
  1565. var AST_Sub = DEFNODE("Sub", null, function AST_Sub(props) {
  1566. if (props) {
  1567. this.expression = props.expression;
  1568. this.property = props.property;
  1569. this.optional = props.optional;
  1570. this.start = props.start;
  1571. this.end = props.end;
  1572. }
  1573. this.flags = 0;
  1574. }, {
  1575. $documentation: "Index-style property access, i.e. `a[\"foo\"]`",
  1576. _walk: function(visitor) {
  1577. return visitor._visit(this, function() {
  1578. this.expression._walk(visitor);
  1579. this.property._walk(visitor);
  1580. });
  1581. },
  1582. _children_backwards(push) {
  1583. push(this.property);
  1584. push(this.expression);
  1585. },
  1586. }, AST_PropAccess);
  1587. var AST_Chain = DEFNODE("Chain", "expression", function AST_Chain(props) {
  1588. if (props) {
  1589. this.expression = props.expression;
  1590. this.start = props.start;
  1591. this.end = props.end;
  1592. }
  1593. this.flags = 0;
  1594. }, {
  1595. $documentation: "A chain expression like a?.b?.(c)?.[d]",
  1596. $propdoc: {
  1597. expression: "[AST_Call|AST_Dot|AST_DotHash|AST_Sub] chain element."
  1598. },
  1599. _walk: function (visitor) {
  1600. return visitor._visit(this, function() {
  1601. this.expression._walk(visitor);
  1602. });
  1603. },
  1604. _children_backwards(push) {
  1605. push(this.expression);
  1606. },
  1607. });
  1608. var AST_Unary = DEFNODE("Unary", "operator expression", function AST_Unary(props) {
  1609. if (props) {
  1610. this.operator = props.operator;
  1611. this.expression = props.expression;
  1612. this.start = props.start;
  1613. this.end = props.end;
  1614. }
  1615. this.flags = 0;
  1616. }, {
  1617. $documentation: "Base class for unary expressions",
  1618. $propdoc: {
  1619. operator: "[string] the operator",
  1620. expression: "[AST_Node] expression that this unary operator applies to"
  1621. },
  1622. _walk: function(visitor) {
  1623. return visitor._visit(this, function() {
  1624. this.expression._walk(visitor);
  1625. });
  1626. },
  1627. _children_backwards(push) {
  1628. push(this.expression);
  1629. },
  1630. });
  1631. var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, function AST_UnaryPrefix(props) {
  1632. if (props) {
  1633. this.operator = props.operator;
  1634. this.expression = props.expression;
  1635. this.start = props.start;
  1636. this.end = props.end;
  1637. }
  1638. this.flags = 0;
  1639. }, {
  1640. $documentation: "Unary prefix expression, i.e. `typeof i` or `++i`"
  1641. }, AST_Unary);
  1642. var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, function AST_UnaryPostfix(props) {
  1643. if (props) {
  1644. this.operator = props.operator;
  1645. this.expression = props.expression;
  1646. this.start = props.start;
  1647. this.end = props.end;
  1648. }
  1649. this.flags = 0;
  1650. }, {
  1651. $documentation: "Unary postfix expression, i.e. `i++`"
  1652. }, AST_Unary);
  1653. var AST_Binary = DEFNODE("Binary", "operator left right", function AST_Binary(props) {
  1654. if (props) {
  1655. this.operator = props.operator;
  1656. this.left = props.left;
  1657. this.right = props.right;
  1658. this.start = props.start;
  1659. this.end = props.end;
  1660. }
  1661. this.flags = 0;
  1662. }, {
  1663. $documentation: "Binary expression, i.e. `a + b`",
  1664. $propdoc: {
  1665. left: "[AST_Node] left-hand side expression",
  1666. operator: "[string] the operator",
  1667. right: "[AST_Node] right-hand side expression"
  1668. },
  1669. _walk: function(visitor) {
  1670. return visitor._visit(this, function() {
  1671. this.left._walk(visitor);
  1672. this.right._walk(visitor);
  1673. });
  1674. },
  1675. _children_backwards(push) {
  1676. push(this.right);
  1677. push(this.left);
  1678. },
  1679. });
  1680. var AST_Conditional = DEFNODE(
  1681. "Conditional",
  1682. "condition consequent alternative",
  1683. function AST_Conditional(props) {
  1684. if (props) {
  1685. this.condition = props.condition;
  1686. this.consequent = props.consequent;
  1687. this.alternative = props.alternative;
  1688. this.start = props.start;
  1689. this.end = props.end;
  1690. }
  1691. this.flags = 0;
  1692. },
  1693. {
  1694. $documentation: "Conditional expression using the ternary operator, i.e. `a ? b : c`",
  1695. $propdoc: {
  1696. condition: "[AST_Node]",
  1697. consequent: "[AST_Node]",
  1698. alternative: "[AST_Node]"
  1699. },
  1700. _walk: function(visitor) {
  1701. return visitor._visit(this, function() {
  1702. this.condition._walk(visitor);
  1703. this.consequent._walk(visitor);
  1704. this.alternative._walk(visitor);
  1705. });
  1706. },
  1707. _children_backwards(push) {
  1708. push(this.alternative);
  1709. push(this.consequent);
  1710. push(this.condition);
  1711. },
  1712. }
  1713. );
  1714. var AST_Assign = DEFNODE("Assign", "logical", function AST_Assign(props) {
  1715. if (props) {
  1716. this.logical = props.logical;
  1717. this.operator = props.operator;
  1718. this.left = props.left;
  1719. this.right = props.right;
  1720. this.start = props.start;
  1721. this.end = props.end;
  1722. }
  1723. this.flags = 0;
  1724. }, {
  1725. $documentation: "An assignment expression — `a = b + 5`",
  1726. $propdoc: {
  1727. logical: "Whether it's a logical assignment"
  1728. }
  1729. }, AST_Binary);
  1730. var AST_DefaultAssign = DEFNODE("DefaultAssign", null, function AST_DefaultAssign(props) {
  1731. if (props) {
  1732. this.operator = props.operator;
  1733. this.left = props.left;
  1734. this.right = props.right;
  1735. this.start = props.start;
  1736. this.end = props.end;
  1737. }
  1738. this.flags = 0;
  1739. }, {
  1740. $documentation: "A default assignment expression like in `(a = 3) => a`"
  1741. }, AST_Binary);
  1742. /* -----[ LITERALS ]----- */
  1743. var AST_Array = DEFNODE("Array", "elements", function AST_Array(props) {
  1744. if (props) {
  1745. this.elements = props.elements;
  1746. this.start = props.start;
  1747. this.end = props.end;
  1748. }
  1749. this.flags = 0;
  1750. }, {
  1751. $documentation: "An array literal",
  1752. $propdoc: {
  1753. elements: "[AST_Node*] array of elements"
  1754. },
  1755. _walk: function(visitor) {
  1756. return visitor._visit(this, function() {
  1757. var elements = this.elements;
  1758. for (var i = 0, len = elements.length; i < len; i++) {
  1759. elements[i]._walk(visitor);
  1760. }
  1761. });
  1762. },
  1763. _children_backwards(push) {
  1764. let i = this.elements.length;
  1765. while (i--) push(this.elements[i]);
  1766. },
  1767. });
  1768. var AST_Object = DEFNODE("Object", "properties", function AST_Object(props) {
  1769. if (props) {
  1770. this.properties = props.properties;
  1771. this.start = props.start;
  1772. this.end = props.end;
  1773. }
  1774. this.flags = 0;
  1775. }, {
  1776. $documentation: "An object literal",
  1777. $propdoc: {
  1778. properties: "[AST_ObjectProperty*] array of properties"
  1779. },
  1780. _walk: function(visitor) {
  1781. return visitor._visit(this, function() {
  1782. var properties = this.properties;
  1783. for (var i = 0, len = properties.length; i < len; i++) {
  1784. properties[i]._walk(visitor);
  1785. }
  1786. });
  1787. },
  1788. _children_backwards(push) {
  1789. let i = this.properties.length;
  1790. while (i--) push(this.properties[i]);
  1791. },
  1792. });
  1793. var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", function AST_ObjectProperty(props) {
  1794. if (props) {
  1795. this.key = props.key;
  1796. this.value = props.value;
  1797. this.start = props.start;
  1798. this.end = props.end;
  1799. }
  1800. this.flags = 0;
  1801. }, {
  1802. $documentation: "Base class for literal object properties",
  1803. $propdoc: {
  1804. key: "[string|AST_Node] property name. For ObjectKeyVal this is a string. For getters, setters and computed property this is an AST_Node.",
  1805. value: "[AST_Node] property value. For getters and setters this is an AST_Accessor."
  1806. },
  1807. _walk: function(visitor) {
  1808. return visitor._visit(this, function() {
  1809. if (this.key instanceof AST_Node)
  1810. this.key._walk(visitor);
  1811. this.value._walk(visitor);
  1812. });
  1813. },
  1814. _children_backwards(push) {
  1815. push(this.value);
  1816. if (this.key instanceof AST_Node) push(this.key);
  1817. }
  1818. });
  1819. var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", function AST_ObjectKeyVal(props) {
  1820. if (props) {
  1821. this.quote = props.quote;
  1822. this.key = props.key;
  1823. this.value = props.value;
  1824. this.start = props.start;
  1825. this.end = props.end;
  1826. }
  1827. this.flags = 0;
  1828. }, {
  1829. $documentation: "A key: value object property",
  1830. $propdoc: {
  1831. quote: "[string] the original quote character"
  1832. },
  1833. computed_key() {
  1834. return this.key instanceof AST_Node;
  1835. }
  1836. }, AST_ObjectProperty);
  1837. var AST_PrivateSetter = DEFNODE("PrivateSetter", "static", function AST_PrivateSetter(props) {
  1838. if (props) {
  1839. this.static = props.static;
  1840. this.key = props.key;
  1841. this.value = props.value;
  1842. this.start = props.start;
  1843. this.end = props.end;
  1844. }
  1845. this.flags = 0;
  1846. }, {
  1847. $propdoc: {
  1848. static: "[boolean] whether this is a static private setter"
  1849. },
  1850. $documentation: "A private setter property",
  1851. computed_key() {
  1852. return false;
  1853. }
  1854. }, AST_ObjectProperty);
  1855. var AST_PrivateGetter = DEFNODE("PrivateGetter", "static", function AST_PrivateGetter(props) {
  1856. if (props) {
  1857. this.static = props.static;
  1858. this.key = props.key;
  1859. this.value = props.value;
  1860. this.start = props.start;
  1861. this.end = props.end;
  1862. }
  1863. this.flags = 0;
  1864. }, {
  1865. $propdoc: {
  1866. static: "[boolean] whether this is a static private getter"
  1867. },
  1868. $documentation: "A private getter property",
  1869. computed_key() {
  1870. return false;
  1871. }
  1872. }, AST_ObjectProperty);
  1873. var AST_ObjectSetter = DEFNODE("ObjectSetter", "quote static", function AST_ObjectSetter(props) {
  1874. if (props) {
  1875. this.quote = props.quote;
  1876. this.static = props.static;
  1877. this.key = props.key;
  1878. this.value = props.value;
  1879. this.start = props.start;
  1880. this.end = props.end;
  1881. }
  1882. this.flags = 0;
  1883. }, {
  1884. $propdoc: {
  1885. quote: "[string|undefined] the original quote character, if any",
  1886. static: "[boolean] whether this is a static setter (classes only)"
  1887. },
  1888. $documentation: "An object setter property",
  1889. computed_key() {
  1890. return !(this.key instanceof AST_SymbolMethod);
  1891. }
  1892. }, AST_ObjectProperty);
  1893. var AST_ObjectGetter = DEFNODE("ObjectGetter", "quote static", function AST_ObjectGetter(props) {
  1894. if (props) {
  1895. this.quote = props.quote;
  1896. this.static = props.static;
  1897. this.key = props.key;
  1898. this.value = props.value;
  1899. this.start = props.start;
  1900. this.end = props.end;
  1901. }
  1902. this.flags = 0;
  1903. }, {
  1904. $propdoc: {
  1905. quote: "[string|undefined] the original quote character, if any",
  1906. static: "[boolean] whether this is a static getter (classes only)"
  1907. },
  1908. $documentation: "An object getter property",
  1909. computed_key() {
  1910. return !(this.key instanceof AST_SymbolMethod);
  1911. }
  1912. }, AST_ObjectProperty);
  1913. var AST_ConciseMethod = DEFNODE(
  1914. "ConciseMethod",
  1915. "quote static is_generator async",
  1916. function AST_ConciseMethod(props) {
  1917. if (props) {
  1918. this.quote = props.quote;
  1919. this.static = props.static;
  1920. this.is_generator = props.is_generator;
  1921. this.async = props.async;
  1922. this.key = props.key;
  1923. this.value = props.value;
  1924. this.start = props.start;
  1925. this.end = props.end;
  1926. }
  1927. this.flags = 0;
  1928. },
  1929. {
  1930. $propdoc: {
  1931. quote: "[string|undefined] the original quote character, if any",
  1932. static: "[boolean] is this method static (classes only)",
  1933. is_generator: "[boolean] is this a generator method",
  1934. async: "[boolean] is this method async",
  1935. },
  1936. $documentation: "An ES6 concise method inside an object or class",
  1937. computed_key() {
  1938. return !(this.key instanceof AST_SymbolMethod);
  1939. }
  1940. },
  1941. AST_ObjectProperty
  1942. );
  1943. var AST_PrivateMethod = DEFNODE("PrivateMethod", "", function AST_PrivateMethod(props) {
  1944. if (props) {
  1945. this.quote = props.quote;
  1946. this.static = props.static;
  1947. this.is_generator = props.is_generator;
  1948. this.async = props.async;
  1949. this.key = props.key;
  1950. this.value = props.value;
  1951. this.start = props.start;
  1952. this.end = props.end;
  1953. }
  1954. this.flags = 0;
  1955. }, {
  1956. $documentation: "A private class method inside a class",
  1957. }, AST_ConciseMethod);
  1958. var AST_Class = DEFNODE("Class", "name extends properties", function AST_Class(props) {
  1959. if (props) {
  1960. this.name = props.name;
  1961. this.extends = props.extends;
  1962. this.properties = props.properties;
  1963. this.variables = props.variables;
  1964. this.uses_with = props.uses_with;
  1965. this.uses_eval = props.uses_eval;
  1966. this.parent_scope = props.parent_scope;
  1967. this.enclosed = props.enclosed;
  1968. this.cname = props.cname;
  1969. this.body = props.body;
  1970. this.block_scope = props.block_scope;
  1971. this.start = props.start;
  1972. this.end = props.end;
  1973. }
  1974. this.flags = 0;
  1975. }, {
  1976. $propdoc: {
  1977. name: "[AST_SymbolClass|AST_SymbolDefClass?] optional class name.",
  1978. extends: "[AST_Node]? optional parent class",
  1979. properties: "[AST_ObjectProperty*] array of properties"
  1980. },
  1981. $documentation: "An ES6 class",
  1982. _walk: function(visitor) {
  1983. return visitor._visit(this, function() {
  1984. if (this.name) {
  1985. this.name._walk(visitor);
  1986. }
  1987. if (this.extends) {
  1988. this.extends._walk(visitor);
  1989. }
  1990. this.properties.forEach((prop) => prop._walk(visitor));
  1991. });
  1992. },
  1993. _children_backwards(push) {
  1994. let i = this.properties.length;
  1995. while (i--) push(this.properties[i]);
  1996. if (this.extends) push(this.extends);
  1997. if (this.name) push(this.name);
  1998. },
  1999. }, AST_Scope /* TODO a class might have a scope but it's not a scope */);
  2000. var AST_ClassProperty = DEFNODE("ClassProperty", "static quote", function AST_ClassProperty(props) {
  2001. if (props) {
  2002. this.static = props.static;
  2003. this.quote = props.quote;
  2004. this.key = props.key;
  2005. this.value = props.value;
  2006. this.start = props.start;
  2007. this.end = props.end;
  2008. }
  2009. this.flags = 0;
  2010. }, {
  2011. $documentation: "A class property",
  2012. $propdoc: {
  2013. static: "[boolean] whether this is a static key",
  2014. quote: "[string] which quote is being used"
  2015. },
  2016. _walk: function(visitor) {
  2017. return visitor._visit(this, function() {
  2018. if (this.key instanceof AST_Node)
  2019. this.key._walk(visitor);
  2020. if (this.value instanceof AST_Node)
  2021. this.value._walk(visitor);
  2022. });
  2023. },
  2024. _children_backwards(push) {
  2025. if (this.value instanceof AST_Node) push(this.value);
  2026. if (this.key instanceof AST_Node) push(this.key);
  2027. },
  2028. computed_key() {
  2029. return !(this.key instanceof AST_SymbolClassProperty);
  2030. }
  2031. }, AST_ObjectProperty);
  2032. var AST_ClassPrivateProperty = DEFNODE("ClassPrivateProperty", "", function AST_ClassPrivateProperty(props) {
  2033. if (props) {
  2034. this.static = props.static;
  2035. this.quote = props.quote;
  2036. this.key = props.key;
  2037. this.value = props.value;
  2038. this.start = props.start;
  2039. this.end = props.end;
  2040. }
  2041. this.flags = 0;
  2042. }, {
  2043. $documentation: "A class property for a private property",
  2044. }, AST_ClassProperty);
  2045. var AST_PrivateIn = DEFNODE("PrivateIn", "key value", function AST_PrivateIn(props) {
  2046. if (props) {
  2047. this.key = props.key;
  2048. this.value = props.value;
  2049. this.start = props.start;
  2050. this.end = props.end;
  2051. }
  2052. this.flags = 0;
  2053. }, {
  2054. $documentation: "An `in` binop when the key is private, eg #x in this",
  2055. _walk: function(visitor) {
  2056. return visitor._visit(this, function() {
  2057. this.key._walk(visitor);
  2058. this.value._walk(visitor);
  2059. });
  2060. },
  2061. _children_backwards(push) {
  2062. push(this.value);
  2063. push(this.key);
  2064. },
  2065. });
  2066. var AST_DefClass = DEFNODE("DefClass", null, function AST_DefClass(props) {
  2067. if (props) {
  2068. this.name = props.name;
  2069. this.extends = props.extends;
  2070. this.properties = props.properties;
  2071. this.variables = props.variables;
  2072. this.uses_with = props.uses_with;
  2073. this.uses_eval = props.uses_eval;
  2074. this.parent_scope = props.parent_scope;
  2075. this.enclosed = props.enclosed;
  2076. this.cname = props.cname;
  2077. this.body = props.body;
  2078. this.block_scope = props.block_scope;
  2079. this.start = props.start;
  2080. this.end = props.end;
  2081. }
  2082. this.flags = 0;
  2083. }, {
  2084. $documentation: "A class definition",
  2085. }, AST_Class);
  2086. var AST_ClassStaticBlock = DEFNODE("ClassStaticBlock", "body block_scope", function AST_ClassStaticBlock (props) {
  2087. this.body = props.body;
  2088. this.block_scope = props.block_scope;
  2089. this.start = props.start;
  2090. this.end = props.end;
  2091. }, {
  2092. $documentation: "A block containing statements to be executed in the context of the class",
  2093. $propdoc: {
  2094. body: "[AST_Statement*] an array of statements",
  2095. },
  2096. _walk: function(visitor) {
  2097. return visitor._visit(this, function() {
  2098. walk_body(this, visitor);
  2099. });
  2100. },
  2101. _children_backwards(push) {
  2102. let i = this.body.length;
  2103. while (i--) push(this.body[i]);
  2104. },
  2105. clone: clone_block_scope,
  2106. }, AST_Scope);
  2107. var AST_ClassExpression = DEFNODE("ClassExpression", null, function AST_ClassExpression(props) {
  2108. if (props) {
  2109. this.name = props.name;
  2110. this.extends = props.extends;
  2111. this.properties = props.properties;
  2112. this.variables = props.variables;
  2113. this.uses_with = props.uses_with;
  2114. this.uses_eval = props.uses_eval;
  2115. this.parent_scope = props.parent_scope;
  2116. this.enclosed = props.enclosed;
  2117. this.cname = props.cname;
  2118. this.body = props.body;
  2119. this.block_scope = props.block_scope;
  2120. this.start = props.start;
  2121. this.end = props.end;
  2122. }
  2123. this.flags = 0;
  2124. }, {
  2125. $documentation: "A class expression."
  2126. }, AST_Class);
  2127. var AST_Symbol = DEFNODE("Symbol", "scope name thedef", function AST_Symbol(props) {
  2128. if (props) {
  2129. this.scope = props.scope;
  2130. this.name = props.name;
  2131. this.thedef = props.thedef;
  2132. this.start = props.start;
  2133. this.end = props.end;
  2134. }
  2135. this.flags = 0;
  2136. }, {
  2137. $propdoc: {
  2138. name: "[string] name of this symbol",
  2139. scope: "[AST_Scope/S] the current scope (not necessarily the definition scope)",
  2140. thedef: "[SymbolDef/S] the definition of this symbol"
  2141. },
  2142. $documentation: "Base class for all symbols"
  2143. });
  2144. var AST_NewTarget = DEFNODE("NewTarget", null, function AST_NewTarget(props) {
  2145. if (props) {
  2146. this.start = props.start;
  2147. this.end = props.end;
  2148. }
  2149. this.flags = 0;
  2150. }, {
  2151. $documentation: "A reference to new.target"
  2152. });
  2153. var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", function AST_SymbolDeclaration(props) {
  2154. if (props) {
  2155. this.init = props.init;
  2156. this.scope = props.scope;
  2157. this.name = props.name;
  2158. this.thedef = props.thedef;
  2159. this.start = props.start;
  2160. this.end = props.end;
  2161. }
  2162. this.flags = 0;
  2163. }, {
  2164. $documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",
  2165. }, AST_Symbol);
  2166. var AST_SymbolVar = DEFNODE("SymbolVar", null, function AST_SymbolVar(props) {
  2167. if (props) {
  2168. this.init = props.init;
  2169. this.scope = props.scope;
  2170. this.name = props.name;
  2171. this.thedef = props.thedef;
  2172. this.start = props.start;
  2173. this.end = props.end;
  2174. }
  2175. this.flags = 0;
  2176. }, {
  2177. $documentation: "Symbol defining a variable",
  2178. }, AST_SymbolDeclaration);
  2179. var AST_SymbolBlockDeclaration = DEFNODE(
  2180. "SymbolBlockDeclaration",
  2181. null,
  2182. function AST_SymbolBlockDeclaration(props) {
  2183. if (props) {
  2184. this.init = props.init;
  2185. this.scope = props.scope;
  2186. this.name = props.name;
  2187. this.thedef = props.thedef;
  2188. this.start = props.start;
  2189. this.end = props.end;
  2190. }
  2191. this.flags = 0;
  2192. },
  2193. {
  2194. $documentation: "Base class for block-scoped declaration symbols"
  2195. },
  2196. AST_SymbolDeclaration
  2197. );
  2198. var AST_SymbolConst = DEFNODE("SymbolConst", null, function AST_SymbolConst(props) {
  2199. if (props) {
  2200. this.init = props.init;
  2201. this.scope = props.scope;
  2202. this.name = props.name;
  2203. this.thedef = props.thedef;
  2204. this.start = props.start;
  2205. this.end = props.end;
  2206. }
  2207. this.flags = 0;
  2208. }, {
  2209. $documentation: "A constant declaration"
  2210. }, AST_SymbolBlockDeclaration);
  2211. var AST_SymbolLet = DEFNODE("SymbolLet", null, function AST_SymbolLet(props) {
  2212. if (props) {
  2213. this.init = props.init;
  2214. this.scope = props.scope;
  2215. this.name = props.name;
  2216. this.thedef = props.thedef;
  2217. this.start = props.start;
  2218. this.end = props.end;
  2219. }
  2220. this.flags = 0;
  2221. }, {
  2222. $documentation: "A block-scoped `let` declaration"
  2223. }, AST_SymbolBlockDeclaration);
  2224. var AST_SymbolFunarg = DEFNODE("SymbolFunarg", null, function AST_SymbolFunarg(props) {
  2225. if (props) {
  2226. this.init = props.init;
  2227. this.scope = props.scope;
  2228. this.name = props.name;
  2229. this.thedef = props.thedef;
  2230. this.start = props.start;
  2231. this.end = props.end;
  2232. }
  2233. this.flags = 0;
  2234. }, {
  2235. $documentation: "Symbol naming a function argument",
  2236. }, AST_SymbolVar);
  2237. var AST_SymbolDefun = DEFNODE("SymbolDefun", null, function AST_SymbolDefun(props) {
  2238. if (props) {
  2239. this.init = props.init;
  2240. this.scope = props.scope;
  2241. this.name = props.name;
  2242. this.thedef = props.thedef;
  2243. this.start = props.start;
  2244. this.end = props.end;
  2245. }
  2246. this.flags = 0;
  2247. }, {
  2248. $documentation: "Symbol defining a function",
  2249. }, AST_SymbolDeclaration);
  2250. var AST_SymbolMethod = DEFNODE("SymbolMethod", null, function AST_SymbolMethod(props) {
  2251. if (props) {
  2252. this.scope = props.scope;
  2253. this.name = props.name;
  2254. this.thedef = props.thedef;
  2255. this.start = props.start;
  2256. this.end = props.end;
  2257. }
  2258. this.flags = 0;
  2259. }, {
  2260. $documentation: "Symbol in an object defining a method",
  2261. }, AST_Symbol);
  2262. var AST_SymbolClassProperty = DEFNODE("SymbolClassProperty", null, function AST_SymbolClassProperty(props) {
  2263. if (props) {
  2264. this.scope = props.scope;
  2265. this.name = props.name;
  2266. this.thedef = props.thedef;
  2267. this.start = props.start;
  2268. this.end = props.end;
  2269. }
  2270. this.flags = 0;
  2271. }, {
  2272. $documentation: "Symbol for a class property",
  2273. }, AST_Symbol);
  2274. var AST_SymbolLambda = DEFNODE("SymbolLambda", null, function AST_SymbolLambda(props) {
  2275. if (props) {
  2276. this.init = props.init;
  2277. this.scope = props.scope;
  2278. this.name = props.name;
  2279. this.thedef = props.thedef;
  2280. this.start = props.start;
  2281. this.end = props.end;
  2282. }
  2283. this.flags = 0;
  2284. }, {
  2285. $documentation: "Symbol naming a function expression",
  2286. }, AST_SymbolDeclaration);
  2287. var AST_SymbolDefClass = DEFNODE("SymbolDefClass", null, function AST_SymbolDefClass(props) {
  2288. if (props) {
  2289. this.init = props.init;
  2290. this.scope = props.scope;
  2291. this.name = props.name;
  2292. this.thedef = props.thedef;
  2293. this.start = props.start;
  2294. this.end = props.end;
  2295. }
  2296. this.flags = 0;
  2297. }, {
  2298. $documentation: "Symbol naming a class's name in a class declaration. Lexically scoped to its containing scope, and accessible within the class."
  2299. }, AST_SymbolBlockDeclaration);
  2300. var AST_SymbolClass = DEFNODE("SymbolClass", null, function AST_SymbolClass(props) {
  2301. if (props) {
  2302. this.init = props.init;
  2303. this.scope = props.scope;
  2304. this.name = props.name;
  2305. this.thedef = props.thedef;
  2306. this.start = props.start;
  2307. this.end = props.end;
  2308. }
  2309. this.flags = 0;
  2310. }, {
  2311. $documentation: "Symbol naming a class's name. Lexically scoped to the class."
  2312. }, AST_SymbolDeclaration);
  2313. var AST_SymbolCatch = DEFNODE("SymbolCatch", null, function AST_SymbolCatch(props) {
  2314. if (props) {
  2315. this.init = props.init;
  2316. this.scope = props.scope;
  2317. this.name = props.name;
  2318. this.thedef = props.thedef;
  2319. this.start = props.start;
  2320. this.end = props.end;
  2321. }
  2322. this.flags = 0;
  2323. }, {
  2324. $documentation: "Symbol naming the exception in catch",
  2325. }, AST_SymbolBlockDeclaration);
  2326. var AST_SymbolImport = DEFNODE("SymbolImport", null, function AST_SymbolImport(props) {
  2327. if (props) {
  2328. this.init = props.init;
  2329. this.scope = props.scope;
  2330. this.name = props.name;
  2331. this.thedef = props.thedef;
  2332. this.start = props.start;
  2333. this.end = props.end;
  2334. }
  2335. this.flags = 0;
  2336. }, {
  2337. $documentation: "Symbol referring to an imported name",
  2338. }, AST_SymbolBlockDeclaration);
  2339. var AST_SymbolImportForeign = DEFNODE("SymbolImportForeign", null, function AST_SymbolImportForeign(props) {
  2340. if (props) {
  2341. this.scope = props.scope;
  2342. this.name = props.name;
  2343. this.thedef = props.thedef;
  2344. this.start = props.start;
  2345. this.end = props.end;
  2346. }
  2347. this.flags = 0;
  2348. }, {
  2349. $documentation: "A symbol imported from a module, but it is defined in the other module, and its real name is irrelevant for this module's purposes",
  2350. }, AST_Symbol);
  2351. var AST_Label = DEFNODE("Label", "references", function AST_Label(props) {
  2352. if (props) {
  2353. this.references = props.references;
  2354. this.scope = props.scope;
  2355. this.name = props.name;
  2356. this.thedef = props.thedef;
  2357. this.start = props.start;
  2358. this.end = props.end;
  2359. this.initialize();
  2360. }
  2361. this.flags = 0;
  2362. }, {
  2363. $documentation: "Symbol naming a label (declaration)",
  2364. $propdoc: {
  2365. references: "[AST_LoopControl*] a list of nodes referring to this label"
  2366. },
  2367. initialize: function() {
  2368. this.references = [];
  2369. this.thedef = this;
  2370. }
  2371. }, AST_Symbol);
  2372. var AST_SymbolRef = DEFNODE("SymbolRef", null, function AST_SymbolRef(props) {
  2373. if (props) {
  2374. this.scope = props.scope;
  2375. this.name = props.name;
  2376. this.thedef = props.thedef;
  2377. this.start = props.start;
  2378. this.end = props.end;
  2379. }
  2380. this.flags = 0;
  2381. }, {
  2382. $documentation: "Reference to some symbol (not definition/declaration)",
  2383. }, AST_Symbol);
  2384. var AST_SymbolExport = DEFNODE("SymbolExport", null, function AST_SymbolExport(props) {
  2385. if (props) {
  2386. this.scope = props.scope;
  2387. this.name = props.name;
  2388. this.thedef = props.thedef;
  2389. this.start = props.start;
  2390. this.end = props.end;
  2391. }
  2392. this.flags = 0;
  2393. }, {
  2394. $documentation: "Symbol referring to a name to export",
  2395. }, AST_SymbolRef);
  2396. var AST_SymbolExportForeign = DEFNODE("SymbolExportForeign", null, function AST_SymbolExportForeign(props) {
  2397. if (props) {
  2398. this.scope = props.scope;
  2399. this.name = props.name;
  2400. this.thedef = props.thedef;
  2401. this.start = props.start;
  2402. this.end = props.end;
  2403. }
  2404. this.flags = 0;
  2405. }, {
  2406. $documentation: "A symbol exported from this module, but it is used in the other module, and its real name is irrelevant for this module's purposes",
  2407. }, AST_Symbol);
  2408. var AST_LabelRef = DEFNODE("LabelRef", null, function AST_LabelRef(props) {
  2409. if (props) {
  2410. this.scope = props.scope;
  2411. this.name = props.name;
  2412. this.thedef = props.thedef;
  2413. this.start = props.start;
  2414. this.end = props.end;
  2415. }
  2416. this.flags = 0;
  2417. }, {
  2418. $documentation: "Reference to a label symbol",
  2419. }, AST_Symbol);
  2420. var AST_SymbolPrivateProperty = DEFNODE("SymbolPrivateProperty", null, function AST_SymbolPrivateProperty(props) {
  2421. if (props) {
  2422. this.scope = props.scope;
  2423. this.name = props.name;
  2424. this.thedef = props.thedef;
  2425. this.start = props.start;
  2426. this.end = props.end;
  2427. }
  2428. this.flags = 0;
  2429. }, {
  2430. $documentation: "A symbol that refers to a private property",
  2431. }, AST_Symbol);
  2432. var AST_This = DEFNODE("This", null, function AST_This(props) {
  2433. if (props) {
  2434. this.scope = props.scope;
  2435. this.name = props.name;
  2436. this.thedef = props.thedef;
  2437. this.start = props.start;
  2438. this.end = props.end;
  2439. }
  2440. this.flags = 0;
  2441. }, {
  2442. $documentation: "The `this` symbol",
  2443. }, AST_Symbol);
  2444. var AST_Super = DEFNODE("Super", null, function AST_Super(props) {
  2445. if (props) {
  2446. this.scope = props.scope;
  2447. this.name = props.name;
  2448. this.thedef = props.thedef;
  2449. this.start = props.start;
  2450. this.end = props.end;
  2451. }
  2452. this.flags = 0;
  2453. }, {
  2454. $documentation: "The `super` symbol",
  2455. }, AST_This);
  2456. var AST_Constant = DEFNODE("Constant", null, function AST_Constant(props) {
  2457. if (props) {
  2458. this.start = props.start;
  2459. this.end = props.end;
  2460. }
  2461. this.flags = 0;
  2462. }, {
  2463. $documentation: "Base class for all constants",
  2464. getValue: function() {
  2465. return this.value;
  2466. }
  2467. });
  2468. var AST_String = DEFNODE("String", "value quote", function AST_String(props) {
  2469. if (props) {
  2470. this.value = props.value;
  2471. this.quote = props.quote;
  2472. this.start = props.start;
  2473. this.end = props.end;
  2474. }
  2475. this.flags = 0;
  2476. }, {
  2477. $documentation: "A string literal",
  2478. $propdoc: {
  2479. value: "[string] the contents of this string",
  2480. quote: "[string] the original quote character"
  2481. }
  2482. }, AST_Constant);
  2483. var AST_Number = DEFNODE("Number", "value raw", function AST_Number(props) {
  2484. if (props) {
  2485. this.value = props.value;
  2486. this.raw = props.raw;
  2487. this.start = props.start;
  2488. this.end = props.end;
  2489. }
  2490. this.flags = 0;
  2491. }, {
  2492. $documentation: "A number literal",
  2493. $propdoc: {
  2494. value: "[number] the numeric value",
  2495. raw: "[string] numeric value as string"
  2496. }
  2497. }, AST_Constant);
  2498. var AST_BigInt = DEFNODE("BigInt", "value", function AST_BigInt(props) {
  2499. if (props) {
  2500. this.value = props.value;
  2501. this.start = props.start;
  2502. this.end = props.end;
  2503. }
  2504. this.flags = 0;
  2505. }, {
  2506. $documentation: "A big int literal",
  2507. $propdoc: {
  2508. value: "[string] big int value"
  2509. }
  2510. }, AST_Constant);
  2511. var AST_RegExp = DEFNODE("RegExp", "value", function AST_RegExp(props) {
  2512. if (props) {
  2513. this.value = props.value;
  2514. this.start = props.start;
  2515. this.end = props.end;
  2516. }
  2517. this.flags = 0;
  2518. }, {
  2519. $documentation: "A regexp literal",
  2520. $propdoc: {
  2521. value: "[RegExp] the actual regexp",
  2522. }
  2523. }, AST_Constant);
  2524. var AST_Atom = DEFNODE("Atom", null, function AST_Atom(props) {
  2525. if (props) {
  2526. this.start = props.start;
  2527. this.end = props.end;
  2528. }
  2529. this.flags = 0;
  2530. }, {
  2531. $documentation: "Base class for atoms",
  2532. }, AST_Constant);
  2533. var AST_Null = DEFNODE("Null", null, function AST_Null(props) {
  2534. if (props) {
  2535. this.start = props.start;
  2536. this.end = props.end;
  2537. }
  2538. this.flags = 0;
  2539. }, {
  2540. $documentation: "The `null` atom",
  2541. value: null
  2542. }, AST_Atom);
  2543. var AST_NaN = DEFNODE("NaN", null, function AST_NaN(props) {
  2544. if (props) {
  2545. this.start = props.start;
  2546. this.end = props.end;
  2547. }
  2548. this.flags = 0;
  2549. }, {
  2550. $documentation: "The impossible value",
  2551. value: 0/0
  2552. }, AST_Atom);
  2553. var AST_Undefined = DEFNODE("Undefined", null, function AST_Undefined(props) {
  2554. if (props) {
  2555. this.start = props.start;
  2556. this.end = props.end;
  2557. }
  2558. this.flags = 0;
  2559. }, {
  2560. $documentation: "The `undefined` value",
  2561. value: (function() {}())
  2562. }, AST_Atom);
  2563. var AST_Hole = DEFNODE("Hole", null, function AST_Hole(props) {
  2564. if (props) {
  2565. this.start = props.start;
  2566. this.end = props.end;
  2567. }
  2568. this.flags = 0;
  2569. }, {
  2570. $documentation: "A hole in an array",
  2571. value: (function() {}())
  2572. }, AST_Atom);
  2573. var AST_Infinity = DEFNODE("Infinity", null, function AST_Infinity(props) {
  2574. if (props) {
  2575. this.start = props.start;
  2576. this.end = props.end;
  2577. }
  2578. this.flags = 0;
  2579. }, {
  2580. $documentation: "The `Infinity` value",
  2581. value: 1/0
  2582. }, AST_Atom);
  2583. var AST_Boolean = DEFNODE("Boolean", null, function AST_Boolean(props) {
  2584. if (props) {
  2585. this.start = props.start;
  2586. this.end = props.end;
  2587. }
  2588. this.flags = 0;
  2589. }, {
  2590. $documentation: "Base class for booleans",
  2591. }, AST_Atom);
  2592. var AST_False = DEFNODE("False", null, function AST_False(props) {
  2593. if (props) {
  2594. this.start = props.start;
  2595. this.end = props.end;
  2596. }
  2597. this.flags = 0;
  2598. }, {
  2599. $documentation: "The `false` atom",
  2600. value: false
  2601. }, AST_Boolean);
  2602. var AST_True = DEFNODE("True", null, function AST_True(props) {
  2603. if (props) {
  2604. this.start = props.start;
  2605. this.end = props.end;
  2606. }
  2607. this.flags = 0;
  2608. }, {
  2609. $documentation: "The `true` atom",
  2610. value: true
  2611. }, AST_Boolean);
  2612. /* -----[ Walk function ]---- */
  2613. /**
  2614. * Walk nodes in depth-first search fashion.
  2615. * Callback can return `walk_abort` symbol to stop iteration.
  2616. * It can also return `true` to stop iteration just for child nodes.
  2617. * Iteration can be stopped and continued by passing the `to_visit` argument,
  2618. * which is given to the callback in the second argument.
  2619. **/
  2620. function walk(node, cb, to_visit = [node]) {
  2621. const push = to_visit.push.bind(to_visit);
  2622. while (to_visit.length) {
  2623. const node = to_visit.pop();
  2624. const ret = cb(node, to_visit);
  2625. if (ret) {
  2626. if (ret === walk_abort) return true;
  2627. continue;
  2628. }
  2629. node._children_backwards(push);
  2630. }
  2631. return false;
  2632. }
  2633. /**
  2634. * Walks an AST node and its children.
  2635. *
  2636. * {cb} can return `walk_abort` to interrupt the walk.
  2637. *
  2638. * @param node
  2639. * @param cb {(node, info: { parent: (nth) => any }) => (boolean | undefined)}
  2640. *
  2641. * @returns {boolean} whether the walk was aborted
  2642. *
  2643. * @example
  2644. * const found_some_cond = walk_parent(my_ast_node, (node, { parent }) => {
  2645. * if (some_cond(node, parent())) return walk_abort
  2646. * });
  2647. */
  2648. function walk_parent(node, cb, initial_stack) {
  2649. const to_visit = [node];
  2650. const push = to_visit.push.bind(to_visit);
  2651. const stack = initial_stack ? initial_stack.slice() : [];
  2652. const parent_pop_indices = [];
  2653. let current;
  2654. const info = {
  2655. parent: (n = 0) => {
  2656. if (n === -1) {
  2657. return current;
  2658. }
  2659. // [ p1 p0 ] [ 1 0 ]
  2660. if (initial_stack && n >= stack.length) {
  2661. n -= stack.length;
  2662. return initial_stack[
  2663. initial_stack.length - (n + 1)
  2664. ];
  2665. }
  2666. return stack[stack.length - (1 + n)];
  2667. },
  2668. };
  2669. while (to_visit.length) {
  2670. current = to_visit.pop();
  2671. while (
  2672. parent_pop_indices.length &&
  2673. to_visit.length == parent_pop_indices[parent_pop_indices.length - 1]
  2674. ) {
  2675. stack.pop();
  2676. parent_pop_indices.pop();
  2677. }
  2678. const ret = cb(current, info);
  2679. if (ret) {
  2680. if (ret === walk_abort) return true;
  2681. continue;
  2682. }
  2683. const visit_length = to_visit.length;
  2684. current._children_backwards(push);
  2685. // Push only if we're going to traverse the children
  2686. if (to_visit.length > visit_length) {
  2687. stack.push(current);
  2688. parent_pop_indices.push(visit_length - 1);
  2689. }
  2690. }
  2691. return false;
  2692. }
  2693. const walk_abort = Symbol("abort walk");
  2694. /* -----[ TreeWalker ]----- */
  2695. class TreeWalker {
  2696. constructor(callback) {
  2697. this.visit = callback;
  2698. this.stack = [];
  2699. this.directives = Object.create(null);
  2700. }
  2701. _visit(node, descend) {
  2702. this.push(node);
  2703. var ret = this.visit(node, descend ? function() {
  2704. descend.call(node);
  2705. } : noop);
  2706. if (!ret && descend) {
  2707. descend.call(node);
  2708. }
  2709. this.pop();
  2710. return ret;
  2711. }
  2712. parent(n) {
  2713. return this.stack[this.stack.length - 2 - (n || 0)];
  2714. }
  2715. push(node) {
  2716. if (node instanceof AST_Lambda) {
  2717. this.directives = Object.create(this.directives);
  2718. } else if (node instanceof AST_Directive && !this.directives[node.value]) {
  2719. this.directives[node.value] = node;
  2720. } else if (node instanceof AST_Class) {
  2721. this.directives = Object.create(this.directives);
  2722. if (!this.directives["use strict"]) {
  2723. this.directives["use strict"] = node;
  2724. }
  2725. }
  2726. this.stack.push(node);
  2727. }
  2728. pop() {
  2729. var node = this.stack.pop();
  2730. if (node instanceof AST_Lambda || node instanceof AST_Class) {
  2731. this.directives = Object.getPrototypeOf(this.directives);
  2732. }
  2733. }
  2734. self() {
  2735. return this.stack[this.stack.length - 1];
  2736. }
  2737. find_parent(type) {
  2738. var stack = this.stack;
  2739. for (var i = stack.length; --i >= 0;) {
  2740. var x = stack[i];
  2741. if (x instanceof type) return x;
  2742. }
  2743. }
  2744. find_scope() {
  2745. var stack = this.stack;
  2746. for (var i = stack.length; --i >= 0;) {
  2747. const p = stack[i];
  2748. if (p instanceof AST_Toplevel) return p;
  2749. if (p instanceof AST_Lambda) return p;
  2750. if (p.block_scope) return p.block_scope;
  2751. }
  2752. }
  2753. has_directive(type) {
  2754. var dir = this.directives[type];
  2755. if (dir) return dir;
  2756. var node = this.stack[this.stack.length - 1];
  2757. if (node instanceof AST_Scope && node.body) {
  2758. for (var i = 0; i < node.body.length; ++i) {
  2759. var st = node.body[i];
  2760. if (!(st instanceof AST_Directive)) break;
  2761. if (st.value == type) return st;
  2762. }
  2763. }
  2764. }
  2765. loopcontrol_target(node) {
  2766. var stack = this.stack;
  2767. if (node.label) for (var i = stack.length; --i >= 0;) {
  2768. var x = stack[i];
  2769. if (x instanceof AST_LabeledStatement && x.label.name == node.label.name)
  2770. return x.body;
  2771. } else for (var i = stack.length; --i >= 0;) {
  2772. var x = stack[i];
  2773. if (x instanceof AST_IterationStatement
  2774. || node instanceof AST_Break && x instanceof AST_Switch)
  2775. return x;
  2776. }
  2777. }
  2778. }
  2779. // Tree transformer helpers.
  2780. class TreeTransformer extends TreeWalker {
  2781. constructor(before, after) {
  2782. super();
  2783. this.before = before;
  2784. this.after = after;
  2785. }
  2786. }
  2787. const _PURE = 0b00000001;
  2788. const _INLINE = 0b00000010;
  2789. const _NOINLINE = 0b00000100;
  2790. export {
  2791. AST_Accessor,
  2792. AST_Array,
  2793. AST_Arrow,
  2794. AST_Assign,
  2795. AST_Atom,
  2796. AST_Await,
  2797. AST_BigInt,
  2798. AST_Binary,
  2799. AST_Block,
  2800. AST_BlockStatement,
  2801. AST_Boolean,
  2802. AST_Break,
  2803. AST_Call,
  2804. AST_Case,
  2805. AST_Catch,
  2806. AST_Chain,
  2807. AST_Class,
  2808. AST_ClassExpression,
  2809. AST_ClassPrivateProperty,
  2810. AST_PrivateIn,
  2811. AST_ClassProperty,
  2812. AST_ClassStaticBlock,
  2813. AST_ConciseMethod,
  2814. AST_Conditional,
  2815. AST_Const,
  2816. AST_Constant,
  2817. AST_Continue,
  2818. AST_Debugger,
  2819. AST_Default,
  2820. AST_DefaultAssign,
  2821. AST_DefClass,
  2822. AST_Definitions,
  2823. AST_Defun,
  2824. AST_Destructuring,
  2825. AST_Directive,
  2826. AST_Do,
  2827. AST_Dot,
  2828. AST_DotHash,
  2829. AST_DWLoop,
  2830. AST_EmptyStatement,
  2831. AST_Exit,
  2832. AST_Expansion,
  2833. AST_Export,
  2834. AST_False,
  2835. AST_Finally,
  2836. AST_For,
  2837. AST_ForIn,
  2838. AST_ForOf,
  2839. AST_Function,
  2840. AST_Hole,
  2841. AST_If,
  2842. AST_Import,
  2843. AST_ImportMeta,
  2844. AST_Infinity,
  2845. AST_IterationStatement,
  2846. AST_Jump,
  2847. AST_Label,
  2848. AST_LabeledStatement,
  2849. AST_LabelRef,
  2850. AST_Lambda,
  2851. AST_Let,
  2852. AST_LoopControl,
  2853. AST_NameMapping,
  2854. AST_NaN,
  2855. AST_New,
  2856. AST_NewTarget,
  2857. AST_Node,
  2858. AST_Null,
  2859. AST_Number,
  2860. AST_Object,
  2861. AST_ObjectGetter,
  2862. AST_ObjectKeyVal,
  2863. AST_ObjectProperty,
  2864. AST_ObjectSetter,
  2865. AST_PrefixedTemplateString,
  2866. AST_PrivateGetter,
  2867. AST_PrivateMethod,
  2868. AST_PrivateSetter,
  2869. AST_PropAccess,
  2870. AST_RegExp,
  2871. AST_Return,
  2872. AST_Scope,
  2873. AST_Sequence,
  2874. AST_SimpleStatement,
  2875. AST_Statement,
  2876. AST_StatementWithBody,
  2877. AST_String,
  2878. AST_Sub,
  2879. AST_Super,
  2880. AST_Switch,
  2881. AST_SwitchBranch,
  2882. AST_Symbol,
  2883. AST_SymbolBlockDeclaration,
  2884. AST_SymbolCatch,
  2885. AST_SymbolClass,
  2886. AST_SymbolClassProperty,
  2887. AST_SymbolConst,
  2888. AST_SymbolDeclaration,
  2889. AST_SymbolDefClass,
  2890. AST_SymbolDefun,
  2891. AST_SymbolExport,
  2892. AST_SymbolExportForeign,
  2893. AST_SymbolFunarg,
  2894. AST_SymbolImport,
  2895. AST_SymbolImportForeign,
  2896. AST_SymbolLambda,
  2897. AST_SymbolLet,
  2898. AST_SymbolMethod,
  2899. AST_SymbolRef,
  2900. AST_SymbolVar,
  2901. AST_TemplateSegment,
  2902. AST_TemplateString,
  2903. AST_SymbolPrivateProperty,
  2904. AST_This,
  2905. AST_Throw,
  2906. AST_Token,
  2907. AST_Toplevel,
  2908. AST_True,
  2909. AST_Try,
  2910. AST_Unary,
  2911. AST_UnaryPostfix,
  2912. AST_UnaryPrefix,
  2913. AST_Undefined,
  2914. AST_Var,
  2915. AST_VarDef,
  2916. AST_While,
  2917. AST_With,
  2918. AST_Yield,
  2919. // Walkers
  2920. TreeTransformer,
  2921. TreeWalker,
  2922. walk,
  2923. walk_abort,
  2924. walk_body,
  2925. walk_parent,
  2926. // annotations
  2927. _INLINE,
  2928. _NOINLINE,
  2929. _PURE,
  2930. };