size.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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_RegExp,
  57. AST_Return,
  58. AST_Sequence,
  59. AST_String,
  60. AST_Sub,
  61. AST_Super,
  62. AST_Switch,
  63. AST_Symbol,
  64. AST_SymbolClassProperty,
  65. AST_SymbolExportForeign,
  66. AST_SymbolImportForeign,
  67. AST_SymbolRef,
  68. AST_SymbolDeclaration,
  69. AST_TemplateSegment,
  70. AST_TemplateString,
  71. AST_This,
  72. AST_Throw,
  73. AST_Toplevel,
  74. AST_True,
  75. AST_Try,
  76. AST_Catch,
  77. AST_Finally,
  78. AST_Unary,
  79. AST_Undefined,
  80. AST_Var,
  81. AST_VarDef,
  82. AST_While,
  83. AST_With,
  84. AST_Yield,
  85. walk_parent
  86. } from "./ast.js";
  87. import { first_in_statement } from "./utils/first_in_statement.js";
  88. let mangle_options = undefined;
  89. AST_Node.prototype.size = function (compressor, stack) {
  90. mangle_options = compressor && compressor.mangle_options;
  91. let size = 0;
  92. walk_parent(this, (node, info) => {
  93. size += node._size(info);
  94. // Braceless arrow functions have fake "return" statements
  95. if (node instanceof AST_Arrow && node.is_braceless()) {
  96. size += node.body[0].value._size(info);
  97. return true;
  98. }
  99. }, stack || (compressor && compressor.stack));
  100. // just to save a bit of memory
  101. mangle_options = undefined;
  102. return size;
  103. };
  104. AST_Node.prototype._size = () => 0;
  105. AST_Debugger.prototype._size = () => 8;
  106. AST_Directive.prototype._size = function () {
  107. // TODO string encoding stuff
  108. return 2 + this.value.length;
  109. };
  110. /** Count commas/semicolons necessary to show a list of expressions/statements */
  111. const list_overhead = (array) => array.length && array.length - 1;
  112. AST_Block.prototype._size = function () {
  113. return 2 + list_overhead(this.body);
  114. };
  115. AST_Toplevel.prototype._size = function() {
  116. return list_overhead(this.body);
  117. };
  118. AST_EmptyStatement.prototype._size = () => 1;
  119. AST_LabeledStatement.prototype._size = () => 2; // x:
  120. AST_Do.prototype._size = () => 9;
  121. AST_While.prototype._size = () => 7;
  122. AST_For.prototype._size = () => 8;
  123. AST_ForIn.prototype._size = () => 8;
  124. // AST_ForOf inherits ^
  125. AST_With.prototype._size = () => 6;
  126. AST_Expansion.prototype._size = () => 3;
  127. const lambda_modifiers = func =>
  128. (func.is_generator ? 1 : 0) + (func.async ? 6 : 0);
  129. AST_Accessor.prototype._size = function () {
  130. return lambda_modifiers(this) + 4 + list_overhead(this.argnames) + list_overhead(this.body);
  131. };
  132. AST_Function.prototype._size = function (info) {
  133. const first = !!first_in_statement(info);
  134. return (first * 2) + lambda_modifiers(this) + 12 + list_overhead(this.argnames) + list_overhead(this.body);
  135. };
  136. AST_Defun.prototype._size = function () {
  137. return lambda_modifiers(this) + 13 + list_overhead(this.argnames) + list_overhead(this.body);
  138. };
  139. AST_Arrow.prototype._size = function () {
  140. let args_and_arrow = 2 + list_overhead(this.argnames);
  141. if (
  142. !(
  143. this.argnames.length === 1
  144. && this.argnames[0] instanceof AST_Symbol
  145. )
  146. ) {
  147. args_and_arrow += 2; // parens around the args
  148. }
  149. const body_overhead = this.is_braceless() ? 0 : list_overhead(this.body) + 2;
  150. return lambda_modifiers(this) + args_and_arrow + body_overhead;
  151. };
  152. AST_Destructuring.prototype._size = () => 2;
  153. AST_TemplateString.prototype._size = function () {
  154. return 2 + (Math.floor(this.segments.length / 2) * 3); /* "${}" */
  155. };
  156. AST_TemplateSegment.prototype._size = function () {
  157. return this.value.length;
  158. };
  159. AST_Return.prototype._size = function () {
  160. return this.value ? 7 : 6;
  161. };
  162. AST_Throw.prototype._size = () => 6;
  163. AST_Break.prototype._size = function () {
  164. return this.label ? 6 : 5;
  165. };
  166. AST_Continue.prototype._size = function () {
  167. return this.label ? 9 : 8;
  168. };
  169. AST_If.prototype._size = () => 4;
  170. AST_Switch.prototype._size = function () {
  171. return 8 + list_overhead(this.body);
  172. };
  173. AST_Case.prototype._size = function () {
  174. return 5 + list_overhead(this.body);
  175. };
  176. AST_Default.prototype._size = function () {
  177. return 8 + list_overhead(this.body);
  178. };
  179. AST_Try.prototype._size = function () {
  180. return 3 + list_overhead(this.body);
  181. };
  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_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_ClassStaticBlock.prototype._size = function () {
  324. // "class{}" + semicolons
  325. return 7 + list_overhead(this.body);
  326. };
  327. AST_ClassProperty.prototype._size = function () {
  328. return (
  329. static_size(this.static)
  330. + (typeof this.key === "string" ? this.key.length + 2 : 0)
  331. + (this.value ? 1 : 0)
  332. );
  333. };
  334. AST_ClassPrivateProperty.prototype._size = function () {
  335. return AST_ClassProperty.prototype._size.call(this) + 1;
  336. };
  337. AST_Symbol.prototype._size = function () {
  338. return !mangle_options || this.definition().unmangleable(mangle_options)
  339. ? this.name.length
  340. : 1;
  341. };
  342. // TODO take propmangle into account
  343. AST_SymbolClassProperty.prototype._size = function () {
  344. return this.name.length;
  345. };
  346. AST_SymbolRef.prototype._size = AST_SymbolDeclaration.prototype._size = function () {
  347. const { name, thedef } = this;
  348. if (thedef && thedef.global) return name.length;
  349. if (name === "arguments") return 9;
  350. return AST_Symbol.prototype._size.call(this);
  351. };
  352. AST_NewTarget.prototype._size = () => 10;
  353. AST_SymbolImportForeign.prototype._size = function () {
  354. return this.name.length;
  355. };
  356. AST_SymbolExportForeign.prototype._size = function () {
  357. return this.name.length;
  358. };
  359. AST_This.prototype._size = () => 4;
  360. AST_Super.prototype._size = () => 5;
  361. AST_String.prototype._size = function () {
  362. return this.value.length + 2;
  363. };
  364. AST_Number.prototype._size = function () {
  365. const { value } = this;
  366. if (value === 0) return 1;
  367. if (value > 0 && Math.floor(value) === value) {
  368. return Math.floor(Math.log10(value) + 1);
  369. }
  370. return value.toString().length;
  371. };
  372. AST_BigInt.prototype._size = function () {
  373. return this.value.length;
  374. };
  375. AST_RegExp.prototype._size = function () {
  376. return this.value.toString().length;
  377. };
  378. AST_Null.prototype._size = () => 4;
  379. AST_NaN.prototype._size = () => 3;
  380. AST_Undefined.prototype._size = () => 6; // "void 0"
  381. AST_Hole.prototype._size = () => 0; // comma is taken into account by list_overhead()
  382. AST_Infinity.prototype._size = () => 8;
  383. AST_True.prototype._size = () => 4;
  384. AST_False.prototype._size = () => 5;
  385. AST_Await.prototype._size = () => 6;
  386. AST_Yield.prototype._size = () => 6;