mozilla-ast.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  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 { make_node } from "./utils/index.js";
  34. import {
  35. AST_Accessor,
  36. AST_Array,
  37. AST_Arrow,
  38. AST_Assign,
  39. AST_Atom,
  40. AST_Await,
  41. AST_BigInt,
  42. AST_Binary,
  43. AST_Block,
  44. AST_BlockStatement,
  45. AST_Boolean,
  46. AST_Break,
  47. AST_Call,
  48. AST_Case,
  49. AST_Catch,
  50. AST_Chain,
  51. AST_Class,
  52. AST_ClassStaticBlock,
  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_PrivateIn,
  106. AST_PropAccess,
  107. AST_RegExp,
  108. AST_Return,
  109. AST_Sequence,
  110. AST_SimpleStatement,
  111. AST_Statement,
  112. AST_String,
  113. AST_Sub,
  114. AST_Super,
  115. AST_Switch,
  116. AST_SwitchBranch,
  117. AST_Symbol,
  118. AST_SymbolCatch,
  119. AST_SymbolClass,
  120. AST_SymbolClassProperty,
  121. AST_SymbolPrivateProperty,
  122. AST_SymbolConst,
  123. AST_SymbolDefClass,
  124. AST_SymbolDefun,
  125. AST_SymbolExport,
  126. AST_SymbolExportForeign,
  127. AST_SymbolFunarg,
  128. AST_SymbolImport,
  129. AST_SymbolImportForeign,
  130. AST_SymbolLambda,
  131. AST_SymbolLet,
  132. AST_SymbolMethod,
  133. AST_SymbolRef,
  134. AST_SymbolVar,
  135. AST_TemplateSegment,
  136. AST_TemplateString,
  137. AST_This,
  138. AST_Throw,
  139. AST_Token,
  140. AST_Toplevel,
  141. AST_True,
  142. AST_Try,
  143. AST_Unary,
  144. AST_UnaryPostfix,
  145. AST_UnaryPrefix,
  146. AST_Var,
  147. AST_VarDef,
  148. AST_While,
  149. AST_With,
  150. AST_Yield,
  151. } from "./ast.js";
  152. import { is_basic_identifier_string } from "./parse.js";
  153. (function() {
  154. var normalize_directives = function(body) {
  155. var in_directive = true;
  156. for (var i = 0; i < body.length; i++) {
  157. if (in_directive && body[i] instanceof AST_Statement && body[i].body instanceof AST_String) {
  158. body[i] = new AST_Directive({
  159. start: body[i].start,
  160. end: body[i].end,
  161. value: body[i].body.value
  162. });
  163. } else if (in_directive && !(body[i] instanceof AST_Statement && body[i].body instanceof AST_String)) {
  164. in_directive = false;
  165. }
  166. }
  167. return body;
  168. };
  169. const assert_clause_from_moz = (assertions) => {
  170. if (assertions && assertions.length > 0) {
  171. return new AST_Object({
  172. start: my_start_token(assertions),
  173. end: my_end_token(assertions),
  174. properties: assertions.map((assertion_kv) =>
  175. new AST_ObjectKeyVal({
  176. start: my_start_token(assertion_kv),
  177. end: my_end_token(assertion_kv),
  178. key: assertion_kv.key.name || assertion_kv.key.value,
  179. value: from_moz(assertion_kv.value)
  180. })
  181. )
  182. });
  183. }
  184. return null;
  185. };
  186. var MOZ_TO_ME = {
  187. Program: function(M) {
  188. return new AST_Toplevel({
  189. start: my_start_token(M),
  190. end: my_end_token(M),
  191. body: normalize_directives(M.body.map(from_moz))
  192. });
  193. },
  194. ArrayPattern: function(M) {
  195. return new AST_Destructuring({
  196. start: my_start_token(M),
  197. end: my_end_token(M),
  198. names: M.elements.map(function(elm) {
  199. if (elm === null) {
  200. return new AST_Hole();
  201. }
  202. return from_moz(elm);
  203. }),
  204. is_array: true
  205. });
  206. },
  207. ObjectPattern: function(M) {
  208. return new AST_Destructuring({
  209. start: my_start_token(M),
  210. end: my_end_token(M),
  211. names: M.properties.map(from_moz),
  212. is_array: false
  213. });
  214. },
  215. AssignmentPattern: function(M) {
  216. return new AST_DefaultAssign({
  217. start: my_start_token(M),
  218. end: my_end_token(M),
  219. left: from_moz(M.left),
  220. operator: "=",
  221. right: from_moz(M.right)
  222. });
  223. },
  224. SpreadElement: function(M) {
  225. return new AST_Expansion({
  226. start: my_start_token(M),
  227. end: my_end_token(M),
  228. expression: from_moz(M.argument)
  229. });
  230. },
  231. RestElement: function(M) {
  232. return new AST_Expansion({
  233. start: my_start_token(M),
  234. end: my_end_token(M),
  235. expression: from_moz(M.argument)
  236. });
  237. },
  238. TemplateElement: function(M) {
  239. return new AST_TemplateSegment({
  240. start: my_start_token(M),
  241. end: my_end_token(M),
  242. value: M.value.cooked,
  243. raw: M.value.raw
  244. });
  245. },
  246. TemplateLiteral: function(M) {
  247. var segments = [];
  248. for (var i = 0; i < M.quasis.length; i++) {
  249. segments.push(from_moz(M.quasis[i]));
  250. if (M.expressions[i]) {
  251. segments.push(from_moz(M.expressions[i]));
  252. }
  253. }
  254. return new AST_TemplateString({
  255. start: my_start_token(M),
  256. end: my_end_token(M),
  257. segments: segments
  258. });
  259. },
  260. TaggedTemplateExpression: function(M) {
  261. return new AST_PrefixedTemplateString({
  262. start: my_start_token(M),
  263. end: my_end_token(M),
  264. template_string: from_moz(M.quasi),
  265. prefix: from_moz(M.tag)
  266. });
  267. },
  268. FunctionDeclaration: function(M) {
  269. return new AST_Defun({
  270. start: my_start_token(M),
  271. end: my_end_token(M),
  272. name: from_moz(M.id),
  273. argnames: M.params.map(from_moz),
  274. is_generator: M.generator,
  275. async: M.async,
  276. body: normalize_directives(from_moz(M.body).body)
  277. });
  278. },
  279. FunctionExpression: function(M) {
  280. return new AST_Function({
  281. start: my_start_token(M),
  282. end: my_end_token(M),
  283. name: from_moz(M.id),
  284. argnames: M.params.map(from_moz),
  285. is_generator: M.generator,
  286. async: M.async,
  287. body: normalize_directives(from_moz(M.body).body)
  288. });
  289. },
  290. ArrowFunctionExpression: function(M) {
  291. const body = M.body.type === "BlockStatement"
  292. ? from_moz(M.body).body
  293. : [make_node(AST_Return, {}, { value: from_moz(M.body) })];
  294. return new AST_Arrow({
  295. start: my_start_token(M),
  296. end: my_end_token(M),
  297. argnames: M.params.map(from_moz),
  298. body,
  299. async: M.async,
  300. });
  301. },
  302. ExpressionStatement: function(M) {
  303. return new AST_SimpleStatement({
  304. start: my_start_token(M),
  305. end: my_end_token(M),
  306. body: from_moz(M.expression)
  307. });
  308. },
  309. TryStatement: function(M) {
  310. var handlers = M.handlers || [M.handler];
  311. if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) {
  312. throw new Error("Multiple catch clauses are not supported.");
  313. }
  314. return new AST_Try({
  315. start : my_start_token(M),
  316. end : my_end_token(M),
  317. body : from_moz(M.block).body,
  318. bcatch : from_moz(handlers[0]),
  319. bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
  320. });
  321. },
  322. Property: function(M) {
  323. var key = M.key;
  324. var args = {
  325. start : my_start_token(key || M.value),
  326. end : my_end_token(M.value),
  327. key : key.type == "Identifier" ? key.name : key.value,
  328. value : from_moz(M.value)
  329. };
  330. if (M.computed) {
  331. args.key = from_moz(M.key);
  332. }
  333. if (M.method) {
  334. args.is_generator = M.value.generator;
  335. args.async = M.value.async;
  336. if (!M.computed) {
  337. args.key = new AST_SymbolMethod({ name: args.key });
  338. } else {
  339. args.key = from_moz(M.key);
  340. }
  341. return new AST_ConciseMethod(args);
  342. }
  343. if (M.kind == "init") {
  344. if (key.type != "Identifier" && key.type != "Literal") {
  345. args.key = from_moz(key);
  346. }
  347. return new AST_ObjectKeyVal(args);
  348. }
  349. if (typeof args.key === "string" || typeof args.key === "number") {
  350. args.key = new AST_SymbolMethod({
  351. name: args.key
  352. });
  353. }
  354. args.value = new AST_Accessor(args.value);
  355. if (M.kind == "get") return new AST_ObjectGetter(args);
  356. if (M.kind == "set") return new AST_ObjectSetter(args);
  357. if (M.kind == "method") {
  358. args.async = M.value.async;
  359. args.is_generator = M.value.generator;
  360. args.quote = M.computed ? "\"" : null;
  361. return new AST_ConciseMethod(args);
  362. }
  363. },
  364. MethodDefinition: function(M) {
  365. var args = {
  366. start : my_start_token(M),
  367. end : my_end_token(M),
  368. key : M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || M.key.value }),
  369. value : from_moz(M.value),
  370. static : M.static,
  371. };
  372. if (M.kind == "get") {
  373. return new AST_ObjectGetter(args);
  374. }
  375. if (M.kind == "set") {
  376. return new AST_ObjectSetter(args);
  377. }
  378. args.is_generator = M.value.generator;
  379. args.async = M.value.async;
  380. return new AST_ConciseMethod(args);
  381. },
  382. FieldDefinition: function(M) {
  383. let key;
  384. if (M.computed) {
  385. key = from_moz(M.key);
  386. } else {
  387. if (M.key.type !== "Identifier") throw new Error("Non-Identifier key in FieldDefinition");
  388. key = from_moz(M.key);
  389. }
  390. return new AST_ClassProperty({
  391. start : my_start_token(M),
  392. end : my_end_token(M),
  393. key,
  394. value : from_moz(M.value),
  395. static : M.static,
  396. });
  397. },
  398. PropertyDefinition: function(M) {
  399. let key;
  400. if (M.computed) {
  401. key = from_moz(M.key);
  402. } else {
  403. if (M.key.type !== "Identifier" && M.key.type !== "PrivateIdentifier") {
  404. throw new Error("Non-Identifier key in PropertyDefinition");
  405. }
  406. key = from_moz(M.key);
  407. }
  408. return new AST_ClassProperty({
  409. start : my_start_token(M),
  410. end : my_end_token(M),
  411. key,
  412. value : from_moz(M.value),
  413. static : M.static,
  414. });
  415. },
  416. StaticBlock: function(M) {
  417. return new AST_ClassStaticBlock({
  418. start : my_start_token(M),
  419. end : my_end_token(M),
  420. body : M.body.map(from_moz),
  421. });
  422. },
  423. ArrayExpression: function(M) {
  424. return new AST_Array({
  425. start : my_start_token(M),
  426. end : my_end_token(M),
  427. elements : M.elements.map(function(elem) {
  428. return elem === null ? new AST_Hole() : from_moz(elem);
  429. })
  430. });
  431. },
  432. ObjectExpression: function(M) {
  433. return new AST_Object({
  434. start : my_start_token(M),
  435. end : my_end_token(M),
  436. properties : M.properties.map(function(prop) {
  437. if (prop.type === "SpreadElement") {
  438. return from_moz(prop);
  439. }
  440. prop.type = "Property";
  441. return from_moz(prop);
  442. })
  443. });
  444. },
  445. SequenceExpression: function(M) {
  446. return new AST_Sequence({
  447. start : my_start_token(M),
  448. end : my_end_token(M),
  449. expressions: M.expressions.map(from_moz)
  450. });
  451. },
  452. MemberExpression: function(M) {
  453. return new (M.computed ? AST_Sub : AST_Dot)({
  454. start : my_start_token(M),
  455. end : my_end_token(M),
  456. property : M.computed ? from_moz(M.property) : M.property.name,
  457. expression : from_moz(M.object),
  458. optional : M.optional || false
  459. });
  460. },
  461. ChainExpression: function(M) {
  462. return new AST_Chain({
  463. start : my_start_token(M),
  464. end : my_end_token(M),
  465. expression : from_moz(M.expression)
  466. });
  467. },
  468. SwitchCase: function(M) {
  469. return new (M.test ? AST_Case : AST_Default)({
  470. start : my_start_token(M),
  471. end : my_end_token(M),
  472. expression : from_moz(M.test),
  473. body : M.consequent.map(from_moz)
  474. });
  475. },
  476. VariableDeclaration: function(M) {
  477. return new (M.kind === "const" ? AST_Const :
  478. M.kind === "let" ? AST_Let : AST_Var)({
  479. start : my_start_token(M),
  480. end : my_end_token(M),
  481. definitions : M.declarations.map(from_moz)
  482. });
  483. },
  484. ImportDeclaration: function(M) {
  485. var imported_name = null;
  486. var imported_names = null;
  487. M.specifiers.forEach(function (specifier) {
  488. if (specifier.type === "ImportSpecifier") {
  489. if (!imported_names) { imported_names = []; }
  490. imported_names.push(new AST_NameMapping({
  491. start: my_start_token(specifier),
  492. end: my_end_token(specifier),
  493. foreign_name: from_moz(specifier.imported),
  494. name: from_moz(specifier.local)
  495. }));
  496. } else if (specifier.type === "ImportDefaultSpecifier") {
  497. imported_name = from_moz(specifier.local);
  498. } else if (specifier.type === "ImportNamespaceSpecifier") {
  499. if (!imported_names) { imported_names = []; }
  500. imported_names.push(new AST_NameMapping({
  501. start: my_start_token(specifier),
  502. end: my_end_token(specifier),
  503. foreign_name: new AST_SymbolImportForeign({ name: "*" }),
  504. name: from_moz(specifier.local)
  505. }));
  506. }
  507. });
  508. return new AST_Import({
  509. start : my_start_token(M),
  510. end : my_end_token(M),
  511. imported_name: imported_name,
  512. imported_names : imported_names,
  513. module_name : from_moz(M.source),
  514. assert_clause: assert_clause_from_moz(M.assertions)
  515. });
  516. },
  517. ExportAllDeclaration: function(M) {
  518. return new AST_Export({
  519. start: my_start_token(M),
  520. end: my_end_token(M),
  521. exported_names: [
  522. new AST_NameMapping({
  523. name: new AST_SymbolExportForeign({ name: "*" }),
  524. foreign_name: new AST_SymbolExportForeign({ name: "*" })
  525. })
  526. ],
  527. module_name: from_moz(M.source),
  528. assert_clause: assert_clause_from_moz(M.assertions)
  529. });
  530. },
  531. ExportNamedDeclaration: function(M) {
  532. return new AST_Export({
  533. start: my_start_token(M),
  534. end: my_end_token(M),
  535. exported_definition: from_moz(M.declaration),
  536. exported_names: M.specifiers && M.specifiers.length ? M.specifiers.map(function (specifier) {
  537. return new AST_NameMapping({
  538. foreign_name: from_moz(specifier.exported),
  539. name: from_moz(specifier.local)
  540. });
  541. }) : null,
  542. module_name: from_moz(M.source),
  543. assert_clause: assert_clause_from_moz(M.assertions)
  544. });
  545. },
  546. ExportDefaultDeclaration: function(M) {
  547. return new AST_Export({
  548. start: my_start_token(M),
  549. end: my_end_token(M),
  550. exported_value: from_moz(M.declaration),
  551. is_default: true
  552. });
  553. },
  554. Literal: function(M) {
  555. var val = M.value, args = {
  556. start : my_start_token(M),
  557. end : my_end_token(M)
  558. };
  559. var rx = M.regex;
  560. if (rx && rx.pattern) {
  561. // RegExpLiteral as per ESTree AST spec
  562. args.value = {
  563. source: rx.pattern,
  564. flags: rx.flags
  565. };
  566. return new AST_RegExp(args);
  567. } else if (rx) {
  568. // support legacy RegExp
  569. const rx_source = M.raw || val;
  570. const match = rx_source.match(/^\/(.*)\/(\w*)$/);
  571. if (!match) throw new Error("Invalid regex source " + rx_source);
  572. const [_, source, flags] = match;
  573. args.value = { source, flags };
  574. return new AST_RegExp(args);
  575. }
  576. if (val === null) return new AST_Null(args);
  577. switch (typeof val) {
  578. case "string":
  579. args.value = val;
  580. return new AST_String(args);
  581. case "number":
  582. args.value = val;
  583. args.raw = M.raw || val.toString();
  584. return new AST_Number(args);
  585. case "boolean":
  586. return new (val ? AST_True : AST_False)(args);
  587. }
  588. },
  589. MetaProperty: function(M) {
  590. if (M.meta.name === "new" && M.property.name === "target") {
  591. return new AST_NewTarget({
  592. start: my_start_token(M),
  593. end: my_end_token(M)
  594. });
  595. } else if (M.meta.name === "import" && M.property.name === "meta") {
  596. return new AST_ImportMeta({
  597. start: my_start_token(M),
  598. end: my_end_token(M)
  599. });
  600. }
  601. },
  602. Identifier: function(M) {
  603. var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
  604. return new ( p.type == "LabeledStatement" ? AST_Label
  605. : p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : p.kind == "let" ? AST_SymbolLet : AST_SymbolVar)
  606. : /Import.*Specifier/.test(p.type) ? (p.local === M ? AST_SymbolImport : AST_SymbolImportForeign)
  607. : p.type == "ExportSpecifier" ? (p.local === M ? AST_SymbolExport : AST_SymbolExportForeign)
  608. : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)
  609. : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
  610. : p.type == "ArrowFunctionExpression" ? (p.params.includes(M)) ? AST_SymbolFunarg : AST_SymbolRef
  611. : p.type == "ClassExpression" ? (p.id === M ? AST_SymbolClass : AST_SymbolRef)
  612. : p.type == "Property" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolMethod)
  613. : p.type == "PropertyDefinition" || p.type === "FieldDefinition" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolClassProperty)
  614. : p.type == "ClassDeclaration" ? (p.id === M ? AST_SymbolDefClass : AST_SymbolRef)
  615. : p.type == "MethodDefinition" ? (p.computed ? AST_SymbolRef : AST_SymbolMethod)
  616. : p.type == "CatchClause" ? AST_SymbolCatch
  617. : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef
  618. : AST_SymbolRef)({
  619. start : my_start_token(M),
  620. end : my_end_token(M),
  621. name : M.name
  622. });
  623. },
  624. BigIntLiteral(M) {
  625. return new AST_BigInt({
  626. start : my_start_token(M),
  627. end : my_end_token(M),
  628. value : M.value
  629. });
  630. },
  631. EmptyStatement: function(M) {
  632. return new AST_EmptyStatement({
  633. start: my_start_token(M),
  634. end: my_end_token(M)
  635. });
  636. },
  637. BlockStatement: function(M) {
  638. return new AST_BlockStatement({
  639. start: my_start_token(M),
  640. end: my_end_token(M),
  641. body: M.body.map(from_moz)
  642. });
  643. },
  644. IfStatement: function(M) {
  645. return new AST_If({
  646. start: my_start_token(M),
  647. end: my_end_token(M),
  648. condition: from_moz(M.test),
  649. body: from_moz(M.consequent),
  650. alternative: from_moz(M.alternate)
  651. });
  652. },
  653. LabeledStatement: function(M) {
  654. return new AST_LabeledStatement({
  655. start: my_start_token(M),
  656. end: my_end_token(M),
  657. label: from_moz(M.label),
  658. body: from_moz(M.body)
  659. });
  660. },
  661. BreakStatement: function(M) {
  662. return new AST_Break({
  663. start: my_start_token(M),
  664. end: my_end_token(M),
  665. label: from_moz(M.label)
  666. });
  667. },
  668. ContinueStatement: function(M) {
  669. return new AST_Continue({
  670. start: my_start_token(M),
  671. end: my_end_token(M),
  672. label: from_moz(M.label)
  673. });
  674. },
  675. WithStatement: function(M) {
  676. return new AST_With({
  677. start: my_start_token(M),
  678. end: my_end_token(M),
  679. expression: from_moz(M.object),
  680. body: from_moz(M.body)
  681. });
  682. },
  683. SwitchStatement: function(M) {
  684. return new AST_Switch({
  685. start: my_start_token(M),
  686. end: my_end_token(M),
  687. expression: from_moz(M.discriminant),
  688. body: M.cases.map(from_moz)
  689. });
  690. },
  691. ReturnStatement: function(M) {
  692. return new AST_Return({
  693. start: my_start_token(M),
  694. end: my_end_token(M),
  695. value: from_moz(M.argument)
  696. });
  697. },
  698. ThrowStatement: function(M) {
  699. return new AST_Throw({
  700. start: my_start_token(M),
  701. end: my_end_token(M),
  702. value: from_moz(M.argument)
  703. });
  704. },
  705. WhileStatement: function(M) {
  706. return new AST_While({
  707. start: my_start_token(M),
  708. end: my_end_token(M),
  709. condition: from_moz(M.test),
  710. body: from_moz(M.body)
  711. });
  712. },
  713. DoWhileStatement: function(M) {
  714. return new AST_Do({
  715. start: my_start_token(M),
  716. end: my_end_token(M),
  717. condition: from_moz(M.test),
  718. body: from_moz(M.body)
  719. });
  720. },
  721. ForStatement: function(M) {
  722. return new AST_For({
  723. start: my_start_token(M),
  724. end: my_end_token(M),
  725. init: from_moz(M.init),
  726. condition: from_moz(M.test),
  727. step: from_moz(M.update),
  728. body: from_moz(M.body)
  729. });
  730. },
  731. ForInStatement: function(M) {
  732. return new AST_ForIn({
  733. start: my_start_token(M),
  734. end: my_end_token(M),
  735. init: from_moz(M.left),
  736. object: from_moz(M.right),
  737. body: from_moz(M.body)
  738. });
  739. },
  740. ForOfStatement: function(M) {
  741. return new AST_ForOf({
  742. start: my_start_token(M),
  743. end: my_end_token(M),
  744. init: from_moz(M.left),
  745. object: from_moz(M.right),
  746. body: from_moz(M.body),
  747. await: M.await
  748. });
  749. },
  750. AwaitExpression: function(M) {
  751. return new AST_Await({
  752. start: my_start_token(M),
  753. end: my_end_token(M),
  754. expression: from_moz(M.argument)
  755. });
  756. },
  757. YieldExpression: function(M) {
  758. return new AST_Yield({
  759. start: my_start_token(M),
  760. end: my_end_token(M),
  761. expression: from_moz(M.argument),
  762. is_star: M.delegate
  763. });
  764. },
  765. DebuggerStatement: function(M) {
  766. return new AST_Debugger({
  767. start: my_start_token(M),
  768. end: my_end_token(M)
  769. });
  770. },
  771. VariableDeclarator: function(M) {
  772. return new AST_VarDef({
  773. start: my_start_token(M),
  774. end: my_end_token(M),
  775. name: from_moz(M.id),
  776. value: from_moz(M.init)
  777. });
  778. },
  779. CatchClause: function(M) {
  780. return new AST_Catch({
  781. start: my_start_token(M),
  782. end: my_end_token(M),
  783. argname: from_moz(M.param),
  784. body: from_moz(M.body).body
  785. });
  786. },
  787. ThisExpression: function(M) {
  788. return new AST_This({
  789. start: my_start_token(M),
  790. end: my_end_token(M)
  791. });
  792. },
  793. Super: function(M) {
  794. return new AST_Super({
  795. start: my_start_token(M),
  796. end: my_end_token(M)
  797. });
  798. },
  799. BinaryExpression: function(M) {
  800. if (M.left.type === "PrivateIdentifier") {
  801. return new AST_PrivateIn({
  802. start: my_start_token(M),
  803. end: my_end_token(M),
  804. key: new AST_SymbolPrivateProperty({
  805. start: my_start_token(M.left),
  806. end: my_end_token(M.left),
  807. name: M.left.name
  808. }),
  809. value: from_moz(M.right),
  810. });
  811. }
  812. return new AST_Binary({
  813. start: my_start_token(M),
  814. end: my_end_token(M),
  815. operator: M.operator,
  816. left: from_moz(M.left),
  817. right: from_moz(M.right)
  818. });
  819. },
  820. LogicalExpression: function(M) {
  821. return new AST_Binary({
  822. start: my_start_token(M),
  823. end: my_end_token(M),
  824. operator: M.operator,
  825. left: from_moz(M.left),
  826. right: from_moz(M.right)
  827. });
  828. },
  829. AssignmentExpression: function(M) {
  830. return new AST_Assign({
  831. start: my_start_token(M),
  832. end: my_end_token(M),
  833. operator: M.operator,
  834. left: from_moz(M.left),
  835. right: from_moz(M.right)
  836. });
  837. },
  838. ConditionalExpression: function(M) {
  839. return new AST_Conditional({
  840. start: my_start_token(M),
  841. end: my_end_token(M),
  842. condition: from_moz(M.test),
  843. consequent: from_moz(M.consequent),
  844. alternative: from_moz(M.alternate)
  845. });
  846. },
  847. NewExpression: function(M) {
  848. return new AST_New({
  849. start: my_start_token(M),
  850. end: my_end_token(M),
  851. expression: from_moz(M.callee),
  852. args: M.arguments.map(from_moz)
  853. });
  854. },
  855. CallExpression: function(M) {
  856. return new AST_Call({
  857. start: my_start_token(M),
  858. end: my_end_token(M),
  859. expression: from_moz(M.callee),
  860. optional: M.optional,
  861. args: M.arguments.map(from_moz)
  862. });
  863. }
  864. };
  865. MOZ_TO_ME.UpdateExpression =
  866. MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) {
  867. var prefix = "prefix" in M ? M.prefix
  868. : M.type == "UnaryExpression" ? true : false;
  869. return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
  870. start : my_start_token(M),
  871. end : my_end_token(M),
  872. operator : M.operator,
  873. expression : from_moz(M.argument)
  874. });
  875. };
  876. MOZ_TO_ME.ClassDeclaration =
  877. MOZ_TO_ME.ClassExpression = function From_Moz_Class(M) {
  878. return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
  879. start : my_start_token(M),
  880. end : my_end_token(M),
  881. name : from_moz(M.id),
  882. extends : from_moz(M.superClass),
  883. properties: M.body.body.map(from_moz)
  884. });
  885. };
  886. def_to_moz(AST_EmptyStatement, function To_Moz_EmptyStatement() {
  887. return {
  888. type: "EmptyStatement"
  889. };
  890. });
  891. def_to_moz(AST_BlockStatement, function To_Moz_BlockStatement(M) {
  892. return {
  893. type: "BlockStatement",
  894. body: M.body.map(to_moz)
  895. };
  896. });
  897. def_to_moz(AST_If, function To_Moz_IfStatement(M) {
  898. return {
  899. type: "IfStatement",
  900. test: to_moz(M.condition),
  901. consequent: to_moz(M.body),
  902. alternate: to_moz(M.alternative)
  903. };
  904. });
  905. def_to_moz(AST_LabeledStatement, function To_Moz_LabeledStatement(M) {
  906. return {
  907. type: "LabeledStatement",
  908. label: to_moz(M.label),
  909. body: to_moz(M.body)
  910. };
  911. });
  912. def_to_moz(AST_Break, function To_Moz_BreakStatement(M) {
  913. return {
  914. type: "BreakStatement",
  915. label: to_moz(M.label)
  916. };
  917. });
  918. def_to_moz(AST_Continue, function To_Moz_ContinueStatement(M) {
  919. return {
  920. type: "ContinueStatement",
  921. label: to_moz(M.label)
  922. };
  923. });
  924. def_to_moz(AST_With, function To_Moz_WithStatement(M) {
  925. return {
  926. type: "WithStatement",
  927. object: to_moz(M.expression),
  928. body: to_moz(M.body)
  929. };
  930. });
  931. def_to_moz(AST_Switch, function To_Moz_SwitchStatement(M) {
  932. return {
  933. type: "SwitchStatement",
  934. discriminant: to_moz(M.expression),
  935. cases: M.body.map(to_moz)
  936. };
  937. });
  938. def_to_moz(AST_Return, function To_Moz_ReturnStatement(M) {
  939. return {
  940. type: "ReturnStatement",
  941. argument: to_moz(M.value)
  942. };
  943. });
  944. def_to_moz(AST_Throw, function To_Moz_ThrowStatement(M) {
  945. return {
  946. type: "ThrowStatement",
  947. argument: to_moz(M.value)
  948. };
  949. });
  950. def_to_moz(AST_While, function To_Moz_WhileStatement(M) {
  951. return {
  952. type: "WhileStatement",
  953. test: to_moz(M.condition),
  954. body: to_moz(M.body)
  955. };
  956. });
  957. def_to_moz(AST_Do, function To_Moz_DoWhileStatement(M) {
  958. return {
  959. type: "DoWhileStatement",
  960. test: to_moz(M.condition),
  961. body: to_moz(M.body)
  962. };
  963. });
  964. def_to_moz(AST_For, function To_Moz_ForStatement(M) {
  965. return {
  966. type: "ForStatement",
  967. init: to_moz(M.init),
  968. test: to_moz(M.condition),
  969. update: to_moz(M.step),
  970. body: to_moz(M.body)
  971. };
  972. });
  973. def_to_moz(AST_ForIn, function To_Moz_ForInStatement(M) {
  974. return {
  975. type: "ForInStatement",
  976. left: to_moz(M.init),
  977. right: to_moz(M.object),
  978. body: to_moz(M.body)
  979. };
  980. });
  981. def_to_moz(AST_ForOf, function To_Moz_ForOfStatement(M) {
  982. return {
  983. type: "ForOfStatement",
  984. left: to_moz(M.init),
  985. right: to_moz(M.object),
  986. body: to_moz(M.body),
  987. await: M.await
  988. };
  989. });
  990. def_to_moz(AST_Await, function To_Moz_AwaitExpression(M) {
  991. return {
  992. type: "AwaitExpression",
  993. argument: to_moz(M.expression)
  994. };
  995. });
  996. def_to_moz(AST_Yield, function To_Moz_YieldExpression(M) {
  997. return {
  998. type: "YieldExpression",
  999. argument: to_moz(M.expression),
  1000. delegate: M.is_star
  1001. };
  1002. });
  1003. def_to_moz(AST_Debugger, function To_Moz_DebuggerStatement() {
  1004. return {
  1005. type: "DebuggerStatement"
  1006. };
  1007. });
  1008. def_to_moz(AST_VarDef, function To_Moz_VariableDeclarator(M) {
  1009. return {
  1010. type: "VariableDeclarator",
  1011. id: to_moz(M.name),
  1012. init: to_moz(M.value)
  1013. };
  1014. });
  1015. def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
  1016. return {
  1017. type: "CatchClause",
  1018. param: to_moz(M.argname),
  1019. body: to_moz_block(M)
  1020. };
  1021. });
  1022. def_to_moz(AST_This, function To_Moz_ThisExpression() {
  1023. return {
  1024. type: "ThisExpression"
  1025. };
  1026. });
  1027. def_to_moz(AST_Super, function To_Moz_Super() {
  1028. return {
  1029. type: "Super"
  1030. };
  1031. });
  1032. def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
  1033. return {
  1034. type: "BinaryExpression",
  1035. operator: M.operator,
  1036. left: to_moz(M.left),
  1037. right: to_moz(M.right)
  1038. };
  1039. });
  1040. def_to_moz(AST_Binary, function To_Moz_LogicalExpression(M) {
  1041. return {
  1042. type: "LogicalExpression",
  1043. operator: M.operator,
  1044. left: to_moz(M.left),
  1045. right: to_moz(M.right)
  1046. };
  1047. });
  1048. def_to_moz(AST_Assign, function To_Moz_AssignmentExpression(M) {
  1049. return {
  1050. type: "AssignmentExpression",
  1051. operator: M.operator,
  1052. left: to_moz(M.left),
  1053. right: to_moz(M.right)
  1054. };
  1055. });
  1056. def_to_moz(AST_Conditional, function To_Moz_ConditionalExpression(M) {
  1057. return {
  1058. type: "ConditionalExpression",
  1059. test: to_moz(M.condition),
  1060. consequent: to_moz(M.consequent),
  1061. alternate: to_moz(M.alternative)
  1062. };
  1063. });
  1064. def_to_moz(AST_New, function To_Moz_NewExpression(M) {
  1065. return {
  1066. type: "NewExpression",
  1067. callee: to_moz(M.expression),
  1068. arguments: M.args.map(to_moz)
  1069. };
  1070. });
  1071. def_to_moz(AST_Call, function To_Moz_CallExpression(M) {
  1072. return {
  1073. type: "CallExpression",
  1074. callee: to_moz(M.expression),
  1075. optional: M.optional,
  1076. arguments: M.args.map(to_moz)
  1077. };
  1078. });
  1079. def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
  1080. return to_moz_scope("Program", M);
  1081. });
  1082. def_to_moz(AST_Expansion, function To_Moz_Spread(M) {
  1083. return {
  1084. type: to_moz_in_destructuring() ? "RestElement" : "SpreadElement",
  1085. argument: to_moz(M.expression)
  1086. };
  1087. });
  1088. def_to_moz(AST_PrefixedTemplateString, function To_Moz_TaggedTemplateExpression(M) {
  1089. return {
  1090. type: "TaggedTemplateExpression",
  1091. tag: to_moz(M.prefix),
  1092. quasi: to_moz(M.template_string)
  1093. };
  1094. });
  1095. def_to_moz(AST_TemplateString, function To_Moz_TemplateLiteral(M) {
  1096. var quasis = [];
  1097. var expressions = [];
  1098. for (var i = 0; i < M.segments.length; i++) {
  1099. if (i % 2 !== 0) {
  1100. expressions.push(to_moz(M.segments[i]));
  1101. } else {
  1102. quasis.push({
  1103. type: "TemplateElement",
  1104. value: {
  1105. raw: M.segments[i].raw,
  1106. cooked: M.segments[i].value
  1107. },
  1108. tail: i === M.segments.length - 1
  1109. });
  1110. }
  1111. }
  1112. return {
  1113. type: "TemplateLiteral",
  1114. quasis: quasis,
  1115. expressions: expressions
  1116. };
  1117. });
  1118. def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
  1119. return {
  1120. type: "FunctionDeclaration",
  1121. id: to_moz(M.name),
  1122. params: M.argnames.map(to_moz),
  1123. generator: M.is_generator,
  1124. async: M.async,
  1125. body: to_moz_scope("BlockStatement", M)
  1126. };
  1127. });
  1128. def_to_moz(AST_Function, function To_Moz_FunctionExpression(M, parent) {
  1129. var is_generator = parent.is_generator !== undefined ?
  1130. parent.is_generator : M.is_generator;
  1131. return {
  1132. type: "FunctionExpression",
  1133. id: to_moz(M.name),
  1134. params: M.argnames.map(to_moz),
  1135. generator: is_generator,
  1136. async: M.async,
  1137. body: to_moz_scope("BlockStatement", M)
  1138. };
  1139. });
  1140. def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) {
  1141. var body = {
  1142. type: "BlockStatement",
  1143. body: M.body.map(to_moz)
  1144. };
  1145. return {
  1146. type: "ArrowFunctionExpression",
  1147. params: M.argnames.map(to_moz),
  1148. async: M.async,
  1149. body: body
  1150. };
  1151. });
  1152. def_to_moz(AST_Destructuring, function To_Moz_ObjectPattern(M) {
  1153. if (M.is_array) {
  1154. return {
  1155. type: "ArrayPattern",
  1156. elements: M.names.map(to_moz)
  1157. };
  1158. }
  1159. return {
  1160. type: "ObjectPattern",
  1161. properties: M.names.map(to_moz)
  1162. };
  1163. });
  1164. def_to_moz(AST_Directive, function To_Moz_Directive(M) {
  1165. return {
  1166. type: "ExpressionStatement",
  1167. expression: {
  1168. type: "Literal",
  1169. value: M.value,
  1170. raw: M.print_to_string()
  1171. },
  1172. directive: M.value
  1173. };
  1174. });
  1175. def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) {
  1176. return {
  1177. type: "ExpressionStatement",
  1178. expression: to_moz(M.body)
  1179. };
  1180. });
  1181. def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) {
  1182. return {
  1183. type: "SwitchCase",
  1184. test: to_moz(M.expression),
  1185. consequent: M.body.map(to_moz)
  1186. };
  1187. });
  1188. def_to_moz(AST_Try, function To_Moz_TryStatement(M) {
  1189. return {
  1190. type: "TryStatement",
  1191. block: to_moz_block(M),
  1192. handler: to_moz(M.bcatch),
  1193. guardedHandlers: [],
  1194. finalizer: to_moz(M.bfinally)
  1195. };
  1196. });
  1197. def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
  1198. return {
  1199. type: "CatchClause",
  1200. param: to_moz(M.argname),
  1201. guard: null,
  1202. body: to_moz_block(M)
  1203. };
  1204. });
  1205. def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) {
  1206. return {
  1207. type: "VariableDeclaration",
  1208. kind:
  1209. M instanceof AST_Const ? "const" :
  1210. M instanceof AST_Let ? "let" : "var",
  1211. declarations: M.definitions.map(to_moz)
  1212. };
  1213. });
  1214. const assert_clause_to_moz = assert_clause => {
  1215. const assertions = [];
  1216. if (assert_clause) {
  1217. for (const { key, value } of assert_clause.properties) {
  1218. const key_moz = is_basic_identifier_string(key)
  1219. ? { type: "Identifier", name: key }
  1220. : { type: "Literal", value: key, raw: JSON.stringify(key) };
  1221. assertions.push({
  1222. type: "ImportAttribute",
  1223. key: key_moz,
  1224. value: to_moz(value)
  1225. });
  1226. }
  1227. }
  1228. return assertions;
  1229. };
  1230. def_to_moz(AST_Export, function To_Moz_ExportDeclaration(M) {
  1231. if (M.exported_names) {
  1232. if (M.exported_names[0].name.name === "*") {
  1233. return {
  1234. type: "ExportAllDeclaration",
  1235. source: to_moz(M.module_name),
  1236. assertions: assert_clause_to_moz(M.assert_clause)
  1237. };
  1238. }
  1239. return {
  1240. type: "ExportNamedDeclaration",
  1241. specifiers: M.exported_names.map(function (name_mapping) {
  1242. return {
  1243. type: "ExportSpecifier",
  1244. exported: to_moz(name_mapping.foreign_name),
  1245. local: to_moz(name_mapping.name)
  1246. };
  1247. }),
  1248. declaration: to_moz(M.exported_definition),
  1249. source: to_moz(M.module_name),
  1250. assertions: assert_clause_to_moz(M.assert_clause)
  1251. };
  1252. }
  1253. return {
  1254. type: M.is_default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration",
  1255. declaration: to_moz(M.exported_value || M.exported_definition)
  1256. };
  1257. });
  1258. def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
  1259. var specifiers = [];
  1260. if (M.imported_name) {
  1261. specifiers.push({
  1262. type: "ImportDefaultSpecifier",
  1263. local: to_moz(M.imported_name)
  1264. });
  1265. }
  1266. if (M.imported_names && M.imported_names[0].foreign_name.name === "*") {
  1267. specifiers.push({
  1268. type: "ImportNamespaceSpecifier",
  1269. local: to_moz(M.imported_names[0].name)
  1270. });
  1271. } else if (M.imported_names) {
  1272. M.imported_names.forEach(function(name_mapping) {
  1273. specifiers.push({
  1274. type: "ImportSpecifier",
  1275. local: to_moz(name_mapping.name),
  1276. imported: to_moz(name_mapping.foreign_name)
  1277. });
  1278. });
  1279. }
  1280. return {
  1281. type: "ImportDeclaration",
  1282. specifiers: specifiers,
  1283. source: to_moz(M.module_name),
  1284. assertions: assert_clause_to_moz(M.assert_clause)
  1285. };
  1286. });
  1287. def_to_moz(AST_ImportMeta, function To_Moz_MetaProperty() {
  1288. return {
  1289. type: "MetaProperty",
  1290. meta: {
  1291. type: "Identifier",
  1292. name: "import"
  1293. },
  1294. property: {
  1295. type: "Identifier",
  1296. name: "meta"
  1297. }
  1298. };
  1299. });
  1300. def_to_moz(AST_Sequence, function To_Moz_SequenceExpression(M) {
  1301. return {
  1302. type: "SequenceExpression",
  1303. expressions: M.expressions.map(to_moz)
  1304. };
  1305. });
  1306. def_to_moz(AST_DotHash, function To_Moz_PrivateMemberExpression(M) {
  1307. return {
  1308. type: "MemberExpression",
  1309. object: to_moz(M.expression),
  1310. computed: false,
  1311. property: {
  1312. type: "PrivateIdentifier",
  1313. name: M.property
  1314. },
  1315. optional: M.optional
  1316. };
  1317. });
  1318. def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {
  1319. var isComputed = M instanceof AST_Sub;
  1320. return {
  1321. type: "MemberExpression",
  1322. object: to_moz(M.expression),
  1323. computed: isComputed,
  1324. property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property},
  1325. optional: M.optional
  1326. };
  1327. });
  1328. def_to_moz(AST_Chain, function To_Moz_ChainExpression(M) {
  1329. return {
  1330. type: "ChainExpression",
  1331. expression: to_moz(M.expression)
  1332. };
  1333. });
  1334. def_to_moz(AST_Unary, function To_Moz_Unary(M) {
  1335. return {
  1336. type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression",
  1337. operator: M.operator,
  1338. prefix: M instanceof AST_UnaryPrefix,
  1339. argument: to_moz(M.expression)
  1340. };
  1341. });
  1342. def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
  1343. if (M.operator == "=" && to_moz_in_destructuring()) {
  1344. return {
  1345. type: "AssignmentPattern",
  1346. left: to_moz(M.left),
  1347. right: to_moz(M.right)
  1348. };
  1349. }
  1350. const type = M.operator == "&&" || M.operator == "||" || M.operator === "??"
  1351. ? "LogicalExpression"
  1352. : "BinaryExpression";
  1353. return {
  1354. type,
  1355. left: to_moz(M.left),
  1356. operator: M.operator,
  1357. right: to_moz(M.right)
  1358. };
  1359. });
  1360. def_to_moz(AST_PrivateIn, function To_Moz_BinaryExpression_PrivateIn(M) {
  1361. return {
  1362. type: "BinaryExpression",
  1363. left: { type: "PrivateIdentifier", name: M.key.name },
  1364. operator: "in",
  1365. right: to_moz(M.value),
  1366. };
  1367. });
  1368. def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) {
  1369. return {
  1370. type: "ArrayExpression",
  1371. elements: M.elements.map(to_moz)
  1372. };
  1373. });
  1374. def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {
  1375. return {
  1376. type: "ObjectExpression",
  1377. properties: M.properties.map(to_moz)
  1378. };
  1379. });
  1380. def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) {
  1381. var key = M.key instanceof AST_Node ? to_moz(M.key) : {
  1382. type: "Identifier",
  1383. value: M.key
  1384. };
  1385. if (typeof M.key === "number") {
  1386. key = {
  1387. type: "Literal",
  1388. value: Number(M.key)
  1389. };
  1390. }
  1391. if (typeof M.key === "string") {
  1392. key = {
  1393. type: "Identifier",
  1394. name: M.key
  1395. };
  1396. }
  1397. var kind;
  1398. var string_or_num = typeof M.key === "string" || typeof M.key === "number";
  1399. var computed = string_or_num ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef;
  1400. if (M instanceof AST_ObjectKeyVal) {
  1401. kind = "init";
  1402. computed = !string_or_num;
  1403. } else
  1404. if (M instanceof AST_ObjectGetter) {
  1405. kind = "get";
  1406. } else
  1407. if (M instanceof AST_ObjectSetter) {
  1408. kind = "set";
  1409. }
  1410. if (M instanceof AST_PrivateGetter || M instanceof AST_PrivateSetter) {
  1411. const kind = M instanceof AST_PrivateGetter ? "get" : "set";
  1412. return {
  1413. type: "MethodDefinition",
  1414. computed: false,
  1415. kind: kind,
  1416. static: M.static,
  1417. key: {
  1418. type: "PrivateIdentifier",
  1419. name: M.key.name
  1420. },
  1421. value: to_moz(M.value)
  1422. };
  1423. }
  1424. if (M instanceof AST_ClassPrivateProperty) {
  1425. return {
  1426. type: "PropertyDefinition",
  1427. key: {
  1428. type: "PrivateIdentifier",
  1429. name: M.key.name
  1430. },
  1431. value: to_moz(M.value),
  1432. computed: false,
  1433. static: M.static
  1434. };
  1435. }
  1436. if (M instanceof AST_ClassProperty) {
  1437. return {
  1438. type: "PropertyDefinition",
  1439. key,
  1440. value: to_moz(M.value),
  1441. computed,
  1442. static: M.static
  1443. };
  1444. }
  1445. if (parent instanceof AST_Class) {
  1446. return {
  1447. type: "MethodDefinition",
  1448. computed: computed,
  1449. kind: kind,
  1450. static: M.static,
  1451. key: to_moz(M.key),
  1452. value: to_moz(M.value)
  1453. };
  1454. }
  1455. return {
  1456. type: "Property",
  1457. computed: computed,
  1458. kind: kind,
  1459. key: key,
  1460. value: to_moz(M.value)
  1461. };
  1462. });
  1463. def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) {
  1464. if (parent instanceof AST_Object) {
  1465. return {
  1466. type: "Property",
  1467. computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
  1468. kind: "init",
  1469. method: true,
  1470. shorthand: false,
  1471. key: to_moz(M.key),
  1472. value: to_moz(M.value)
  1473. };
  1474. }
  1475. const key = M instanceof AST_PrivateMethod
  1476. ? {
  1477. type: "PrivateIdentifier",
  1478. name: M.key.name
  1479. }
  1480. : to_moz(M.key);
  1481. return {
  1482. type: "MethodDefinition",
  1483. kind: M.key === "constructor" ? "constructor" : "method",
  1484. key,
  1485. value: to_moz(M.value),
  1486. computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
  1487. static: M.static,
  1488. };
  1489. });
  1490. def_to_moz(AST_Class, function To_Moz_Class(M) {
  1491. var type = M instanceof AST_ClassExpression ? "ClassExpression" : "ClassDeclaration";
  1492. return {
  1493. type: type,
  1494. superClass: to_moz(M.extends),
  1495. id: M.name ? to_moz(M.name) : null,
  1496. body: {
  1497. type: "ClassBody",
  1498. body: M.properties.map(to_moz)
  1499. }
  1500. };
  1501. });
  1502. def_to_moz(AST_ClassStaticBlock, function To_Moz_StaticBlock(M) {
  1503. return {
  1504. type: "StaticBlock",
  1505. body: M.body.map(to_moz),
  1506. };
  1507. });
  1508. def_to_moz(AST_NewTarget, function To_Moz_MetaProperty() {
  1509. return {
  1510. type: "MetaProperty",
  1511. meta: {
  1512. type: "Identifier",
  1513. name: "new"
  1514. },
  1515. property: {
  1516. type: "Identifier",
  1517. name: "target"
  1518. }
  1519. };
  1520. });
  1521. def_to_moz(AST_Symbol, function To_Moz_Identifier(M, parent) {
  1522. if (M instanceof AST_SymbolMethod && parent.quote) {
  1523. return {
  1524. type: "Literal",
  1525. value: M.name
  1526. };
  1527. }
  1528. var def = M.definition();
  1529. return {
  1530. type: "Identifier",
  1531. name: def ? def.mangled_name || def.name : M.name
  1532. };
  1533. });
  1534. def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) {
  1535. const pattern = M.value.source;
  1536. const flags = M.value.flags;
  1537. return {
  1538. type: "Literal",
  1539. value: null,
  1540. raw: M.print_to_string(),
  1541. regex: { pattern, flags }
  1542. };
  1543. });
  1544. def_to_moz(AST_Constant, function To_Moz_Literal(M) {
  1545. var value = M.value;
  1546. return {
  1547. type: "Literal",
  1548. value: value,
  1549. raw: M.raw || M.print_to_string()
  1550. };
  1551. });
  1552. def_to_moz(AST_Atom, function To_Moz_Atom(M) {
  1553. return {
  1554. type: "Identifier",
  1555. name: String(M.value)
  1556. };
  1557. });
  1558. def_to_moz(AST_BigInt, M => ({
  1559. type: "BigIntLiteral",
  1560. value: M.value
  1561. }));
  1562. AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
  1563. AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
  1564. AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null; });
  1565. AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast);
  1566. AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast);
  1567. /* -----[ tools ]----- */
  1568. function my_start_token(moznode) {
  1569. var loc = moznode.loc, start = loc && loc.start;
  1570. var range = moznode.range;
  1571. return new AST_Token(
  1572. "",
  1573. "",
  1574. start && start.line || 0,
  1575. start && start.column || 0,
  1576. range ? range [0] : moznode.start,
  1577. false,
  1578. [],
  1579. [],
  1580. loc && loc.source,
  1581. );
  1582. }
  1583. function my_end_token(moznode) {
  1584. var loc = moznode.loc, end = loc && loc.end;
  1585. var range = moznode.range;
  1586. return new AST_Token(
  1587. "",
  1588. "",
  1589. end && end.line || 0,
  1590. end && end.column || 0,
  1591. range ? range [0] : moznode.end,
  1592. false,
  1593. [],
  1594. [],
  1595. loc && loc.source,
  1596. );
  1597. }
  1598. var FROM_MOZ_STACK = null;
  1599. function from_moz(node) {
  1600. FROM_MOZ_STACK.push(node);
  1601. var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
  1602. FROM_MOZ_STACK.pop();
  1603. return ret;
  1604. }
  1605. AST_Node.from_mozilla_ast = function(node) {
  1606. var save_stack = FROM_MOZ_STACK;
  1607. FROM_MOZ_STACK = [];
  1608. var ast = from_moz(node);
  1609. FROM_MOZ_STACK = save_stack;
  1610. return ast;
  1611. };
  1612. function set_moz_loc(mynode, moznode) {
  1613. var start = mynode.start;
  1614. var end = mynode.end;
  1615. if (!(start && end)) {
  1616. return moznode;
  1617. }
  1618. if (start.pos != null && end.endpos != null) {
  1619. moznode.range = [start.pos, end.endpos];
  1620. }
  1621. if (start.line) {
  1622. moznode.loc = {
  1623. start: {line: start.line, column: start.col},
  1624. end: end.endline ? {line: end.endline, column: end.endcol} : null
  1625. };
  1626. if (start.file) {
  1627. moznode.loc.source = start.file;
  1628. }
  1629. }
  1630. return moznode;
  1631. }
  1632. function def_to_moz(mytype, handler) {
  1633. mytype.DEFMETHOD("to_mozilla_ast", function(parent) {
  1634. return set_moz_loc(this, handler(this, parent));
  1635. });
  1636. }
  1637. var TO_MOZ_STACK = null;
  1638. function to_moz(node) {
  1639. if (TO_MOZ_STACK === null) { TO_MOZ_STACK = []; }
  1640. TO_MOZ_STACK.push(node);
  1641. var ast = node != null ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
  1642. TO_MOZ_STACK.pop();
  1643. if (TO_MOZ_STACK.length === 0) { TO_MOZ_STACK = null; }
  1644. return ast;
  1645. }
  1646. function to_moz_in_destructuring() {
  1647. var i = TO_MOZ_STACK.length;
  1648. while (i--) {
  1649. if (TO_MOZ_STACK[i] instanceof AST_Destructuring) {
  1650. return true;
  1651. }
  1652. }
  1653. return false;
  1654. }
  1655. function to_moz_block(node) {
  1656. return {
  1657. type: "BlockStatement",
  1658. body: node.body.map(to_moz)
  1659. };
  1660. }
  1661. function to_moz_scope(type, node) {
  1662. var body = node.body.map(to_moz);
  1663. if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) {
  1664. body.unshift(to_moz(new AST_EmptyStatement(node.body[0])));
  1665. }
  1666. return {
  1667. type: type,
  1668. body: body
  1669. };
  1670. }
  1671. })();