mozilla-ast.js 60 KB

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