mozilla-ast.js 59 KB

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