You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mozilla-ast.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. function normalize_directives(body) {
  36. var in_directive = true;
  37. for (var i = 0; i < body.length; i++) {
  38. if (in_directive && body[i] instanceof AST_Statement && body[i].body instanceof AST_String) {
  39. body[i] = new AST_Directive({
  40. start: body[i].start,
  41. end: body[i].end,
  42. value: body[i].body.value
  43. });
  44. } else if (in_directive && !(body[i] instanceof AST_Statement && body[i].body instanceof AST_String)) {
  45. in_directive = false;
  46. }
  47. }
  48. return body;
  49. }
  50. var MOZ_TO_ME = {
  51. Program: function(M) {
  52. return new AST_Toplevel({
  53. start: my_start_token(M),
  54. end: my_end_token(M),
  55. body: normalize_directives(M.body.map(from_moz))
  56. });
  57. },
  58. FunctionDeclaration: function(M) {
  59. return new AST_Defun({
  60. start: my_start_token(M),
  61. end: my_end_token(M),
  62. name: from_moz(M.id),
  63. argnames: M.params.map(from_moz),
  64. body: normalize_directives(from_moz(M.body).body)
  65. });
  66. },
  67. FunctionExpression: function(M) {
  68. return new AST_Function({
  69. start: my_start_token(M),
  70. end: my_end_token(M),
  71. name: from_moz(M.id),
  72. argnames: M.params.map(from_moz),
  73. body: normalize_directives(from_moz(M.body).body)
  74. });
  75. },
  76. ExpressionStatement: function(M) {
  77. return new AST_SimpleStatement({
  78. start: my_start_token(M),
  79. end: my_end_token(M),
  80. body: from_moz(M.expression)
  81. });
  82. },
  83. TryStatement: function(M) {
  84. var handlers = M.handlers || [M.handler];
  85. if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) {
  86. throw new Error("Multiple catch clauses are not supported.");
  87. }
  88. return new AST_Try({
  89. start : my_start_token(M),
  90. end : my_end_token(M),
  91. body : from_moz(M.block).body,
  92. bcatch : from_moz(handlers[0]),
  93. bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
  94. });
  95. },
  96. Property: function(M) {
  97. var key = M.key;
  98. var args = {
  99. start : my_start_token(key),
  100. end : my_end_token(M.value),
  101. key : "" + key[key.type == "Identifier" ? "name" : "value"],
  102. value : from_moz(M.value)
  103. };
  104. if (M.kind == "init") return new AST_ObjectKeyVal(args);
  105. args.key = new AST_SymbolAccessor({
  106. name: args.key
  107. });
  108. args.value = new AST_Accessor(args.value);
  109. if (M.kind == "get") return new AST_ObjectGetter(args);
  110. if (M.kind == "set") return new AST_ObjectSetter(args);
  111. },
  112. ArrayExpression: function(M) {
  113. return new AST_Array({
  114. start : my_start_token(M),
  115. end : my_end_token(M),
  116. elements : M.elements.map(function(elem) {
  117. return elem === null ? new AST_Hole() : from_moz(elem);
  118. })
  119. });
  120. },
  121. ObjectExpression: function(M) {
  122. return new AST_Object({
  123. start : my_start_token(M),
  124. end : my_end_token(M),
  125. properties : M.properties.map(function(prop) {
  126. prop.type = "Property";
  127. return from_moz(prop)
  128. })
  129. });
  130. },
  131. SequenceExpression: function(M) {
  132. return new AST_Sequence({
  133. start : my_start_token(M),
  134. end : my_end_token(M),
  135. expressions: M.expressions.map(from_moz)
  136. });
  137. },
  138. MemberExpression: function(M) {
  139. return new (M.computed ? AST_Sub : AST_Dot)({
  140. start : my_start_token(M),
  141. end : my_end_token(M),
  142. property : M.computed ? from_moz(M.property) : M.property.name,
  143. expression : from_moz(M.object)
  144. });
  145. },
  146. SwitchCase: function(M) {
  147. return new (M.test ? AST_Case : AST_Default)({
  148. start : my_start_token(M),
  149. end : my_end_token(M),
  150. expression : from_moz(M.test),
  151. body : M.consequent.map(from_moz)
  152. });
  153. },
  154. VariableDeclaration: function(M) {
  155. return new AST_Var({
  156. start : my_start_token(M),
  157. end : my_end_token(M),
  158. definitions : M.declarations.map(from_moz)
  159. });
  160. },
  161. Literal: function(M) {
  162. var val = M.value, args = {
  163. start : my_start_token(M),
  164. end : my_end_token(M)
  165. };
  166. if (val === null) return new AST_Null(args);
  167. var rx = M.regex;
  168. if (rx && rx.pattern) {
  169. // RegExpLiteral as per ESTree AST spec
  170. args.value = new RegExp(rx.pattern, rx.flags);
  171. args.value.raw_source = rx.pattern;
  172. return new AST_RegExp(args);
  173. } else if (rx) {
  174. // support legacy RegExp
  175. args.value = M.regex && M.raw ? M.raw : val;
  176. return new AST_RegExp(args);
  177. }
  178. switch (typeof val) {
  179. case "string":
  180. args.value = val;
  181. return new AST_String(args);
  182. case "number":
  183. args.value = val;
  184. return new AST_Number(args);
  185. case "boolean":
  186. return new (val ? AST_True : AST_False)(args);
  187. }
  188. },
  189. Identifier: function(M) {
  190. var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
  191. return new ( p.type == "LabeledStatement" ? AST_Label
  192. : p.type == "VariableDeclarator" && p.id === M ? AST_SymbolVar
  193. : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)
  194. : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
  195. : p.type == "CatchClause" ? AST_SymbolCatch
  196. : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef
  197. : AST_SymbolRef)({
  198. start : my_start_token(M),
  199. end : my_end_token(M),
  200. name : M.name
  201. });
  202. },
  203. ThisExpression: function(M) {
  204. return new AST_This({
  205. start : my_start_token(M),
  206. end : my_end_token(M),
  207. name : "this",
  208. });
  209. },
  210. };
  211. MOZ_TO_ME.UpdateExpression =
  212. MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) {
  213. var prefix = "prefix" in M ? M.prefix
  214. : M.type == "UnaryExpression" ? true : false;
  215. return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
  216. start : my_start_token(M),
  217. end : my_end_token(M),
  218. operator : M.operator,
  219. expression : from_moz(M.argument)
  220. });
  221. };
  222. map("EmptyStatement", AST_EmptyStatement);
  223. map("BlockStatement", AST_BlockStatement, "body@body");
  224. map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");
  225. map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");
  226. map("BreakStatement", AST_Break, "label>label");
  227. map("ContinueStatement", AST_Continue, "label>label");
  228. map("WithStatement", AST_With, "object>expression, body>body");
  229. map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");
  230. map("ReturnStatement", AST_Return, "argument>value");
  231. map("ThrowStatement", AST_Throw, "argument>value");
  232. map("WhileStatement", AST_While, "test>condition, body>body");
  233. map("DoWhileStatement", AST_Do, "test>condition, body>body");
  234. map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");
  235. map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");
  236. map("DebuggerStatement", AST_Debugger);
  237. map("VariableDeclarator", AST_VarDef, "id>name, init>value");
  238. map("CatchClause", AST_Catch, "param>argname, body%body");
  239. map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
  240. map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
  241. map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
  242. map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");
  243. map("NewExpression", AST_New, "callee>expression, arguments@args");
  244. map("CallExpression", AST_Call, "callee>expression, arguments@args");
  245. def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
  246. return to_moz_scope("Program", M);
  247. });
  248. def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
  249. return {
  250. type: "FunctionDeclaration",
  251. id: to_moz(M.name),
  252. params: M.argnames.map(to_moz),
  253. body: to_moz_scope("BlockStatement", M)
  254. }
  255. });
  256. def_to_moz(AST_Function, function To_Moz_FunctionExpression(M) {
  257. return {
  258. type: "FunctionExpression",
  259. id: to_moz(M.name),
  260. params: M.argnames.map(to_moz),
  261. body: to_moz_scope("BlockStatement", M)
  262. }
  263. });
  264. def_to_moz(AST_Directive, function To_Moz_Directive(M) {
  265. return {
  266. type: "ExpressionStatement",
  267. expression: {
  268. type: "Literal",
  269. value: M.value
  270. }
  271. };
  272. });
  273. def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) {
  274. return {
  275. type: "ExpressionStatement",
  276. expression: to_moz(M.body)
  277. };
  278. });
  279. def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) {
  280. return {
  281. type: "SwitchCase",
  282. test: to_moz(M.expression),
  283. consequent: M.body.map(to_moz)
  284. };
  285. });
  286. def_to_moz(AST_Try, function To_Moz_TryStatement(M) {
  287. return {
  288. type: "TryStatement",
  289. block: to_moz_block(M),
  290. handler: to_moz(M.bcatch),
  291. guardedHandlers: [],
  292. finalizer: to_moz(M.bfinally)
  293. };
  294. });
  295. def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
  296. return {
  297. type: "CatchClause",
  298. param: to_moz(M.argname),
  299. guard: null,
  300. body: to_moz_block(M)
  301. };
  302. });
  303. def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) {
  304. return {
  305. type: "VariableDeclaration",
  306. kind: "var",
  307. declarations: M.definitions.map(to_moz)
  308. };
  309. });
  310. def_to_moz(AST_Sequence, function To_Moz_SequenceExpression(M) {
  311. return {
  312. type: "SequenceExpression",
  313. expressions: M.expressions.map(to_moz)
  314. };
  315. });
  316. def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {
  317. var isComputed = M instanceof AST_Sub;
  318. return {
  319. type: "MemberExpression",
  320. object: to_moz(M.expression),
  321. computed: isComputed,
  322. property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property}
  323. };
  324. });
  325. def_to_moz(AST_Unary, function To_Moz_Unary(M) {
  326. return {
  327. type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression",
  328. operator: M.operator,
  329. prefix: M instanceof AST_UnaryPrefix,
  330. argument: to_moz(M.expression)
  331. };
  332. });
  333. def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
  334. return {
  335. type: M.operator == "&&" || M.operator == "||" ? "LogicalExpression" : "BinaryExpression",
  336. left: to_moz(M.left),
  337. operator: M.operator,
  338. right: to_moz(M.right)
  339. };
  340. });
  341. def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) {
  342. return {
  343. type: "ArrayExpression",
  344. elements: M.elements.map(to_moz)
  345. };
  346. });
  347. def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {
  348. return {
  349. type: "ObjectExpression",
  350. properties: M.properties.map(to_moz)
  351. };
  352. });
  353. def_to_moz(AST_ObjectProperty, function To_Moz_Property(M) {
  354. var key = {
  355. type: "Literal",
  356. value: M.key instanceof AST_SymbolAccessor ? M.key.name : M.key
  357. };
  358. var kind;
  359. if (M instanceof AST_ObjectKeyVal) {
  360. kind = "init";
  361. } else
  362. if (M instanceof AST_ObjectGetter) {
  363. kind = "get";
  364. } else
  365. if (M instanceof AST_ObjectSetter) {
  366. kind = "set";
  367. }
  368. return {
  369. type: "Property",
  370. kind: kind,
  371. key: key,
  372. value: to_moz(M.value)
  373. };
  374. });
  375. def_to_moz(AST_Symbol, function To_Moz_Identifier(M) {
  376. var def = M.definition();
  377. return {
  378. type: "Identifier",
  379. name: def && def.mangled_name || M.name
  380. };
  381. });
  382. def_to_moz(AST_This, function To_Moz_ThisExpression() {
  383. return { type: "ThisExpression" };
  384. });
  385. def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) {
  386. var flags = M.value.toString().match(/[gimuy]*$/)[0];
  387. var value = "/" + M.value.raw_source + "/" + flags;
  388. return {
  389. type: "Literal",
  390. value: value,
  391. raw: value,
  392. regex: {
  393. pattern: M.value.raw_source,
  394. flags: flags
  395. }
  396. };
  397. });
  398. def_to_moz(AST_Constant, function To_Moz_Literal(M) {
  399. var value = M.value;
  400. if (typeof value === 'number' && (value < 0 || (value === 0 && 1 / value < 0))) {
  401. return {
  402. type: "UnaryExpression",
  403. operator: "-",
  404. prefix: true,
  405. argument: {
  406. type: "Literal",
  407. value: -value,
  408. raw: M.start.raw
  409. }
  410. };
  411. }
  412. return {
  413. type: "Literal",
  414. value: value,
  415. raw: M.start.raw
  416. };
  417. });
  418. def_to_moz(AST_Atom, function To_Moz_Atom(M) {
  419. return {
  420. type: "Identifier",
  421. name: String(M.value)
  422. };
  423. });
  424. AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
  425. AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
  426. AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null });
  427. AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast);
  428. AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast);
  429. /* -----[ tools ]----- */
  430. function raw_token(moznode) {
  431. if (moznode.type == "Literal") {
  432. return moznode.raw != null ? moznode.raw : moznode.value + "";
  433. }
  434. }
  435. function my_start_token(moznode) {
  436. var loc = moznode.loc, start = loc && loc.start;
  437. var range = moznode.range;
  438. return new AST_Token({
  439. file : loc && loc.source,
  440. line : start && start.line,
  441. col : start && start.column,
  442. pos : range ? range[0] : moznode.start,
  443. endline : start && start.line,
  444. endcol : start && start.column,
  445. endpos : range ? range[0] : moznode.start,
  446. raw : raw_token(moznode),
  447. });
  448. }
  449. function my_end_token(moznode) {
  450. var loc = moznode.loc, end = loc && loc.end;
  451. var range = moznode.range;
  452. return new AST_Token({
  453. file : loc && loc.source,
  454. line : end && end.line,
  455. col : end && end.column,
  456. pos : range ? range[1] : moznode.end,
  457. endline : end && end.line,
  458. endcol : end && end.column,
  459. endpos : range ? range[1] : moznode.end,
  460. raw : raw_token(moznode),
  461. });
  462. }
  463. function map(moztype, mytype, propmap) {
  464. var moz_to_me = "function From_Moz_" + moztype + "(M){\n";
  465. moz_to_me += "return new U2." + mytype.name + "({\n" +
  466. "start: my_start_token(M),\n" +
  467. "end: my_end_token(M)";
  468. var me_to_moz = "function To_Moz_" + moztype + "(M){\n";
  469. me_to_moz += "return {\n" +
  470. "type: " + JSON.stringify(moztype);
  471. if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop) {
  472. var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop);
  473. if (!m) throw new Error("Can't understand property map: " + prop);
  474. var moz = m[1], how = m[2], my = m[3];
  475. moz_to_me += ",\n" + my + ": ";
  476. me_to_moz += ",\n" + moz + ": ";
  477. switch (how) {
  478. case "@":
  479. moz_to_me += "M." + moz + ".map(from_moz)";
  480. me_to_moz += "M." + my + ".map(to_moz)";
  481. break;
  482. case ">":
  483. moz_to_me += "from_moz(M." + moz + ")";
  484. me_to_moz += "to_moz(M." + my + ")";
  485. break;
  486. case "=":
  487. moz_to_me += "M." + moz;
  488. me_to_moz += "M." + my;
  489. break;
  490. case "%":
  491. moz_to_me += "from_moz(M." + moz + ").body";
  492. me_to_moz += "to_moz_block(M)";
  493. break;
  494. default:
  495. throw new Error("Can't understand operator in propmap: " + prop);
  496. }
  497. });
  498. moz_to_me += "\n})\n}";
  499. me_to_moz += "\n}\n}";
  500. //moz_to_me = parse(moz_to_me).print_to_string({ beautify: true });
  501. //me_to_moz = parse(me_to_moz).print_to_string({ beautify: true });
  502. //console.log(moz_to_me);
  503. moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
  504. exports, my_start_token, my_end_token, from_moz
  505. );
  506. me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")(
  507. to_moz, to_moz_block, to_moz_scope
  508. );
  509. MOZ_TO_ME[moztype] = moz_to_me;
  510. def_to_moz(mytype, me_to_moz);
  511. }
  512. var FROM_MOZ_STACK = null;
  513. function from_moz(node) {
  514. FROM_MOZ_STACK.push(node);
  515. var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
  516. FROM_MOZ_STACK.pop();
  517. return ret;
  518. }
  519. AST_Node.from_mozilla_ast = function(node) {
  520. var save_stack = FROM_MOZ_STACK;
  521. FROM_MOZ_STACK = [];
  522. var ast = from_moz(node);
  523. FROM_MOZ_STACK = save_stack;
  524. ast.walk(new TreeWalker(function(node) {
  525. if (node instanceof AST_LabelRef) {
  526. for (var level = 0, parent; parent = this.parent(level); level++) {
  527. if (parent instanceof AST_Scope) break;
  528. if (parent instanceof AST_LabeledStatement && parent.label.name == node.name) {
  529. node.thedef = parent.label;
  530. break;
  531. }
  532. }
  533. if (!node.thedef) {
  534. var s = node.start;
  535. js_error("Undefined label " + node.name, s.file, s.line, s.col, s.pos);
  536. }
  537. }
  538. }));
  539. return ast;
  540. };
  541. function set_moz_loc(mynode, moznode, myparent) {
  542. var start = mynode.start;
  543. var end = mynode.end;
  544. if (start.pos != null && end.endpos != null) {
  545. moznode.range = [start.pos, end.endpos];
  546. }
  547. if (start.line) {
  548. moznode.loc = {
  549. start: {line: start.line, column: start.col},
  550. end: end.endline ? {line: end.endline, column: end.endcol} : null
  551. };
  552. if (start.file) {
  553. moznode.loc.source = start.file;
  554. }
  555. }
  556. return moznode;
  557. }
  558. function def_to_moz(mytype, handler) {
  559. mytype.DEFMETHOD("to_mozilla_ast", function() {
  560. return set_moz_loc(this, handler(this));
  561. });
  562. }
  563. function to_moz(node) {
  564. return node != null ? node.to_mozilla_ast() : null;
  565. }
  566. function to_moz_block(node) {
  567. return {
  568. type: "BlockStatement",
  569. body: node.body.map(to_moz)
  570. };
  571. }
  572. function to_moz_scope(type, node) {
  573. var body = node.body.map(to_moz);
  574. if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) {
  575. body.unshift(to_moz(new AST_EmptyStatement(node.body[0])));
  576. }
  577. return {
  578. type: type,
  579. body: body
  580. };
  581. }
  582. })();