tighten-body.js 58 KB

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