tighten-body.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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_Array,
  35. AST_Arrow,
  36. AST_Assign,
  37. AST_Await,
  38. AST_Binary,
  39. AST_Block,
  40. AST_BlockStatement,
  41. AST_Break,
  42. AST_Call,
  43. AST_Case,
  44. AST_Catch,
  45. AST_Chain,
  46. AST_Class,
  47. AST_Conditional,
  48. AST_Const,
  49. AST_Constant,
  50. AST_Continue,
  51. AST_Debugger,
  52. AST_Default,
  53. AST_Definitions,
  54. AST_Defun,
  55. AST_Destructuring,
  56. AST_Directive,
  57. AST_Dot,
  58. AST_DWLoop,
  59. AST_EmptyStatement,
  60. AST_Exit,
  61. AST_Expansion,
  62. AST_Export,
  63. AST_Finally,
  64. AST_For,
  65. AST_ForIn,
  66. AST_If,
  67. AST_Import,
  68. AST_IterationStatement,
  69. AST_Lambda,
  70. AST_Let,
  71. AST_LoopControl,
  72. AST_Node,
  73. AST_Number,
  74. AST_Object,
  75. AST_ObjectKeyVal,
  76. AST_PropAccess,
  77. AST_RegExp,
  78. AST_Return,
  79. AST_Scope,
  80. AST_Sequence,
  81. AST_SimpleStatement,
  82. AST_Sub,
  83. AST_Switch,
  84. AST_Symbol,
  85. AST_SymbolConst,
  86. AST_SymbolDeclaration,
  87. AST_SymbolDefun,
  88. AST_SymbolFunarg,
  89. AST_SymbolLambda,
  90. AST_SymbolLet,
  91. AST_SymbolRef,
  92. AST_SymbolVar,
  93. AST_This,
  94. AST_Try,
  95. AST_Unary,
  96. AST_UnaryPostfix,
  97. AST_UnaryPrefix,
  98. AST_Undefined,
  99. AST_Var,
  100. AST_VarDef,
  101. AST_With,
  102. AST_Yield,
  103. TreeTransformer,
  104. TreeWalker,
  105. walk,
  106. walk_abort,
  107. _NOINLINE
  108. } from "../ast.js";
  109. import {
  110. make_node,
  111. MAP,
  112. member,
  113. remove,
  114. has_annotation
  115. } from "../utils/index.js";
  116. import { pure_prop_access_globals } from "./native-objects.js";
  117. import {
  118. lazy_op,
  119. unary_side_effects,
  120. is_modified,
  121. is_lhs,
  122. aborts
  123. } from "./inference.js";
  124. import { WRITE_ONLY, clear_flag } from "./compressor-flags.js";
  125. import {
  126. make_sequence,
  127. merge_sequence,
  128. maintain_this_binding,
  129. is_func_expr,
  130. is_identifier_atom,
  131. is_ref_of,
  132. can_be_evicted_from_block,
  133. as_statement_array,
  134. } from "./common.js";
  135. function loop_body(x) {
  136. if (x instanceof AST_IterationStatement) {
  137. return x.body instanceof AST_BlockStatement ? x.body : x;
  138. }
  139. return x;
  140. }
  141. function is_lhs_read_only(lhs) {
  142. if (lhs instanceof AST_This) return true;
  143. if (lhs instanceof AST_SymbolRef) return lhs.definition().orig[0] instanceof AST_SymbolLambda;
  144. if (lhs instanceof AST_PropAccess) {
  145. lhs = lhs.expression;
  146. if (lhs instanceof AST_SymbolRef) {
  147. if (lhs.is_immutable()) return false;
  148. lhs = lhs.fixed_value();
  149. }
  150. if (!lhs) return true;
  151. if (lhs instanceof AST_RegExp) return false;
  152. if (lhs instanceof AST_Constant) return true;
  153. return is_lhs_read_only(lhs);
  154. }
  155. return false;
  156. }
  157. // Remove code which we know is unreachable.
  158. export function trim_unreachable_code(compressor, stat, target) {
  159. walk(stat, node => {
  160. if (node instanceof AST_Var) {
  161. node.remove_initializers();
  162. target.push(node);
  163. return true;
  164. }
  165. if (
  166. node instanceof AST_Defun
  167. && (node === stat || !compressor.has_directive("use strict"))
  168. ) {
  169. target.push(node === stat ? node : make_node(AST_Var, node, {
  170. definitions: [
  171. make_node(AST_VarDef, node, {
  172. name: make_node(AST_SymbolVar, node.name, node.name),
  173. value: null
  174. })
  175. ]
  176. }));
  177. return true;
  178. }
  179. if (node instanceof AST_Export || node instanceof AST_Import) {
  180. target.push(node);
  181. return true;
  182. }
  183. if (node instanceof AST_Scope) {
  184. return true;
  185. }
  186. });
  187. }
  188. /** Tighten a bunch of statements together, and perform statement-level optimization. */
  189. export function tighten_body(statements, compressor) {
  190. const nearest_scope = compressor.find_scope();
  191. const defun_scope = nearest_scope.get_defun_scope();
  192. const { in_loop, in_try } = find_loop_scope_try();
  193. var CHANGED, max_iter = 10;
  194. do {
  195. CHANGED = false;
  196. eliminate_spurious_blocks(statements);
  197. if (compressor.option("dead_code")) {
  198. eliminate_dead_code(statements, compressor);
  199. }
  200. if (compressor.option("if_return")) {
  201. handle_if_return(statements, compressor);
  202. }
  203. if (compressor.sequences_limit > 0) {
  204. sequencesize(statements, compressor);
  205. sequencesize_2(statements, compressor);
  206. }
  207. if (compressor.option("join_vars")) {
  208. join_consecutive_vars(statements);
  209. }
  210. if (compressor.option("collapse_vars")) {
  211. collapse(statements, compressor);
  212. }
  213. } while (CHANGED && max_iter-- > 0);
  214. function find_loop_scope_try() {
  215. var node = compressor.self(), level = 0, in_loop = false, in_try = false;
  216. do {
  217. if (node instanceof AST_Catch || node instanceof AST_Finally) {
  218. level++;
  219. } else if (node instanceof AST_IterationStatement) {
  220. in_loop = true;
  221. } else if (node instanceof AST_Scope) {
  222. break;
  223. } else if (node instanceof AST_Try) {
  224. in_try = true;
  225. }
  226. } while (node = compressor.parent(level++));
  227. return { in_loop, in_try };
  228. }
  229. // Search from right to left for assignment-like expressions:
  230. // - `var a = x;`
  231. // - `a = x;`
  232. // - `++a`
  233. // For each candidate, scan from left to right for first usage, then try
  234. // to fold assignment into the site for compression.
  235. // Will not attempt to collapse assignments into or past code blocks
  236. // which are not sequentially executed, e.g. loops and conditionals.
  237. function collapse(statements, compressor) {
  238. if (nearest_scope.pinned() || defun_scope.pinned())
  239. return statements;
  240. var args;
  241. var candidates = [];
  242. var stat_index = statements.length;
  243. var scanner = new TreeTransformer(function (node) {
  244. if (abort)
  245. return node;
  246. // Skip nodes before `candidate` as quickly as possible
  247. if (!hit) {
  248. if (node !== hit_stack[hit_index])
  249. return node;
  250. hit_index++;
  251. if (hit_index < hit_stack.length)
  252. return handle_custom_scan_order(node);
  253. hit = true;
  254. stop_after = find_stop(node, 0);
  255. if (stop_after === node)
  256. abort = true;
  257. return node;
  258. }
  259. // Stop immediately if these node types are encountered
  260. var parent = scanner.parent();
  261. if (node instanceof AST_Assign
  262. && (node.logical || node.operator != "=" && lhs.equivalent_to(node.left))
  263. || node instanceof AST_Await
  264. || node instanceof AST_Call && lhs instanceof AST_PropAccess && lhs.equivalent_to(node.expression)
  265. || node instanceof AST_Debugger
  266. || node instanceof AST_Destructuring
  267. || node instanceof AST_Expansion
  268. && node.expression instanceof AST_Symbol
  269. && (
  270. node.expression instanceof AST_This
  271. || node.expression.definition().references.length > 1
  272. )
  273. || node instanceof AST_IterationStatement && !(node instanceof AST_For)
  274. || node instanceof AST_LoopControl
  275. || node instanceof AST_Try
  276. || node instanceof AST_With
  277. || node instanceof AST_Yield
  278. || node instanceof AST_Export
  279. || node instanceof AST_Class
  280. || parent instanceof AST_For && node !== parent.init
  281. || !replace_all
  282. && (
  283. node instanceof AST_SymbolRef
  284. && !node.is_declared(compressor)
  285. && !pure_prop_access_globals.has(node)
  286. )
  287. || node instanceof AST_SymbolRef
  288. && parent instanceof AST_Call
  289. && has_annotation(parent, _NOINLINE)
  290. ) {
  291. abort = true;
  292. return node;
  293. }
  294. // Stop only if candidate is found within conditional branches
  295. if (!stop_if_hit && (!lhs_local || !replace_all)
  296. && (parent instanceof AST_Binary && lazy_op.has(parent.operator) && parent.left !== node
  297. || parent instanceof AST_Conditional && parent.condition !== node
  298. || parent instanceof AST_If && parent.condition !== node)) {
  299. stop_if_hit = parent;
  300. }
  301. // Replace variable with assignment when found
  302. if (
  303. can_replace
  304. && !(node instanceof AST_SymbolDeclaration)
  305. && lhs.equivalent_to(node)
  306. && !shadows(scanner.find_scope() || nearest_scope, lvalues)
  307. ) {
  308. if (stop_if_hit) {
  309. abort = true;
  310. return node;
  311. }
  312. if (is_lhs(node, parent)) {
  313. if (value_def)
  314. replaced++;
  315. return node;
  316. } else {
  317. replaced++;
  318. if (value_def && candidate instanceof AST_VarDef)
  319. return node;
  320. }
  321. CHANGED = abort = true;
  322. if (candidate instanceof AST_UnaryPostfix) {
  323. return make_node(AST_UnaryPrefix, candidate, candidate);
  324. }
  325. if (candidate instanceof AST_VarDef) {
  326. var def = candidate.name.definition();
  327. var value = candidate.value;
  328. if (def.references.length - def.replaced == 1 && !compressor.exposed(def)) {
  329. def.replaced++;
  330. if (funarg && is_identifier_atom(value)) {
  331. return value.transform(compressor);
  332. } else {
  333. return maintain_this_binding(parent, node, value);
  334. }
  335. }
  336. return make_node(AST_Assign, candidate, {
  337. operator: "=",
  338. logical: false,
  339. left: make_node(AST_SymbolRef, candidate.name, candidate.name),
  340. right: value
  341. });
  342. }
  343. clear_flag(candidate, WRITE_ONLY);
  344. return candidate;
  345. }
  346. // These node types have child nodes that execute sequentially,
  347. // but are otherwise not safe to scan into or beyond them.
  348. var sym;
  349. if (node instanceof AST_Call
  350. || node instanceof AST_Exit
  351. && (side_effects || lhs instanceof AST_PropAccess || may_modify(lhs))
  352. || node instanceof AST_PropAccess
  353. && (side_effects || node.expression.may_throw_on_access(compressor))
  354. || node instanceof AST_SymbolRef
  355. && ((lvalues.has(node.name) && lvalues.get(node.name).modified) || side_effects && may_modify(node))
  356. || node instanceof AST_VarDef && node.value
  357. && (lvalues.has(node.name.name) || side_effects && may_modify(node.name))
  358. || (sym = is_lhs(node.left, node))
  359. && (sym instanceof AST_PropAccess || lvalues.has(sym.name))
  360. || may_throw
  361. && (in_try ? node.has_side_effects(compressor) : side_effects_external(node))) {
  362. stop_after = node;
  363. if (node instanceof AST_Scope)
  364. abort = true;
  365. }
  366. return handle_custom_scan_order(node);
  367. }, function (node) {
  368. if (abort)
  369. return;
  370. if (stop_after === node)
  371. abort = true;
  372. if (stop_if_hit === node)
  373. stop_if_hit = null;
  374. });
  375. var multi_replacer = new TreeTransformer(function (node) {
  376. if (abort)
  377. return node;
  378. // Skip nodes before `candidate` as quickly as possible
  379. if (!hit) {
  380. if (node !== hit_stack[hit_index])
  381. return node;
  382. hit_index++;
  383. if (hit_index < hit_stack.length)
  384. return;
  385. hit = true;
  386. return node;
  387. }
  388. // Replace variable when found
  389. if (node instanceof AST_SymbolRef
  390. && node.name == def.name) {
  391. if (!--replaced)
  392. abort = true;
  393. if (is_lhs(node, multi_replacer.parent()))
  394. return node;
  395. def.replaced++;
  396. value_def.replaced--;
  397. return candidate.value;
  398. }
  399. // Skip (non-executed) functions and (leading) default case in switch statements
  400. if (node instanceof AST_Default || node instanceof AST_Scope)
  401. return node;
  402. });
  403. while (--stat_index >= 0) {
  404. // Treat parameters as collapsible in IIFE, i.e.
  405. // function(a, b){ ... }(x());
  406. // would be translated into equivalent assignments:
  407. // var a = x(), b = undefined;
  408. if (stat_index == 0 && compressor.option("unused"))
  409. extract_args();
  410. // Find collapsible assignments
  411. var hit_stack = [];
  412. extract_candidates(statements[stat_index]);
  413. while (candidates.length > 0) {
  414. hit_stack = candidates.pop();
  415. var hit_index = 0;
  416. var candidate = hit_stack[hit_stack.length - 1];
  417. var value_def = null;
  418. var stop_after = null;
  419. var stop_if_hit = null;
  420. var lhs = get_lhs(candidate);
  421. if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor))
  422. continue;
  423. // Locate symbols which may execute code outside of scanning range
  424. var lvalues = get_lvalues(candidate);
  425. var lhs_local = is_lhs_local(lhs);
  426. if (lhs instanceof AST_SymbolRef) {
  427. lvalues.set(lhs.name, { def: lhs.definition(), modified: false });
  428. }
  429. var side_effects = value_has_side_effects(candidate);
  430. var replace_all = replace_all_symbols();
  431. var may_throw = candidate.may_throw(compressor);
  432. var funarg = candidate.name instanceof AST_SymbolFunarg;
  433. var hit = funarg;
  434. var abort = false, replaced = 0, can_replace = !args || !hit;
  435. if (!can_replace) {
  436. for (var j = compressor.self().argnames.lastIndexOf(candidate.name) + 1; !abort && j < args.length; j++) {
  437. args[j].transform(scanner);
  438. }
  439. can_replace = true;
  440. }
  441. for (var i = stat_index; !abort && i < statements.length; i++) {
  442. statements[i].transform(scanner);
  443. }
  444. if (value_def) {
  445. var def = candidate.name.definition();
  446. if (abort && def.references.length - def.replaced > replaced)
  447. replaced = false;
  448. else {
  449. abort = false;
  450. hit_index = 0;
  451. hit = funarg;
  452. for (var i = stat_index; !abort && i < statements.length; i++) {
  453. statements[i].transform(multi_replacer);
  454. }
  455. value_def.single_use = false;
  456. }
  457. }
  458. if (replaced && !remove_candidate(candidate))
  459. statements.splice(stat_index, 1);
  460. }
  461. }
  462. function handle_custom_scan_order(node) {
  463. // Skip (non-executed) functions
  464. if (node instanceof AST_Scope)
  465. return node;
  466. // Scan case expressions first in a switch statement
  467. if (node instanceof AST_Switch) {
  468. node.expression = node.expression.transform(scanner);
  469. for (var i = 0, len = node.body.length; !abort && i < len; i++) {
  470. var branch = node.body[i];
  471. if (branch instanceof AST_Case) {
  472. if (!hit) {
  473. if (branch !== hit_stack[hit_index])
  474. continue;
  475. hit_index++;
  476. }
  477. branch.expression = branch.expression.transform(scanner);
  478. if (!replace_all)
  479. break;
  480. }
  481. }
  482. abort = true;
  483. return node;
  484. }
  485. }
  486. function redefined_within_scope(def, scope) {
  487. if (def.global)
  488. return false;
  489. let cur_scope = def.scope;
  490. while (cur_scope && cur_scope !== scope) {
  491. if (cur_scope.variables.has(def.name)) {
  492. return true;
  493. }
  494. cur_scope = cur_scope.parent_scope;
  495. }
  496. return false;
  497. }
  498. function has_overlapping_symbol(fn, arg, fn_strict) {
  499. var found = false, scan_this = !(fn instanceof AST_Arrow);
  500. arg.walk(new TreeWalker(function (node, descend) {
  501. if (found)
  502. return true;
  503. if (node instanceof AST_SymbolRef && (fn.variables.has(node.name) || redefined_within_scope(node.definition(), fn))) {
  504. var s = node.definition().scope;
  505. if (s !== defun_scope)
  506. while (s = s.parent_scope) {
  507. if (s === defun_scope)
  508. return true;
  509. }
  510. return found = true;
  511. }
  512. if ((fn_strict || scan_this) && node instanceof AST_This) {
  513. return found = true;
  514. }
  515. if (node instanceof AST_Scope && !(node instanceof AST_Arrow)) {
  516. var prev = scan_this;
  517. scan_this = false;
  518. descend();
  519. scan_this = prev;
  520. return true;
  521. }
  522. }));
  523. return found;
  524. }
  525. function extract_args() {
  526. var iife, fn = compressor.self();
  527. if (is_func_expr(fn)
  528. && !fn.name
  529. && !fn.uses_arguments
  530. && !fn.pinned()
  531. && (iife = compressor.parent()) instanceof AST_Call
  532. && iife.expression === fn
  533. && iife.args.every((arg) => !(arg instanceof AST_Expansion))) {
  534. var fn_strict = compressor.has_directive("use strict");
  535. if (fn_strict && !member(fn_strict, fn.body))
  536. fn_strict = false;
  537. var len = fn.argnames.length;
  538. args = iife.args.slice(len);
  539. var names = new Set();
  540. for (var i = len; --i >= 0;) {
  541. var sym = fn.argnames[i];
  542. var arg = iife.args[i];
  543. // The following two line fix is a duplicate of the fix at
  544. // https://github.com/terser/terser/commit/011d3eb08cefe6922c7d1bdfa113fc4aeaca1b75
  545. // This might mean that these two pieces of code (one here in collapse_vars and another in reduce_vars
  546. // Might be doing the exact same thing.
  547. const def = sym.definition && sym.definition();
  548. const is_reassigned = def && def.orig.length > 1;
  549. if (is_reassigned)
  550. continue;
  551. args.unshift(make_node(AST_VarDef, sym, {
  552. name: sym,
  553. value: arg
  554. }));
  555. if (names.has(sym.name))
  556. continue;
  557. names.add(sym.name);
  558. if (sym instanceof AST_Expansion) {
  559. var elements = iife.args.slice(i);
  560. if (elements.every((arg) => !has_overlapping_symbol(fn, arg, fn_strict)
  561. )) {
  562. candidates.unshift([make_node(AST_VarDef, sym, {
  563. name: sym.expression,
  564. value: make_node(AST_Array, iife, {
  565. elements: elements
  566. })
  567. })]);
  568. }
  569. } else {
  570. if (!arg) {
  571. arg = make_node(AST_Undefined, sym).transform(compressor);
  572. } else if (arg instanceof AST_Lambda && arg.pinned()
  573. || has_overlapping_symbol(fn, arg, fn_strict)) {
  574. arg = null;
  575. }
  576. if (arg)
  577. candidates.unshift([make_node(AST_VarDef, sym, {
  578. name: sym,
  579. value: arg
  580. })]);
  581. }
  582. }
  583. }
  584. }
  585. function extract_candidates(expr) {
  586. hit_stack.push(expr);
  587. if (expr instanceof AST_Assign) {
  588. if (!expr.left.has_side_effects(compressor)
  589. && !(expr.right instanceof AST_Chain)) {
  590. candidates.push(hit_stack.slice());
  591. }
  592. extract_candidates(expr.right);
  593. } else if (expr instanceof AST_Binary) {
  594. extract_candidates(expr.left);
  595. extract_candidates(expr.right);
  596. } else if (expr instanceof AST_Call && !has_annotation(expr, _NOINLINE)) {
  597. extract_candidates(expr.expression);
  598. expr.args.forEach(extract_candidates);
  599. } else if (expr instanceof AST_Case) {
  600. extract_candidates(expr.expression);
  601. } else if (expr instanceof AST_Conditional) {
  602. extract_candidates(expr.condition);
  603. extract_candidates(expr.consequent);
  604. extract_candidates(expr.alternative);
  605. } else if (expr instanceof AST_Definitions) {
  606. var len = expr.definitions.length;
  607. // limit number of trailing variable definitions for consideration
  608. var i = len - 200;
  609. if (i < 0)
  610. i = 0;
  611. for (; i < len; i++) {
  612. extract_candidates(expr.definitions[i]);
  613. }
  614. } else if (expr instanceof AST_DWLoop) {
  615. extract_candidates(expr.condition);
  616. if (!(expr.body instanceof AST_Block)) {
  617. extract_candidates(expr.body);
  618. }
  619. } else if (expr instanceof AST_Exit) {
  620. if (expr.value)
  621. extract_candidates(expr.value);
  622. } else if (expr instanceof AST_For) {
  623. if (expr.init)
  624. extract_candidates(expr.init);
  625. if (expr.condition)
  626. extract_candidates(expr.condition);
  627. if (expr.step)
  628. extract_candidates(expr.step);
  629. if (!(expr.body instanceof AST_Block)) {
  630. extract_candidates(expr.body);
  631. }
  632. } else if (expr instanceof AST_ForIn) {
  633. extract_candidates(expr.object);
  634. if (!(expr.body instanceof AST_Block)) {
  635. extract_candidates(expr.body);
  636. }
  637. } else if (expr instanceof AST_If) {
  638. extract_candidates(expr.condition);
  639. if (!(expr.body instanceof AST_Block)) {
  640. extract_candidates(expr.body);
  641. }
  642. if (expr.alternative && !(expr.alternative instanceof AST_Block)) {
  643. extract_candidates(expr.alternative);
  644. }
  645. } else if (expr instanceof AST_Sequence) {
  646. expr.expressions.forEach(extract_candidates);
  647. } else if (expr instanceof AST_SimpleStatement) {
  648. extract_candidates(expr.body);
  649. } else if (expr instanceof AST_Switch) {
  650. extract_candidates(expr.expression);
  651. expr.body.forEach(extract_candidates);
  652. } else if (expr instanceof AST_Unary) {
  653. if (expr.operator == "++" || expr.operator == "--") {
  654. candidates.push(hit_stack.slice());
  655. }
  656. } else if (expr instanceof AST_VarDef) {
  657. if (expr.value && !(expr.value instanceof AST_Chain)) {
  658. candidates.push(hit_stack.slice());
  659. extract_candidates(expr.value);
  660. }
  661. }
  662. hit_stack.pop();
  663. }
  664. function find_stop(node, level, write_only) {
  665. var parent = scanner.parent(level);
  666. if (parent instanceof AST_Assign) {
  667. if (write_only
  668. && !parent.logical
  669. && !(parent.left instanceof AST_PropAccess
  670. || lvalues.has(parent.left.name))) {
  671. return find_stop(parent, level + 1, write_only);
  672. }
  673. return node;
  674. }
  675. if (parent instanceof AST_Binary) {
  676. if (write_only && (!lazy_op.has(parent.operator) || parent.left === node)) {
  677. return find_stop(parent, level + 1, write_only);
  678. }
  679. return node;
  680. }
  681. if (parent instanceof AST_Call)
  682. return node;
  683. if (parent instanceof AST_Case)
  684. return node;
  685. if (parent instanceof AST_Conditional) {
  686. if (write_only && parent.condition === node) {
  687. return find_stop(parent, level + 1, write_only);
  688. }
  689. return node;
  690. }
  691. if (parent instanceof AST_Definitions) {
  692. return find_stop(parent, level + 1, true);
  693. }
  694. if (parent instanceof AST_Exit) {
  695. return write_only ? find_stop(parent, level + 1, write_only) : node;
  696. }
  697. if (parent instanceof AST_If) {
  698. if (write_only && parent.condition === node) {
  699. return find_stop(parent, level + 1, write_only);
  700. }
  701. return node;
  702. }
  703. if (parent instanceof AST_IterationStatement)
  704. return node;
  705. if (parent instanceof AST_Sequence) {
  706. return find_stop(parent, level + 1, parent.tail_node() !== node);
  707. }
  708. if (parent instanceof AST_SimpleStatement) {
  709. return find_stop(parent, level + 1, true);
  710. }
  711. if (parent instanceof AST_Switch)
  712. return node;
  713. if (parent instanceof AST_VarDef)
  714. return node;
  715. return null;
  716. }
  717. function mangleable_var(var_def) {
  718. var value = var_def.value;
  719. if (!(value instanceof AST_SymbolRef))
  720. return;
  721. if (value.name == "arguments")
  722. return;
  723. var def = value.definition();
  724. if (def.undeclared)
  725. return;
  726. return value_def = def;
  727. }
  728. function get_lhs(expr) {
  729. if (expr instanceof AST_Assign && expr.logical) {
  730. return false;
  731. } else if (expr instanceof AST_VarDef && expr.name instanceof AST_SymbolDeclaration) {
  732. var def = expr.name.definition();
  733. if (!member(expr.name, def.orig))
  734. return;
  735. var referenced = def.references.length - def.replaced;
  736. if (!referenced)
  737. return;
  738. var declared = def.orig.length - def.eliminated;
  739. if (declared > 1 && !(expr.name instanceof AST_SymbolFunarg)
  740. || (referenced > 1 ? mangleable_var(expr) : !compressor.exposed(def))) {
  741. return make_node(AST_SymbolRef, expr.name, expr.name);
  742. }
  743. } else {
  744. const lhs = expr instanceof AST_Assign
  745. ? expr.left
  746. : expr.expression;
  747. return !is_ref_of(lhs, AST_SymbolConst)
  748. && !is_ref_of(lhs, AST_SymbolLet) && lhs;
  749. }
  750. }
  751. function get_rvalue(expr) {
  752. if (expr instanceof AST_Assign) {
  753. return expr.right;
  754. } else {
  755. return expr.value;
  756. }
  757. }
  758. function get_lvalues(expr) {
  759. var lvalues = new Map();
  760. if (expr instanceof AST_Unary)
  761. return lvalues;
  762. var tw = new TreeWalker(function (node) {
  763. var sym = node;
  764. while (sym instanceof AST_PropAccess)
  765. sym = sym.expression;
  766. if (sym instanceof AST_SymbolRef) {
  767. const prev = lvalues.get(sym.name);
  768. if (!prev || !prev.modified) {
  769. lvalues.set(sym.name, {
  770. def: sym.definition(),
  771. modified: is_modified(compressor, tw, node, node, 0)
  772. });
  773. }
  774. }
  775. });
  776. get_rvalue(expr).walk(tw);
  777. return lvalues;
  778. }
  779. function remove_candidate(expr) {
  780. if (expr.name instanceof AST_SymbolFunarg) {
  781. var iife = compressor.parent(), argnames = compressor.self().argnames;
  782. var index = argnames.indexOf(expr.name);
  783. if (index < 0) {
  784. iife.args.length = Math.min(iife.args.length, argnames.length - 1);
  785. } else {
  786. var args = iife.args;
  787. if (args[index])
  788. args[index] = make_node(AST_Number, args[index], {
  789. value: 0
  790. });
  791. }
  792. return true;
  793. }
  794. var found = false;
  795. return statements[stat_index].transform(new TreeTransformer(function (node, descend, in_list) {
  796. if (found)
  797. return node;
  798. if (node === expr || node.body === expr) {
  799. found = true;
  800. if (node instanceof AST_VarDef) {
  801. node.value = node.name instanceof AST_SymbolConst
  802. ? make_node(AST_Undefined, node.value) // `const` always needs value.
  803. : null;
  804. return node;
  805. }
  806. return in_list ? MAP.skip : null;
  807. }
  808. }, function (node) {
  809. if (node instanceof AST_Sequence)
  810. switch (node.expressions.length) {
  811. case 0: return null;
  812. case 1: return node.expressions[0];
  813. }
  814. }));
  815. }
  816. function is_lhs_local(lhs) {
  817. while (lhs instanceof AST_PropAccess)
  818. lhs = lhs.expression;
  819. return lhs instanceof AST_SymbolRef
  820. && lhs.definition().scope.get_defun_scope() === defun_scope
  821. && !(in_loop
  822. && (lvalues.has(lhs.name)
  823. || candidate instanceof AST_Unary
  824. || (candidate instanceof AST_Assign
  825. && !candidate.logical
  826. && candidate.operator != "=")));
  827. }
  828. function value_has_side_effects(expr) {
  829. if (expr instanceof AST_Unary)
  830. return unary_side_effects.has(expr.operator);
  831. return get_rvalue(expr).has_side_effects(compressor);
  832. }
  833. function replace_all_symbols() {
  834. if (side_effects)
  835. return false;
  836. if (value_def)
  837. return true;
  838. if (lhs instanceof AST_SymbolRef) {
  839. var def = lhs.definition();
  840. if (def.references.length - def.replaced == (candidate instanceof AST_VarDef ? 1 : 2)) {
  841. return true;
  842. }
  843. }
  844. return false;
  845. }
  846. function may_modify(sym) {
  847. if (!sym.definition)
  848. return true; // AST_Destructuring
  849. var def = sym.definition();
  850. if (def.orig.length == 1 && def.orig[0] instanceof AST_SymbolDefun)
  851. return false;
  852. if (def.scope.get_defun_scope() !== defun_scope)
  853. return true;
  854. return def.references.some((ref) =>
  855. ref.scope.get_defun_scope() !== defun_scope
  856. );
  857. }
  858. function side_effects_external(node, lhs) {
  859. if (node instanceof AST_Assign)
  860. return side_effects_external(node.left, true);
  861. if (node instanceof AST_Unary)
  862. return side_effects_external(node.expression, true);
  863. if (node instanceof AST_VarDef)
  864. return node.value && side_effects_external(node.value);
  865. if (lhs) {
  866. if (node instanceof AST_Dot)
  867. return side_effects_external(node.expression, true);
  868. if (node instanceof AST_Sub)
  869. return side_effects_external(node.expression, true);
  870. if (node instanceof AST_SymbolRef)
  871. return node.definition().scope.get_defun_scope() !== defun_scope;
  872. }
  873. return false;
  874. }
  875. /**
  876. * Will any of the pulled-in lvalues shadow a variable in newScope or parents?
  877. * similar to scope_encloses_variables_in_this_scope */
  878. function shadows(my_scope, lvalues) {
  879. for (const { def } of lvalues.values()) {
  880. const looked_up = my_scope.find_variable(def.name);
  881. if (looked_up) {
  882. if (looked_up === def) continue;
  883. return true;
  884. }
  885. }
  886. return false;
  887. }
  888. }
  889. function eliminate_spurious_blocks(statements) {
  890. var seen_dirs = [];
  891. for (var i = 0; i < statements.length;) {
  892. var stat = statements[i];
  893. if (stat instanceof AST_BlockStatement && stat.body.every(can_be_evicted_from_block)) {
  894. CHANGED = true;
  895. eliminate_spurious_blocks(stat.body);
  896. statements.splice(i, 1, ...stat.body);
  897. i += stat.body.length;
  898. } else if (stat instanceof AST_EmptyStatement) {
  899. CHANGED = true;
  900. statements.splice(i, 1);
  901. } else if (stat instanceof AST_Directive) {
  902. if (seen_dirs.indexOf(stat.value) < 0) {
  903. i++;
  904. seen_dirs.push(stat.value);
  905. } else {
  906. CHANGED = true;
  907. statements.splice(i, 1);
  908. }
  909. } else
  910. i++;
  911. }
  912. }
  913. function handle_if_return(statements, compressor) {
  914. var self = compressor.self();
  915. var multiple_if_returns = has_multiple_if_returns(statements);
  916. var in_lambda = self instanceof AST_Lambda;
  917. for (var i = statements.length; --i >= 0;) {
  918. var stat = statements[i];
  919. var j = next_index(i);
  920. var next = statements[j];
  921. if (in_lambda && !next && stat instanceof AST_Return) {
  922. if (!stat.value) {
  923. CHANGED = true;
  924. statements.splice(i, 1);
  925. continue;
  926. }
  927. if (stat.value instanceof AST_UnaryPrefix && stat.value.operator == "void") {
  928. CHANGED = true;
  929. statements[i] = make_node(AST_SimpleStatement, stat, {
  930. body: stat.value.expression
  931. });
  932. continue;
  933. }
  934. }
  935. if (stat instanceof AST_If) {
  936. var ab = aborts(stat.body);
  937. if (can_merge_flow(ab)) {
  938. if (ab.label) {
  939. remove(ab.label.thedef.references, ab);
  940. }
  941. CHANGED = true;
  942. stat = stat.clone();
  943. stat.condition = stat.condition.negate(compressor);
  944. var body = as_statement_array_with_return(stat.body, ab);
  945. stat.body = make_node(AST_BlockStatement, stat, {
  946. body: as_statement_array(stat.alternative).concat(extract_functions())
  947. });
  948. stat.alternative = make_node(AST_BlockStatement, stat, {
  949. body: body
  950. });
  951. statements[i] = stat.transform(compressor);
  952. continue;
  953. }
  954. var ab = aborts(stat.alternative);
  955. if (can_merge_flow(ab)) {
  956. if (ab.label) {
  957. remove(ab.label.thedef.references, ab);
  958. }
  959. CHANGED = true;
  960. stat = stat.clone();
  961. stat.body = make_node(AST_BlockStatement, stat.body, {
  962. body: as_statement_array(stat.body).concat(extract_functions())
  963. });
  964. var body = as_statement_array_with_return(stat.alternative, ab);
  965. stat.alternative = make_node(AST_BlockStatement, stat.alternative, {
  966. body: body
  967. });
  968. statements[i] = stat.transform(compressor);
  969. continue;
  970. }
  971. }
  972. if (stat instanceof AST_If && stat.body instanceof AST_Return) {
  973. var value = stat.body.value;
  974. //---
  975. // pretty silly case, but:
  976. // if (foo()) return; return; ==> foo(); return;
  977. if (!value && !stat.alternative
  978. && (in_lambda && !next || next instanceof AST_Return && !next.value)) {
  979. CHANGED = true;
  980. statements[i] = make_node(AST_SimpleStatement, stat.condition, {
  981. body: stat.condition
  982. });
  983. continue;
  984. }
  985. //---
  986. // if (foo()) return x; return y; ==> return foo() ? x : y;
  987. if (value && !stat.alternative && next instanceof AST_Return && next.value) {
  988. CHANGED = true;
  989. stat = stat.clone();
  990. stat.alternative = next;
  991. statements[i] = stat.transform(compressor);
  992. statements.splice(j, 1);
  993. continue;
  994. }
  995. //---
  996. // if (foo()) return x; [ return ; ] ==> return foo() ? x : undefined;
  997. if (value && !stat.alternative
  998. && (!next && in_lambda && multiple_if_returns
  999. || next instanceof AST_Return)) {
  1000. CHANGED = true;
  1001. stat = stat.clone();
  1002. stat.alternative = next || make_node(AST_Return, stat, {
  1003. value: null
  1004. });
  1005. statements[i] = stat.transform(compressor);
  1006. if (next)
  1007. statements.splice(j, 1);
  1008. continue;
  1009. }
  1010. //---
  1011. // if (a) return b; if (c) return d; e; ==> return a ? b : c ? d : void e;
  1012. //
  1013. // if sequences is not enabled, this can lead to an endless loop (issue #866).
  1014. // however, with sequences on this helps producing slightly better output for
  1015. // the example code.
  1016. var prev = statements[prev_index(i)];
  1017. if (compressor.option("sequences") && in_lambda && !stat.alternative
  1018. && prev instanceof AST_If && prev.body instanceof AST_Return
  1019. && next_index(j) == statements.length && next instanceof AST_SimpleStatement) {
  1020. CHANGED = true;
  1021. stat = stat.clone();
  1022. stat.alternative = make_node(AST_BlockStatement, next, {
  1023. body: [
  1024. next,
  1025. make_node(AST_Return, next, {
  1026. value: null
  1027. })
  1028. ]
  1029. });
  1030. statements[i] = stat.transform(compressor);
  1031. statements.splice(j, 1);
  1032. continue;
  1033. }
  1034. }
  1035. }
  1036. function has_multiple_if_returns(statements) {
  1037. var n = 0;
  1038. for (var i = statements.length; --i >= 0;) {
  1039. var stat = statements[i];
  1040. if (stat instanceof AST_If && stat.body instanceof AST_Return) {
  1041. if (++n > 1)
  1042. return true;
  1043. }
  1044. }
  1045. return false;
  1046. }
  1047. function is_return_void(value) {
  1048. return !value || value instanceof AST_UnaryPrefix && value.operator == "void";
  1049. }
  1050. function can_merge_flow(ab) {
  1051. if (!ab)
  1052. return false;
  1053. for (var j = i + 1, len = statements.length; j < len; j++) {
  1054. var stat = statements[j];
  1055. if (stat instanceof AST_Const || stat instanceof AST_Let)
  1056. return false;
  1057. }
  1058. var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab) : null;
  1059. return ab instanceof AST_Return && in_lambda && is_return_void(ab.value)
  1060. || ab instanceof AST_Continue && self === loop_body(lct)
  1061. || ab instanceof AST_Break && lct instanceof AST_BlockStatement && self === lct;
  1062. }
  1063. function extract_functions() {
  1064. var tail = statements.slice(i + 1);
  1065. statements.length = i + 1;
  1066. return tail.filter(function (stat) {
  1067. if (stat instanceof AST_Defun) {
  1068. statements.push(stat);
  1069. return false;
  1070. }
  1071. return true;
  1072. });
  1073. }
  1074. function as_statement_array_with_return(node, ab) {
  1075. var body = as_statement_array(node).slice(0, -1);
  1076. if (ab.value) {
  1077. body.push(make_node(AST_SimpleStatement, ab.value, {
  1078. body: ab.value.expression
  1079. }));
  1080. }
  1081. return body;
  1082. }
  1083. function next_index(i) {
  1084. for (var j = i + 1, len = statements.length; j < len; j++) {
  1085. var stat = statements[j];
  1086. if (!(stat instanceof AST_Var && declarations_only(stat))) {
  1087. break;
  1088. }
  1089. }
  1090. return j;
  1091. }
  1092. function prev_index(i) {
  1093. for (var j = i; --j >= 0;) {
  1094. var stat = statements[j];
  1095. if (!(stat instanceof AST_Var && declarations_only(stat))) {
  1096. break;
  1097. }
  1098. }
  1099. return j;
  1100. }
  1101. }
  1102. function eliminate_dead_code(statements, compressor) {
  1103. var has_quit;
  1104. var self = compressor.self();
  1105. for (var i = 0, n = 0, len = statements.length; i < len; i++) {
  1106. var stat = statements[i];
  1107. if (stat instanceof AST_LoopControl) {
  1108. var lct = compressor.loopcontrol_target(stat);
  1109. if (stat instanceof AST_Break
  1110. && !(lct instanceof AST_IterationStatement)
  1111. && loop_body(lct) === self
  1112. || stat instanceof AST_Continue
  1113. && loop_body(lct) === self) {
  1114. if (stat.label) {
  1115. remove(stat.label.thedef.references, stat);
  1116. }
  1117. } else {
  1118. statements[n++] = stat;
  1119. }
  1120. } else {
  1121. statements[n++] = stat;
  1122. }
  1123. if (aborts(stat)) {
  1124. has_quit = statements.slice(i + 1);
  1125. break;
  1126. }
  1127. }
  1128. statements.length = n;
  1129. CHANGED = n != len;
  1130. if (has_quit)
  1131. has_quit.forEach(function (stat) {
  1132. trim_unreachable_code(compressor, stat, statements);
  1133. });
  1134. }
  1135. function declarations_only(node) {
  1136. return node.definitions.every((var_def) => !var_def.value);
  1137. }
  1138. function sequencesize(statements, compressor) {
  1139. if (statements.length < 2)
  1140. return;
  1141. var seq = [], n = 0;
  1142. function push_seq() {
  1143. if (!seq.length)
  1144. return;
  1145. var body = make_sequence(seq[0], seq);
  1146. statements[n++] = make_node(AST_SimpleStatement, body, { body: body });
  1147. seq = [];
  1148. }
  1149. for (var i = 0, len = statements.length; i < len; i++) {
  1150. var stat = statements[i];
  1151. if (stat instanceof AST_SimpleStatement) {
  1152. if (seq.length >= compressor.sequences_limit)
  1153. push_seq();
  1154. var body = stat.body;
  1155. if (seq.length > 0)
  1156. body = body.drop_side_effect_free(compressor);
  1157. if (body)
  1158. merge_sequence(seq, body);
  1159. } else if (stat instanceof AST_Definitions && declarations_only(stat)
  1160. || stat instanceof AST_Defun) {
  1161. statements[n++] = stat;
  1162. } else {
  1163. push_seq();
  1164. statements[n++] = stat;
  1165. }
  1166. }
  1167. push_seq();
  1168. statements.length = n;
  1169. if (n != len)
  1170. CHANGED = true;
  1171. }
  1172. function to_simple_statement(block, decls) {
  1173. if (!(block instanceof AST_BlockStatement))
  1174. return block;
  1175. var stat = null;
  1176. for (var i = 0, len = block.body.length; i < len; i++) {
  1177. var line = block.body[i];
  1178. if (line instanceof AST_Var && declarations_only(line)) {
  1179. decls.push(line);
  1180. } else if (stat || line instanceof AST_Const || line instanceof AST_Let) {
  1181. return false;
  1182. } else {
  1183. stat = line;
  1184. }
  1185. }
  1186. return stat;
  1187. }
  1188. function sequencesize_2(statements, compressor) {
  1189. function cons_seq(right) {
  1190. n--;
  1191. CHANGED = true;
  1192. var left = prev.body;
  1193. return make_sequence(left, [left, right]).transform(compressor);
  1194. }
  1195. var n = 0, prev;
  1196. for (var i = 0; i < statements.length; i++) {
  1197. var stat = statements[i];
  1198. if (prev) {
  1199. if (stat instanceof AST_Exit) {
  1200. stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat).transform(compressor));
  1201. } else if (stat instanceof AST_For) {
  1202. if (!(stat.init instanceof AST_Definitions)) {
  1203. const abort = walk(prev.body, node => {
  1204. if (node instanceof AST_Scope)
  1205. return true;
  1206. if (node instanceof AST_Binary
  1207. && node.operator === "in") {
  1208. return walk_abort;
  1209. }
  1210. });
  1211. if (!abort) {
  1212. if (stat.init)
  1213. stat.init = cons_seq(stat.init);
  1214. else {
  1215. stat.init = prev.body;
  1216. n--;
  1217. CHANGED = true;
  1218. }
  1219. }
  1220. }
  1221. } else if (stat instanceof AST_ForIn) {
  1222. if (!(stat.init instanceof AST_Const) && !(stat.init instanceof AST_Let)) {
  1223. stat.object = cons_seq(stat.object);
  1224. }
  1225. } else if (stat instanceof AST_If) {
  1226. stat.condition = cons_seq(stat.condition);
  1227. } else if (stat instanceof AST_Switch) {
  1228. stat.expression = cons_seq(stat.expression);
  1229. } else if (stat instanceof AST_With) {
  1230. stat.expression = cons_seq(stat.expression);
  1231. }
  1232. }
  1233. if (compressor.option("conditionals") && stat instanceof AST_If) {
  1234. var decls = [];
  1235. var body = to_simple_statement(stat.body, decls);
  1236. var alt = to_simple_statement(stat.alternative, decls);
  1237. if (body !== false && alt !== false && decls.length > 0) {
  1238. var len = decls.length;
  1239. decls.push(make_node(AST_If, stat, {
  1240. condition: stat.condition,
  1241. body: body || make_node(AST_EmptyStatement, stat.body),
  1242. alternative: alt
  1243. }));
  1244. decls.unshift(n, 1);
  1245. [].splice.apply(statements, decls);
  1246. i += len;
  1247. n += len + 1;
  1248. prev = null;
  1249. CHANGED = true;
  1250. continue;
  1251. }
  1252. }
  1253. statements[n++] = stat;
  1254. prev = stat instanceof AST_SimpleStatement ? stat : null;
  1255. }
  1256. statements.length = n;
  1257. }
  1258. function join_object_assignments(defn, body) {
  1259. if (!(defn instanceof AST_Definitions))
  1260. return;
  1261. var def = defn.definitions[defn.definitions.length - 1];
  1262. if (!(def.value instanceof AST_Object))
  1263. return;
  1264. var exprs;
  1265. if (body instanceof AST_Assign && !body.logical) {
  1266. exprs = [body];
  1267. } else if (body instanceof AST_Sequence) {
  1268. exprs = body.expressions.slice();
  1269. }
  1270. if (!exprs)
  1271. return;
  1272. var trimmed = false;
  1273. do {
  1274. var node = exprs[0];
  1275. if (!(node instanceof AST_Assign))
  1276. break;
  1277. if (node.operator != "=")
  1278. break;
  1279. if (!(node.left instanceof AST_PropAccess))
  1280. break;
  1281. var sym = node.left.expression;
  1282. if (!(sym instanceof AST_SymbolRef))
  1283. break;
  1284. if (def.name.name != sym.name)
  1285. break;
  1286. if (!node.right.is_constant_expression(nearest_scope))
  1287. break;
  1288. var prop = node.left.property;
  1289. if (prop instanceof AST_Node) {
  1290. prop = prop.evaluate(compressor);
  1291. }
  1292. if (prop instanceof AST_Node)
  1293. break;
  1294. prop = "" + prop;
  1295. var diff = compressor.option("ecma") < 2015
  1296. && compressor.has_directive("use strict") ? function (node) {
  1297. return node.key != prop && (node.key && node.key.name != prop);
  1298. } : function (node) {
  1299. return node.key && node.key.name != prop;
  1300. };
  1301. if (!def.value.properties.every(diff))
  1302. break;
  1303. var p = def.value.properties.filter(function (p) { return p.key === prop; })[0];
  1304. if (!p) {
  1305. def.value.properties.push(make_node(AST_ObjectKeyVal, node, {
  1306. key: prop,
  1307. value: node.right
  1308. }));
  1309. } else {
  1310. p.value = new AST_Sequence({
  1311. start: p.start,
  1312. expressions: [p.value.clone(), node.right.clone()],
  1313. end: p.end
  1314. });
  1315. }
  1316. exprs.shift();
  1317. trimmed = true;
  1318. } while (exprs.length);
  1319. return trimmed && exprs;
  1320. }
  1321. function join_consecutive_vars(statements) {
  1322. var defs;
  1323. for (var i = 0, j = -1, len = statements.length; i < len; i++) {
  1324. var stat = statements[i];
  1325. var prev = statements[j];
  1326. if (stat instanceof AST_Definitions) {
  1327. if (prev && prev.TYPE == stat.TYPE) {
  1328. prev.definitions = prev.definitions.concat(stat.definitions);
  1329. CHANGED = true;
  1330. } else if (defs && defs.TYPE == stat.TYPE && declarations_only(stat)) {
  1331. defs.definitions = defs.definitions.concat(stat.definitions);
  1332. CHANGED = true;
  1333. } else {
  1334. statements[++j] = stat;
  1335. defs = stat;
  1336. }
  1337. } else if (stat instanceof AST_Exit) {
  1338. stat.value = extract_object_assignments(stat.value);
  1339. } else if (stat instanceof AST_For) {
  1340. var exprs = join_object_assignments(prev, stat.init);
  1341. if (exprs) {
  1342. CHANGED = true;
  1343. stat.init = exprs.length ? make_sequence(stat.init, exprs) : null;
  1344. statements[++j] = stat;
  1345. } else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) {
  1346. if (stat.init) {
  1347. prev.definitions = prev.definitions.concat(stat.init.definitions);
  1348. }
  1349. stat.init = prev;
  1350. statements[j] = stat;
  1351. CHANGED = true;
  1352. } else if (defs && stat.init && defs.TYPE == stat.init.TYPE && declarations_only(stat.init)) {
  1353. defs.definitions = defs.definitions.concat(stat.init.definitions);
  1354. stat.init = null;
  1355. statements[++j] = stat;
  1356. CHANGED = true;
  1357. } else {
  1358. statements[++j] = stat;
  1359. }
  1360. } else if (stat instanceof AST_ForIn) {
  1361. stat.object = extract_object_assignments(stat.object);
  1362. } else if (stat instanceof AST_If) {
  1363. stat.condition = extract_object_assignments(stat.condition);
  1364. } else if (stat instanceof AST_SimpleStatement) {
  1365. var exprs = join_object_assignments(prev, stat.body);
  1366. if (exprs) {
  1367. CHANGED = true;
  1368. if (!exprs.length)
  1369. continue;
  1370. stat.body = make_sequence(stat.body, exprs);
  1371. }
  1372. statements[++j] = stat;
  1373. } else if (stat instanceof AST_Switch) {
  1374. stat.expression = extract_object_assignments(stat.expression);
  1375. } else if (stat instanceof AST_With) {
  1376. stat.expression = extract_object_assignments(stat.expression);
  1377. } else {
  1378. statements[++j] = stat;
  1379. }
  1380. }
  1381. statements.length = j + 1;
  1382. function extract_object_assignments(value) {
  1383. statements[++j] = stat;
  1384. var exprs = join_object_assignments(prev, value);
  1385. if (exprs) {
  1386. CHANGED = true;
  1387. if (exprs.length) {
  1388. return make_sequence(value, exprs);
  1389. } else if (value instanceof AST_Sequence) {
  1390. return value.tail_node().left;
  1391. } else {
  1392. return value.left;
  1393. }
  1394. }
  1395. return value;
  1396. }
  1397. }
  1398. }