size.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. /** Count commas/semicolons necessary to show a list of expressions/statements */
  110. const list_overhead = (array) => array.length && array.length - 1;
  111. AST_Block.prototype._size = function () {
  112. return 2 + list_overhead(this.body);
  113. };
  114. AST_Toplevel.prototype._size = function() {
  115. return list_overhead(this.body);
  116. };
  117. AST_EmptyStatement.prototype._size = () => 1;
  118. AST_LabeledStatement.prototype._size = () => 2; // x:
  119. AST_Do.prototype._size = () => 9;
  120. AST_While.prototype._size = () => 7;
  121. AST_For.prototype._size = () => 8;
  122. AST_ForIn.prototype._size = () => 8;
  123. // AST_ForOf inherits ^
  124. AST_With.prototype._size = () => 6;
  125. AST_Expansion.prototype._size = () => 3;
  126. const lambda_modifiers = func =>
  127. (func.is_generator ? 1 : 0) + (func.async ? 6 : 0);
  128. AST_Accessor.prototype._size = function () {
  129. return lambda_modifiers(this) + 4 + list_overhead(this.argnames) + list_overhead(this.body);
  130. };
  131. AST_Function.prototype._size = function (info) {
  132. const first = !!first_in_statement(info);
  133. return (first * 2) + lambda_modifiers(this) + 12 + list_overhead(this.argnames) + list_overhead(this.body);
  134. };
  135. AST_Defun.prototype._size = function () {
  136. return lambda_modifiers(this) + 13 + list_overhead(this.argnames) + list_overhead(this.body);
  137. };
  138. AST_Arrow.prototype._size = function () {
  139. let args_and_arrow = 2 + list_overhead(this.argnames);
  140. if (
  141. !(
  142. this.argnames.length === 1
  143. && this.argnames[0] instanceof AST_Symbol
  144. )
  145. ) {
  146. args_and_arrow += 2; // parens around the args
  147. }
  148. const body_overhead = this.is_braceless() ? 0 : list_overhead(this.body) + 2;
  149. return lambda_modifiers(this) + args_and_arrow + body_overhead;
  150. };
  151. AST_Destructuring.prototype._size = () => 2;
  152. AST_TemplateString.prototype._size = function () {
  153. return 2 + (Math.floor(this.segments.length / 2) * 3); /* "${}" */
  154. };
  155. AST_TemplateSegment.prototype._size = function () {
  156. return this.value.length;
  157. };
  158. AST_Return.prototype._size = function () {
  159. return this.value ? 7 : 6;
  160. };
  161. AST_Throw.prototype._size = () => 6;
  162. AST_Break.prototype._size = function () {
  163. return this.label ? 6 : 5;
  164. };
  165. AST_Continue.prototype._size = function () {
  166. return this.label ? 9 : 8;
  167. };
  168. AST_If.prototype._size = () => 4;
  169. AST_Switch.prototype._size = function () {
  170. return 8 + list_overhead(this.body);
  171. };
  172. AST_Case.prototype._size = function () {
  173. return 5 + list_overhead(this.body);
  174. };
  175. AST_Default.prototype._size = function () {
  176. return 8 + list_overhead(this.body);
  177. };
  178. AST_Try.prototype._size = function () {
  179. return 3 + list_overhead(this.body);
  180. };
  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_Class.prototype._size = function () {
  317. return (
  318. (this.name ? 8 : 7)
  319. + (this.extends ? 8 : 0)
  320. );
  321. };
  322. AST_ClassProperty.prototype._size = function () {
  323. return (
  324. static_size(this.static)
  325. + (typeof this.key === "string" ? this.key.length + 2 : 0)
  326. + (this.value ? 1 : 0)
  327. );
  328. };
  329. AST_ClassPrivateProperty.prototype._size = function () {
  330. return AST_ClassProperty.prototype._size.call(this) + 1;
  331. };
  332. AST_Symbol.prototype._size = function () {
  333. return !mangle_options || this.definition().unmangleable(mangle_options)
  334. ? this.name.length
  335. : 1;
  336. };
  337. // TODO take propmangle into account
  338. AST_SymbolClassProperty.prototype._size = function () {
  339. return this.name.length;
  340. };
  341. AST_SymbolRef.prototype._size = AST_SymbolDeclaration.prototype._size = function () {
  342. const { name, thedef } = this;
  343. if (thedef && thedef.global) return name.length;
  344. if (name === "arguments") return 9;
  345. return AST_Symbol.prototype._size.call(this);
  346. };
  347. AST_NewTarget.prototype._size = () => 10;
  348. AST_SymbolImportForeign.prototype._size = function () {
  349. return this.name.length;
  350. };
  351. AST_SymbolExportForeign.prototype._size = function () {
  352. return this.name.length;
  353. };
  354. AST_This.prototype._size = () => 4;
  355. AST_Super.prototype._size = () => 5;
  356. AST_String.prototype._size = function () {
  357. return this.value.length + 2;
  358. };
  359. AST_Number.prototype._size = function () {
  360. const { value } = this;
  361. if (value === 0) return 1;
  362. if (value > 0 && Math.floor(value) === value) {
  363. return Math.floor(Math.log10(value) + 1);
  364. }
  365. return value.toString().length;
  366. };
  367. AST_BigInt.prototype._size = function () {
  368. return this.value.length;
  369. };
  370. AST_RegExp.prototype._size = function () {
  371. return this.value.toString().length;
  372. };
  373. AST_Null.prototype._size = () => 4;
  374. AST_NaN.prototype._size = () => 3;
  375. AST_Undefined.prototype._size = () => 6; // "void 0"
  376. AST_Hole.prototype._size = () => 0; // comma is taken into account by list_overhead()
  377. AST_Infinity.prototype._size = () => 8;
  378. AST_True.prototype._size = () => 4;
  379. AST_False.prototype._size = () => 5;
  380. AST_Await.prototype._size = () => 6;
  381. AST_Yield.prototype._size = () => 6;