size.js 11 KB

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