mozilla-ast.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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 * as ast from "./ast.js";
  34. import { make_node } from "./utils/index.js";
  35. import {
  36. AST_Accessor,
  37. AST_Array,
  38. AST_Arrow,
  39. AST_Assign,
  40. AST_Atom,
  41. AST_Await,
  42. AST_BigInt,
  43. AST_Binary,
  44. AST_Block,
  45. AST_BlockStatement,
  46. AST_Boolean,
  47. AST_Break,
  48. AST_Call,
  49. AST_Case,
  50. AST_Catch,
  51. AST_Chain,
  52. AST_Class,
  53. AST_ClassExpression,
  54. AST_ClassProperty,
  55. AST_ClassPrivateProperty,
  56. AST_ConciseMethod,
  57. AST_Conditional,
  58. AST_Const,
  59. AST_Constant,
  60. AST_Continue,
  61. AST_Debugger,
  62. AST_Default,
  63. AST_DefaultAssign,
  64. AST_DefClass,
  65. AST_Definitions,
  66. AST_Defun,
  67. AST_Destructuring,
  68. AST_Directive,
  69. AST_Do,
  70. AST_Dot,
  71. AST_DotHash,
  72. AST_EmptyStatement,
  73. AST_Expansion,
  74. AST_Export,
  75. AST_False,
  76. AST_Finally,
  77. AST_For,
  78. AST_ForIn,
  79. AST_ForOf,
  80. AST_Function,
  81. AST_Hole,
  82. AST_If,
  83. AST_Import,
  84. AST_ImportMeta,
  85. AST_Label,
  86. AST_LabeledStatement,
  87. AST_LabelRef,
  88. AST_Lambda,
  89. AST_Let,
  90. AST_NameMapping,
  91. AST_New,
  92. AST_NewTarget,
  93. AST_Node,
  94. AST_Null,
  95. AST_Number,
  96. AST_Object,
  97. AST_ObjectGetter,
  98. AST_ObjectKeyVal,
  99. AST_ObjectProperty,
  100. AST_ObjectSetter,
  101. AST_PrefixedTemplateString,
  102. AST_PrivateGetter,
  103. AST_PrivateMethod,
  104. AST_PrivateSetter,
  105. AST_PropAccess,
  106. AST_RegExp,
  107. AST_Return,
  108. AST_Sequence,
  109. AST_SimpleStatement,
  110. AST_Statement,
  111. AST_String,
  112. AST_Sub,
  113. AST_Super,
  114. AST_Switch,
  115. AST_SwitchBranch,
  116. AST_Symbol,
  117. AST_SymbolCatch,
  118. AST_SymbolClass,
  119. AST_SymbolClassProperty,
  120. AST_SymbolConst,
  121. AST_SymbolDefClass,
  122. AST_SymbolDefun,
  123. AST_SymbolExport,
  124. AST_SymbolExportForeign,
  125. AST_SymbolFunarg,
  126. AST_SymbolImport,
  127. AST_SymbolImportForeign,
  128. AST_SymbolLambda,
  129. AST_SymbolLet,
  130. AST_SymbolMethod,
  131. AST_SymbolRef,
  132. AST_SymbolVar,
  133. AST_TemplateSegment,
  134. AST_TemplateString,
  135. AST_This,
  136. AST_Throw,
  137. AST_Token,
  138. AST_Toplevel,
  139. AST_True,
  140. AST_Try,
  141. AST_Unary,
  142. AST_UnaryPostfix,
  143. AST_UnaryPrefix,
  144. AST_Var,
  145. AST_VarDef,
  146. AST_While,
  147. AST_With,
  148. AST_Yield,
  149. } from "./ast.js";
  150. (function() {
  151. var normalize_directives = function(body) {
  152. var in_directive = true;
  153. for (var i = 0; i < body.length; i++) {
  154. if (in_directive && body[i] instanceof AST_Statement && body[i].body instanceof AST_String) {
  155. body[i] = new AST_Directive({
  156. start: body[i].start,
  157. end: body[i].end,
  158. value: body[i].body.value
  159. });
  160. } else if (in_directive && !(body[i] instanceof AST_Statement && body[i].body instanceof AST_String)) {
  161. in_directive = false;
  162. }
  163. }
  164. return body;
  165. };
  166. var MOZ_TO_ME = {
  167. Program: function(M) {
  168. return new AST_Toplevel({
  169. start: my_start_token(M),
  170. end: my_end_token(M),
  171. body: normalize_directives(M.body.map(from_moz))
  172. });
  173. },
  174. ArrayPattern: function(M) {
  175. return new AST_Destructuring({
  176. start: my_start_token(M),
  177. end: my_end_token(M),
  178. names: M.elements.map(function(elm) {
  179. if (elm === null) {
  180. return new AST_Hole();
  181. }
  182. return from_moz(elm);
  183. }),
  184. is_array: true
  185. });
  186. },
  187. ObjectPattern: function(M) {
  188. return new AST_Destructuring({
  189. start: my_start_token(M),
  190. end: my_end_token(M),
  191. names: M.properties.map(from_moz),
  192. is_array: false
  193. });
  194. },
  195. AssignmentPattern: function(M) {
  196. return new AST_DefaultAssign({
  197. start: my_start_token(M),
  198. end: my_end_token(M),
  199. left: from_moz(M.left),
  200. operator: "=",
  201. right: from_moz(M.right)
  202. });
  203. },
  204. SpreadElement: function(M) {
  205. return new AST_Expansion({
  206. start: my_start_token(M),
  207. end: my_end_token(M),
  208. expression: from_moz(M.argument)
  209. });
  210. },
  211. RestElement: function(M) {
  212. return new AST_Expansion({
  213. start: my_start_token(M),
  214. end: my_end_token(M),
  215. expression: from_moz(M.argument)
  216. });
  217. },
  218. TemplateElement: function(M) {
  219. return new AST_TemplateSegment({
  220. start: my_start_token(M),
  221. end: my_end_token(M),
  222. value: M.value.cooked,
  223. raw: M.value.raw
  224. });
  225. },
  226. TemplateLiteral: function(M) {
  227. var segments = [];
  228. for (var i = 0; i < M.quasis.length; i++) {
  229. segments.push(from_moz(M.quasis[i]));
  230. if (M.expressions[i]) {
  231. segments.push(from_moz(M.expressions[i]));
  232. }
  233. }
  234. return new AST_TemplateString({
  235. start: my_start_token(M),
  236. end: my_end_token(M),
  237. segments: segments
  238. });
  239. },
  240. TaggedTemplateExpression: function(M) {
  241. return new AST_PrefixedTemplateString({
  242. start: my_start_token(M),
  243. end: my_end_token(M),
  244. template_string: from_moz(M.quasi),
  245. prefix: from_moz(M.tag)
  246. });
  247. },
  248. FunctionDeclaration: function(M) {
  249. return new AST_Defun({
  250. start: my_start_token(M),
  251. end: my_end_token(M),
  252. name: from_moz(M.id),
  253. argnames: M.params.map(from_moz),
  254. is_generator: M.generator,
  255. async: M.async,
  256. body: normalize_directives(from_moz(M.body).body)
  257. });
  258. },
  259. FunctionExpression: function(M) {
  260. return new AST_Function({
  261. start: my_start_token(M),
  262. end: my_end_token(M),
  263. name: from_moz(M.id),
  264. argnames: M.params.map(from_moz),
  265. is_generator: M.generator,
  266. async: M.async,
  267. body: normalize_directives(from_moz(M.body).body)
  268. });
  269. },
  270. ArrowFunctionExpression: function(M) {
  271. const body = M.body.type === "BlockStatement"
  272. ? from_moz(M.body).body
  273. : [make_node(AST_Return, {}, { value: from_moz(M.body) })];
  274. return new AST_Arrow({
  275. start: my_start_token(M),
  276. end: my_end_token(M),
  277. argnames: M.params.map(from_moz),
  278. body,
  279. async: M.async,
  280. });
  281. },
  282. ExpressionStatement: function(M) {
  283. return new AST_SimpleStatement({
  284. start: my_start_token(M),
  285. end: my_end_token(M),
  286. body: from_moz(M.expression)
  287. });
  288. },
  289. TryStatement: function(M) {
  290. var handlers = M.handlers || [M.handler];
  291. if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) {
  292. throw new Error("Multiple catch clauses are not supported.");
  293. }
  294. return new AST_Try({
  295. start : my_start_token(M),
  296. end : my_end_token(M),
  297. body : from_moz(M.block).body,
  298. bcatch : from_moz(handlers[0]),
  299. bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
  300. });
  301. },
  302. Property: function(M) {
  303. var key = M.key;
  304. var args = {
  305. start : my_start_token(key || M.value),
  306. end : my_end_token(M.value),
  307. key : key.type == "Identifier" ? key.name : key.value,
  308. value : from_moz(M.value)
  309. };
  310. if (M.computed) {
  311. args.key = from_moz(M.key);
  312. }
  313. if (M.method) {
  314. args.is_generator = M.value.generator;
  315. args.async = M.value.async;
  316. if (!M.computed) {
  317. args.key = new AST_SymbolMethod({ name: args.key });
  318. } else {
  319. args.key = from_moz(M.key);
  320. }
  321. return new AST_ConciseMethod(args);
  322. }
  323. if (M.kind == "init") {
  324. if (key.type != "Identifier" && key.type != "Literal") {
  325. args.key = from_moz(key);
  326. }
  327. return new AST_ObjectKeyVal(args);
  328. }
  329. if (typeof args.key === "string" || typeof args.key === "number") {
  330. args.key = new AST_SymbolMethod({
  331. name: args.key
  332. });
  333. }
  334. args.value = new AST_Accessor(args.value);
  335. if (M.kind == "get") return new AST_ObjectGetter(args);
  336. if (M.kind == "set") return new AST_ObjectSetter(args);
  337. if (M.kind == "method") {
  338. args.async = M.value.async;
  339. args.is_generator = M.value.generator;
  340. args.quote = M.computed ? "\"" : null;
  341. return new AST_ConciseMethod(args);
  342. }
  343. },
  344. MethodDefinition: function(M) {
  345. var args = {
  346. start : my_start_token(M),
  347. end : my_end_token(M),
  348. key : M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || M.key.value }),
  349. value : from_moz(M.value),
  350. static : M.static,
  351. };
  352. if (M.kind == "get") {
  353. return new AST_ObjectGetter(args);
  354. }
  355. if (M.kind == "set") {
  356. return new AST_ObjectSetter(args);
  357. }
  358. args.is_generator = M.value.generator;
  359. args.async = M.value.async;
  360. return new AST_ConciseMethod(args);
  361. },
  362. FieldDefinition: function(M) {
  363. let key;
  364. if (M.computed) {
  365. key = from_moz(M.key);
  366. } else {
  367. if (M.key.type !== "Identifier") throw new Error("Non-Identifier key in FieldDefinition");
  368. key = from_moz(M.key);
  369. }
  370. return new AST_ClassProperty({
  371. start : my_start_token(M),
  372. end : my_end_token(M),
  373. key,
  374. value : from_moz(M.value),
  375. static : M.static,
  376. });
  377. },
  378. PropertyDefinition: function(M) {
  379. let key;
  380. if (M.computed) {
  381. key = from_moz(M.key);
  382. } else {
  383. if (M.key.type !== "Identifier") throw new Error("Non-Identifier key in PropertyDefinition");
  384. key = from_moz(M.key);
  385. }
  386. return new AST_ClassProperty({
  387. start : my_start_token(M),
  388. end : my_end_token(M),
  389. key,
  390. value : from_moz(M.value),
  391. static : M.static,
  392. });
  393. },
  394. ArrayExpression: function(M) {
  395. return new AST_Array({
  396. start : my_start_token(M),
  397. end : my_end_token(M),
  398. elements : M.elements.map(function(elem) {
  399. return elem === null ? new AST_Hole() : from_moz(elem);
  400. })
  401. });
  402. },
  403. ObjectExpression: function(M) {
  404. return new AST_Object({
  405. start : my_start_token(M),
  406. end : my_end_token(M),
  407. properties : M.properties.map(function(prop) {
  408. if (prop.type === "SpreadElement") {
  409. return from_moz(prop);
  410. }
  411. prop.type = "Property";
  412. return from_moz(prop);
  413. })
  414. });
  415. },
  416. SequenceExpression: function(M) {
  417. return new AST_Sequence({
  418. start : my_start_token(M),
  419. end : my_end_token(M),
  420. expressions: M.expressions.map(from_moz)
  421. });
  422. },
  423. MemberExpression: function(M) {
  424. return new (M.computed ? AST_Sub : AST_Dot)({
  425. start : my_start_token(M),
  426. end : my_end_token(M),
  427. property : M.computed ? from_moz(M.property) : M.property.name,
  428. expression : from_moz(M.object),
  429. optional : M.optional || false
  430. });
  431. },
  432. ChainExpression: function(M) {
  433. return new AST_Chain({
  434. start : my_start_token(M),
  435. end : my_end_token(M),
  436. expression : from_moz(M.expression)
  437. });
  438. },
  439. SwitchCase: function(M) {
  440. return new (M.test ? AST_Case : AST_Default)({
  441. start : my_start_token(M),
  442. end : my_end_token(M),
  443. expression : from_moz(M.test),
  444. body : M.consequent.map(from_moz)
  445. });
  446. },
  447. VariableDeclaration: function(M) {
  448. return new (M.kind === "const" ? AST_Const :
  449. M.kind === "let" ? AST_Let : AST_Var)({
  450. start : my_start_token(M),
  451. end : my_end_token(M),
  452. definitions : M.declarations.map(from_moz)
  453. });
  454. },
  455. ImportDeclaration: function(M) {
  456. var imported_name = null;
  457. var imported_names = null;
  458. M.specifiers.forEach(function (specifier) {
  459. if (specifier.type === "ImportSpecifier") {
  460. if (!imported_names) { imported_names = []; }
  461. imported_names.push(new AST_NameMapping({
  462. start: my_start_token(specifier),
  463. end: my_end_token(specifier),
  464. foreign_name: from_moz(specifier.imported),
  465. name: from_moz(specifier.local)
  466. }));
  467. } else if (specifier.type === "ImportDefaultSpecifier") {
  468. imported_name = from_moz(specifier.local);
  469. } else if (specifier.type === "ImportNamespaceSpecifier") {
  470. if (!imported_names) { imported_names = []; }
  471. imported_names.push(new AST_NameMapping({
  472. start: my_start_token(specifier),
  473. end: my_end_token(specifier),
  474. foreign_name: new AST_SymbolImportForeign({ name: "*" }),
  475. name: from_moz(specifier.local)
  476. }));
  477. }
  478. });
  479. return new AST_Import({
  480. start : my_start_token(M),
  481. end : my_end_token(M),
  482. imported_name: imported_name,
  483. imported_names : imported_names,
  484. module_name : from_moz(M.source)
  485. });
  486. },
  487. ExportAllDeclaration: function(M) {
  488. return new AST_Export({
  489. start: my_start_token(M),
  490. end: my_end_token(M),
  491. exported_names: [
  492. new AST_NameMapping({
  493. name: new AST_SymbolExportForeign({ name: "*" }),
  494. foreign_name: new AST_SymbolExportForeign({ name: "*" })
  495. })
  496. ],
  497. module_name: from_moz(M.source)
  498. });
  499. },
  500. ExportNamedDeclaration: function(M) {
  501. return new AST_Export({
  502. start: my_start_token(M),
  503. end: my_end_token(M),
  504. exported_definition: from_moz(M.declaration),
  505. exported_names: M.specifiers && M.specifiers.length ? M.specifiers.map(function (specifier) {
  506. return new AST_NameMapping({
  507. foreign_name: from_moz(specifier.exported),
  508. name: from_moz(specifier.local)
  509. });
  510. }) : null,
  511. module_name: from_moz(M.source)
  512. });
  513. },
  514. ExportDefaultDeclaration: function(M) {
  515. return new AST_Export({
  516. start: my_start_token(M),
  517. end: my_end_token(M),
  518. exported_value: from_moz(M.declaration),
  519. is_default: true
  520. });
  521. },
  522. Literal: function(M) {
  523. var val = M.value, args = {
  524. start : my_start_token(M),
  525. end : my_end_token(M)
  526. };
  527. var rx = M.regex;
  528. if (rx && rx.pattern) {
  529. // RegExpLiteral as per ESTree AST spec
  530. args.value = {
  531. source: rx.pattern,
  532. flags: rx.flags
  533. };
  534. return new AST_RegExp(args);
  535. } else if (rx) {
  536. // support legacy RegExp
  537. const rx_source = M.raw || val;
  538. const match = rx_source.match(/^\/(.*)\/(\w*)$/);
  539. if (!match) throw new Error("Invalid regex source " + rx_source);
  540. const [_, source, flags] = match;
  541. args.value = { source, flags };
  542. return new AST_RegExp(args);
  543. }
  544. if (val === null) return new AST_Null(args);
  545. switch (typeof val) {
  546. case "string":
  547. args.value = val;
  548. return new AST_String(args);
  549. case "number":
  550. args.value = val;
  551. args.raw = M.raw || val.toString();
  552. return new AST_Number(args);
  553. case "boolean":
  554. return new (val ? AST_True : AST_False)(args);
  555. }
  556. },
  557. MetaProperty: function(M) {
  558. if (M.meta.name === "new" && M.property.name === "target") {
  559. return new AST_NewTarget({
  560. start: my_start_token(M),
  561. end: my_end_token(M)
  562. });
  563. } else if (M.meta.name === "import" && M.property.name === "meta") {
  564. return new AST_ImportMeta({
  565. start: my_start_token(M),
  566. end: my_end_token(M)
  567. });
  568. }
  569. },
  570. Identifier: function(M) {
  571. var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
  572. return new ( p.type == "LabeledStatement" ? AST_Label
  573. : p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : p.kind == "let" ? AST_SymbolLet : AST_SymbolVar)
  574. : /Import.*Specifier/.test(p.type) ? (p.local === M ? AST_SymbolImport : AST_SymbolImportForeign)
  575. : p.type == "ExportSpecifier" ? (p.local === M ? AST_SymbolExport : AST_SymbolExportForeign)
  576. : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)
  577. : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
  578. : p.type == "ArrowFunctionExpression" ? (p.params.includes(M)) ? AST_SymbolFunarg : AST_SymbolRef
  579. : p.type == "ClassExpression" ? (p.id === M ? AST_SymbolClass : AST_SymbolRef)
  580. : p.type == "Property" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolMethod)
  581. : p.type == "PropertyDefinition" || p.type === "FieldDefinition" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolClassProperty)
  582. : p.type == "ClassDeclaration" ? (p.id === M ? AST_SymbolDefClass : AST_SymbolRef)
  583. : p.type == "MethodDefinition" ? (p.computed ? AST_SymbolRef : AST_SymbolMethod)
  584. : p.type == "CatchClause" ? AST_SymbolCatch
  585. : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef
  586. : AST_SymbolRef)({
  587. start : my_start_token(M),
  588. end : my_end_token(M),
  589. name : M.name
  590. });
  591. },
  592. BigIntLiteral(M) {
  593. return new AST_BigInt({
  594. start : my_start_token(M),
  595. end : my_end_token(M),
  596. value : M.value
  597. });
  598. }
  599. };
  600. MOZ_TO_ME.UpdateExpression =
  601. MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) {
  602. var prefix = "prefix" in M ? M.prefix
  603. : M.type == "UnaryExpression" ? true : false;
  604. return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
  605. start : my_start_token(M),
  606. end : my_end_token(M),
  607. operator : M.operator,
  608. expression : from_moz(M.argument)
  609. });
  610. };
  611. MOZ_TO_ME.ClassDeclaration =
  612. MOZ_TO_ME.ClassExpression = function From_Moz_Class(M) {
  613. return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
  614. start : my_start_token(M),
  615. end : my_end_token(M),
  616. name : from_moz(M.id),
  617. extends : from_moz(M.superClass),
  618. properties: M.body.body.map(from_moz)
  619. });
  620. };
  621. map("EmptyStatement", AST_EmptyStatement);
  622. map("BlockStatement", AST_BlockStatement, "body@body");
  623. map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");
  624. map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");
  625. map("BreakStatement", AST_Break, "label>label");
  626. map("ContinueStatement", AST_Continue, "label>label");
  627. map("WithStatement", AST_With, "object>expression, body>body");
  628. map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");
  629. map("ReturnStatement", AST_Return, "argument>value");
  630. map("ThrowStatement", AST_Throw, "argument>value");
  631. map("WhileStatement", AST_While, "test>condition, body>body");
  632. map("DoWhileStatement", AST_Do, "test>condition, body>body");
  633. map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");
  634. map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");
  635. map("ForOfStatement", AST_ForOf, "left>init, right>object, body>body, await=await");
  636. map("AwaitExpression", AST_Await, "argument>expression");
  637. map("YieldExpression", AST_Yield, "argument>expression, delegate=is_star");
  638. map("DebuggerStatement", AST_Debugger);
  639. map("VariableDeclarator", AST_VarDef, "id>name, init>value");
  640. map("CatchClause", AST_Catch, "param>argname, body%body");
  641. map("ThisExpression", AST_This);
  642. map("Super", AST_Super);
  643. map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
  644. map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
  645. map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
  646. map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");
  647. map("NewExpression", AST_New, "callee>expression, arguments@args");
  648. map("CallExpression", AST_Call, "callee>expression, optional=optional, arguments@args");
  649. def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
  650. return to_moz_scope("Program", M);
  651. });
  652. def_to_moz(AST_Expansion, function To_Moz_Spread(M) {
  653. return {
  654. type: to_moz_in_destructuring() ? "RestElement" : "SpreadElement",
  655. argument: to_moz(M.expression)
  656. };
  657. });
  658. def_to_moz(AST_PrefixedTemplateString, function To_Moz_TaggedTemplateExpression(M) {
  659. return {
  660. type: "TaggedTemplateExpression",
  661. tag: to_moz(M.prefix),
  662. quasi: to_moz(M.template_string)
  663. };
  664. });
  665. def_to_moz(AST_TemplateString, function To_Moz_TemplateLiteral(M) {
  666. var quasis = [];
  667. var expressions = [];
  668. for (var i = 0; i < M.segments.length; i++) {
  669. if (i % 2 !== 0) {
  670. expressions.push(to_moz(M.segments[i]));
  671. } else {
  672. quasis.push({
  673. type: "TemplateElement",
  674. value: {
  675. raw: M.segments[i].raw,
  676. cooked: M.segments[i].value
  677. },
  678. tail: i === M.segments.length - 1
  679. });
  680. }
  681. }
  682. return {
  683. type: "TemplateLiteral",
  684. quasis: quasis,
  685. expressions: expressions
  686. };
  687. });
  688. def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
  689. return {
  690. type: "FunctionDeclaration",
  691. id: to_moz(M.name),
  692. params: M.argnames.map(to_moz),
  693. generator: M.is_generator,
  694. async: M.async,
  695. body: to_moz_scope("BlockStatement", M)
  696. };
  697. });
  698. def_to_moz(AST_Function, function To_Moz_FunctionExpression(M, parent) {
  699. var is_generator = parent.is_generator !== undefined ?
  700. parent.is_generator : M.is_generator;
  701. return {
  702. type: "FunctionExpression",
  703. id: to_moz(M.name),
  704. params: M.argnames.map(to_moz),
  705. generator: is_generator,
  706. async: M.async,
  707. body: to_moz_scope("BlockStatement", M)
  708. };
  709. });
  710. def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) {
  711. var body = {
  712. type: "BlockStatement",
  713. body: M.body.map(to_moz)
  714. };
  715. return {
  716. type: "ArrowFunctionExpression",
  717. params: M.argnames.map(to_moz),
  718. async: M.async,
  719. body: body
  720. };
  721. });
  722. def_to_moz(AST_Destructuring, function To_Moz_ObjectPattern(M) {
  723. if (M.is_array) {
  724. return {
  725. type: "ArrayPattern",
  726. elements: M.names.map(to_moz)
  727. };
  728. }
  729. return {
  730. type: "ObjectPattern",
  731. properties: M.names.map(to_moz)
  732. };
  733. });
  734. def_to_moz(AST_Directive, function To_Moz_Directive(M) {
  735. return {
  736. type: "ExpressionStatement",
  737. expression: {
  738. type: "Literal",
  739. value: M.value,
  740. raw: M.print_to_string()
  741. },
  742. directive: M.value
  743. };
  744. });
  745. def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) {
  746. return {
  747. type: "ExpressionStatement",
  748. expression: to_moz(M.body)
  749. };
  750. });
  751. def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) {
  752. return {
  753. type: "SwitchCase",
  754. test: to_moz(M.expression),
  755. consequent: M.body.map(to_moz)
  756. };
  757. });
  758. def_to_moz(AST_Try, function To_Moz_TryStatement(M) {
  759. return {
  760. type: "TryStatement",
  761. block: to_moz_block(M),
  762. handler: to_moz(M.bcatch),
  763. guardedHandlers: [],
  764. finalizer: to_moz(M.bfinally)
  765. };
  766. });
  767. def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
  768. return {
  769. type: "CatchClause",
  770. param: to_moz(M.argname),
  771. guard: null,
  772. body: to_moz_block(M)
  773. };
  774. });
  775. def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) {
  776. return {
  777. type: "VariableDeclaration",
  778. kind:
  779. M instanceof AST_Const ? "const" :
  780. M instanceof AST_Let ? "let" : "var",
  781. declarations: M.definitions.map(to_moz)
  782. };
  783. });
  784. def_to_moz(AST_Export, function To_Moz_ExportDeclaration(M) {
  785. if (M.exported_names) {
  786. if (M.exported_names[0].name.name === "*") {
  787. return {
  788. type: "ExportAllDeclaration",
  789. source: to_moz(M.module_name)
  790. };
  791. }
  792. return {
  793. type: "ExportNamedDeclaration",
  794. specifiers: M.exported_names.map(function (name_mapping) {
  795. return {
  796. type: "ExportSpecifier",
  797. exported: to_moz(name_mapping.foreign_name),
  798. local: to_moz(name_mapping.name)
  799. };
  800. }),
  801. declaration: to_moz(M.exported_definition),
  802. source: to_moz(M.module_name)
  803. };
  804. }
  805. return {
  806. type: M.is_default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration",
  807. declaration: to_moz(M.exported_value || M.exported_definition)
  808. };
  809. });
  810. def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
  811. var specifiers = [];
  812. if (M.imported_name) {
  813. specifiers.push({
  814. type: "ImportDefaultSpecifier",
  815. local: to_moz(M.imported_name)
  816. });
  817. }
  818. if (M.imported_names && M.imported_names[0].foreign_name.name === "*") {
  819. specifiers.push({
  820. type: "ImportNamespaceSpecifier",
  821. local: to_moz(M.imported_names[0].name)
  822. });
  823. } else if (M.imported_names) {
  824. M.imported_names.forEach(function(name_mapping) {
  825. specifiers.push({
  826. type: "ImportSpecifier",
  827. local: to_moz(name_mapping.name),
  828. imported: to_moz(name_mapping.foreign_name)
  829. });
  830. });
  831. }
  832. return {
  833. type: "ImportDeclaration",
  834. specifiers: specifiers,
  835. source: to_moz(M.module_name)
  836. };
  837. });
  838. def_to_moz(AST_ImportMeta, function To_Moz_MetaProperty() {
  839. return {
  840. type: "MetaProperty",
  841. meta: {
  842. type: "Identifier",
  843. name: "import"
  844. },
  845. property: {
  846. type: "Identifier",
  847. name: "meta"
  848. }
  849. };
  850. });
  851. def_to_moz(AST_Sequence, function To_Moz_SequenceExpression(M) {
  852. return {
  853. type: "SequenceExpression",
  854. expressions: M.expressions.map(to_moz)
  855. };
  856. });
  857. def_to_moz(AST_DotHash, function To_Moz_PrivateMemberExpression(M) {
  858. return {
  859. type: "MemberExpression",
  860. object: to_moz(M.expression),
  861. computed: false,
  862. property: {
  863. type: "PrivateIdentifier",
  864. name: M.property
  865. },
  866. optional: M.optional
  867. };
  868. });
  869. def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {
  870. var isComputed = M instanceof AST_Sub;
  871. return {
  872. type: "MemberExpression",
  873. object: to_moz(M.expression),
  874. computed: isComputed,
  875. property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property},
  876. optional: M.optional
  877. };
  878. });
  879. def_to_moz(AST_Chain, function To_Moz_ChainExpression(M) {
  880. return {
  881. type: "ChainExpression",
  882. expression: to_moz(M.expression)
  883. };
  884. });
  885. def_to_moz(AST_Unary, function To_Moz_Unary(M) {
  886. return {
  887. type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression",
  888. operator: M.operator,
  889. prefix: M instanceof AST_UnaryPrefix,
  890. argument: to_moz(M.expression)
  891. };
  892. });
  893. def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
  894. if (M.operator == "=" && to_moz_in_destructuring()) {
  895. return {
  896. type: "AssignmentPattern",
  897. left: to_moz(M.left),
  898. right: to_moz(M.right)
  899. };
  900. }
  901. const type = M.operator == "&&" || M.operator == "||" || M.operator === "??"
  902. ? "LogicalExpression"
  903. : "BinaryExpression";
  904. return {
  905. type,
  906. left: to_moz(M.left),
  907. operator: M.operator,
  908. right: to_moz(M.right)
  909. };
  910. });
  911. def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) {
  912. return {
  913. type: "ArrayExpression",
  914. elements: M.elements.map(to_moz)
  915. };
  916. });
  917. def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {
  918. return {
  919. type: "ObjectExpression",
  920. properties: M.properties.map(to_moz)
  921. };
  922. });
  923. def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) {
  924. var key = M.key instanceof AST_Node ? to_moz(M.key) : {
  925. type: "Identifier",
  926. value: M.key
  927. };
  928. if (typeof M.key === "number") {
  929. key = {
  930. type: "Literal",
  931. value: Number(M.key)
  932. };
  933. }
  934. if (typeof M.key === "string") {
  935. key = {
  936. type: "Identifier",
  937. name: M.key
  938. };
  939. }
  940. var kind;
  941. var string_or_num = typeof M.key === "string" || typeof M.key === "number";
  942. var computed = string_or_num ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef;
  943. if (M instanceof AST_ObjectKeyVal) {
  944. kind = "init";
  945. computed = !string_or_num;
  946. } else
  947. if (M instanceof AST_ObjectGetter) {
  948. kind = "get";
  949. } else
  950. if (M instanceof AST_ObjectSetter) {
  951. kind = "set";
  952. }
  953. if (M instanceof AST_PrivateGetter || M instanceof AST_PrivateSetter) {
  954. const kind = M instanceof AST_PrivateGetter ? "get" : "set";
  955. return {
  956. type: "MethodDefinition",
  957. computed: false,
  958. kind: kind,
  959. static: M.static,
  960. key: {
  961. type: "PrivateIdentifier",
  962. name: M.key.name
  963. },
  964. value: to_moz(M.value)
  965. };
  966. }
  967. if (M instanceof AST_ClassPrivateProperty) {
  968. return {
  969. type: "PropertyDefinition",
  970. key: {
  971. type: "PrivateIdentifier",
  972. name: M.key.name
  973. },
  974. value: to_moz(M.value),
  975. computed: false,
  976. static: M.static
  977. };
  978. }
  979. if (M instanceof AST_ClassProperty) {
  980. return {
  981. type: "PropertyDefinition",
  982. key,
  983. value: to_moz(M.value),
  984. computed,
  985. static: M.static
  986. };
  987. }
  988. if (parent instanceof AST_Class) {
  989. return {
  990. type: "MethodDefinition",
  991. computed: computed,
  992. kind: kind,
  993. static: M.static,
  994. key: to_moz(M.key),
  995. value: to_moz(M.value)
  996. };
  997. }
  998. return {
  999. type: "Property",
  1000. computed: computed,
  1001. kind: kind,
  1002. key: key,
  1003. value: to_moz(M.value)
  1004. };
  1005. });
  1006. def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) {
  1007. if (parent instanceof AST_Object) {
  1008. return {
  1009. type: "Property",
  1010. computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
  1011. kind: "init",
  1012. method: true,
  1013. shorthand: false,
  1014. key: to_moz(M.key),
  1015. value: to_moz(M.value)
  1016. };
  1017. }
  1018. const key = M instanceof AST_PrivateMethod
  1019. ? {
  1020. type: "PrivateIdentifier",
  1021. name: M.key.name
  1022. }
  1023. : to_moz(M.key);
  1024. return {
  1025. type: "MethodDefinition",
  1026. kind: M.key === "constructor" ? "constructor" : "method",
  1027. key,
  1028. value: to_moz(M.value),
  1029. computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
  1030. static: M.static,
  1031. };
  1032. });
  1033. def_to_moz(AST_Class, function To_Moz_Class(M) {
  1034. var type = M instanceof AST_ClassExpression ? "ClassExpression" : "ClassDeclaration";
  1035. return {
  1036. type: type,
  1037. superClass: to_moz(M.extends),
  1038. id: M.name ? to_moz(M.name) : null,
  1039. body: {
  1040. type: "ClassBody",
  1041. body: M.properties.map(to_moz)
  1042. }
  1043. };
  1044. });
  1045. def_to_moz(AST_NewTarget, function To_Moz_MetaProperty() {
  1046. return {
  1047. type: "MetaProperty",
  1048. meta: {
  1049. type: "Identifier",
  1050. name: "new"
  1051. },
  1052. property: {
  1053. type: "Identifier",
  1054. name: "target"
  1055. }
  1056. };
  1057. });
  1058. def_to_moz(AST_Symbol, function To_Moz_Identifier(M, parent) {
  1059. if (M instanceof AST_SymbolMethod && parent.quote) {
  1060. return {
  1061. type: "Literal",
  1062. value: M.name
  1063. };
  1064. }
  1065. var def = M.definition();
  1066. return {
  1067. type: "Identifier",
  1068. name: def ? def.mangled_name || def.name : M.name
  1069. };
  1070. });
  1071. def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) {
  1072. const pattern = M.value.source;
  1073. const flags = M.value.flags;
  1074. return {
  1075. type: "Literal",
  1076. value: null,
  1077. raw: M.print_to_string(),
  1078. regex: { pattern, flags }
  1079. };
  1080. });
  1081. def_to_moz(AST_Constant, function To_Moz_Literal(M) {
  1082. var value = M.value;
  1083. return {
  1084. type: "Literal",
  1085. value: value,
  1086. raw: M.raw || M.print_to_string()
  1087. };
  1088. });
  1089. def_to_moz(AST_Atom, function To_Moz_Atom(M) {
  1090. return {
  1091. type: "Identifier",
  1092. name: String(M.value)
  1093. };
  1094. });
  1095. def_to_moz(AST_BigInt, M => ({
  1096. type: "BigIntLiteral",
  1097. value: M.value
  1098. }));
  1099. AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
  1100. AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
  1101. AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null; });
  1102. AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast);
  1103. AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast);
  1104. /* -----[ tools ]----- */
  1105. function my_start_token(moznode) {
  1106. var loc = moznode.loc, start = loc && loc.start;
  1107. var range = moznode.range;
  1108. return new AST_Token(
  1109. "",
  1110. "",
  1111. start && start.line || 0,
  1112. start && start.column || 0,
  1113. range ? range [0] : moznode.start,
  1114. false,
  1115. [],
  1116. [],
  1117. loc && loc.source,
  1118. );
  1119. }
  1120. function my_end_token(moznode) {
  1121. var loc = moznode.loc, end = loc && loc.end;
  1122. var range = moznode.range;
  1123. return new AST_Token(
  1124. "",
  1125. "",
  1126. end && end.line || 0,
  1127. end && end.column || 0,
  1128. range ? range [0] : moznode.end,
  1129. false,
  1130. [],
  1131. [],
  1132. loc && loc.source,
  1133. );
  1134. }
  1135. function map(moztype, mytype, propmap) {
  1136. var moz_to_me = "function From_Moz_" + moztype + "(M){\n";
  1137. moz_to_me += "return new U2." + mytype.name + "({\n" +
  1138. "start: my_start_token(M),\n" +
  1139. "end: my_end_token(M)";
  1140. var me_to_moz = "function To_Moz_" + moztype + "(M){\n";
  1141. me_to_moz += "return {\n" +
  1142. "type: " + JSON.stringify(moztype);
  1143. if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop) {
  1144. var m = /([a-z0-9$_]+)([=@>%])([a-z0-9$_]+)/i.exec(prop);
  1145. if (!m) throw new Error("Can't understand property map: " + prop);
  1146. var moz = m[1], how = m[2], my = m[3];
  1147. moz_to_me += ",\n" + my + ": ";
  1148. me_to_moz += ",\n" + moz + ": ";
  1149. switch (how) {
  1150. case "@":
  1151. moz_to_me += "M." + moz + ".map(from_moz)";
  1152. me_to_moz += "M." + my + ".map(to_moz)";
  1153. break;
  1154. case ">":
  1155. moz_to_me += "from_moz(M." + moz + ")";
  1156. me_to_moz += "to_moz(M." + my + ")";
  1157. break;
  1158. case "=":
  1159. moz_to_me += "M." + moz;
  1160. me_to_moz += "M." + my;
  1161. break;
  1162. case "%":
  1163. moz_to_me += "from_moz(M." + moz + ").body";
  1164. me_to_moz += "to_moz_block(M)";
  1165. break;
  1166. default:
  1167. throw new Error("Can't understand operator in propmap: " + prop);
  1168. }
  1169. });
  1170. moz_to_me += "\n})\n}";
  1171. me_to_moz += "\n}\n}";
  1172. moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
  1173. ast, my_start_token, my_end_token, from_moz
  1174. );
  1175. me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")(
  1176. to_moz, to_moz_block, to_moz_scope
  1177. );
  1178. MOZ_TO_ME[moztype] = moz_to_me;
  1179. def_to_moz(mytype, me_to_moz);
  1180. }
  1181. var FROM_MOZ_STACK = null;
  1182. function from_moz(node) {
  1183. FROM_MOZ_STACK.push(node);
  1184. var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
  1185. FROM_MOZ_STACK.pop();
  1186. return ret;
  1187. }
  1188. AST_Node.from_mozilla_ast = function(node) {
  1189. var save_stack = FROM_MOZ_STACK;
  1190. FROM_MOZ_STACK = [];
  1191. var ast = from_moz(node);
  1192. FROM_MOZ_STACK = save_stack;
  1193. return ast;
  1194. };
  1195. function set_moz_loc(mynode, moznode) {
  1196. var start = mynode.start;
  1197. var end = mynode.end;
  1198. if (!(start && end)) {
  1199. return moznode;
  1200. }
  1201. if (start.pos != null && end.endpos != null) {
  1202. moznode.range = [start.pos, end.endpos];
  1203. }
  1204. if (start.line) {
  1205. moznode.loc = {
  1206. start: {line: start.line, column: start.col},
  1207. end: end.endline ? {line: end.endline, column: end.endcol} : null
  1208. };
  1209. if (start.file) {
  1210. moznode.loc.source = start.file;
  1211. }
  1212. }
  1213. return moznode;
  1214. }
  1215. function def_to_moz(mytype, handler) {
  1216. mytype.DEFMETHOD("to_mozilla_ast", function(parent) {
  1217. return set_moz_loc(this, handler(this, parent));
  1218. });
  1219. }
  1220. var TO_MOZ_STACK = null;
  1221. function to_moz(node) {
  1222. if (TO_MOZ_STACK === null) { TO_MOZ_STACK = []; }
  1223. TO_MOZ_STACK.push(node);
  1224. var ast = node != null ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
  1225. TO_MOZ_STACK.pop();
  1226. if (TO_MOZ_STACK.length === 0) { TO_MOZ_STACK = null; }
  1227. return ast;
  1228. }
  1229. function to_moz_in_destructuring() {
  1230. var i = TO_MOZ_STACK.length;
  1231. while (i--) {
  1232. if (TO_MOZ_STACK[i] instanceof AST_Destructuring) {
  1233. return true;
  1234. }
  1235. }
  1236. return false;
  1237. }
  1238. function to_moz_block(node) {
  1239. return {
  1240. type: "BlockStatement",
  1241. body: node.body.map(to_moz)
  1242. };
  1243. }
  1244. function to_moz_scope(type, node) {
  1245. var body = node.body.map(to_moz);
  1246. if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) {
  1247. body.unshift(to_moz(new AST_EmptyStatement(node.body[0])));
  1248. }
  1249. return {
  1250. type: type,
  1251. body: body
  1252. };
  1253. }
  1254. })();