drop-side-effect-free.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. import {
  34. AST_Accessor,
  35. AST_Array,
  36. AST_Arrow,
  37. AST_Assign,
  38. AST_Binary,
  39. AST_Call,
  40. AST_Chain,
  41. AST_Class,
  42. AST_ClassStaticBlock,
  43. AST_ClassProperty,
  44. AST_ConciseMethod,
  45. AST_Conditional,
  46. AST_Constant,
  47. AST_Dot,
  48. AST_Expansion,
  49. AST_Function,
  50. AST_Node,
  51. AST_Number,
  52. AST_Object,
  53. AST_ObjectGetter,
  54. AST_ObjectKeyVal,
  55. AST_ObjectProperty,
  56. AST_ObjectSetter,
  57. AST_PropAccess,
  58. AST_Scope,
  59. AST_Sequence,
  60. AST_Sub,
  61. AST_SymbolRef,
  62. AST_TemplateSegment,
  63. AST_TemplateString,
  64. AST_This,
  65. AST_Unary,
  66. } from "../ast.js";
  67. import { make_node, return_null, return_this } from "../utils/index.js";
  68. import { first_in_statement } from "../utils/first_in_statement.js";
  69. import { pure_prop_access_globals } from "./native-objects.js";
  70. import { lazy_op, unary_side_effects, is_nullish_shortcircuited } from "./inference.js";
  71. import { WRITE_ONLY, set_flag, clear_flag } from "./compressor-flags.js";
  72. import { make_sequence, is_func_expr, is_iife_call } from "./common.js";
  73. // AST_Node#drop_side_effect_free() gets called when we don't care about the value,
  74. // only about side effects. We'll be defining this method for each node type in this module
  75. //
  76. // Examples:
  77. // foo++ -> foo++
  78. // 1 + func() -> func()
  79. // 10 -> (nothing)
  80. // knownPureFunc(foo++) -> foo++
  81. function def_drop_side_effect_free(node, func) {
  82. node.DEFMETHOD("drop_side_effect_free", func);
  83. }
  84. // Drop side-effect-free elements from an array of expressions.
  85. // Returns an array of expressions with side-effects or null
  86. // if all elements were dropped. Note: original array may be
  87. // returned if nothing changed.
  88. function trim(nodes, compressor, first_in_statement) {
  89. var len = nodes.length;
  90. if (!len) return null;
  91. var ret = [], changed = false;
  92. for (var i = 0; i < len; i++) {
  93. var node = nodes[i].drop_side_effect_free(compressor, first_in_statement);
  94. changed |= node !== nodes[i];
  95. if (node) {
  96. ret.push(node);
  97. first_in_statement = false;
  98. }
  99. }
  100. return changed ? ret.length ? ret : null : nodes;
  101. }
  102. def_drop_side_effect_free(AST_Node, return_this);
  103. def_drop_side_effect_free(AST_Constant, return_null);
  104. def_drop_side_effect_free(AST_This, return_null);
  105. def_drop_side_effect_free(AST_Call, function (compressor, first_in_statement) {
  106. if (is_nullish_shortcircuited(this, compressor)) {
  107. return this.expression.drop_side_effect_free(compressor, first_in_statement);
  108. }
  109. if (!this.is_callee_pure(compressor)) {
  110. if (this.expression.is_call_pure(compressor)) {
  111. var exprs = this.args.slice();
  112. exprs.unshift(this.expression.expression);
  113. exprs = trim(exprs, compressor, first_in_statement);
  114. return exprs && make_sequence(this, exprs);
  115. }
  116. if (is_func_expr(this.expression)
  117. && (!this.expression.name || !this.expression.name.definition().references.length)) {
  118. var node = this.clone();
  119. node.expression.process_expression(false, compressor);
  120. return node;
  121. }
  122. return this;
  123. }
  124. var args = trim(this.args, compressor, first_in_statement);
  125. return args && make_sequence(this, args);
  126. });
  127. def_drop_side_effect_free(AST_Accessor, return_null);
  128. def_drop_side_effect_free(AST_Function, return_null);
  129. def_drop_side_effect_free(AST_Arrow, return_null);
  130. def_drop_side_effect_free(AST_Class, function (compressor) {
  131. const with_effects = [];
  132. const trimmed_extends = this.extends && this.extends.drop_side_effect_free(compressor);
  133. if (trimmed_extends)
  134. with_effects.push(trimmed_extends);
  135. for (const prop of this.properties) {
  136. if (prop instanceof AST_ClassStaticBlock) {
  137. if (prop.body.some(stat => stat.has_side_effects(compressor))) {
  138. return this;
  139. } else {
  140. continue;
  141. }
  142. }
  143. const trimmed_prop = prop.drop_side_effect_free(compressor);
  144. if (trimmed_prop)
  145. with_effects.push(trimmed_prop);
  146. }
  147. if (!with_effects.length)
  148. return null;
  149. return make_sequence(this, with_effects);
  150. });
  151. def_drop_side_effect_free(AST_Binary, function (compressor, first_in_statement) {
  152. var right = this.right.drop_side_effect_free(compressor);
  153. if (!right)
  154. return this.left.drop_side_effect_free(compressor, first_in_statement);
  155. if (lazy_op.has(this.operator)) {
  156. if (right === this.right)
  157. return this;
  158. var node = this.clone();
  159. node.right = right;
  160. return node;
  161. } else {
  162. var left = this.left.drop_side_effect_free(compressor, first_in_statement);
  163. if (!left)
  164. return this.right.drop_side_effect_free(compressor, first_in_statement);
  165. return make_sequence(this, [left, right]);
  166. }
  167. });
  168. def_drop_side_effect_free(AST_Assign, function (compressor) {
  169. if (this.logical)
  170. return this;
  171. var left = this.left;
  172. if (left.has_side_effects(compressor)
  173. || compressor.has_directive("use strict")
  174. && left instanceof AST_PropAccess
  175. && left.expression.is_constant()) {
  176. return this;
  177. }
  178. set_flag(this, WRITE_ONLY);
  179. while (left instanceof AST_PropAccess) {
  180. left = left.expression;
  181. }
  182. if (left.is_constant_expression(compressor.find_parent(AST_Scope))) {
  183. return this.right.drop_side_effect_free(compressor);
  184. }
  185. return this;
  186. });
  187. def_drop_side_effect_free(AST_Conditional, function (compressor) {
  188. var consequent = this.consequent.drop_side_effect_free(compressor);
  189. var alternative = this.alternative.drop_side_effect_free(compressor);
  190. if (consequent === this.consequent && alternative === this.alternative)
  191. return this;
  192. if (!consequent)
  193. return alternative ? make_node(AST_Binary, this, {
  194. operator: "||",
  195. left: this.condition,
  196. right: alternative
  197. }) : this.condition.drop_side_effect_free(compressor);
  198. if (!alternative)
  199. return make_node(AST_Binary, this, {
  200. operator: "&&",
  201. left: this.condition,
  202. right: consequent
  203. });
  204. var node = this.clone();
  205. node.consequent = consequent;
  206. node.alternative = alternative;
  207. return node;
  208. });
  209. def_drop_side_effect_free(AST_Unary, function (compressor, first_in_statement) {
  210. if (unary_side_effects.has(this.operator)) {
  211. if (!this.expression.has_side_effects(compressor)) {
  212. set_flag(this, WRITE_ONLY);
  213. } else {
  214. clear_flag(this, WRITE_ONLY);
  215. }
  216. return this;
  217. }
  218. if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef)
  219. return null;
  220. var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
  221. if (first_in_statement && expression && is_iife_call(expression)) {
  222. if (expression === this.expression && this.operator == "!")
  223. return this;
  224. return expression.negate(compressor, first_in_statement);
  225. }
  226. return expression;
  227. });
  228. def_drop_side_effect_free(AST_SymbolRef, function (compressor) {
  229. const safe_access = this.is_declared(compressor)
  230. || pure_prop_access_globals.has(this.name);
  231. return safe_access ? null : this;
  232. });
  233. def_drop_side_effect_free(AST_Object, function (compressor, first_in_statement) {
  234. var values = trim(this.properties, compressor, first_in_statement);
  235. return values && make_sequence(this, values);
  236. });
  237. def_drop_side_effect_free(AST_ObjectProperty, function (compressor, first_in_statement) {
  238. const computed_key = this instanceof AST_ObjectKeyVal && this.key instanceof AST_Node;
  239. const key = computed_key && this.key.drop_side_effect_free(compressor, first_in_statement);
  240. const value = this.value && this.value.drop_side_effect_free(compressor, first_in_statement);
  241. if (key && value) {
  242. return make_sequence(this, [key, value]);
  243. }
  244. return key || value;
  245. });
  246. def_drop_side_effect_free(AST_ClassProperty, function (compressor) {
  247. const key = this.computed_key() && this.key.drop_side_effect_free(compressor);
  248. const value = this.static && this.value
  249. && this.value.drop_side_effect_free(compressor);
  250. if (key && value)
  251. return make_sequence(this, [key, value]);
  252. return key || value || null;
  253. });
  254. def_drop_side_effect_free(AST_ConciseMethod, function () {
  255. return this.computed_key() ? this.key : null;
  256. });
  257. def_drop_side_effect_free(AST_ObjectGetter, function () {
  258. return this.computed_key() ? this.key : null;
  259. });
  260. def_drop_side_effect_free(AST_ObjectSetter, function () {
  261. return this.computed_key() ? this.key : null;
  262. });
  263. def_drop_side_effect_free(AST_Array, function (compressor, first_in_statement) {
  264. var values = trim(this.elements, compressor, first_in_statement);
  265. return values && make_sequence(this, values);
  266. });
  267. def_drop_side_effect_free(AST_Dot, function (compressor, first_in_statement) {
  268. if (is_nullish_shortcircuited(this, compressor)) {
  269. return this.expression.drop_side_effect_free(compressor, first_in_statement);
  270. }
  271. if (this.expression.may_throw_on_access(compressor)) return this;
  272. return this.expression.drop_side_effect_free(compressor, first_in_statement);
  273. });
  274. def_drop_side_effect_free(AST_Sub, function (compressor, first_in_statement) {
  275. if (is_nullish_shortcircuited(this, compressor)) {
  276. return this.expression.drop_side_effect_free(compressor, first_in_statement);
  277. }
  278. if (this.expression.may_throw_on_access(compressor)) return this;
  279. var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
  280. if (!expression)
  281. return this.property.drop_side_effect_free(compressor, first_in_statement);
  282. var property = this.property.drop_side_effect_free(compressor);
  283. if (!property)
  284. return expression;
  285. return make_sequence(this, [expression, property]);
  286. });
  287. def_drop_side_effect_free(AST_Chain, function (compressor, first_in_statement) {
  288. return this.expression.drop_side_effect_free(compressor, first_in_statement);
  289. });
  290. def_drop_side_effect_free(AST_Sequence, function (compressor) {
  291. var last = this.tail_node();
  292. var expr = last.drop_side_effect_free(compressor);
  293. if (expr === last)
  294. return this;
  295. var expressions = this.expressions.slice(0, -1);
  296. if (expr)
  297. expressions.push(expr);
  298. if (!expressions.length) {
  299. return make_node(AST_Number, this, { value: 0 });
  300. }
  301. return make_sequence(this, expressions);
  302. });
  303. def_drop_side_effect_free(AST_Expansion, function (compressor, first_in_statement) {
  304. return this.expression.drop_side_effect_free(compressor, first_in_statement);
  305. });
  306. def_drop_side_effect_free(AST_TemplateSegment, return_null);
  307. def_drop_side_effect_free(AST_TemplateString, function (compressor) {
  308. var values = trim(this.segments, compressor, first_in_statement);
  309. return values && make_sequence(this, values);
  310. });