size.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 = () => 3;
  181. AST_Catch.prototype._size = function () {
  182. let size = 7 + list_overhead(this.body);
  183. if (this.argname) {
  184. size += 2;
  185. }
  186. return size;
  187. };
  188. AST_Finally.prototype._size = function () {
  189. return 7 + list_overhead(this.body);
  190. };
  191. AST_Var.prototype._size = function () {
  192. return 4 + list_overhead(this.definitions);
  193. };
  194. AST_Let.prototype._size = function () {
  195. return 4 + list_overhead(this.definitions);
  196. };
  197. AST_Const.prototype._size = function () {
  198. return 6 + list_overhead(this.definitions);
  199. };
  200. AST_VarDef.prototype._size = function () {
  201. return this.value ? 1 : 0;
  202. };
  203. AST_NameMapping.prototype._size = function () {
  204. // foreign name isn't mangled
  205. return this.name ? 4 : 0;
  206. };
  207. AST_Import.prototype._size = function () {
  208. // import
  209. let size = 6;
  210. if (this.imported_name) size += 1;
  211. // from
  212. if (this.imported_name || this.imported_names) size += 5;
  213. // braces, and the commas
  214. if (this.imported_names) {
  215. size += 2 + list_overhead(this.imported_names);
  216. }
  217. return size;
  218. };
  219. AST_ImportMeta.prototype._size = () => 11;
  220. AST_Export.prototype._size = function () {
  221. let size = 7 + (this.is_default ? 8 : 0);
  222. if (this.exported_value) {
  223. size += this.exported_value._size();
  224. }
  225. if (this.exported_names) {
  226. // Braces and commas
  227. size += 2 + list_overhead(this.exported_names);
  228. }
  229. if (this.module_name) {
  230. // "from "
  231. size += 5;
  232. }
  233. return size;
  234. };
  235. AST_Call.prototype._size = function () {
  236. if (this.optional) {
  237. return 4 + list_overhead(this.args);
  238. }
  239. return 2 + list_overhead(this.args);
  240. };
  241. AST_New.prototype._size = function () {
  242. return 6 + list_overhead(this.args);
  243. };
  244. AST_Sequence.prototype._size = function () {
  245. return list_overhead(this.expressions);
  246. };
  247. AST_Dot.prototype._size = function () {
  248. if (this.optional) {
  249. return this.property.length + 2;
  250. }
  251. return this.property.length + 1;
  252. };
  253. AST_DotHash.prototype._size = function () {
  254. if (this.optional) {
  255. return this.property.length + 3;
  256. }
  257. return this.property.length + 2;
  258. };
  259. AST_Sub.prototype._size = function () {
  260. return this.optional ? 4 : 2;
  261. };
  262. AST_Unary.prototype._size = function () {
  263. if (this.operator === "typeof") return 7;
  264. if (this.operator === "void") return 5;
  265. return this.operator.length;
  266. };
  267. AST_Binary.prototype._size = function (info) {
  268. if (this.operator === "in") return 4;
  269. let size = this.operator.length;
  270. if (
  271. (this.operator === "+" || this.operator === "-")
  272. && this.right instanceof AST_Unary && this.right.operator === this.operator
  273. ) {
  274. // 1+ +a > needs space between the +
  275. size += 1;
  276. }
  277. if (this.needs_parens(info)) {
  278. size += 2;
  279. }
  280. return size;
  281. };
  282. AST_Conditional.prototype._size = () => 3;
  283. AST_Array.prototype._size = function () {
  284. return 2 + list_overhead(this.elements);
  285. };
  286. AST_Object.prototype._size = function (info) {
  287. let base = 2;
  288. if (first_in_statement(info)) {
  289. base += 2; // parens
  290. }
  291. return base + list_overhead(this.properties);
  292. };
  293. /*#__INLINE__*/
  294. const key_size = key =>
  295. typeof key === "string" ? key.length : 0;
  296. AST_ObjectKeyVal.prototype._size = function () {
  297. return key_size(this.key) + 1;
  298. };
  299. /*#__INLINE__*/
  300. const static_size = is_static => is_static ? 7 : 0;
  301. AST_ObjectGetter.prototype._size = function () {
  302. return 5 + static_size(this.static) + key_size(this.key);
  303. };
  304. AST_ObjectSetter.prototype._size = function () {
  305. return 5 + static_size(this.static) + key_size(this.key);
  306. };
  307. AST_ConciseMethod.prototype._size = function () {
  308. return static_size(this.static) + key_size(this.key) + lambda_modifiers(this);
  309. };
  310. AST_PrivateMethod.prototype._size = function () {
  311. return AST_ConciseMethod.prototype._size.call(this) + 1;
  312. };
  313. AST_PrivateGetter.prototype._size = AST_PrivateSetter.prototype._size = function () {
  314. return AST_ConciseMethod.prototype._size.call(this) + 4;
  315. };
  316. AST_PrivateIn.prototype._size = function () {
  317. return 5; // "#", and " in "
  318. };
  319. AST_Class.prototype._size = function () {
  320. return (
  321. (this.name ? 8 : 7)
  322. + (this.extends ? 8 : 0)
  323. );
  324. };
  325. AST_ClassStaticBlock.prototype._size = function () {
  326. // "static{}" + semicolons
  327. return 8 + list_overhead(this.body);
  328. };
  329. AST_ClassProperty.prototype._size = function () {
  330. return (
  331. static_size(this.static)
  332. + (typeof this.key === "string" ? this.key.length + 2 : 0)
  333. + (this.value ? 1 : 0)
  334. );
  335. };
  336. AST_ClassPrivateProperty.prototype._size = function () {
  337. return AST_ClassProperty.prototype._size.call(this) + 1;
  338. };
  339. AST_Symbol.prototype._size = function () {
  340. if (!(mangle_options && this.thedef && !this.thedef.unmangleable(mangle_options))) {
  341. return this.name.length;
  342. } else {
  343. return 1;
  344. }
  345. };
  346. // TODO take propmangle into account
  347. AST_SymbolClassProperty.prototype._size = function () {
  348. return this.name.length;
  349. };
  350. AST_SymbolRef.prototype._size = AST_SymbolDeclaration.prototype._size = function () {
  351. if (this.name === "arguments") return 9;
  352. return AST_Symbol.prototype._size.call(this);
  353. };
  354. AST_NewTarget.prototype._size = () => 10;
  355. AST_SymbolImportForeign.prototype._size = function () {
  356. return this.name.length;
  357. };
  358. AST_SymbolExportForeign.prototype._size = function () {
  359. return this.name.length;
  360. };
  361. AST_This.prototype._size = () => 4;
  362. AST_Super.prototype._size = () => 5;
  363. AST_String.prototype._size = function () {
  364. return this.value.length + 2;
  365. };
  366. AST_Number.prototype._size = function () {
  367. const { value } = this;
  368. if (value === 0) return 1;
  369. if (value > 0 && Math.floor(value) === value) {
  370. return Math.floor(Math.log10(value) + 1);
  371. }
  372. return value.toString().length;
  373. };
  374. AST_BigInt.prototype._size = function () {
  375. return this.value.length;
  376. };
  377. AST_RegExp.prototype._size = function () {
  378. return this.value.toString().length;
  379. };
  380. AST_Null.prototype._size = () => 4;
  381. AST_NaN.prototype._size = () => 3;
  382. AST_Undefined.prototype._size = () => 6; // "void 0"
  383. AST_Hole.prototype._size = () => 0; // comma is taken into account by list_overhead()
  384. AST_Infinity.prototype._size = () => 8;
  385. AST_True.prototype._size = () => 4;
  386. AST_False.prototype._size = () => 5;
  387. AST_Await.prototype._size = () => 6;
  388. AST_Yield.prototype._size = () => 6;