size.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import {
  2. AST_Accessor,
  3. AST_Array,
  4. AST_Arrow,
  5. AST_Await,
  6. AST_BigInt,
  7. AST_Binary,
  8. AST_Block,
  9. AST_Break,
  10. AST_Call,
  11. AST_Case,
  12. AST_Class,
  13. AST_ClassPrivateProperty,
  14. AST_ClassProperty,
  15. AST_ConciseMethod,
  16. AST_Conditional,
  17. AST_Const,
  18. AST_Continue,
  19. AST_Debugger,
  20. AST_Default,
  21. AST_Defun,
  22. AST_Destructuring,
  23. AST_Directive,
  24. AST_Do,
  25. AST_Dot,
  26. AST_DotHash,
  27. AST_EmptyStatement,
  28. AST_Expansion,
  29. AST_Export,
  30. AST_False,
  31. AST_For,
  32. AST_ForIn,
  33. AST_Function,
  34. AST_Hole,
  35. AST_If,
  36. AST_Import,
  37. AST_ImportMeta,
  38. AST_Infinity,
  39. AST_LabeledStatement,
  40. AST_Let,
  41. AST_NameMapping,
  42. AST_NaN,
  43. AST_New,
  44. AST_NewTarget,
  45. AST_Node,
  46. AST_Null,
  47. AST_Number,
  48. AST_Object,
  49. AST_ObjectKeyVal,
  50. AST_ObjectGetter,
  51. AST_ObjectSetter,
  52. AST_PrivateGetter,
  53. AST_PrivateMethod,
  54. AST_PrivateSetter,
  55. AST_RegExp,
  56. AST_Return,
  57. AST_Sequence,
  58. AST_String,
  59. AST_Sub,
  60. AST_Super,
  61. AST_Switch,
  62. AST_Symbol,
  63. AST_SymbolClassProperty,
  64. AST_SymbolExportForeign,
  65. AST_SymbolImportForeign,
  66. AST_SymbolRef,
  67. AST_SymbolDeclaration,
  68. AST_TemplateSegment,
  69. AST_TemplateString,
  70. AST_This,
  71. AST_Throw,
  72. AST_Toplevel,
  73. AST_True,
  74. AST_Try,
  75. AST_Catch,
  76. AST_Finally,
  77. AST_Unary,
  78. AST_Undefined,
  79. AST_Var,
  80. AST_VarDef,
  81. AST_While,
  82. AST_With,
  83. AST_Yield,
  84. walk_parent
  85. } from "./ast.js";
  86. import { first_in_statement } from "./utils/first_in_statement.js";
  87. let mangle_options = undefined;
  88. AST_Node.prototype.size = function (compressor, stack) {
  89. mangle_options = compressor && compressor.mangle_options;
  90. let size = 0;
  91. walk_parent(this, (node, info) => {
  92. size += node._size(info);
  93. // Braceless arrow functions have fake "return" statements
  94. if (node instanceof AST_Arrow && node.is_braceless()) {
  95. size += node.body[0].value._size(info);
  96. return true;
  97. }
  98. }, stack || (compressor && compressor.stack));
  99. // just to save a bit of memory
  100. mangle_options = undefined;
  101. return size;
  102. };
  103. AST_Node.prototype._size = () => 0;
  104. AST_Debugger.prototype._size = () => 8;
  105. AST_Directive.prototype._size = function () {
  106. // TODO string encoding stuff
  107. return 2 + this.value.length;
  108. };
  109. const list_overhead = (array) => array.length && array.length - 1;
  110. AST_Block.prototype._size = function () {
  111. return 2 + list_overhead(this.body);
  112. };
  113. AST_Toplevel.prototype._size = function() {
  114. return list_overhead(this.body);
  115. };
  116. AST_EmptyStatement.prototype._size = () => 1;
  117. AST_LabeledStatement.prototype._size = () => 2; // x:
  118. AST_Do.prototype._size = () => 9;
  119. AST_While.prototype._size = () => 7;
  120. AST_For.prototype._size = () => 8;
  121. AST_ForIn.prototype._size = () => 8;
  122. // AST_ForOf inherits ^
  123. AST_With.prototype._size = () => 6;
  124. AST_Expansion.prototype._size = () => 3;
  125. const lambda_modifiers = func =>
  126. (func.is_generator ? 1 : 0) + (func.async ? 6 : 0);
  127. AST_Accessor.prototype._size = function () {
  128. return lambda_modifiers(this) + 4 + list_overhead(this.argnames) + list_overhead(this.body);
  129. };
  130. AST_Function.prototype._size = function (info) {
  131. const first = !!first_in_statement(info);
  132. return (first * 2) + lambda_modifiers(this) + 12 + list_overhead(this.argnames) + list_overhead(this.body);
  133. };
  134. AST_Defun.prototype._size = function () {
  135. return lambda_modifiers(this) + 13 + list_overhead(this.argnames) + list_overhead(this.body);
  136. };
  137. AST_Arrow.prototype._size = function () {
  138. let args_and_arrow = 2 + list_overhead(this.argnames);
  139. if (
  140. !(
  141. this.argnames.length === 1
  142. && this.argnames[0] instanceof AST_Symbol
  143. )
  144. ) {
  145. args_and_arrow += 2;
  146. }
  147. const body_overhead = this.is_braceless() ? 0 : list_overhead(this.body) + 2;
  148. return lambda_modifiers(this) + args_and_arrow + body_overhead;
  149. };
  150. AST_Destructuring.prototype._size = () => 2;
  151. AST_TemplateString.prototype._size = function () {
  152. return 2 + (Math.floor(this.segments.length / 2) * 3); /* "${}" */
  153. };
  154. AST_TemplateSegment.prototype._size = function () {
  155. return this.value.length;
  156. };
  157. AST_Return.prototype._size = function () {
  158. return this.value ? 7 : 6;
  159. };
  160. AST_Throw.prototype._size = () => 6;
  161. AST_Break.prototype._size = function () {
  162. return this.label ? 6 : 5;
  163. };
  164. AST_Continue.prototype._size = function () {
  165. return this.label ? 9 : 8;
  166. };
  167. AST_If.prototype._size = () => 4;
  168. AST_Switch.prototype._size = function () {
  169. return 8 + list_overhead(this.body);
  170. };
  171. AST_Case.prototype._size = function () {
  172. return 5 + list_overhead(this.body);
  173. };
  174. AST_Default.prototype._size = function () {
  175. return 8 + list_overhead(this.body);
  176. };
  177. AST_Try.prototype._size = function () {
  178. return 3 + list_overhead(this.body);
  179. };
  180. AST_Catch.prototype._size = function () {
  181. let size = 7 + list_overhead(this.body);
  182. if (this.argname) {
  183. size += 2;
  184. }
  185. return size;
  186. };
  187. AST_Finally.prototype._size = function () {
  188. return 7 + list_overhead(this.body);
  189. };
  190. /*#__INLINE__*/
  191. const def_size = (size, def) => size + list_overhead(def.definitions);
  192. AST_Var.prototype._size = function () {
  193. return def_size(4, this);
  194. };
  195. AST_Let.prototype._size = function () {
  196. return def_size(4, this);
  197. };
  198. AST_Const.prototype._size = function () {
  199. return def_size(6, this);
  200. };
  201. AST_VarDef.prototype._size = function () {
  202. return this.value ? 1 : 0;
  203. };
  204. AST_NameMapping.prototype._size = function () {
  205. // foreign name isn't mangled
  206. return this.name ? 4 : 0;
  207. };
  208. AST_Import.prototype._size = function () {
  209. // import
  210. let size = 6;
  211. if (this.imported_name) size += 1;
  212. // from
  213. if (this.imported_name || this.imported_names) size += 5;
  214. // braces, and the commas
  215. if (this.imported_names) {
  216. size += 2 + list_overhead(this.imported_names);
  217. }
  218. return size;
  219. };
  220. AST_ImportMeta.prototype._size = () => 11;
  221. AST_Export.prototype._size = function () {
  222. let size = 7 + (this.is_default ? 8 : 0);
  223. if (this.exported_value) {
  224. size += this.exported_value._size();
  225. }
  226. if (this.exported_names) {
  227. // Braces and commas
  228. size += 2 + list_overhead(this.exported_names);
  229. }
  230. if (this.module_name) {
  231. // "from "
  232. size += 5;
  233. }
  234. return size;
  235. };
  236. AST_Call.prototype._size = function () {
  237. if (this.optional) {
  238. return 4 + list_overhead(this.args);
  239. }
  240. return 2 + list_overhead(this.args);
  241. };
  242. AST_New.prototype._size = function () {
  243. return 6 + list_overhead(this.args);
  244. };
  245. AST_Sequence.prototype._size = function () {
  246. return list_overhead(this.expressions);
  247. };
  248. AST_Dot.prototype._size = function () {
  249. if (this.optional) {
  250. return this.property.length + 2;
  251. }
  252. return this.property.length + 1;
  253. };
  254. AST_DotHash.prototype._size = function () {
  255. if (this.optional) {
  256. return this.property.length + 3;
  257. }
  258. return this.property.length + 2;
  259. };
  260. AST_Sub.prototype._size = function () {
  261. return this.optional ? 4 : 2;
  262. };
  263. AST_Unary.prototype._size = function () {
  264. if (this.operator === "typeof") return 7;
  265. if (this.operator === "void") return 5;
  266. return this.operator.length;
  267. };
  268. AST_Binary.prototype._size = function (info) {
  269. if (this.operator === "in") return 4;
  270. let size = this.operator.length;
  271. if (
  272. (this.operator === "+" || this.operator === "-")
  273. && this.right instanceof AST_Unary && this.right.operator === this.operator
  274. ) {
  275. // 1+ +a > needs space between the +
  276. size += 1;
  277. }
  278. if (this.needs_parens(info)) {
  279. size += 2;
  280. }
  281. return size;
  282. };
  283. AST_Conditional.prototype._size = () => 3;
  284. AST_Array.prototype._size = function () {
  285. return 2 + list_overhead(this.elements);
  286. };
  287. AST_Object.prototype._size = function (info) {
  288. let base = 2;
  289. if (first_in_statement(info)) {
  290. base += 2; // parens
  291. }
  292. return base + list_overhead(this.properties);
  293. };
  294. /*#__INLINE__*/
  295. const key_size = key =>
  296. typeof key === "string" ? key.length : 0;
  297. AST_ObjectKeyVal.prototype._size = function () {
  298. return key_size(this.key) + 1;
  299. };
  300. /*#__INLINE__*/
  301. const static_size = is_static => is_static ? 7 : 0;
  302. AST_ObjectGetter.prototype._size = function () {
  303. return 5 + static_size(this.static) + key_size(this.key);
  304. };
  305. AST_ObjectSetter.prototype._size = function () {
  306. return 5 + static_size(this.static) + key_size(this.key);
  307. };
  308. AST_ConciseMethod.prototype._size = function () {
  309. return static_size(this.static) + key_size(this.key) + lambda_modifiers(this);
  310. };
  311. AST_PrivateMethod.prototype._size = function () {
  312. return AST_ConciseMethod.prototype._size.call(this) + 1;
  313. };
  314. AST_PrivateGetter.prototype._size = AST_PrivateSetter.prototype._size = function () {
  315. return AST_ConciseMethod.prototype._size.call(this) + 4;
  316. };
  317. AST_Class.prototype._size = function () {
  318. return (
  319. (this.name ? 8 : 7)
  320. + (this.extends ? 8 : 0)
  321. );
  322. };
  323. AST_ClassProperty.prototype._size = function () {
  324. return (
  325. static_size(this.static)
  326. + (typeof this.key === "string" ? this.key.length + 2 : 0)
  327. + (this.value ? 1 : 0)
  328. );
  329. };
  330. AST_ClassPrivateProperty.prototype._size = function () {
  331. return AST_ClassProperty.prototype._size.call(this) + 1;
  332. };
  333. AST_Symbol.prototype._size = function () {
  334. return !mangle_options || this.definition().unmangleable(mangle_options)
  335. ? this.name.length
  336. : 1;
  337. };
  338. // TODO take propmangle into account
  339. AST_SymbolClassProperty.prototype._size = function () {
  340. return this.name.length;
  341. };
  342. AST_SymbolRef.prototype._size = AST_SymbolDeclaration.prototype._size = function () {
  343. const { name, thedef } = this;
  344. if (thedef && thedef.global) return name.length;
  345. if (name === "arguments") return 9;
  346. return AST_Symbol.prototype._size.call(this);
  347. };
  348. AST_NewTarget.prototype._size = () => 10;
  349. AST_SymbolImportForeign.prototype._size = function () {
  350. return this.name.length;
  351. };
  352. AST_SymbolExportForeign.prototype._size = function () {
  353. return this.name.length;
  354. };
  355. AST_This.prototype._size = () => 4;
  356. AST_Super.prototype._size = () => 5;
  357. AST_String.prototype._size = function () {
  358. return this.value.length + 2;
  359. };
  360. AST_Number.prototype._size = function () {
  361. const { value } = this;
  362. if (value === 0) return 1;
  363. if (value > 0 && Math.floor(value) === value) {
  364. return Math.floor(Math.log10(value) + 1);
  365. }
  366. return value.toString().length;
  367. };
  368. AST_BigInt.prototype._size = function () {
  369. return this.value.length;
  370. };
  371. AST_RegExp.prototype._size = function () {
  372. return this.value.toString().length;
  373. };
  374. AST_Null.prototype._size = () => 4;
  375. AST_NaN.prototype._size = () => 3;
  376. AST_Undefined.prototype._size = () => 6; // "void 0"
  377. AST_Hole.prototype._size = () => 0; // comma is taken into account
  378. AST_Infinity.prototype._size = () => 8;
  379. AST_True.prototype._size = () => 4;
  380. AST_False.prototype._size = () => 5;
  381. AST_Await.prototype._size = () => 6;
  382. AST_Yield.prototype._size = () => 6;