reduce-vars.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 {
  34. AST_Accessor,
  35. AST_Array,
  36. AST_Assign,
  37. AST_Await,
  38. AST_Binary,
  39. AST_Block,
  40. AST_Call,
  41. AST_Case,
  42. AST_Chain,
  43. AST_Class,
  44. AST_ClassStaticBlock,
  45. AST_ClassExpression,
  46. AST_Conditional,
  47. AST_Default,
  48. AST_Defun,
  49. AST_Destructuring,
  50. AST_Do,
  51. AST_Exit,
  52. AST_Expansion,
  53. AST_For,
  54. AST_ForIn,
  55. AST_If,
  56. AST_LabeledStatement,
  57. AST_Lambda,
  58. AST_New,
  59. AST_Node,
  60. AST_Number,
  61. AST_ObjectKeyVal,
  62. AST_PropAccess,
  63. AST_Scope,
  64. AST_Sequence,
  65. AST_SimpleStatement,
  66. AST_Symbol,
  67. AST_SymbolCatch,
  68. AST_SymbolConst,
  69. AST_SymbolDefun,
  70. AST_SymbolFunarg,
  71. AST_SymbolLambda,
  72. AST_SymbolRef,
  73. AST_This,
  74. AST_Toplevel,
  75. AST_Try,
  76. AST_Unary,
  77. AST_UnaryPrefix,
  78. AST_Undefined,
  79. AST_VarDef,
  80. AST_While,
  81. AST_Yield,
  82. walk,
  83. walk_abort,
  84. walk_body,
  85. _INLINE,
  86. _NOINLINE,
  87. _PURE
  88. } from "../ast.js";
  89. import { HOP, make_node, noop } from "../utils/index.js";
  90. import { lazy_op, is_modified } from "./inference.js";
  91. import { INLINED, clear_flag } from "./compressor-flags.js";
  92. import { read_property, has_break_or_continue, is_recursive_ref } from "./common.js";
  93. /**
  94. * Define the method AST_Node#reduce_vars, which goes through the AST in
  95. * execution order to perform basic flow analysis
  96. */
  97. function def_reduce_vars(node, func) {
  98. node.DEFMETHOD("reduce_vars", func);
  99. }
  100. def_reduce_vars(AST_Node, noop);
  101. /** Clear definition properties */
  102. function reset_def(compressor, def) {
  103. def.assignments = 0;
  104. def.chained = false;
  105. def.direct_access = false;
  106. def.escaped = 0;
  107. def.recursive_refs = 0;
  108. def.references = [];
  109. def.single_use = undefined;
  110. if (
  111. def.scope.pinned()
  112. || (def.orig[0] instanceof AST_SymbolFunarg && def.scope.uses_arguments)
  113. ) {
  114. def.fixed = false;
  115. } else if (def.orig[0] instanceof AST_SymbolConst || !compressor.exposed(def)) {
  116. def.fixed = def.init;
  117. } else {
  118. def.fixed = false;
  119. }
  120. }
  121. function reset_variables(tw, compressor, node) {
  122. node.variables.forEach(function(def) {
  123. reset_def(compressor, def);
  124. if (def.fixed === null) {
  125. tw.defs_to_safe_ids.set(def.id, tw.safe_ids);
  126. mark(tw, def, true);
  127. } else if (def.fixed) {
  128. tw.loop_ids.set(def.id, tw.in_loop);
  129. mark(tw, def, true);
  130. }
  131. });
  132. }
  133. function reset_block_variables(compressor, node) {
  134. if (node.block_scope) node.block_scope.variables.forEach((def) => {
  135. reset_def(compressor, def);
  136. });
  137. }
  138. function push(tw) {
  139. tw.safe_ids = Object.create(tw.safe_ids);
  140. }
  141. function pop(tw) {
  142. tw.safe_ids = Object.getPrototypeOf(tw.safe_ids);
  143. }
  144. function mark(tw, def, safe) {
  145. tw.safe_ids[def.id] = safe;
  146. }
  147. function safe_to_read(tw, def) {
  148. if (def.single_use == "m") return false;
  149. if (tw.safe_ids[def.id]) {
  150. if (def.fixed == null) {
  151. var orig = def.orig[0];
  152. if (orig instanceof AST_SymbolFunarg || orig.name == "arguments") return false;
  153. def.fixed = make_node(AST_Undefined, orig);
  154. }
  155. return true;
  156. }
  157. return def.fixed instanceof AST_Defun;
  158. }
  159. function safe_to_assign(tw, def, scope, value) {
  160. if (def.fixed === undefined) return true;
  161. let def_safe_ids;
  162. if (def.fixed === null
  163. && (def_safe_ids = tw.defs_to_safe_ids.get(def.id))
  164. ) {
  165. def_safe_ids[def.id] = false;
  166. tw.defs_to_safe_ids.delete(def.id);
  167. return true;
  168. }
  169. if (!HOP(tw.safe_ids, def.id)) return false;
  170. if (!safe_to_read(tw, def)) return false;
  171. if (def.fixed === false) return false;
  172. if (def.fixed != null && (!value || def.references.length > def.assignments)) return false;
  173. if (def.fixed instanceof AST_Defun) {
  174. return value instanceof AST_Node && def.fixed.parent_scope === scope;
  175. }
  176. return def.orig.every((sym) => {
  177. return !(sym instanceof AST_SymbolConst
  178. || sym instanceof AST_SymbolDefun
  179. || sym instanceof AST_SymbolLambda);
  180. });
  181. }
  182. function ref_once(tw, compressor, def) {
  183. return compressor.option("unused")
  184. && !def.scope.pinned()
  185. && def.references.length - def.recursive_refs == 1
  186. && tw.loop_ids.get(def.id) === tw.in_loop;
  187. }
  188. function is_immutable(value) {
  189. if (!value) return false;
  190. return value.is_constant()
  191. || value instanceof AST_Lambda
  192. || value instanceof AST_This;
  193. }
  194. // A definition "escapes" when its value can leave the point of use.
  195. // Example: `a = b || c`
  196. // In this example, "b" and "c" are escaping, because they're going into "a"
  197. //
  198. // def.escaped is != 0 when it escapes.
  199. //
  200. // When greater than 1, it means that N chained properties will be read off
  201. // of that def before an escape occurs. This is useful for evaluating
  202. // property accesses, where you need to know when to stop.
  203. function mark_escaped(tw, d, scope, node, value, level = 0, depth = 1) {
  204. var parent = tw.parent(level);
  205. if (value) {
  206. if (value.is_constant()) return;
  207. if (value instanceof AST_ClassExpression) return;
  208. }
  209. if (
  210. parent instanceof AST_Assign && (parent.operator === "=" || parent.logical) && node === parent.right
  211. || parent instanceof AST_Call && (node !== parent.expression || parent instanceof AST_New)
  212. || parent instanceof AST_Exit && node === parent.value && node.scope !== d.scope
  213. || parent instanceof AST_VarDef && node === parent.value
  214. || parent instanceof AST_Yield && node === parent.value && node.scope !== d.scope
  215. ) {
  216. if (depth > 1 && !(value && value.is_constant_expression(scope))) depth = 1;
  217. if (!d.escaped || d.escaped > depth) d.escaped = depth;
  218. return;
  219. } else if (
  220. parent instanceof AST_Array
  221. || parent instanceof AST_Await
  222. || parent instanceof AST_Binary && lazy_op.has(parent.operator)
  223. || parent instanceof AST_Conditional && node !== parent.condition
  224. || parent instanceof AST_Expansion
  225. || parent instanceof AST_Sequence && node === parent.tail_node()
  226. ) {
  227. mark_escaped(tw, d, scope, parent, parent, level + 1, depth);
  228. } else if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
  229. var obj = tw.parent(level + 1);
  230. mark_escaped(tw, d, scope, obj, obj, level + 2, depth);
  231. } else if (parent instanceof AST_PropAccess && node === parent.expression) {
  232. value = read_property(value, parent.property);
  233. mark_escaped(tw, d, scope, parent, value, level + 1, depth + 1);
  234. if (value) return;
  235. }
  236. if (level > 0) return;
  237. if (parent instanceof AST_Sequence && node !== parent.tail_node()) return;
  238. if (parent instanceof AST_SimpleStatement) return;
  239. d.direct_access = true;
  240. }
  241. const suppress = node => walk(node, node => {
  242. if (!(node instanceof AST_Symbol)) return;
  243. var d = node.definition();
  244. if (!d) return;
  245. if (node instanceof AST_SymbolRef) d.references.push(node);
  246. d.fixed = false;
  247. });
  248. def_reduce_vars(AST_Accessor, function(tw, descend, compressor) {
  249. push(tw);
  250. reset_variables(tw, compressor, this);
  251. descend();
  252. pop(tw);
  253. return true;
  254. });
  255. def_reduce_vars(AST_Assign, function(tw, descend, compressor) {
  256. var node = this;
  257. if (node.left instanceof AST_Destructuring) {
  258. suppress(node.left);
  259. return;
  260. }
  261. const finish_walk = () => {
  262. if (node.logical) {
  263. node.left.walk(tw);
  264. push(tw);
  265. node.right.walk(tw);
  266. pop(tw);
  267. return true;
  268. }
  269. };
  270. var sym = node.left;
  271. if (!(sym instanceof AST_SymbolRef)) return finish_walk();
  272. var def = sym.definition();
  273. var safe = safe_to_assign(tw, def, sym.scope, node.right);
  274. def.assignments++;
  275. if (!safe) return finish_walk();
  276. var fixed = def.fixed;
  277. if (!fixed && node.operator != "=" && !node.logical) return finish_walk();
  278. var eq = node.operator == "=";
  279. var value = eq ? node.right : node;
  280. if (is_modified(compressor, tw, node, value, 0)) return finish_walk();
  281. def.references.push(sym);
  282. if (!node.logical) {
  283. if (!eq) def.chained = true;
  284. def.fixed = eq ? function() {
  285. return node.right;
  286. } : function() {
  287. return make_node(AST_Binary, node, {
  288. operator: node.operator.slice(0, -1),
  289. left: fixed instanceof AST_Node ? fixed : fixed(),
  290. right: node.right
  291. });
  292. };
  293. }
  294. if (node.logical) {
  295. mark(tw, def, false);
  296. push(tw);
  297. node.right.walk(tw);
  298. pop(tw);
  299. return true;
  300. }
  301. mark(tw, def, false);
  302. node.right.walk(tw);
  303. mark(tw, def, true);
  304. mark_escaped(tw, def, sym.scope, node, value, 0, 1);
  305. return true;
  306. });
  307. def_reduce_vars(AST_Binary, function(tw) {
  308. if (!lazy_op.has(this.operator)) return;
  309. this.left.walk(tw);
  310. push(tw);
  311. this.right.walk(tw);
  312. pop(tw);
  313. return true;
  314. });
  315. def_reduce_vars(AST_Block, function(tw, descend, compressor) {
  316. reset_block_variables(compressor, this);
  317. });
  318. def_reduce_vars(AST_Case, function(tw) {
  319. push(tw);
  320. this.expression.walk(tw);
  321. pop(tw);
  322. push(tw);
  323. walk_body(this, tw);
  324. pop(tw);
  325. return true;
  326. });
  327. def_reduce_vars(AST_Class, function(tw, descend) {
  328. clear_flag(this, INLINED);
  329. push(tw);
  330. descend();
  331. pop(tw);
  332. return true;
  333. });
  334. def_reduce_vars(AST_ClassStaticBlock, function(tw, descend, compressor) {
  335. reset_block_variables(compressor, this);
  336. });
  337. def_reduce_vars(AST_Conditional, function(tw) {
  338. this.condition.walk(tw);
  339. push(tw);
  340. this.consequent.walk(tw);
  341. pop(tw);
  342. push(tw);
  343. this.alternative.walk(tw);
  344. pop(tw);
  345. return true;
  346. });
  347. def_reduce_vars(AST_Chain, function(tw, descend) {
  348. // Chains' conditions apply left-to-right, cumulatively.
  349. // If we walk normally we don't go in that order because we would pop before pushing again
  350. // Solution: AST_PropAccess and AST_Call push when they are optional, and never pop.
  351. // Then we pop everything when they are done being walked.
  352. const safe_ids = tw.safe_ids;
  353. descend();
  354. // Unroll back to start
  355. tw.safe_ids = safe_ids;
  356. return true;
  357. });
  358. def_reduce_vars(AST_Call, function (tw) {
  359. this.expression.walk(tw);
  360. if (this.optional) {
  361. // Never pop -- it's popped at AST_Chain above
  362. push(tw);
  363. }
  364. for (const arg of this.args) arg.walk(tw);
  365. return true;
  366. });
  367. def_reduce_vars(AST_PropAccess, function (tw) {
  368. if (!this.optional) return;
  369. this.expression.walk(tw);
  370. // Never pop -- it's popped at AST_Chain above
  371. push(tw);
  372. if (this.property instanceof AST_Node) this.property.walk(tw);
  373. return true;
  374. });
  375. def_reduce_vars(AST_Default, function(tw, descend) {
  376. push(tw);
  377. descend();
  378. pop(tw);
  379. return true;
  380. });
  381. function mark_lambda(tw, descend, compressor) {
  382. clear_flag(this, INLINED);
  383. push(tw);
  384. reset_variables(tw, compressor, this);
  385. var iife;
  386. if (!this.name
  387. && !this.uses_arguments
  388. && !this.pinned()
  389. && (iife = tw.parent()) instanceof AST_Call
  390. && iife.expression === this
  391. && !iife.args.some(arg => arg instanceof AST_Expansion)
  392. && this.argnames.every(arg_name => arg_name instanceof AST_Symbol)
  393. ) {
  394. // Virtually turn IIFE parameters into variable definitions:
  395. // (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})()
  396. // So existing transformation rules can work on them.
  397. this.argnames.forEach((arg, i) => {
  398. if (!arg.definition) return;
  399. var d = arg.definition();
  400. // Avoid setting fixed when there's more than one origin for a variable value
  401. if (d.orig.length > 1) return;
  402. if (d.fixed === undefined && (!this.uses_arguments || tw.has_directive("use strict"))) {
  403. d.fixed = function() {
  404. return iife.args[i] || make_node(AST_Undefined, iife);
  405. };
  406. tw.loop_ids.set(d.id, tw.in_loop);
  407. mark(tw, d, true);
  408. } else {
  409. d.fixed = false;
  410. }
  411. });
  412. }
  413. descend();
  414. pop(tw);
  415. handle_defined_after_hoist(this);
  416. return true;
  417. }
  418. /**
  419. * It's possible for a hoisted function to use something that's not defined yet. Example:
  420. *
  421. * hoisted();
  422. * var defined_after = true;
  423. * function hoisted() {
  424. * // use defined_after
  425. * }
  426. *
  427. * This function is called on the parent to handle this issue.
  428. */
  429. function handle_defined_after_hoist(parent) {
  430. const defuns = [];
  431. walk(parent, node => {
  432. if (node === parent) return;
  433. if (node instanceof AST_Defun) defuns.push(node);
  434. if (
  435. node instanceof AST_Scope
  436. || node instanceof AST_SimpleStatement
  437. ) return true;
  438. });
  439. for (const defun of defuns) {
  440. const fname_def = defun.name.definition();
  441. const found_self_ref_in_other_defuns = defuns.some(
  442. d => d !== defun && d.enclosed.indexOf(fname_def) !== -1
  443. );
  444. for (const def of defun.enclosed) {
  445. if (
  446. def.fixed === false
  447. || def === fname_def
  448. || def.scope.get_defun_scope() !== parent
  449. ) {
  450. continue;
  451. }
  452. // defun is hoisted, so always safe
  453. if (
  454. def.assignments === 0
  455. && def.orig.length === 1
  456. && def.orig[0] instanceof AST_SymbolDefun
  457. ) {
  458. continue;
  459. }
  460. if (found_self_ref_in_other_defuns) {
  461. def.fixed = false;
  462. continue;
  463. }
  464. // Detect `call_defun(); var used_in_defun = ...`
  465. // Because `used_in_defun` can no longer be fixed
  466. let found_defun = false;
  467. let found_def_after_defun = false;
  468. walk(parent, node => {
  469. if (node === defun) return true;
  470. if (node instanceof AST_Symbol) {
  471. if (!found_defun && node.thedef === fname_def) {
  472. found_defun = true;
  473. } else if (found_defun && node.thedef === def) {
  474. found_def_after_defun = true;
  475. return walk_abort;
  476. }
  477. }
  478. });
  479. if (found_def_after_defun) {
  480. def.fixed = false;
  481. }
  482. }
  483. }
  484. }
  485. def_reduce_vars(AST_Lambda, mark_lambda);
  486. def_reduce_vars(AST_Do, function(tw, descend, compressor) {
  487. reset_block_variables(compressor, this);
  488. const saved_loop = tw.in_loop;
  489. tw.in_loop = this;
  490. push(tw);
  491. this.body.walk(tw);
  492. if (has_break_or_continue(this)) {
  493. pop(tw);
  494. push(tw);
  495. }
  496. this.condition.walk(tw);
  497. pop(tw);
  498. tw.in_loop = saved_loop;
  499. return true;
  500. });
  501. def_reduce_vars(AST_For, function(tw, descend, compressor) {
  502. reset_block_variables(compressor, this);
  503. if (this.init) this.init.walk(tw);
  504. const saved_loop = tw.in_loop;
  505. tw.in_loop = this;
  506. push(tw);
  507. if (this.condition) this.condition.walk(tw);
  508. this.body.walk(tw);
  509. if (this.step) {
  510. if (has_break_or_continue(this)) {
  511. pop(tw);
  512. push(tw);
  513. }
  514. this.step.walk(tw);
  515. }
  516. pop(tw);
  517. tw.in_loop = saved_loop;
  518. return true;
  519. });
  520. def_reduce_vars(AST_ForIn, function(tw, descend, compressor) {
  521. reset_block_variables(compressor, this);
  522. suppress(this.init);
  523. this.object.walk(tw);
  524. const saved_loop = tw.in_loop;
  525. tw.in_loop = this;
  526. push(tw);
  527. this.body.walk(tw);
  528. pop(tw);
  529. tw.in_loop = saved_loop;
  530. return true;
  531. });
  532. def_reduce_vars(AST_If, function(tw) {
  533. this.condition.walk(tw);
  534. push(tw);
  535. this.body.walk(tw);
  536. pop(tw);
  537. if (this.alternative) {
  538. push(tw);
  539. this.alternative.walk(tw);
  540. pop(tw);
  541. }
  542. return true;
  543. });
  544. def_reduce_vars(AST_LabeledStatement, function(tw) {
  545. push(tw);
  546. this.body.walk(tw);
  547. pop(tw);
  548. return true;
  549. });
  550. def_reduce_vars(AST_SymbolCatch, function() {
  551. this.definition().fixed = false;
  552. });
  553. def_reduce_vars(AST_SymbolRef, function(tw, descend, compressor) {
  554. var d = this.definition();
  555. d.references.push(this);
  556. if (d.references.length == 1
  557. && !d.fixed
  558. && d.orig[0] instanceof AST_SymbolDefun) {
  559. tw.loop_ids.set(d.id, tw.in_loop);
  560. }
  561. var fixed_value;
  562. if (d.fixed === undefined || !safe_to_read(tw, d)) {
  563. d.fixed = false;
  564. } else if (d.fixed) {
  565. fixed_value = this.fixed_value();
  566. if (
  567. fixed_value instanceof AST_Lambda
  568. && is_recursive_ref(tw, d)
  569. ) {
  570. d.recursive_refs++;
  571. } else if (fixed_value
  572. && !compressor.exposed(d)
  573. && ref_once(tw, compressor, d)
  574. ) {
  575. d.single_use =
  576. fixed_value instanceof AST_Lambda && !fixed_value.pinned()
  577. || fixed_value instanceof AST_Class
  578. || d.scope === this.scope && fixed_value.is_constant_expression();
  579. } else {
  580. d.single_use = false;
  581. }
  582. if (is_modified(compressor, tw, this, fixed_value, 0, is_immutable(fixed_value))) {
  583. if (d.single_use) {
  584. d.single_use = "m";
  585. } else {
  586. d.fixed = false;
  587. }
  588. }
  589. }
  590. mark_escaped(tw, d, this.scope, this, fixed_value, 0, 1);
  591. });
  592. def_reduce_vars(AST_Toplevel, function(tw, descend, compressor) {
  593. this.globals.forEach(function(def) {
  594. reset_def(compressor, def);
  595. });
  596. reset_variables(tw, compressor, this);
  597. descend();
  598. handle_defined_after_hoist(this);
  599. return true;
  600. });
  601. def_reduce_vars(AST_Try, function(tw, descend, compressor) {
  602. reset_block_variables(compressor, this);
  603. push(tw);
  604. this.body.walk(tw);
  605. pop(tw);
  606. if (this.bcatch) {
  607. push(tw);
  608. this.bcatch.walk(tw);
  609. pop(tw);
  610. }
  611. if (this.bfinally) this.bfinally.walk(tw);
  612. return true;
  613. });
  614. def_reduce_vars(AST_Unary, function(tw) {
  615. var node = this;
  616. if (node.operator !== "++" && node.operator !== "--") return;
  617. var exp = node.expression;
  618. if (!(exp instanceof AST_SymbolRef)) return;
  619. var def = exp.definition();
  620. var safe = safe_to_assign(tw, def, exp.scope, true);
  621. def.assignments++;
  622. if (!safe) return;
  623. var fixed = def.fixed;
  624. if (!fixed) return;
  625. def.references.push(exp);
  626. def.chained = true;
  627. def.fixed = function() {
  628. return make_node(AST_Binary, node, {
  629. operator: node.operator.slice(0, -1),
  630. left: make_node(AST_UnaryPrefix, node, {
  631. operator: "+",
  632. expression: fixed instanceof AST_Node ? fixed : fixed()
  633. }),
  634. right: make_node(AST_Number, node, {
  635. value: 1
  636. })
  637. });
  638. };
  639. mark(tw, def, true);
  640. return true;
  641. });
  642. def_reduce_vars(AST_VarDef, function(tw, descend) {
  643. var node = this;
  644. if (node.name instanceof AST_Destructuring) {
  645. suppress(node.name);
  646. return;
  647. }
  648. var d = node.name.definition();
  649. if (node.value) {
  650. if (safe_to_assign(tw, d, node.name.scope, node.value)) {
  651. d.fixed = function() {
  652. return node.value;
  653. };
  654. tw.loop_ids.set(d.id, tw.in_loop);
  655. mark(tw, d, false);
  656. descend();
  657. mark(tw, d, true);
  658. return true;
  659. } else {
  660. d.fixed = false;
  661. }
  662. }
  663. });
  664. def_reduce_vars(AST_While, function(tw, descend, compressor) {
  665. reset_block_variables(compressor, this);
  666. const saved_loop = tw.in_loop;
  667. tw.in_loop = this;
  668. push(tw);
  669. descend();
  670. pop(tw);
  671. tw.in_loop = saved_loop;
  672. return true;
  673. });