terser 5.18.1 → 5.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # Changelog
2
2
 
3
- ## v7.18.1
3
+ ## v5.19.0
4
+ - Allow `/*@__MANGLE_PROP__*/` annotation in `object.property`, in addition to property declarations.
5
+
6
+ ## v5.18.2
7
+ - Stop using recursion in hoisted defuns fix.
8
+
9
+ ## v5.18.1
4
10
  - Fix major performance issue caused by hoisted defuns' scopes bugfix.
5
11
 
6
- ## v7.18.0
12
+ ## v5.18.0
7
13
  - Add new `/*@__MANGLE_PROP__*/` annotation, to mark properties that should be mangled.
8
14
 
9
15
  ## v5.17.7
@@ -3283,25 +3283,25 @@ function parse($TEXT, options) {
3283
3283
  if(is("privatename") && !S.in_class)
3284
3284
  croak("Private field must be used in an enclosing class");
3285
3285
  const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot;
3286
- return subscripts(new AST_DotVariant({
3286
+ return annotate(subscripts(new AST_DotVariant({
3287
3287
  start : start,
3288
3288
  expression : expr,
3289
3289
  optional : false,
3290
3290
  property : as_name(),
3291
3291
  end : prev()
3292
- }), allow_calls, is_chain);
3292
+ }), allow_calls, is_chain));
3293
3293
  }
3294
3294
  if (is("punc", "[")) {
3295
3295
  next();
3296
3296
  var prop = expression(true);
3297
3297
  expect("]");
3298
- return subscripts(new AST_Sub({
3298
+ return annotate(subscripts(new AST_Sub({
3299
3299
  start : start,
3300
3300
  expression : expr,
3301
3301
  optional : false,
3302
3302
  property : prop,
3303
3303
  end : prev()
3304
- }), allow_calls, is_chain);
3304
+ }), allow_calls, is_chain));
3305
3305
  }
3306
3306
  if (allow_calls && is("punc", "(")) {
3307
3307
  next();
@@ -3338,24 +3338,24 @@ function parse($TEXT, options) {
3338
3338
  if(is("privatename") && !S.in_class)
3339
3339
  croak("Private field must be used in an enclosing class");
3340
3340
  const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot;
3341
- chain_contents = subscripts(new AST_DotVariant({
3341
+ chain_contents = annotate(subscripts(new AST_DotVariant({
3342
3342
  start,
3343
3343
  expression: expr,
3344
3344
  optional: true,
3345
3345
  property: as_name(),
3346
3346
  end: prev()
3347
- }), allow_calls, true);
3347
+ }), allow_calls, true));
3348
3348
  } else if (is("punc", "[")) {
3349
3349
  next();
3350
3350
  const property = expression(true);
3351
3351
  expect("]");
3352
- chain_contents = subscripts(new AST_Sub({
3352
+ chain_contents = annotate(subscripts(new AST_Sub({
3353
3353
  start,
3354
3354
  expression: expr,
3355
3355
  optional: true,
3356
3356
  property,
3357
3357
  end: prev()
3358
- }), allow_calls, true);
3358
+ }), allow_calls, true));
3359
3359
  }
3360
3360
 
3361
3361
  if (!chain_contents) unexpected();
@@ -5333,6 +5333,7 @@ var AST_Dot = DEFNODE("Dot", "quote", function AST_Dot(props) {
5333
5333
  this.expression = props.expression;
5334
5334
  this.property = props.property;
5335
5335
  this.optional = props.optional;
5336
+ this._annotations = props._annotations;
5336
5337
  this.start = props.start;
5337
5338
  this.end = props.end;
5338
5339
  }
@@ -5380,6 +5381,7 @@ var AST_Sub = DEFNODE("Sub", null, function AST_Sub(props) {
5380
5381
  this.expression = props.expression;
5381
5382
  this.property = props.property;
5382
5383
  this.optional = props.optional;
5384
+ this._annotations = props._annotations;
5383
5385
  this.start = props.start;
5384
5386
  this.end = props.end;
5385
5387
  }
@@ -16013,23 +16015,25 @@ function handle_defined_after_hoist(parent) {
16013
16015
 
16014
16016
  // find the index in `found_symbols`, with some special rules:
16015
16017
  const find = (sym_id, starting_at = 0, must_be_write = false) => {
16016
- const index = found_symbols.indexOf(sym_id, starting_at);
16018
+ let index = starting_at;
16017
16019
 
16018
- if (
16019
- defun_range
16020
- && index >= defun_range.start
16021
- && index < defun_range.end
16022
- ) {
16023
- return find(sym_id, defun_range.end, must_be_write);
16024
- } else if (
16025
- must_be_write
16026
- && index >= 0
16027
- && !found_symbol_writes.has(index)
16028
- ) {
16029
- return find(sym_id, index + 1, must_be_write);
16030
- } else {
16031
- return index;
16020
+ for (;;) {
16021
+ index = found_symbols.indexOf(sym_id, index);
16022
+
16023
+ if (index === -1) {
16024
+ break;
16025
+ } else if (index >= defun_range.start && index < defun_range.end) {
16026
+ index = defun_range.end;
16027
+ continue;
16028
+ } else if (must_be_write && !found_symbol_writes.has(index)) {
16029
+ index++;
16030
+ continue;
16031
+ } else {
16032
+ break;
16033
+ }
16032
16034
  }
16035
+
16036
+ return index;
16033
16037
  };
16034
16038
 
16035
16039
  const read_defun_at = find(fname_def.id);
@@ -29870,7 +29874,38 @@ function mangle_private_properties(ast, options) {
29870
29874
  }
29871
29875
  }
29872
29876
 
29873
- function mangle_properties(ast, options) {
29877
+ function find_annotated_props(ast) {
29878
+ var annotated_props = new Set();
29879
+ walk(ast, node => {
29880
+ if (
29881
+ node instanceof AST_ClassPrivateProperty
29882
+ || node instanceof AST_PrivateMethod
29883
+ || node instanceof AST_PrivateGetter
29884
+ || node instanceof AST_PrivateSetter
29885
+ || node instanceof AST_DotHash
29886
+ ) ; else if (node instanceof AST_ObjectKeyVal) {
29887
+ if (typeof node.key == "string" && has_annotation(node, _MANGLEPROP)) {
29888
+ annotated_props.add(node.key);
29889
+ }
29890
+ } else if (node instanceof AST_ObjectProperty) {
29891
+ // setter or getter, since KeyVal is handled above
29892
+ if (has_annotation(node, _MANGLEPROP)) {
29893
+ annotated_props.add(node.key.name);
29894
+ }
29895
+ } else if (node instanceof AST_Dot) {
29896
+ if (has_annotation(node, _MANGLEPROP)) {
29897
+ annotated_props.add(node.property);
29898
+ }
29899
+ } else if (node instanceof AST_Sub) {
29900
+ if (node.property instanceof AST_String && has_annotation(node, _MANGLEPROP)) {
29901
+ annotated_props.add(node.property.value);
29902
+ }
29903
+ }
29904
+ });
29905
+ return annotated_props;
29906
+ }
29907
+
29908
+ function mangle_properties(ast, options, annotated_props = find_annotated_props(ast)) {
29874
29909
  options = defaults(options, {
29875
29910
  builtins: false,
29876
29911
  cache: null,
@@ -29912,7 +29947,6 @@ function mangle_properties(ast, options) {
29912
29947
  debug_name_suffix = (options.debug === true ? "" : options.debug);
29913
29948
  }
29914
29949
 
29915
- var annotated_names = new Set();
29916
29950
  var names_to_mangle = new Set();
29917
29951
  var unmangleable = new Set();
29918
29952
  // Track each already-mangled name to prevent nth_identifier from generating
@@ -29921,36 +29955,7 @@ function mangle_properties(ast, options) {
29921
29955
 
29922
29956
  var keep_quoted = !!options.keep_quoted;
29923
29957
 
29924
- // Step 1: Find all annotated /*@__MANGLEPROP__*/
29925
- walk(ast, node => {
29926
- if (
29927
- node instanceof AST_ClassPrivateProperty
29928
- || node instanceof AST_PrivateMethod
29929
- || node instanceof AST_PrivateGetter
29930
- || node instanceof AST_PrivateSetter
29931
- || node instanceof AST_DotHash
29932
- ) ; else if (node instanceof AST_ObjectKeyVal) {
29933
- if (
29934
- typeof node.key == "string"
29935
- && has_annotation(node, _MANGLEPROP)
29936
- && can_mangle(node.key)
29937
- ) {
29938
- annotated_names.add(node.key);
29939
- clear_annotation(node, _MANGLEPROP);
29940
- }
29941
- } else if (node instanceof AST_ObjectProperty) {
29942
- // setter or getter, since KeyVal is handled above
29943
- if (
29944
- has_annotation(node, _MANGLEPROP)
29945
- && can_mangle(node.key.name)
29946
- ) {
29947
- annotated_names.add(node.key.name);
29948
- clear_annotation(node, _MANGLEPROP);
29949
- }
29950
- }
29951
- });
29952
-
29953
- // step 2: find candidates to mangle
29958
+ // step 1: find candidates to mangle
29954
29959
  ast.walk(new TreeWalker(function(node) {
29955
29960
  if (
29956
29961
  node instanceof AST_ClassPrivateProperty
@@ -30042,9 +30047,9 @@ function mangle_properties(ast, options) {
30042
30047
  }
30043
30048
 
30044
30049
  function should_mangle(name) {
30045
- if (only_annotated && !annotated_names.has(name)) return false;
30050
+ if (only_annotated && !annotated_props.has(name)) return false;
30046
30051
  if (regex && !regex.test(name)) {
30047
- return annotated_names.has(name);
30052
+ return annotated_props.has(name);
30048
30053
  }
30049
30054
  if (reserved.has(name)) return false;
30050
30055
  return cache.has(name)
@@ -30323,6 +30328,10 @@ async function minify(files, options, _fs_module) {
30323
30328
  if (quoted_props && options.mangle.properties.keep_quoted !== "strict") {
30324
30329
  reserve_quoted_keys(toplevel, quoted_props);
30325
30330
  }
30331
+ var annotated_props;
30332
+ if (options.mangle && options.mangle.properties) {
30333
+ annotated_props = find_annotated_props(toplevel);
30334
+ }
30326
30335
  if (options.wrap) {
30327
30336
  toplevel = toplevel.wrap_commonjs(options.wrap);
30328
30337
  }
@@ -30350,7 +30359,7 @@ async function minify(files, options, _fs_module) {
30350
30359
  }
30351
30360
  if (timings) timings.properties = Date.now();
30352
30361
  if (options.mangle && options.mangle.properties) {
30353
- toplevel = mangle_properties(toplevel, options.mangle.properties);
30362
+ toplevel = mangle_properties(toplevel, options.mangle.properties, annotated_props);
30354
30363
  }
30355
30364
 
30356
30365
  // Format phase
package/lib/ast.js CHANGED
@@ -1714,6 +1714,7 @@ var AST_Dot = DEFNODE("Dot", "quote", function AST_Dot(props) {
1714
1714
  this.expression = props.expression;
1715
1715
  this.property = props.property;
1716
1716
  this.optional = props.optional;
1717
+ this._annotations = props._annotations;
1717
1718
  this.start = props.start;
1718
1719
  this.end = props.end;
1719
1720
  }
@@ -1761,6 +1762,7 @@ var AST_Sub = DEFNODE("Sub", null, function AST_Sub(props) {
1761
1762
  this.expression = props.expression;
1762
1763
  this.property = props.property;
1763
1764
  this.optional = props.optional;
1765
+ this._annotations = props._annotations;
1764
1766
  this.start = props.start;
1765
1767
  this.end = props.end;
1766
1768
  }
@@ -592,23 +592,25 @@ function handle_defined_after_hoist(parent) {
592
592
 
593
593
  // find the index in `found_symbols`, with some special rules:
594
594
  const find = (sym_id, starting_at = 0, must_be_write = false) => {
595
- const index = found_symbols.indexOf(sym_id, starting_at);
596
-
597
- if (
598
- defun_range
599
- && index >= defun_range.start
600
- && index < defun_range.end
601
- ) {
602
- return find(sym_id, defun_range.end, must_be_write);
603
- } else if (
604
- must_be_write
605
- && index >= 0
606
- && !found_symbol_writes.has(index)
607
- ) {
608
- return find(sym_id, index + 1, must_be_write);
609
- } else {
610
- return index;
595
+ let index = starting_at;
596
+
597
+ for (;;) {
598
+ index = found_symbols.indexOf(sym_id, index);
599
+
600
+ if (index === -1) {
601
+ break;
602
+ } else if (index >= defun_range.start && index < defun_range.end) {
603
+ index = defun_range.end;
604
+ continue;
605
+ } else if (must_be_write && !found_symbol_writes.has(index)) {
606
+ index++;
607
+ continue;
608
+ } else {
609
+ break;
610
+ }
611
611
  }
612
+
613
+ return index;
612
614
  };
613
615
 
614
616
  const read_defun_at = find(fname_def.id);
package/lib/minify.js CHANGED
@@ -17,6 +17,7 @@ import {
17
17
  mangle_properties,
18
18
  mangle_private_properties,
19
19
  reserve_quoted_keys,
20
+ find_annotated_props,
20
21
  } from "./propmangle.js";
21
22
 
22
23
  // to/from base64 functions
@@ -235,6 +236,10 @@ async function minify(files, options, _fs_module) {
235
236
  if (quoted_props && options.mangle.properties.keep_quoted !== "strict") {
236
237
  reserve_quoted_keys(toplevel, quoted_props);
237
238
  }
239
+ var annotated_props;
240
+ if (options.mangle && options.mangle.properties) {
241
+ annotated_props = find_annotated_props(toplevel);
242
+ }
238
243
  if (options.wrap) {
239
244
  toplevel = toplevel.wrap_commonjs(options.wrap);
240
245
  }
@@ -268,7 +273,7 @@ async function minify(files, options, _fs_module) {
268
273
  }
269
274
  if (timings) timings.properties = Date.now();
270
275
  if (options.mangle && options.mangle.properties) {
271
- toplevel = mangle_properties(toplevel, options.mangle.properties);
276
+ toplevel = mangle_properties(toplevel, options.mangle.properties, annotated_props);
272
277
  }
273
278
 
274
279
  // Format phase
package/lib/parse.js CHANGED
@@ -3138,25 +3138,25 @@ function parse($TEXT, options) {
3138
3138
  if(is("privatename") && !S.in_class)
3139
3139
  croak("Private field must be used in an enclosing class");
3140
3140
  const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot;
3141
- return subscripts(new AST_DotVariant({
3141
+ return annotate(subscripts(new AST_DotVariant({
3142
3142
  start : start,
3143
3143
  expression : expr,
3144
3144
  optional : false,
3145
3145
  property : as_name(),
3146
3146
  end : prev()
3147
- }), allow_calls, is_chain);
3147
+ }), allow_calls, is_chain));
3148
3148
  }
3149
3149
  if (is("punc", "[")) {
3150
3150
  next();
3151
3151
  var prop = expression(true);
3152
3152
  expect("]");
3153
- return subscripts(new AST_Sub({
3153
+ return annotate(subscripts(new AST_Sub({
3154
3154
  start : start,
3155
3155
  expression : expr,
3156
3156
  optional : false,
3157
3157
  property : prop,
3158
3158
  end : prev()
3159
- }), allow_calls, is_chain);
3159
+ }), allow_calls, is_chain));
3160
3160
  }
3161
3161
  if (allow_calls && is("punc", "(")) {
3162
3162
  next();
@@ -3193,24 +3193,24 @@ function parse($TEXT, options) {
3193
3193
  if(is("privatename") && !S.in_class)
3194
3194
  croak("Private field must be used in an enclosing class");
3195
3195
  const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot;
3196
- chain_contents = subscripts(new AST_DotVariant({
3196
+ chain_contents = annotate(subscripts(new AST_DotVariant({
3197
3197
  start,
3198
3198
  expression: expr,
3199
3199
  optional: true,
3200
3200
  property: as_name(),
3201
3201
  end: prev()
3202
- }), allow_calls, true);
3202
+ }), allow_calls, true));
3203
3203
  } else if (is("punc", "[")) {
3204
3204
  next();
3205
3205
  const property = expression(true);
3206
3206
  expect("]");
3207
- chain_contents = subscripts(new AST_Sub({
3207
+ chain_contents = annotate(subscripts(new AST_Sub({
3208
3208
  start,
3209
3209
  expression: expr,
3210
3210
  optional: true,
3211
3211
  property,
3212
3212
  end: prev()
3213
- }), allow_calls, true);
3213
+ }), allow_calls, true));
3214
3214
  }
3215
3215
 
3216
3216
  if (!chain_contents) unexpected();
package/lib/propmangle.js CHANGED
@@ -180,7 +180,40 @@ function mangle_private_properties(ast, options) {
180
180
  }
181
181
  }
182
182
 
183
- function mangle_properties(ast, options) {
183
+ function find_annotated_props(ast) {
184
+ var annotated_props = new Set();
185
+ walk(ast, node => {
186
+ if (
187
+ node instanceof AST_ClassPrivateProperty
188
+ || node instanceof AST_PrivateMethod
189
+ || node instanceof AST_PrivateGetter
190
+ || node instanceof AST_PrivateSetter
191
+ || node instanceof AST_DotHash
192
+ ) {
193
+ // handled by mangle_private_properties
194
+ } else if (node instanceof AST_ObjectKeyVal) {
195
+ if (typeof node.key == "string" && has_annotation(node, _MANGLEPROP)) {
196
+ annotated_props.add(node.key);
197
+ }
198
+ } else if (node instanceof AST_ObjectProperty) {
199
+ // setter or getter, since KeyVal is handled above
200
+ if (has_annotation(node, _MANGLEPROP)) {
201
+ annotated_props.add(node.key.name);
202
+ }
203
+ } else if (node instanceof AST_Dot) {
204
+ if (has_annotation(node, _MANGLEPROP)) {
205
+ annotated_props.add(node.property);
206
+ }
207
+ } else if (node instanceof AST_Sub) {
208
+ if (node.property instanceof AST_String && has_annotation(node, _MANGLEPROP)) {
209
+ annotated_props.add(node.property.value);
210
+ }
211
+ }
212
+ });
213
+ return annotated_props;
214
+ }
215
+
216
+ function mangle_properties(ast, options, annotated_props = find_annotated_props(ast)) {
184
217
  options = defaults(options, {
185
218
  builtins: false,
186
219
  cache: null,
@@ -222,7 +255,6 @@ function mangle_properties(ast, options) {
222
255
  debug_name_suffix = (options.debug === true ? "" : options.debug);
223
256
  }
224
257
 
225
- var annotated_names = new Set();
226
258
  var names_to_mangle = new Set();
227
259
  var unmangleable = new Set();
228
260
  // Track each already-mangled name to prevent nth_identifier from generating
@@ -231,38 +263,7 @@ function mangle_properties(ast, options) {
231
263
 
232
264
  var keep_quoted = !!options.keep_quoted;
233
265
 
234
- // Step 1: Find all annotated /*@__MANGLEPROP__*/
235
- walk(ast, node => {
236
- if (
237
- node instanceof AST_ClassPrivateProperty
238
- || node instanceof AST_PrivateMethod
239
- || node instanceof AST_PrivateGetter
240
- || node instanceof AST_PrivateSetter
241
- || node instanceof AST_DotHash
242
- ) {
243
- // handled by mangle_private_properties
244
- } else if (node instanceof AST_ObjectKeyVal) {
245
- if (
246
- typeof node.key == "string"
247
- && has_annotation(node, _MANGLEPROP)
248
- && can_mangle(node.key)
249
- ) {
250
- annotated_names.add(node.key);
251
- clear_annotation(node, _MANGLEPROP);
252
- }
253
- } else if (node instanceof AST_ObjectProperty) {
254
- // setter or getter, since KeyVal is handled above
255
- if (
256
- has_annotation(node, _MANGLEPROP)
257
- && can_mangle(node.key.name)
258
- ) {
259
- annotated_names.add(node.key.name);
260
- clear_annotation(node, _MANGLEPROP);
261
- }
262
- }
263
- });
264
-
265
- // step 2: find candidates to mangle
266
+ // step 1: find candidates to mangle
266
267
  ast.walk(new TreeWalker(function(node) {
267
268
  if (
268
269
  node instanceof AST_ClassPrivateProperty
@@ -358,9 +359,9 @@ function mangle_properties(ast, options) {
358
359
  }
359
360
 
360
361
  function should_mangle(name) {
361
- if (only_annotated && !annotated_names.has(name)) return false;
362
+ if (only_annotated && !annotated_props.has(name)) return false;
362
363
  if (regex && !regex.test(name)) {
363
- return annotated_names.has(name);
364
+ return annotated_props.has(name);
364
365
  }
365
366
  if (reserved.has(name)) return false;
366
367
  return cache.has(name)
@@ -427,4 +428,5 @@ export {
427
428
  reserve_quoted_keys,
428
429
  mangle_properties,
429
430
  mangle_private_properties,
431
+ find_annotated_props,
430
432
  };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "homepage": "https://terser.org",
5
5
  "author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
6
6
  "license": "BSD-2-Clause",
7
- "version": "5.18.1",
7
+ "version": "5.19.0",
8
8
  "engines": {
9
9
  "node": ">=10"
10
10
  },