mozilla-ast.js 43 KB

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