terser 5.12.1 → 5.14.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.
@@ -80,9 +80,13 @@ import {
80
80
  AST_Undefined,
81
81
 
82
82
  TreeWalker,
83
+ walk,
84
+ walk_abort,
85
+ walk_parent,
83
86
  } from "../ast.js";
84
87
  import { make_node, regexp_source_fix, string_template, makePredicate } from "../utils/index.js";
85
88
  import { first_in_statement } from "../utils/first_in_statement.js";
89
+ import { has_flag, TOP } from "./compressor-flags.js";
86
90
 
87
91
  export function merge_sequence(array, node) {
88
92
  if (node instanceof AST_Sequence) {
@@ -244,6 +248,13 @@ export function is_iife_call(node) {
244
248
  return node.expression instanceof AST_Function || is_iife_call(node.expression);
245
249
  }
246
250
 
251
+ export function is_empty(thing) {
252
+ if (thing === null) return true;
253
+ if (thing instanceof AST_EmptyStatement) return true;
254
+ if (thing instanceof AST_BlockStatement) return thing.body.length == 0;
255
+ return false;
256
+ }
257
+
247
258
  export const identifier_atom = makePredicate("Infinity NaN undefined");
248
259
  export function is_identifier_atom(node) {
249
260
  return node instanceof AST_Infinity
@@ -281,6 +292,34 @@ export function as_statement_array(thing) {
281
292
  throw new Error("Can't convert thing to statement array");
282
293
  }
283
294
 
295
+ export function is_reachable(scope_node, defs) {
296
+ const find_ref = node => {
297
+ if (node instanceof AST_SymbolRef && defs.includes(node.definition())) {
298
+ return walk_abort;
299
+ }
300
+ };
301
+
302
+ return walk_parent(scope_node, (node, info) => {
303
+ if (node instanceof AST_Scope && node !== scope_node) {
304
+ var parent = info.parent();
305
+
306
+ if (
307
+ parent instanceof AST_Call
308
+ && parent.expression === node
309
+ // Async/Generators aren't guaranteed to sync evaluate all of
310
+ // their body steps, so it's possible they close over the variable.
311
+ && !(node.async || node.is_generator)
312
+ ) {
313
+ return;
314
+ }
315
+
316
+ if (walk(node, find_ref)) return walk_abort;
317
+
318
+ return true;
319
+ }
320
+ });
321
+ }
322
+
284
323
  /** Check if a ref refers to the name of a function/class it's defined within */
285
324
  export function is_recursive_ref(compressor, def) {
286
325
  var node;
@@ -294,3 +333,12 @@ export function is_recursive_ref(compressor, def) {
294
333
  }
295
334
  return false;
296
335
  }
336
+
337
+ // TODO this only works with AST_Defun, shouldn't it work for other ways of defining functions?
338
+ export function retain_top_func(fn, compressor) {
339
+ return compressor.top_retain
340
+ && fn instanceof AST_Defun
341
+ && has_flag(fn, TOP)
342
+ && fn.name
343
+ && compressor.top_retain(fn.name);
344
+ }
@@ -128,14 +128,15 @@ def_eval(AST_Constant, function () {
128
128
  def_eval(AST_BigInt, return_this);
129
129
 
130
130
  def_eval(AST_RegExp, function (compressor) {
131
- let evaluated = compressor.evaluated_regexps.get(this);
131
+ let evaluated = compressor.evaluated_regexps.get(this.value);
132
132
  if (evaluated === undefined) {
133
133
  try {
134
- evaluated = (0, eval)(this.print_to_string());
134
+ const { source, flags } = this.value;
135
+ evaluated = new RegExp(source, flags);
135
136
  } catch (e) {
136
137
  evaluated = null;
137
138
  }
138
- compressor.evaluated_regexps.set(this, evaluated);
139
+ compressor.evaluated_regexps.set(this.value, evaluated);
139
140
  }
140
141
  return evaluated || this;
141
142
  });