size.js 11 KB

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