ast.js 96 KB

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