rollup 2.75.2 → 2.75.5

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.
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v2.75.2
4
- Sun, 29 May 2022 13:58:04 GMT - commit a971f09f6c34c65e71470249783d0dcce02a9468
3
+ Rollup.js v2.75.5
4
+ Wed, 01 Jun 2022 12:43:56 GMT - commit 14dedb52b4d799deebd8b711c1e14759fc0213d9
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -14,7 +14,7 @@ import { createHash as createHash$1 } from 'crypto';
14
14
  import { promises } from 'fs';
15
15
  import { EventEmitter } from 'events';
16
16
 
17
- var version$1 = "2.75.2";
17
+ var version$1 = "2.75.5";
18
18
 
19
19
  var charToInteger = {};
20
20
  var chars$1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
@@ -5204,6 +5204,11 @@ const INCLUDE_PARAMETERS = 'variables';
5204
5204
  class NodeBase extends ExpressionEntity {
5205
5205
  constructor(esTreeNode, parent, parentScope) {
5206
5206
  super();
5207
+ // Nodes can apply custom deoptimizations once they become part of the
5208
+ // executed code. To do this, they must initialize this as false, implement
5209
+ // applyDeoptimizations and call this from include and hasEffects if they
5210
+ // have custom handlers
5211
+ this.deoptimized = false;
5207
5212
  this.esTreeNode = esTreeNode;
5208
5213
  this.keys = keys[esTreeNode.type] || getAndCreateKeys(esTreeNode);
5209
5214
  this.parent = parent;
@@ -5241,7 +5246,7 @@ class NodeBase extends ExpressionEntity {
5241
5246
  this.scope = parentScope;
5242
5247
  }
5243
5248
  hasEffects(context) {
5244
- if (this.deoptimized === false)
5249
+ if (!this.deoptimized)
5245
5250
  this.applyDeoptimizations();
5246
5251
  for (const key of this.keys) {
5247
5252
  const value = this[key];
@@ -5259,7 +5264,7 @@ class NodeBase extends ExpressionEntity {
5259
5264
  return false;
5260
5265
  }
5261
5266
  include(context, includeChildrenRecursively, _options) {
5262
- if (this.deoptimized === false)
5267
+ if (!this.deoptimized)
5263
5268
  this.applyDeoptimizations();
5264
5269
  this.included = true;
5265
5270
  for (const key of this.keys) {
@@ -5358,10 +5363,6 @@ class NodeBase extends ExpressionEntity {
5358
5363
  }
5359
5364
 
5360
5365
  class SpreadElement extends NodeBase {
5361
- constructor() {
5362
- super(...arguments);
5363
- this.deoptimized = false;
5364
- }
5365
5366
  deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
5366
5367
  if (path.length > 0) {
5367
5368
  this.argument.deoptimizeThisOnEventAtPath(event, [UnknownKey, ...path], thisParameter, recursionTracker);
@@ -6040,7 +6041,6 @@ const ARRAY_PROTOTYPE = new ObjectEntity({
6040
6041
  class ArrayExpression extends NodeBase {
6041
6042
  constructor() {
6042
6043
  super(...arguments);
6043
- this.deoptimized = false;
6044
6044
  this.objectEntity = null;
6045
6045
  }
6046
6046
  deoptimizePath(path) {
@@ -6069,8 +6069,8 @@ class ArrayExpression extends NodeBase {
6069
6069
  let hasSpread = false;
6070
6070
  for (let index = 0; index < this.elements.length; index++) {
6071
6071
  const element = this.elements[index];
6072
- if (hasSpread || element instanceof SpreadElement) {
6073
- if (element) {
6072
+ if (element) {
6073
+ if (hasSpread || element instanceof SpreadElement) {
6074
6074
  hasSpread = true;
6075
6075
  element.deoptimizePath(UNKNOWN_PATH);
6076
6076
  }
@@ -6512,629 +6512,87 @@ class ReturnValueScope extends ParameterScope {
6512
6512
  }
6513
6513
  }
6514
6514
 
6515
- class AssignmentPattern extends NodeBase {
6516
- constructor() {
6517
- super(...arguments);
6518
- this.deoptimized = false;
6519
- }
6520
- addExportedVariables(variables, exportNamesByVariable) {
6521
- this.left.addExportedVariables(variables, exportNamesByVariable);
6522
- }
6523
- declare(kind, init) {
6524
- return this.left.declare(kind, init);
6525
- }
6526
- deoptimizePath(path) {
6527
- path.length === 0 && this.left.deoptimizePath(path);
6528
- }
6529
- hasEffectsWhenAssignedAtPath(path, context) {
6530
- return path.length > 0 || this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
6531
- }
6532
- // Note that FunctionBase may directly include .left and .right without
6533
- // including the pattern itself. This is how default parameter tree-shaking
6534
- // works at the moment.
6535
- include(context, includeChildrenRecursively) {
6536
- this.included = true;
6537
- this.left.include(context, includeChildrenRecursively);
6538
- this.right.include(context, includeChildrenRecursively);
6539
- }
6540
- markDeclarationReached() {
6541
- this.left.markDeclarationReached();
6542
- }
6543
- render(code, options, { isShorthandProperty } = BLANK) {
6544
- this.left.render(code, options, { isShorthandProperty });
6545
- if (this.right.included) {
6546
- this.right.render(code, options);
6547
- }
6548
- else {
6549
- code.remove(this.left.end, this.end);
6550
- }
6551
- }
6552
- applyDeoptimizations() {
6553
- this.deoptimized = true;
6554
- this.left.deoptimizePath(EMPTY_PATH);
6555
- this.right.deoptimizePath(UNKNOWN_PATH);
6556
- this.context.requestTreeshakingPass();
6557
- }
6558
- }
6515
+ //@ts-check
6516
+ /** @typedef { import('estree').Node} Node */
6517
+ /** @typedef {Node | {
6518
+ * type: 'PropertyDefinition';
6519
+ * computed: boolean;
6520
+ * value: Node
6521
+ * }} NodeWithPropertyDefinition */
6559
6522
 
6560
- function treeshakeNode(node, code, start, end) {
6561
- code.remove(start, end);
6562
- if (node.annotations) {
6563
- for (const annotation of node.annotations) {
6564
- if (annotation.start < start) {
6565
- code.remove(annotation.start, annotation.end);
6566
- }
6567
- else {
6568
- return;
6569
- }
6570
- }
6571
- }
6572
- }
6573
- function removeAnnotations(node, code) {
6574
- if (!node.annotations && node.parent.type === ExpressionStatement$1) {
6575
- node = node.parent;
6576
- }
6577
- if (node.annotations) {
6578
- for (const annotation of node.annotations) {
6579
- code.remove(annotation.start, annotation.end);
6580
- }
6581
- }
6582
- }
6523
+ /**
6524
+ *
6525
+ * @param {NodeWithPropertyDefinition} node
6526
+ * @param {NodeWithPropertyDefinition} parent
6527
+ * @returns boolean
6528
+ */
6529
+ function is_reference (node, parent) {
6530
+ if (node.type === 'MemberExpression') {
6531
+ return !node.computed && is_reference(node.object, node);
6532
+ }
6583
6533
 
6584
- const NO_SEMICOLON = { isNoStatement: true };
6585
- // This assumes there are only white-space and comments between start and the string we are looking for
6586
- function findFirstOccurrenceOutsideComment(code, searchString, start = 0) {
6587
- let searchPos, charCodeAfterSlash;
6588
- searchPos = code.indexOf(searchString, start);
6589
- while (true) {
6590
- start = code.indexOf('/', start);
6591
- if (start === -1 || start >= searchPos)
6592
- return searchPos;
6593
- charCodeAfterSlash = code.charCodeAt(++start);
6594
- ++start;
6595
- // With our assumption, '/' always starts a comment. Determine comment type:
6596
- start =
6597
- charCodeAfterSlash === 47 /*"/"*/
6598
- ? code.indexOf('\n', start) + 1
6599
- : code.indexOf('*/', start) + 2;
6600
- if (start > searchPos) {
6601
- searchPos = code.indexOf(searchString, start);
6602
- }
6603
- }
6604
- }
6605
- const NON_WHITESPACE = /\S/g;
6606
- function findNonWhiteSpace(code, index) {
6607
- NON_WHITESPACE.lastIndex = index;
6608
- const result = NON_WHITESPACE.exec(code);
6609
- return result.index;
6534
+ if (node.type === 'Identifier') {
6535
+ if (!parent) return true;
6536
+
6537
+ switch (parent.type) {
6538
+ // disregard `bar` in `foo.bar`
6539
+ case 'MemberExpression': return parent.computed || node === parent.object;
6540
+
6541
+ // disregard the `foo` in `class {foo(){}}` but keep it in `class {[foo](){}}`
6542
+ case 'MethodDefinition': return parent.computed;
6543
+
6544
+ // disregard the `foo` in `class {foo=bar}` but keep it in `class {[foo]=bar}` and `class {bar=foo}`
6545
+ case 'PropertyDefinition': return parent.computed || node === parent.value;
6546
+
6547
+ // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
6548
+ case 'Property': return parent.computed || node === parent.value;
6549
+
6550
+ // disregard the `bar` in `export { foo as bar }` or
6551
+ // the foo in `import { foo as bar }`
6552
+ case 'ExportSpecifier':
6553
+ case 'ImportSpecifier': return node === parent.local;
6554
+
6555
+ // disregard the `foo` in `foo: while (...) { ... break foo; ... continue foo;}`
6556
+ case 'LabeledStatement':
6557
+ case 'BreakStatement':
6558
+ case 'ContinueStatement': return false;
6559
+ default: return true;
6560
+ }
6561
+ }
6562
+
6563
+ return false;
6610
6564
  }
6611
- // This assumes "code" only contains white-space and comments
6612
- // Returns position of line-comment if applicable
6613
- function findFirstLineBreakOutsideComment(code) {
6614
- let lineBreakPos, charCodeAfterSlash, start = 0;
6615
- lineBreakPos = code.indexOf('\n', start);
6616
- while (true) {
6617
- start = code.indexOf('/', start);
6618
- if (start === -1 || start > lineBreakPos)
6619
- return [lineBreakPos, lineBreakPos + 1];
6620
- // With our assumption, '/' always starts a comment. Determine comment type:
6621
- charCodeAfterSlash = code.charCodeAt(start + 1);
6622
- if (charCodeAfterSlash === 47 /*"/"*/)
6623
- return [start, lineBreakPos + 1];
6624
- start = code.indexOf('*/', start + 3) + 2;
6625
- if (start > lineBreakPos) {
6626
- lineBreakPos = code.indexOf('\n', start);
6627
- }
6565
+
6566
+ /* eslint sort-keys: "off" */
6567
+ const ValueProperties = Symbol('Value Properties');
6568
+ const PURE = {
6569
+ hasEffectsWhenCalled() {
6570
+ return false;
6628
6571
  }
6629
- }
6630
- function renderStatementList(statements, code, start, end, options) {
6631
- let currentNode, currentNodeStart, currentNodeNeedsBoundaries, nextNodeStart;
6632
- let nextNode = statements[0];
6633
- let nextNodeNeedsBoundaries = !nextNode.included || nextNode.needsBoundaries;
6634
- if (nextNodeNeedsBoundaries) {
6635
- nextNodeStart =
6636
- start + findFirstLineBreakOutsideComment(code.original.slice(start, nextNode.start))[1];
6572
+ };
6573
+ const IMPURE = {
6574
+ hasEffectsWhenCalled() {
6575
+ return true;
6637
6576
  }
6638
- for (let nextIndex = 1; nextIndex <= statements.length; nextIndex++) {
6639
- currentNode = nextNode;
6640
- currentNodeStart = nextNodeStart;
6641
- currentNodeNeedsBoundaries = nextNodeNeedsBoundaries;
6642
- nextNode = statements[nextIndex];
6643
- nextNodeNeedsBoundaries =
6644
- nextNode === undefined ? false : !nextNode.included || nextNode.needsBoundaries;
6645
- if (currentNodeNeedsBoundaries || nextNodeNeedsBoundaries) {
6646
- nextNodeStart =
6647
- currentNode.end +
6648
- findFirstLineBreakOutsideComment(code.original.slice(currentNode.end, nextNode === undefined ? end : nextNode.start))[1];
6649
- if (currentNode.included) {
6650
- currentNodeNeedsBoundaries
6651
- ? currentNode.render(code, options, {
6652
- end: nextNodeStart,
6653
- start: currentNodeStart
6654
- })
6655
- : currentNode.render(code, options);
6656
- }
6657
- else {
6658
- treeshakeNode(currentNode, code, currentNodeStart, nextNodeStart);
6659
- }
6660
- }
6661
- else {
6662
- currentNode.render(code, options);
6663
- }
6664
- }
6665
- }
6666
- // This assumes that the first character is not part of the first node
6667
- function getCommaSeparatedNodesWithBoundaries(nodes, code, start, end) {
6668
- const splitUpNodes = [];
6669
- let node, nextNode, nextNodeStart, contentEnd, char;
6670
- let separator = start - 1;
6671
- for (let nextIndex = 0; nextIndex < nodes.length; nextIndex++) {
6672
- nextNode = nodes[nextIndex];
6673
- if (node !== undefined) {
6674
- separator =
6675
- node.end +
6676
- findFirstOccurrenceOutsideComment(code.original.slice(node.end, nextNode.start), ',');
6677
- }
6678
- nextNodeStart = contentEnd =
6679
- separator +
6680
- 1 +
6681
- findFirstLineBreakOutsideComment(code.original.slice(separator + 1, nextNode.start))[1];
6682
- while (((char = code.original.charCodeAt(nextNodeStart)),
6683
- char === 32 /*" "*/ || char === 9 /*"\t"*/ || char === 10 /*"\n"*/ || char === 13) /*"\r"*/)
6684
- nextNodeStart++;
6685
- if (node !== undefined) {
6686
- splitUpNodes.push({
6687
- contentEnd,
6688
- end: nextNodeStart,
6689
- node,
6690
- separator,
6691
- start
6692
- });
6693
- }
6694
- node = nextNode;
6695
- start = nextNodeStart;
6696
- }
6697
- splitUpNodes.push({
6698
- contentEnd: end,
6699
- end,
6700
- node: node,
6701
- separator: null,
6702
- start
6703
- });
6704
- return splitUpNodes;
6705
- }
6706
- // This assumes there are only white-space and comments between start and end
6707
- function removeLineBreaks(code, start, end) {
6708
- while (true) {
6709
- const [removeStart, removeEnd] = findFirstLineBreakOutsideComment(code.original.slice(start, end));
6710
- if (removeStart === -1) {
6711
- break;
6712
- }
6713
- code.remove(start + removeStart, (start += removeEnd));
6714
- }
6715
- }
6716
-
6717
- class BlockScope extends ChildScope {
6718
- addDeclaration(identifier, context, init, isHoisted) {
6719
- if (isHoisted) {
6720
- const variable = this.parent.addDeclaration(identifier, context, init, isHoisted);
6721
- // Necessary to make sure the init is deoptimized for conditional declarations.
6722
- // We cannot call deoptimizePath here.
6723
- variable.markInitializersForDeoptimization();
6724
- return variable;
6725
- }
6726
- else {
6727
- return super.addDeclaration(identifier, context, init, false);
6728
- }
6729
- }
6730
- }
6731
-
6732
- class ExpressionStatement extends NodeBase {
6733
- initialise() {
6734
- if (this.directive &&
6735
- this.directive !== 'use strict' &&
6736
- this.parent.type === Program$1) {
6737
- this.context.warn(
6738
- // This is necessary, because either way (deleting or not) can lead to errors.
6739
- {
6740
- code: 'MODULE_LEVEL_DIRECTIVE',
6741
- message: `Module level directives cause errors when bundled, '${this.directive}' was ignored.`
6742
- }, this.start);
6743
- }
6744
- }
6745
- render(code, options) {
6746
- super.render(code, options);
6747
- if (this.included)
6748
- this.insertSemicolon(code);
6749
- }
6750
- shouldBeIncluded(context) {
6751
- if (this.directive && this.directive !== 'use strict')
6752
- return this.parent.type !== Program$1;
6753
- return super.shouldBeIncluded(context);
6754
- }
6755
- }
6756
-
6757
- class BlockStatement extends NodeBase {
6758
- constructor() {
6759
- super(...arguments);
6760
- this.directlyIncluded = false;
6761
- }
6762
- addImplicitReturnExpressionToScope() {
6763
- const lastStatement = this.body[this.body.length - 1];
6764
- if (!lastStatement || lastStatement.type !== ReturnStatement$1) {
6765
- this.scope.addReturnExpression(UNKNOWN_EXPRESSION);
6766
- }
6767
- }
6768
- createScope(parentScope) {
6769
- this.scope = this.parent.preventChildBlockScope
6770
- ? parentScope
6771
- : new BlockScope(parentScope);
6772
- }
6773
- hasEffects(context) {
6774
- if (this.deoptimizeBody)
6775
- return true;
6776
- for (const node of this.body) {
6777
- if (context.brokenFlow)
6778
- break;
6779
- if (node.hasEffects(context))
6780
- return true;
6781
- }
6782
- return false;
6783
- }
6784
- include(context, includeChildrenRecursively) {
6785
- if (!(this.deoptimizeBody && this.directlyIncluded)) {
6786
- this.included = true;
6787
- this.directlyIncluded = true;
6788
- if (this.deoptimizeBody)
6789
- includeChildrenRecursively = true;
6790
- for (const node of this.body) {
6791
- if (includeChildrenRecursively || node.shouldBeIncluded(context))
6792
- node.include(context, includeChildrenRecursively);
6793
- }
6794
- }
6795
- }
6796
- initialise() {
6797
- const firstBodyStatement = this.body[0];
6798
- this.deoptimizeBody =
6799
- firstBodyStatement instanceof ExpressionStatement &&
6800
- firstBodyStatement.directive === 'use asm';
6801
- }
6802
- render(code, options) {
6803
- if (this.body.length) {
6804
- renderStatementList(this.body, code, this.start + 1, this.end - 1, options);
6805
- }
6806
- else {
6807
- super.render(code, options);
6808
- }
6809
- }
6810
- }
6811
-
6812
- class RestElement extends NodeBase {
6813
- constructor() {
6814
- super(...arguments);
6815
- this.deoptimized = false;
6816
- this.declarationInit = null;
6817
- }
6818
- addExportedVariables(variables, exportNamesByVariable) {
6819
- this.argument.addExportedVariables(variables, exportNamesByVariable);
6820
- }
6821
- declare(kind, init) {
6822
- this.declarationInit = init;
6823
- return this.argument.declare(kind, UNKNOWN_EXPRESSION);
6824
- }
6825
- deoptimizePath(path) {
6826
- path.length === 0 && this.argument.deoptimizePath(EMPTY_PATH);
6827
- }
6828
- hasEffectsWhenAssignedAtPath(path, context) {
6829
- return path.length > 0 || this.argument.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
6830
- }
6831
- markDeclarationReached() {
6832
- this.argument.markDeclarationReached();
6833
- }
6834
- applyDeoptimizations() {
6835
- this.deoptimized = true;
6836
- if (this.declarationInit !== null) {
6837
- this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
6838
- this.context.requestTreeshakingPass();
6839
- }
6840
- }
6841
- }
6842
-
6843
- class FunctionBase extends NodeBase {
6844
- constructor() {
6845
- super(...arguments);
6846
- this.objectEntity = null;
6847
- this.deoptimizedReturn = false;
6848
- this.forceIncludeParameters = false;
6849
- }
6850
- deoptimizeCache() {
6851
- this.forceIncludeParameters = true;
6852
- }
6853
- deoptimizePath(path) {
6854
- this.getObjectEntity().deoptimizePath(path);
6855
- if (path.length === 1 && path[0] === UnknownKey) {
6856
- // A reassignment of UNKNOWN_PATH is considered equivalent to having lost track
6857
- // which means the return expression needs to be reassigned
6858
- this.forceIncludeParameters = true;
6859
- this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH);
6860
- }
6861
- }
6862
- deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
6863
- if (path.length > 0) {
6864
- this.getObjectEntity().deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
6865
- }
6866
- }
6867
- getLiteralValueAtPath(path, recursionTracker, origin) {
6868
- return this.getObjectEntity().getLiteralValueAtPath(path, recursionTracker, origin);
6869
- }
6870
- getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin) {
6871
- if (path.length > 0) {
6872
- return this.getObjectEntity().getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin);
6873
- }
6874
- if (this.async) {
6875
- if (!this.deoptimizedReturn) {
6876
- this.deoptimizedReturn = true;
6877
- this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH);
6878
- this.context.requestTreeshakingPass();
6879
- }
6880
- return UNKNOWN_EXPRESSION;
6881
- }
6882
- return this.scope.getReturnExpression();
6883
- }
6884
- hasEffectsWhenAccessedAtPath(path, context) {
6885
- return this.getObjectEntity().hasEffectsWhenAccessedAtPath(path, context);
6886
- }
6887
- hasEffectsWhenAssignedAtPath(path, context) {
6888
- return this.getObjectEntity().hasEffectsWhenAssignedAtPath(path, context);
6889
- }
6890
- hasEffectsWhenCalledAtPath(path, callOptions, context) {
6891
- var _a;
6892
- if (path.length > 0) {
6893
- return this.getObjectEntity().hasEffectsWhenCalledAtPath(path, callOptions, context);
6894
- }
6895
- if (this.async) {
6896
- const { propertyReadSideEffects } = this.context.options
6897
- .treeshake;
6898
- const returnExpression = this.scope.getReturnExpression();
6899
- if (returnExpression.hasEffectsWhenCalledAtPath(['then'], { args: NO_ARGS, thisParam: null, withNew: false }, context) ||
6900
- (propertyReadSideEffects &&
6901
- (propertyReadSideEffects === 'always' ||
6902
- returnExpression.hasEffectsWhenAccessedAtPath(['then'], context)))) {
6903
- return true;
6904
- }
6905
- }
6906
- for (let position = 0; position < this.params.length; position++) {
6907
- const parameter = this.params[position];
6908
- if (parameter instanceof AssignmentPattern) {
6909
- if (parameter.left.hasEffects(context)) {
6910
- return true;
6911
- }
6912
- const argumentValue = (_a = callOptions.args[position]) === null || _a === void 0 ? void 0 : _a.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this);
6913
- if ((argumentValue === undefined || argumentValue === UnknownValue) &&
6914
- parameter.right.hasEffects(context)) {
6915
- return true;
6916
- }
6917
- }
6918
- else if (parameter.hasEffects(context)) {
6919
- return true;
6920
- }
6921
- }
6922
- return false;
6923
- }
6924
- include(context, includeChildrenRecursively, { includeWithoutParameterDefaults } = BLANK) {
6925
- this.included = true;
6926
- const { brokenFlow } = context;
6927
- context.brokenFlow = BROKEN_FLOW_NONE;
6928
- this.body.include(context, includeChildrenRecursively);
6929
- context.brokenFlow = brokenFlow;
6930
- if (!includeWithoutParameterDefaults ||
6931
- includeChildrenRecursively ||
6932
- this.forceIncludeParameters) {
6933
- for (const param of this.params) {
6934
- param.include(context, includeChildrenRecursively);
6935
- }
6936
- }
6937
- }
6938
- includeCallArguments(context, args) {
6939
- var _a;
6940
- for (let position = 0; position < this.params.length; position++) {
6941
- const parameter = this.params[position];
6942
- if (parameter instanceof AssignmentPattern) {
6943
- if (parameter.left.shouldBeIncluded(context)) {
6944
- parameter.left.include(context, false);
6945
- }
6946
- const argumentValue = (_a = args[position]) === null || _a === void 0 ? void 0 : _a.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this);
6947
- // If argumentValue === UnknownTruthyValue, then we do not need to
6948
- // include the default
6949
- if ((argumentValue === undefined || argumentValue === UnknownValue) &&
6950
- (this.parameterVariables[position].some(variable => variable.included) ||
6951
- parameter.right.shouldBeIncluded(context))) {
6952
- parameter.right.include(context, false);
6953
- }
6954
- }
6955
- else if (parameter.shouldBeIncluded(context)) {
6956
- parameter.include(context, false);
6957
- }
6958
- }
6959
- this.scope.includeCallArguments(context, args);
6960
- }
6961
- initialise() {
6962
- this.parameterVariables = this.params.map(param => param.declare('parameter', UNKNOWN_EXPRESSION));
6963
- this.scope.addParameterVariables(this.parameterVariables, this.params[this.params.length - 1] instanceof RestElement);
6964
- if (this.body instanceof BlockStatement) {
6965
- this.body.addImplicitReturnExpressionToScope();
6966
- }
6967
- else {
6968
- this.scope.addReturnExpression(this.body);
6969
- }
6970
- }
6971
- parseNode(esTreeNode) {
6972
- if (esTreeNode.body.type === BlockStatement$1) {
6973
- this.body = new BlockStatement(esTreeNode.body, this, this.scope.hoistedBodyVarScope);
6974
- }
6975
- super.parseNode(esTreeNode);
6976
- }
6977
- }
6978
- FunctionBase.prototype.preventChildBlockScope = true;
6979
-
6980
- class ArrowFunctionExpression extends FunctionBase {
6981
- constructor() {
6982
- super(...arguments);
6983
- this.objectEntity = null;
6984
- }
6985
- createScope(parentScope) {
6986
- this.scope = new ReturnValueScope(parentScope, this.context);
6987
- }
6988
- hasEffects() {
6989
- return false;
6990
- }
6991
- hasEffectsWhenCalledAtPath(path, callOptions, context) {
6992
- if (super.hasEffectsWhenCalledAtPath(path, callOptions, context))
6993
- return true;
6994
- const { ignore, brokenFlow } = context;
6995
- context.ignore = {
6996
- breaks: false,
6997
- continues: false,
6998
- labels: new Set(),
6999
- returnYield: true
7000
- };
7001
- if (this.body.hasEffects(context))
7002
- return true;
7003
- context.ignore = ignore;
7004
- context.brokenFlow = brokenFlow;
7005
- return false;
7006
- }
7007
- getObjectEntity() {
7008
- if (this.objectEntity !== null) {
7009
- return this.objectEntity;
7010
- }
7011
- return (this.objectEntity = new ObjectEntity([], OBJECT_PROTOTYPE));
7012
- }
7013
- }
7014
-
7015
- function getSystemExportStatement(exportedVariables, { exportNamesByVariable, snippets: { _, getObject, getPropertyAccess } }, modifier = '') {
7016
- if (exportedVariables.length === 1 &&
7017
- exportNamesByVariable.get(exportedVariables[0]).length === 1) {
7018
- const variable = exportedVariables[0];
7019
- return `exports('${exportNamesByVariable.get(variable)}',${_}${variable.getName(getPropertyAccess)}${modifier})`;
7020
- }
7021
- else {
7022
- const fields = [];
7023
- for (const variable of exportedVariables) {
7024
- for (const exportName of exportNamesByVariable.get(variable)) {
7025
- fields.push([exportName, variable.getName(getPropertyAccess) + modifier]);
7026
- }
7027
- }
7028
- return `exports(${getObject(fields, { lineBreakIndent: null })})`;
7029
- }
7030
- }
7031
- function renderSystemExportExpression(exportedVariable, expressionStart, expressionEnd, code, { exportNamesByVariable, snippets: { _ } }) {
7032
- code.prependRight(expressionStart, `exports('${exportNamesByVariable.get(exportedVariable)}',${_}`);
7033
- code.appendLeft(expressionEnd, ')');
7034
- }
7035
- function renderSystemExportFunction(exportedVariables, expressionStart, expressionEnd, needsParens, code, options) {
7036
- const { _, getDirectReturnIifeLeft } = options.snippets;
7037
- code.prependRight(expressionStart, getDirectReturnIifeLeft(['v'], `${getSystemExportStatement(exportedVariables, options)},${_}v`, { needsArrowReturnParens: true, needsWrappedFunction: needsParens }));
7038
- code.appendLeft(expressionEnd, ')');
7039
- }
7040
- function renderSystemExportSequenceAfterExpression(exportedVariable, expressionStart, expressionEnd, needsParens, code, options) {
7041
- const { _, getPropertyAccess } = options.snippets;
7042
- code.appendLeft(expressionEnd, `,${_}${getSystemExportStatement([exportedVariable], options)},${_}${exportedVariable.getName(getPropertyAccess)}`);
7043
- if (needsParens) {
7044
- code.prependRight(expressionStart, '(');
7045
- code.appendLeft(expressionEnd, ')');
7046
- }
7047
- }
7048
- function renderSystemExportSequenceBeforeExpression(exportedVariable, expressionStart, expressionEnd, needsParens, code, options, modifier) {
7049
- const { _ } = options.snippets;
7050
- code.prependRight(expressionStart, `${getSystemExportStatement([exportedVariable], options, modifier)},${_}`);
7051
- if (needsParens) {
7052
- code.prependRight(expressionStart, '(');
7053
- code.appendLeft(expressionEnd, ')');
7054
- }
7055
- }
7056
-
7057
- //@ts-check
7058
- /** @typedef { import('estree').Node} Node */
7059
- /** @typedef {Node | {
7060
- * type: 'PropertyDefinition';
7061
- * computed: boolean;
7062
- * value: Node
7063
- * }} NodeWithPropertyDefinition */
7064
-
7065
- /**
7066
- *
7067
- * @param {NodeWithPropertyDefinition} node
7068
- * @param {NodeWithPropertyDefinition} parent
7069
- * @returns boolean
7070
- */
7071
- function is_reference (node, parent) {
7072
- if (node.type === 'MemberExpression') {
7073
- return !node.computed && is_reference(node.object, node);
7074
- }
7075
-
7076
- if (node.type === 'Identifier') {
7077
- if (!parent) return true;
7078
-
7079
- switch (parent.type) {
7080
- // disregard `bar` in `foo.bar`
7081
- case 'MemberExpression': return parent.computed || node === parent.object;
7082
-
7083
- // disregard the `foo` in `class {foo(){}}` but keep it in `class {[foo](){}}`
7084
- case 'MethodDefinition': return parent.computed;
7085
-
7086
- // disregard the `foo` in `class {foo=bar}` but keep it in `class {[foo]=bar}` and `class {bar=foo}`
7087
- case 'PropertyDefinition': return parent.computed || node === parent.value;
7088
-
7089
- // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
7090
- case 'Property': return parent.computed || node === parent.value;
7091
-
7092
- // disregard the `bar` in `export { foo as bar }` or
7093
- // the foo in `import { foo as bar }`
7094
- case 'ExportSpecifier':
7095
- case 'ImportSpecifier': return node === parent.local;
7096
-
7097
- // disregard the `foo` in `foo: while (...) { ... break foo; ... continue foo;}`
7098
- case 'LabeledStatement':
7099
- case 'BreakStatement':
7100
- case 'ContinueStatement': return false;
7101
- default: return true;
7102
- }
7103
- }
7104
-
7105
- return false;
7106
- }
7107
-
7108
- /* eslint sort-keys: "off" */
7109
- const ValueProperties = Symbol('Value Properties');
7110
- const PURE = {
7111
- hasEffectsWhenCalled() {
7112
- return false;
7113
- }
7114
- };
7115
- const IMPURE = {
7116
- hasEffectsWhenCalled() {
7117
- return true;
7118
- }
7119
- };
7120
- // We use shortened variables to reduce file size here
7121
- /* OBJECT */
7122
- const O = {
7123
- __proto__: null,
7124
- [ValueProperties]: IMPURE
7125
- };
7126
- /* PURE FUNCTION */
7127
- const PF = {
7128
- __proto__: null,
7129
- [ValueProperties]: PURE
7130
- };
7131
- /* FUNCTION THAT MUTATES FIRST ARG WITHOUT TRIGGERING ACCESSORS */
7132
- const MUTATES_ARG_WITHOUT_ACCESSOR = {
7133
- __proto__: null,
7134
- [ValueProperties]: {
7135
- hasEffectsWhenCalled(callOptions, context) {
7136
- return (!callOptions.args.length ||
7137
- callOptions.args[0].hasEffectsWhenAssignedAtPath(UNKNOWN_NON_ACCESSOR_PATH, context));
6577
+ };
6578
+ // We use shortened variables to reduce file size here
6579
+ /* OBJECT */
6580
+ const O = {
6581
+ __proto__: null,
6582
+ [ValueProperties]: IMPURE
6583
+ };
6584
+ /* PURE FUNCTION */
6585
+ const PF = {
6586
+ __proto__: null,
6587
+ [ValueProperties]: PURE
6588
+ };
6589
+ /* FUNCTION THAT MUTATES FIRST ARG WITHOUT TRIGGERING ACCESSORS */
6590
+ const MUTATES_ARG_WITHOUT_ACCESSOR = {
6591
+ __proto__: null,
6592
+ [ValueProperties]: {
6593
+ hasEffectsWhenCalled(callOptions, context) {
6594
+ return (!callOptions.args.length ||
6595
+ callOptions.args[0].hasEffectsWhenAssignedAtPath(UNKNOWN_NON_ACCESSOR_PATH, context));
7138
6596
  }
7139
6597
  }
7140
6598
  };
@@ -7951,222 +7409,688 @@ const knownGlobals = {
7951
7409
  for (const global of ['window', 'global', 'self', 'globalThis']) {
7952
7410
  knownGlobals[global] = knownGlobals;
7953
7411
  }
7954
- function getGlobalAtPath(path) {
7955
- let currentGlobal = knownGlobals;
7956
- for (const pathSegment of path) {
7957
- if (typeof pathSegment !== 'string') {
7958
- return null;
7412
+ function getGlobalAtPath(path) {
7413
+ let currentGlobal = knownGlobals;
7414
+ for (const pathSegment of path) {
7415
+ if (typeof pathSegment !== 'string') {
7416
+ return null;
7417
+ }
7418
+ currentGlobal = currentGlobal[pathSegment];
7419
+ if (!currentGlobal) {
7420
+ return null;
7421
+ }
7422
+ }
7423
+ return currentGlobal[ValueProperties];
7424
+ }
7425
+
7426
+ class GlobalVariable extends Variable {
7427
+ constructor() {
7428
+ super(...arguments);
7429
+ // Ensure we use live-bindings for globals as we do not know if they have
7430
+ // been reassigned
7431
+ this.isReassigned = true;
7432
+ }
7433
+ getLiteralValueAtPath(path, _recursionTracker, _origin) {
7434
+ return getGlobalAtPath([this.name, ...path]) ? UnknownTruthyValue : UnknownValue;
7435
+ }
7436
+ hasEffectsWhenAccessedAtPath(path) {
7437
+ if (path.length === 0) {
7438
+ // Technically, "undefined" is a global variable of sorts
7439
+ return this.name !== 'undefined' && !getGlobalAtPath([this.name]);
7440
+ }
7441
+ return !getGlobalAtPath([this.name, ...path].slice(0, -1));
7442
+ }
7443
+ hasEffectsWhenCalledAtPath(path, callOptions, context) {
7444
+ const globalAtPath = getGlobalAtPath([this.name, ...path]);
7445
+ return !globalAtPath || globalAtPath.hasEffectsWhenCalled(callOptions, context);
7446
+ }
7447
+ }
7448
+
7449
+ const tdzVariableKinds = {
7450
+ __proto__: null,
7451
+ class: true,
7452
+ const: true,
7453
+ let: true,
7454
+ var: true
7455
+ };
7456
+ class Identifier extends NodeBase {
7457
+ constructor() {
7458
+ super(...arguments);
7459
+ this.variable = null;
7460
+ this.isTDZAccess = null;
7461
+ }
7462
+ addExportedVariables(variables, exportNamesByVariable) {
7463
+ if (exportNamesByVariable.has(this.variable)) {
7464
+ variables.push(this.variable);
7465
+ }
7466
+ }
7467
+ bind() {
7468
+ if (!this.variable && is_reference(this, this.parent)) {
7469
+ this.variable = this.scope.findVariable(this.name);
7470
+ this.variable.addReference(this);
7471
+ }
7472
+ }
7473
+ declare(kind, init) {
7474
+ let variable;
7475
+ const { treeshake } = this.context.options;
7476
+ switch (kind) {
7477
+ case 'var':
7478
+ variable = this.scope.addDeclaration(this, this.context, init, true);
7479
+ if (treeshake && treeshake.correctVarValueBeforeDeclaration) {
7480
+ // Necessary to make sure the init is deoptimized. We cannot call deoptimizePath here.
7481
+ variable.markInitializersForDeoptimization();
7482
+ }
7483
+ break;
7484
+ case 'function':
7485
+ // in strict mode, functions are only hoisted within a scope but not across block scopes
7486
+ variable = this.scope.addDeclaration(this, this.context, init, false);
7487
+ break;
7488
+ case 'let':
7489
+ case 'const':
7490
+ case 'class':
7491
+ variable = this.scope.addDeclaration(this, this.context, init, false);
7492
+ break;
7493
+ case 'parameter':
7494
+ variable = this.scope.addParameterDeclaration(this);
7495
+ break;
7496
+ /* istanbul ignore next */
7497
+ default:
7498
+ /* istanbul ignore next */
7499
+ throw new Error(`Internal Error: Unexpected identifier kind ${kind}.`);
7500
+ }
7501
+ variable.kind = kind;
7502
+ return [(this.variable = variable)];
7503
+ }
7504
+ deoptimizePath(path) {
7505
+ var _a;
7506
+ if (path.length === 0 && !this.scope.contains(this.name)) {
7507
+ this.disallowImportReassignment();
7508
+ }
7509
+ // We keep conditional chaining because an unknown Node could have an
7510
+ // Identifier as property that might be deoptimized by default
7511
+ (_a = this.variable) === null || _a === void 0 ? void 0 : _a.deoptimizePath(path);
7512
+ }
7513
+ deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
7514
+ this.variable.deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
7515
+ }
7516
+ getLiteralValueAtPath(path, recursionTracker, origin) {
7517
+ return this.getVariableRespectingTDZ().getLiteralValueAtPath(path, recursionTracker, origin);
7518
+ }
7519
+ getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin) {
7520
+ return this.getVariableRespectingTDZ().getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin);
7521
+ }
7522
+ hasEffects() {
7523
+ if (!this.deoptimized)
7524
+ this.applyDeoptimizations();
7525
+ if (this.isPossibleTDZ() && this.variable.kind !== 'var') {
7526
+ return true;
7527
+ }
7528
+ return (this.context.options.treeshake.unknownGlobalSideEffects &&
7529
+ this.variable instanceof GlobalVariable &&
7530
+ this.variable.hasEffectsWhenAccessedAtPath(EMPTY_PATH));
7531
+ }
7532
+ hasEffectsWhenAccessedAtPath(path, context) {
7533
+ return (this.variable !== null &&
7534
+ this.getVariableRespectingTDZ().hasEffectsWhenAccessedAtPath(path, context));
7535
+ }
7536
+ hasEffectsWhenAssignedAtPath(path, context) {
7537
+ return (path.length > 0 ? this.getVariableRespectingTDZ() : this.variable).hasEffectsWhenAssignedAtPath(path, context);
7538
+ }
7539
+ hasEffectsWhenCalledAtPath(path, callOptions, context) {
7540
+ return this.getVariableRespectingTDZ().hasEffectsWhenCalledAtPath(path, callOptions, context);
7541
+ }
7542
+ include() {
7543
+ if (!this.deoptimized)
7544
+ this.applyDeoptimizations();
7545
+ if (!this.included) {
7546
+ this.included = true;
7547
+ if (this.variable !== null) {
7548
+ this.context.includeVariableInModule(this.variable);
7549
+ }
7550
+ }
7551
+ }
7552
+ includeCallArguments(context, args) {
7553
+ this.variable.includeCallArguments(context, args);
7554
+ }
7555
+ isPossibleTDZ() {
7556
+ // return cached value to avoid issues with the next tree-shaking pass
7557
+ if (this.isTDZAccess !== null)
7558
+ return this.isTDZAccess;
7559
+ if (!(this.variable instanceof LocalVariable) ||
7560
+ !this.variable.kind ||
7561
+ !(this.variable.kind in tdzVariableKinds)) {
7562
+ return (this.isTDZAccess = false);
7563
+ }
7564
+ let decl_id;
7565
+ if (this.variable.declarations &&
7566
+ this.variable.declarations.length === 1 &&
7567
+ (decl_id = this.variable.declarations[0]) &&
7568
+ this.start < decl_id.start &&
7569
+ closestParentFunctionOrProgram(this) === closestParentFunctionOrProgram(decl_id)) {
7570
+ // a variable accessed before its declaration
7571
+ // in the same function or at top level of module
7572
+ return (this.isTDZAccess = true);
7573
+ }
7574
+ if (!this.variable.initReached) {
7575
+ // Either a const/let TDZ violation or
7576
+ // var use before declaration was encountered.
7577
+ return (this.isTDZAccess = true);
7578
+ }
7579
+ return (this.isTDZAccess = false);
7580
+ }
7581
+ markDeclarationReached() {
7582
+ this.variable.initReached = true;
7583
+ }
7584
+ render(code, { snippets: { getPropertyAccess } }, { renderedParentType, isCalleeOfRenderedParent, isShorthandProperty } = BLANK) {
7585
+ if (this.variable) {
7586
+ const name = this.variable.getName(getPropertyAccess);
7587
+ if (name !== this.name) {
7588
+ code.overwrite(this.start, this.end, name, {
7589
+ contentOnly: true,
7590
+ storeName: true
7591
+ });
7592
+ if (isShorthandProperty) {
7593
+ code.prependRight(this.start, `${this.name}: `);
7594
+ }
7595
+ }
7596
+ // In strict mode, any variable named "eval" must be the actual "eval" function
7597
+ if (name === 'eval' &&
7598
+ renderedParentType === CallExpression$1 &&
7599
+ isCalleeOfRenderedParent) {
7600
+ code.appendRight(this.start, '0, ');
7601
+ }
7602
+ }
7603
+ }
7604
+ applyDeoptimizations() {
7605
+ this.deoptimized = true;
7606
+ if (this.variable instanceof LocalVariable) {
7607
+ this.variable.consolidateInitializers();
7608
+ this.context.requestTreeshakingPass();
7609
+ }
7610
+ }
7611
+ disallowImportReassignment() {
7612
+ return this.context.error({
7613
+ code: 'ILLEGAL_REASSIGNMENT',
7614
+ message: `Illegal reassignment to import '${this.name}'`
7615
+ }, this.start);
7616
+ }
7617
+ getVariableRespectingTDZ() {
7618
+ if (this.isPossibleTDZ()) {
7619
+ return UNKNOWN_EXPRESSION;
7620
+ }
7621
+ return this.variable;
7622
+ }
7623
+ }
7624
+ function closestParentFunctionOrProgram(node) {
7625
+ while (node && !/^Program|Function/.test(node.type)) {
7626
+ node = node.parent;
7627
+ }
7628
+ // one of: ArrowFunctionExpression, FunctionDeclaration, FunctionExpression or Program
7629
+ return node;
7630
+ }
7631
+
7632
+ function treeshakeNode(node, code, start, end) {
7633
+ code.remove(start, end);
7634
+ if (node.annotations) {
7635
+ for (const annotation of node.annotations) {
7636
+ if (annotation.start < start) {
7637
+ code.remove(annotation.start, annotation.end);
7638
+ }
7639
+ else {
7640
+ return;
7641
+ }
7642
+ }
7643
+ }
7644
+ }
7645
+ function removeAnnotations(node, code) {
7646
+ if (!node.annotations && node.parent.type === ExpressionStatement$1) {
7647
+ node = node.parent;
7648
+ }
7649
+ if (node.annotations) {
7650
+ for (const annotation of node.annotations) {
7651
+ code.remove(annotation.start, annotation.end);
7652
+ }
7653
+ }
7654
+ }
7655
+
7656
+ const NO_SEMICOLON = { isNoStatement: true };
7657
+ // This assumes there are only white-space and comments between start and the string we are looking for
7658
+ function findFirstOccurrenceOutsideComment(code, searchString, start = 0) {
7659
+ let searchPos, charCodeAfterSlash;
7660
+ searchPos = code.indexOf(searchString, start);
7661
+ while (true) {
7662
+ start = code.indexOf('/', start);
7663
+ if (start === -1 || start >= searchPos)
7664
+ return searchPos;
7665
+ charCodeAfterSlash = code.charCodeAt(++start);
7666
+ ++start;
7667
+ // With our assumption, '/' always starts a comment. Determine comment type:
7668
+ start =
7669
+ charCodeAfterSlash === 47 /*"/"*/
7670
+ ? code.indexOf('\n', start) + 1
7671
+ : code.indexOf('*/', start) + 2;
7672
+ if (start > searchPos) {
7673
+ searchPos = code.indexOf(searchString, start);
7674
+ }
7675
+ }
7676
+ }
7677
+ const NON_WHITESPACE = /\S/g;
7678
+ function findNonWhiteSpace(code, index) {
7679
+ NON_WHITESPACE.lastIndex = index;
7680
+ const result = NON_WHITESPACE.exec(code);
7681
+ return result.index;
7682
+ }
7683
+ // This assumes "code" only contains white-space and comments
7684
+ // Returns position of line-comment if applicable
7685
+ function findFirstLineBreakOutsideComment(code) {
7686
+ let lineBreakPos, charCodeAfterSlash, start = 0;
7687
+ lineBreakPos = code.indexOf('\n', start);
7688
+ while (true) {
7689
+ start = code.indexOf('/', start);
7690
+ if (start === -1 || start > lineBreakPos)
7691
+ return [lineBreakPos, lineBreakPos + 1];
7692
+ // With our assumption, '/' always starts a comment. Determine comment type:
7693
+ charCodeAfterSlash = code.charCodeAt(start + 1);
7694
+ if (charCodeAfterSlash === 47 /*"/"*/)
7695
+ return [start, lineBreakPos + 1];
7696
+ start = code.indexOf('*/', start + 3) + 2;
7697
+ if (start > lineBreakPos) {
7698
+ lineBreakPos = code.indexOf('\n', start);
7699
+ }
7700
+ }
7701
+ }
7702
+ function renderStatementList(statements, code, start, end, options) {
7703
+ let currentNode, currentNodeStart, currentNodeNeedsBoundaries, nextNodeStart;
7704
+ let nextNode = statements[0];
7705
+ let nextNodeNeedsBoundaries = !nextNode.included || nextNode.needsBoundaries;
7706
+ if (nextNodeNeedsBoundaries) {
7707
+ nextNodeStart =
7708
+ start + findFirstLineBreakOutsideComment(code.original.slice(start, nextNode.start))[1];
7709
+ }
7710
+ for (let nextIndex = 1; nextIndex <= statements.length; nextIndex++) {
7711
+ currentNode = nextNode;
7712
+ currentNodeStart = nextNodeStart;
7713
+ currentNodeNeedsBoundaries = nextNodeNeedsBoundaries;
7714
+ nextNode = statements[nextIndex];
7715
+ nextNodeNeedsBoundaries =
7716
+ nextNode === undefined ? false : !nextNode.included || nextNode.needsBoundaries;
7717
+ if (currentNodeNeedsBoundaries || nextNodeNeedsBoundaries) {
7718
+ nextNodeStart =
7719
+ currentNode.end +
7720
+ findFirstLineBreakOutsideComment(code.original.slice(currentNode.end, nextNode === undefined ? end : nextNode.start))[1];
7721
+ if (currentNode.included) {
7722
+ currentNodeNeedsBoundaries
7723
+ ? currentNode.render(code, options, {
7724
+ end: nextNodeStart,
7725
+ start: currentNodeStart
7726
+ })
7727
+ : currentNode.render(code, options);
7728
+ }
7729
+ else {
7730
+ treeshakeNode(currentNode, code, currentNodeStart, nextNodeStart);
7731
+ }
7732
+ }
7733
+ else {
7734
+ currentNode.render(code, options);
7735
+ }
7736
+ }
7737
+ }
7738
+ // This assumes that the first character is not part of the first node
7739
+ function getCommaSeparatedNodesWithBoundaries(nodes, code, start, end) {
7740
+ const splitUpNodes = [];
7741
+ let node, nextNode, nextNodeStart, contentEnd, char;
7742
+ let separator = start - 1;
7743
+ for (let nextIndex = 0; nextIndex < nodes.length; nextIndex++) {
7744
+ nextNode = nodes[nextIndex];
7745
+ if (node !== undefined) {
7746
+ separator =
7747
+ node.end +
7748
+ findFirstOccurrenceOutsideComment(code.original.slice(node.end, nextNode.start), ',');
7749
+ }
7750
+ nextNodeStart = contentEnd =
7751
+ separator +
7752
+ 1 +
7753
+ findFirstLineBreakOutsideComment(code.original.slice(separator + 1, nextNode.start))[1];
7754
+ while (((char = code.original.charCodeAt(nextNodeStart)),
7755
+ char === 32 /*" "*/ || char === 9 /*"\t"*/ || char === 10 /*"\n"*/ || char === 13) /*"\r"*/)
7756
+ nextNodeStart++;
7757
+ if (node !== undefined) {
7758
+ splitUpNodes.push({
7759
+ contentEnd,
7760
+ end: nextNodeStart,
7761
+ node,
7762
+ separator,
7763
+ start
7764
+ });
7765
+ }
7766
+ node = nextNode;
7767
+ start = nextNodeStart;
7768
+ }
7769
+ splitUpNodes.push({
7770
+ contentEnd: end,
7771
+ end,
7772
+ node: node,
7773
+ separator: null,
7774
+ start
7775
+ });
7776
+ return splitUpNodes;
7777
+ }
7778
+ // This assumes there are only white-space and comments between start and end
7779
+ function removeLineBreaks(code, start, end) {
7780
+ while (true) {
7781
+ const [removeStart, removeEnd] = findFirstLineBreakOutsideComment(code.original.slice(start, end));
7782
+ if (removeStart === -1) {
7783
+ break;
7784
+ }
7785
+ code.remove(start + removeStart, (start += removeEnd));
7786
+ }
7787
+ }
7788
+
7789
+ class BlockScope extends ChildScope {
7790
+ addDeclaration(identifier, context, init, isHoisted) {
7791
+ if (isHoisted) {
7792
+ const variable = this.parent.addDeclaration(identifier, context, init, isHoisted);
7793
+ // Necessary to make sure the init is deoptimized for conditional declarations.
7794
+ // We cannot call deoptimizePath here.
7795
+ variable.markInitializersForDeoptimization();
7796
+ return variable;
7959
7797
  }
7960
- currentGlobal = currentGlobal[pathSegment];
7961
- if (!currentGlobal) {
7962
- return null;
7798
+ else {
7799
+ return super.addDeclaration(identifier, context, init, false);
7963
7800
  }
7964
7801
  }
7965
- return currentGlobal[ValueProperties];
7966
7802
  }
7967
7803
 
7968
- class GlobalVariable extends Variable {
7804
+ class ExpressionStatement extends NodeBase {
7805
+ initialise() {
7806
+ if (this.directive &&
7807
+ this.directive !== 'use strict' &&
7808
+ this.parent.type === Program$1) {
7809
+ this.context.warn(
7810
+ // This is necessary, because either way (deleting or not) can lead to errors.
7811
+ {
7812
+ code: 'MODULE_LEVEL_DIRECTIVE',
7813
+ message: `Module level directives cause errors when bundled, '${this.directive}' was ignored.`
7814
+ }, this.start);
7815
+ }
7816
+ }
7817
+ render(code, options) {
7818
+ super.render(code, options);
7819
+ if (this.included)
7820
+ this.insertSemicolon(code);
7821
+ }
7822
+ shouldBeIncluded(context) {
7823
+ if (this.directive && this.directive !== 'use strict')
7824
+ return this.parent.type !== Program$1;
7825
+ return super.shouldBeIncluded(context);
7826
+ }
7827
+ applyDeoptimizations() { }
7828
+ }
7829
+
7830
+ class BlockStatement extends NodeBase {
7969
7831
  constructor() {
7970
7832
  super(...arguments);
7971
- // Ensure we use live-bindings for globals as we do not know if they have
7972
- // been reassigned
7973
- this.isReassigned = true;
7833
+ this.directlyIncluded = false;
7974
7834
  }
7975
- getLiteralValueAtPath(path, _recursionTracker, _origin) {
7976
- return getGlobalAtPath([this.name, ...path]) ? UnknownTruthyValue : UnknownValue;
7835
+ addImplicitReturnExpressionToScope() {
7836
+ const lastStatement = this.body[this.body.length - 1];
7837
+ if (!lastStatement || lastStatement.type !== ReturnStatement$1) {
7838
+ this.scope.addReturnExpression(UNKNOWN_EXPRESSION);
7839
+ }
7977
7840
  }
7978
- hasEffectsWhenAccessedAtPath(path) {
7979
- if (path.length === 0) {
7980
- // Technically, "undefined" is a global variable of sorts
7981
- return this.name !== 'undefined' && !getGlobalAtPath([this.name]);
7841
+ createScope(parentScope) {
7842
+ this.scope = this.parent.preventChildBlockScope
7843
+ ? parentScope
7844
+ : new BlockScope(parentScope);
7845
+ }
7846
+ hasEffects(context) {
7847
+ if (this.deoptimizeBody)
7848
+ return true;
7849
+ for (const node of this.body) {
7850
+ if (context.brokenFlow)
7851
+ break;
7852
+ if (node.hasEffects(context))
7853
+ return true;
7982
7854
  }
7983
- return !getGlobalAtPath([this.name, ...path].slice(0, -1));
7855
+ return false;
7984
7856
  }
7985
- hasEffectsWhenCalledAtPath(path, callOptions, context) {
7986
- const globalAtPath = getGlobalAtPath([this.name, ...path]);
7987
- return !globalAtPath || globalAtPath.hasEffectsWhenCalled(callOptions, context);
7857
+ include(context, includeChildrenRecursively) {
7858
+ if (!(this.deoptimizeBody && this.directlyIncluded)) {
7859
+ this.included = true;
7860
+ this.directlyIncluded = true;
7861
+ if (this.deoptimizeBody)
7862
+ includeChildrenRecursively = true;
7863
+ for (const node of this.body) {
7864
+ if (includeChildrenRecursively || node.shouldBeIncluded(context))
7865
+ node.include(context, includeChildrenRecursively);
7866
+ }
7867
+ }
7868
+ }
7869
+ initialise() {
7870
+ const firstBodyStatement = this.body[0];
7871
+ this.deoptimizeBody =
7872
+ firstBodyStatement instanceof ExpressionStatement &&
7873
+ firstBodyStatement.directive === 'use asm';
7874
+ }
7875
+ render(code, options) {
7876
+ if (this.body.length) {
7877
+ renderStatementList(this.body, code, this.start + 1, this.end - 1, options);
7878
+ }
7879
+ else {
7880
+ super.render(code, options);
7881
+ }
7988
7882
  }
7989
7883
  }
7990
7884
 
7991
- const tdzVariableKinds = {
7992
- __proto__: null,
7993
- class: true,
7994
- const: true,
7995
- let: true,
7996
- var: true
7997
- };
7998
- class Identifier extends NodeBase {
7885
+ class RestElement extends NodeBase {
7999
7886
  constructor() {
8000
7887
  super(...arguments);
8001
- this.variable = null;
8002
- this.deoptimized = false;
8003
- this.isTDZAccess = null;
7888
+ this.declarationInit = null;
8004
7889
  }
8005
7890
  addExportedVariables(variables, exportNamesByVariable) {
8006
- if (exportNamesByVariable.has(this.variable)) {
8007
- variables.push(this.variable);
8008
- }
8009
- }
8010
- bind() {
8011
- if (!this.variable && is_reference(this, this.parent)) {
8012
- this.variable = this.scope.findVariable(this.name);
8013
- this.variable.addReference(this);
8014
- }
7891
+ this.argument.addExportedVariables(variables, exportNamesByVariable);
8015
7892
  }
8016
7893
  declare(kind, init) {
8017
- let variable;
8018
- const { treeshake } = this.context.options;
8019
- switch (kind) {
8020
- case 'var':
8021
- variable = this.scope.addDeclaration(this, this.context, init, true);
8022
- if (treeshake && treeshake.correctVarValueBeforeDeclaration) {
8023
- // Necessary to make sure the init is deoptimized. We cannot call deoptimizePath here.
8024
- variable.markInitializersForDeoptimization();
8025
- }
8026
- break;
8027
- case 'function':
8028
- // in strict mode, functions are only hoisted within a scope but not across block scopes
8029
- variable = this.scope.addDeclaration(this, this.context, init, false);
8030
- break;
8031
- case 'let':
8032
- case 'const':
8033
- case 'class':
8034
- variable = this.scope.addDeclaration(this, this.context, init, false);
8035
- break;
8036
- case 'parameter':
8037
- variable = this.scope.addParameterDeclaration(this);
8038
- break;
8039
- /* istanbul ignore next */
8040
- default:
8041
- /* istanbul ignore next */
8042
- throw new Error(`Internal Error: Unexpected identifier kind ${kind}.`);
7894
+ this.declarationInit = init;
7895
+ return this.argument.declare(kind, UNKNOWN_EXPRESSION);
7896
+ }
7897
+ deoptimizePath(path) {
7898
+ path.length === 0 && this.argument.deoptimizePath(EMPTY_PATH);
7899
+ }
7900
+ hasEffectsWhenAssignedAtPath(path, context) {
7901
+ return path.length > 0 || this.argument.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
7902
+ }
7903
+ markDeclarationReached() {
7904
+ this.argument.markDeclarationReached();
7905
+ }
7906
+ applyDeoptimizations() {
7907
+ this.deoptimized = true;
7908
+ if (this.declarationInit !== null) {
7909
+ this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
7910
+ this.context.requestTreeshakingPass();
8043
7911
  }
8044
- variable.kind = kind;
8045
- return [(this.variable = variable)];
7912
+ }
7913
+ }
7914
+
7915
+ class FunctionBase extends NodeBase {
7916
+ constructor() {
7917
+ super(...arguments);
7918
+ this.objectEntity = null;
7919
+ this.deoptimizedReturn = false;
8046
7920
  }
8047
7921
  deoptimizePath(path) {
8048
- if (path.length === 0 && !this.scope.contains(this.name)) {
8049
- this.disallowImportReassignment();
7922
+ this.getObjectEntity().deoptimizePath(path);
7923
+ if (path.length === 1 && path[0] === UnknownKey) {
7924
+ // A reassignment of UNKNOWN_PATH is considered equivalent to having lost track
7925
+ // which means the return expression needs to be reassigned
7926
+ this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH);
8050
7927
  }
8051
- this.variable.deoptimizePath(path);
8052
7928
  }
8053
7929
  deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
8054
- this.variable.deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
7930
+ if (path.length > 0) {
7931
+ this.getObjectEntity().deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
7932
+ }
8055
7933
  }
8056
7934
  getLiteralValueAtPath(path, recursionTracker, origin) {
8057
- return this.getVariableRespectingTDZ().getLiteralValueAtPath(path, recursionTracker, origin);
7935
+ return this.getObjectEntity().getLiteralValueAtPath(path, recursionTracker, origin);
8058
7936
  }
8059
7937
  getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin) {
8060
- return this.getVariableRespectingTDZ().getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin);
8061
- }
8062
- hasEffects() {
8063
- if (!this.deoptimized)
8064
- this.applyDeoptimizations();
8065
- if (this.isPossibleTDZ() && this.variable.kind !== 'var') {
8066
- return true;
7938
+ if (path.length > 0) {
7939
+ return this.getObjectEntity().getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin);
8067
7940
  }
8068
- return (this.context.options.treeshake.unknownGlobalSideEffects &&
8069
- this.variable instanceof GlobalVariable &&
8070
- this.variable.hasEffectsWhenAccessedAtPath(EMPTY_PATH));
7941
+ if (this.async) {
7942
+ if (!this.deoptimizedReturn) {
7943
+ this.deoptimizedReturn = true;
7944
+ this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH);
7945
+ this.context.requestTreeshakingPass();
7946
+ }
7947
+ return UNKNOWN_EXPRESSION;
7948
+ }
7949
+ return this.scope.getReturnExpression();
8071
7950
  }
8072
7951
  hasEffectsWhenAccessedAtPath(path, context) {
8073
- return (this.variable !== null &&
8074
- this.getVariableRespectingTDZ().hasEffectsWhenAccessedAtPath(path, context));
7952
+ return this.getObjectEntity().hasEffectsWhenAccessedAtPath(path, context);
8075
7953
  }
8076
7954
  hasEffectsWhenAssignedAtPath(path, context) {
8077
- return (path.length > 0 ? this.getVariableRespectingTDZ() : this.variable).hasEffectsWhenAssignedAtPath(path, context);
7955
+ return this.getObjectEntity().hasEffectsWhenAssignedAtPath(path, context);
8078
7956
  }
8079
7957
  hasEffectsWhenCalledAtPath(path, callOptions, context) {
8080
- return this.getVariableRespectingTDZ().hasEffectsWhenCalledAtPath(path, callOptions, context);
7958
+ if (path.length > 0) {
7959
+ return this.getObjectEntity().hasEffectsWhenCalledAtPath(path, callOptions, context);
7960
+ }
7961
+ if (this.async) {
7962
+ const { propertyReadSideEffects } = this.context.options
7963
+ .treeshake;
7964
+ const returnExpression = this.scope.getReturnExpression();
7965
+ if (returnExpression.hasEffectsWhenCalledAtPath(['then'], { args: NO_ARGS, thisParam: null, withNew: false }, context) ||
7966
+ (propertyReadSideEffects &&
7967
+ (propertyReadSideEffects === 'always' ||
7968
+ returnExpression.hasEffectsWhenAccessedAtPath(['then'], context)))) {
7969
+ return true;
7970
+ }
7971
+ }
7972
+ for (const param of this.params) {
7973
+ if (param.hasEffects(context))
7974
+ return true;
7975
+ }
7976
+ return false;
8081
7977
  }
8082
- include() {
7978
+ include(context, includeChildrenRecursively) {
8083
7979
  if (!this.deoptimized)
8084
7980
  this.applyDeoptimizations();
8085
- if (!this.included) {
8086
- this.included = true;
8087
- if (this.variable !== null) {
8088
- this.context.includeVariableInModule(this.variable);
8089
- }
8090
- }
7981
+ this.included = true;
7982
+ const { brokenFlow } = context;
7983
+ context.brokenFlow = BROKEN_FLOW_NONE;
7984
+ this.body.include(context, includeChildrenRecursively);
7985
+ context.brokenFlow = brokenFlow;
8091
7986
  }
8092
7987
  includeCallArguments(context, args) {
8093
- this.variable.includeCallArguments(context, args);
8094
- }
8095
- isPossibleTDZ() {
8096
- // return cached value to avoid issues with the next tree-shaking pass
8097
- if (this.isTDZAccess !== null)
8098
- return this.isTDZAccess;
8099
- if (!(this.variable instanceof LocalVariable) ||
8100
- !this.variable.kind ||
8101
- !(this.variable.kind in tdzVariableKinds)) {
8102
- return (this.isTDZAccess = false);
7988
+ this.scope.includeCallArguments(context, args);
7989
+ }
7990
+ initialise() {
7991
+ this.scope.addParameterVariables(this.params.map(param => param.declare('parameter', UNKNOWN_EXPRESSION)), this.params[this.params.length - 1] instanceof RestElement);
7992
+ if (this.body instanceof BlockStatement) {
7993
+ this.body.addImplicitReturnExpressionToScope();
8103
7994
  }
8104
- let decl_id;
8105
- if (this.variable.declarations &&
8106
- this.variable.declarations.length === 1 &&
8107
- (decl_id = this.variable.declarations[0]) &&
8108
- this.start < decl_id.start &&
8109
- closestParentFunctionOrProgram(this) === closestParentFunctionOrProgram(decl_id)) {
8110
- // a variable accessed before its declaration
8111
- // in the same function or at top level of module
8112
- return (this.isTDZAccess = true);
7995
+ else {
7996
+ this.scope.addReturnExpression(this.body);
8113
7997
  }
8114
- if (!this.variable.initReached) {
8115
- // Either a const/let TDZ violation or
8116
- // var use before declaration was encountered.
8117
- return (this.isTDZAccess = true);
7998
+ }
7999
+ parseNode(esTreeNode) {
8000
+ if (esTreeNode.body.type === BlockStatement$1) {
8001
+ this.body = new BlockStatement(esTreeNode.body, this, this.scope.hoistedBodyVarScope);
8118
8002
  }
8119
- return (this.isTDZAccess = false);
8003
+ super.parseNode(esTreeNode);
8120
8004
  }
8121
- markDeclarationReached() {
8122
- this.variable.initReached = true;
8005
+ applyDeoptimizations() { }
8006
+ }
8007
+ FunctionBase.prototype.preventChildBlockScope = true;
8008
+
8009
+ class ArrowFunctionExpression extends FunctionBase {
8010
+ constructor() {
8011
+ super(...arguments);
8012
+ this.objectEntity = null;
8123
8013
  }
8124
- render(code, { snippets: { getPropertyAccess } }, { renderedParentType, isCalleeOfRenderedParent, isShorthandProperty } = BLANK) {
8125
- if (this.variable) {
8126
- const name = this.variable.getName(getPropertyAccess);
8127
- if (name !== this.name) {
8128
- code.overwrite(this.start, this.end, name, {
8129
- contentOnly: true,
8130
- storeName: true
8131
- });
8132
- if (isShorthandProperty) {
8133
- code.prependRight(this.start, `${this.name}: `);
8134
- }
8135
- }
8136
- // In strict mode, any variable named "eval" must be the actual "eval" function
8137
- if (name === 'eval' &&
8138
- renderedParentType === CallExpression$1 &&
8139
- isCalleeOfRenderedParent) {
8140
- code.appendRight(this.start, '0, ');
8014
+ createScope(parentScope) {
8015
+ this.scope = new ReturnValueScope(parentScope, this.context);
8016
+ }
8017
+ hasEffects() {
8018
+ if (!this.deoptimized)
8019
+ this.applyDeoptimizations();
8020
+ return false;
8021
+ }
8022
+ hasEffectsWhenCalledAtPath(path, callOptions, context) {
8023
+ if (super.hasEffectsWhenCalledAtPath(path, callOptions, context))
8024
+ return true;
8025
+ const { ignore, brokenFlow } = context;
8026
+ context.ignore = {
8027
+ breaks: false,
8028
+ continues: false,
8029
+ labels: new Set(),
8030
+ returnYield: true
8031
+ };
8032
+ if (this.body.hasEffects(context))
8033
+ return true;
8034
+ context.ignore = ignore;
8035
+ context.brokenFlow = brokenFlow;
8036
+ return false;
8037
+ }
8038
+ include(context, includeChildrenRecursively) {
8039
+ super.include(context, includeChildrenRecursively);
8040
+ for (const param of this.params) {
8041
+ if (!(param instanceof Identifier)) {
8042
+ param.include(context, includeChildrenRecursively);
8141
8043
  }
8142
8044
  }
8143
8045
  }
8144
- applyDeoptimizations() {
8145
- this.deoptimized = true;
8146
- if (this.variable instanceof LocalVariable) {
8147
- this.variable.consolidateInitializers();
8148
- this.context.requestTreeshakingPass();
8046
+ getObjectEntity() {
8047
+ if (this.objectEntity !== null) {
8048
+ return this.objectEntity;
8149
8049
  }
8050
+ return (this.objectEntity = new ObjectEntity([], OBJECT_PROTOTYPE));
8150
8051
  }
8151
- disallowImportReassignment() {
8152
- return this.context.error({
8153
- code: 'ILLEGAL_REASSIGNMENT',
8154
- message: `Illegal reassignment to import '${this.name}'`
8155
- }, this.start);
8052
+ }
8053
+
8054
+ function getSystemExportStatement(exportedVariables, { exportNamesByVariable, snippets: { _, getObject, getPropertyAccess } }, modifier = '') {
8055
+ if (exportedVariables.length === 1 &&
8056
+ exportNamesByVariable.get(exportedVariables[0]).length === 1) {
8057
+ const variable = exportedVariables[0];
8058
+ return `exports('${exportNamesByVariable.get(variable)}',${_}${variable.getName(getPropertyAccess)}${modifier})`;
8156
8059
  }
8157
- getVariableRespectingTDZ() {
8158
- if (this.isPossibleTDZ()) {
8159
- return UNKNOWN_EXPRESSION;
8060
+ else {
8061
+ const fields = [];
8062
+ for (const variable of exportedVariables) {
8063
+ for (const exportName of exportNamesByVariable.get(variable)) {
8064
+ fields.push([exportName, variable.getName(getPropertyAccess) + modifier]);
8065
+ }
8160
8066
  }
8161
- return this.variable;
8067
+ return `exports(${getObject(fields, { lineBreakIndent: null })})`;
8162
8068
  }
8163
8069
  }
8164
- function closestParentFunctionOrProgram(node) {
8165
- while (node && !/^Program|Function/.test(node.type)) {
8166
- node = node.parent;
8070
+ function renderSystemExportExpression(exportedVariable, expressionStart, expressionEnd, code, { exportNamesByVariable, snippets: { _ } }) {
8071
+ code.prependRight(expressionStart, `exports('${exportNamesByVariable.get(exportedVariable)}',${_}`);
8072
+ code.appendLeft(expressionEnd, ')');
8073
+ }
8074
+ function renderSystemExportFunction(exportedVariables, expressionStart, expressionEnd, needsParens, code, options) {
8075
+ const { _, getDirectReturnIifeLeft } = options.snippets;
8076
+ code.prependRight(expressionStart, getDirectReturnIifeLeft(['v'], `${getSystemExportStatement(exportedVariables, options)},${_}v`, { needsArrowReturnParens: true, needsWrappedFunction: needsParens }));
8077
+ code.appendLeft(expressionEnd, ')');
8078
+ }
8079
+ function renderSystemExportSequenceAfterExpression(exportedVariable, expressionStart, expressionEnd, needsParens, code, options) {
8080
+ const { _, getPropertyAccess } = options.snippets;
8081
+ code.appendLeft(expressionEnd, `,${_}${getSystemExportStatement([exportedVariable], options)},${_}${exportedVariable.getName(getPropertyAccess)}`);
8082
+ if (needsParens) {
8083
+ code.prependRight(expressionStart, '(');
8084
+ code.appendLeft(expressionEnd, ')');
8085
+ }
8086
+ }
8087
+ function renderSystemExportSequenceBeforeExpression(exportedVariable, expressionStart, expressionEnd, needsParens, code, options, modifier) {
8088
+ const { _ } = options.snippets;
8089
+ code.prependRight(expressionStart, `${getSystemExportStatement([exportedVariable], options, modifier)},${_}`);
8090
+ if (needsParens) {
8091
+ code.prependRight(expressionStart, '(');
8092
+ code.appendLeft(expressionEnd, ')');
8167
8093
  }
8168
- // one of: ArrowFunctionExpression, FunctionDeclaration, FunctionExpression or Program
8169
- return node;
8170
8094
  }
8171
8095
 
8172
8096
  class ObjectPattern extends NodeBase {
@@ -8211,10 +8135,6 @@ class ObjectPattern extends NodeBase {
8211
8135
  }
8212
8136
 
8213
8137
  class AssignmentExpression extends NodeBase {
8214
- constructor() {
8215
- super(...arguments);
8216
- this.deoptimized = false;
8217
- }
8218
8138
  hasEffects(context) {
8219
8139
  if (!this.deoptimized)
8220
8140
  this.applyDeoptimizations();
@@ -8295,6 +8215,46 @@ class AssignmentExpression extends NodeBase {
8295
8215
  }
8296
8216
  }
8297
8217
 
8218
+ class AssignmentPattern extends NodeBase {
8219
+ addExportedVariables(variables, exportNamesByVariable) {
8220
+ this.left.addExportedVariables(variables, exportNamesByVariable);
8221
+ }
8222
+ declare(kind, init) {
8223
+ return this.left.declare(kind, init);
8224
+ }
8225
+ deoptimizePath(path) {
8226
+ path.length === 0 && this.left.deoptimizePath(path);
8227
+ }
8228
+ hasEffectsWhenAssignedAtPath(path, context) {
8229
+ return path.length > 0 || this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
8230
+ }
8231
+ include(context, includeChildrenRecursively) {
8232
+ if (!this.deoptimized)
8233
+ this.applyDeoptimizations();
8234
+ this.included = true;
8235
+ this.left.include(context, includeChildrenRecursively);
8236
+ this.right.include(context, includeChildrenRecursively);
8237
+ }
8238
+ markDeclarationReached() {
8239
+ this.left.markDeclarationReached();
8240
+ }
8241
+ render(code, options, { isShorthandProperty } = BLANK) {
8242
+ this.left.render(code, options, { isShorthandProperty });
8243
+ if (this.right.included) {
8244
+ this.right.render(code, options);
8245
+ }
8246
+ else {
8247
+ code.remove(this.left.end, this.end);
8248
+ }
8249
+ }
8250
+ applyDeoptimizations() {
8251
+ this.deoptimized = true;
8252
+ this.left.deoptimizePath(EMPTY_PATH);
8253
+ this.right.deoptimizePath(UNKNOWN_PATH);
8254
+ this.context.requestTreeshakingPass();
8255
+ }
8256
+ }
8257
+
8298
8258
  class ArgumentsVariable extends LocalVariable {
8299
8259
  constructor(context) {
8300
8260
  super('arguments', null, UNKNOWN_EXPRESSION, context);
@@ -8403,6 +8363,8 @@ class FunctionNode extends FunctionBase {
8403
8363
  }
8404
8364
  hasEffects() {
8405
8365
  var _a;
8366
+ if (!this.deoptimized)
8367
+ this.applyDeoptimizations();
8406
8368
  return !!((_a = this.id) === null || _a === void 0 ? void 0 : _a.hasEffects());
8407
8369
  }
8408
8370
  hasEffectsWhenCalledAtPath(path, callOptions, context) {
@@ -8431,12 +8393,16 @@ class FunctionNode extends FunctionBase {
8431
8393
  context.ignore = ignore;
8432
8394
  return false;
8433
8395
  }
8434
- include(context, includeChildrenRecursively, { includeWithoutParameterDefaults } = BLANK) {
8396
+ include(context, includeChildrenRecursively) {
8435
8397
  var _a;
8398
+ super.include(context, includeChildrenRecursively);
8436
8399
  (_a = this.id) === null || _a === void 0 ? void 0 : _a.include();
8437
- super.include(context, includeChildrenRecursively, {
8438
- includeWithoutParameterDefaults: includeWithoutParameterDefaults && !this.scope.argumentsVariable.included
8439
- });
8400
+ const hasArguments = this.scope.argumentsVariable.included;
8401
+ for (const param of this.params) {
8402
+ if (!(param instanceof Identifier) || hasArguments) {
8403
+ param.include(context, includeChildrenRecursively);
8404
+ }
8405
+ }
8440
8406
  }
8441
8407
  initialise() {
8442
8408
  var _a;
@@ -8458,10 +8424,6 @@ class FunctionNode extends FunctionBase {
8458
8424
  }
8459
8425
 
8460
8426
  class AwaitExpression extends NodeBase {
8461
- constructor() {
8462
- super(...arguments);
8463
- this.deoptimized = false;
8464
- }
8465
8427
  hasEffects() {
8466
8428
  if (!this.deoptimized)
8467
8429
  this.applyDeoptimizations();
@@ -8569,6 +8531,31 @@ class BreakStatement extends NodeBase {
8569
8531
  }
8570
8532
  }
8571
8533
 
8534
+ function renderCallArguments(code, options, node) {
8535
+ if (node.arguments.length > 0) {
8536
+ if (node.arguments[node.arguments.length - 1].included) {
8537
+ for (const arg of node.arguments) {
8538
+ arg.render(code, options);
8539
+ }
8540
+ }
8541
+ else {
8542
+ let lastIncludedIndex = node.arguments.length - 2;
8543
+ while (lastIncludedIndex >= 0 && !node.arguments[lastIncludedIndex].included) {
8544
+ lastIncludedIndex--;
8545
+ }
8546
+ if (lastIncludedIndex >= 0) {
8547
+ for (let index = 0; index <= lastIncludedIndex; index++) {
8548
+ node.arguments[index].render(code, options);
8549
+ }
8550
+ code.remove(findFirstOccurrenceOutsideComment(code.original, ',', node.arguments[lastIncludedIndex].end), node.end - 1);
8551
+ }
8552
+ else {
8553
+ code.remove(findFirstOccurrenceOutsideComment(code.original, '(', node.callee.end) + 1, node.end - 1);
8554
+ }
8555
+ }
8556
+ }
8557
+ }
8558
+
8572
8559
  class Literal extends NodeBase {
8573
8560
  deoptimizeThisOnEventAtPath() { }
8574
8561
  getLiteralValueAtPath(path) {
@@ -8655,7 +8642,6 @@ class MemberExpression extends NodeBase {
8655
8642
  constructor() {
8656
8643
  super(...arguments);
8657
8644
  this.variable = null;
8658
- this.deoptimized = false;
8659
8645
  this.bound = false;
8660
8646
  this.expressionsToBeDeoptimized = [];
8661
8647
  this.replacement = null;
@@ -8905,7 +8891,6 @@ class MemberExpression extends NodeBase {
8905
8891
  class CallExpressionBase extends NodeBase {
8906
8892
  constructor() {
8907
8893
  super(...arguments);
8908
- this.deoptimized = false;
8909
8894
  this.returnExpression = null;
8910
8895
  this.deoptimizableDependentExpressions = [];
8911
8896
  this.expressionsToBeDeoptimized = new Set();
@@ -9034,7 +9019,7 @@ class CallExpression extends CallExpressionBase {
9034
9019
  }
9035
9020
  else {
9036
9021
  this.included = true;
9037
- this.callee.include(context, false, { includeWithoutParameterDefaults: true });
9022
+ this.callee.include(context, false);
9038
9023
  }
9039
9024
  this.callee.includeCallArguments(context, this.arguments);
9040
9025
  const returnExpression = this.getReturnExpression();
@@ -9047,28 +9032,7 @@ class CallExpression extends CallExpressionBase {
9047
9032
  isCalleeOfRenderedParent: true,
9048
9033
  renderedSurroundingElement
9049
9034
  });
9050
- if (this.arguments.length > 0) {
9051
- if (this.arguments[this.arguments.length - 1].included) {
9052
- for (const arg of this.arguments) {
9053
- arg.render(code, options);
9054
- }
9055
- }
9056
- else {
9057
- let lastIncludedIndex = this.arguments.length - 2;
9058
- while (lastIncludedIndex >= 0 && !this.arguments[lastIncludedIndex].included) {
9059
- lastIncludedIndex--;
9060
- }
9061
- if (lastIncludedIndex >= 0) {
9062
- for (let index = 0; index <= lastIncludedIndex; index++) {
9063
- this.arguments[index].render(code, options);
9064
- }
9065
- code.remove(findFirstOccurrenceOutsideComment(code.original, ',', this.arguments[lastIncludedIndex].end), this.end - 1);
9066
- }
9067
- else {
9068
- code.remove(findFirstOccurrenceOutsideComment(code.original, '(', this.callee.end) + 1, this.end - 1);
9069
- }
9070
- }
9071
- }
9035
+ renderCallArguments(code, options, this);
9072
9036
  }
9073
9037
  applyDeoptimizations() {
9074
9038
  this.deoptimized = true;
@@ -9156,6 +9120,7 @@ class ClassBody extends NodeBase {
9156
9120
  }
9157
9121
  super.parseNode(esTreeNode);
9158
9122
  }
9123
+ applyDeoptimizations() { }
9159
9124
  }
9160
9125
 
9161
9126
  class MethodBase extends NodeBase {
@@ -9207,6 +9172,7 @@ class MethodBase extends NodeBase {
9207
9172
  hasEffectsWhenCalledAtPath(path, callOptions, context) {
9208
9173
  return this.getAccessedValue().hasEffectsWhenCalledAtPath(path, callOptions, context);
9209
9174
  }
9175
+ applyDeoptimizations() { }
9210
9176
  getAccessedValue() {
9211
9177
  if (this.accessedValue === null) {
9212
9178
  if (this.kind === 'get') {
@@ -9222,6 +9188,7 @@ class MethodBase extends NodeBase {
9222
9188
  }
9223
9189
 
9224
9190
  class MethodDefinition extends MethodBase {
9191
+ applyDeoptimizations() { }
9225
9192
  }
9226
9193
 
9227
9194
  class ObjectMember extends ExpressionEntity {
@@ -9256,7 +9223,6 @@ class ObjectMember extends ExpressionEntity {
9256
9223
  class ClassNode extends NodeBase {
9257
9224
  constructor() {
9258
9225
  super(...arguments);
9259
- this.deoptimized = false;
9260
9226
  this.objectEntity = null;
9261
9227
  }
9262
9228
  createScope(parentScope) {
@@ -9266,14 +9232,7 @@ class ClassNode extends NodeBase {
9266
9232
  this.getObjectEntity().deoptimizeAllProperties();
9267
9233
  }
9268
9234
  deoptimizePath(path) {
9269
- var _a, _b;
9270
9235
  this.getObjectEntity().deoptimizePath(path);
9271
- if (path.length === 1 && path[0] === UnknownKey) {
9272
- // A reassignment of UNKNOWN_PATH is considered equivalent to having lost track
9273
- // which means the constructor needs to be reassigned
9274
- (_a = this.classConstructor) === null || _a === void 0 ? void 0 : _a.deoptimizePath(UNKNOWN_PATH);
9275
- (_b = this.superClass) === null || _b === void 0 ? void 0 : _b.deoptimizePath(UNKNOWN_PATH);
9276
- }
9277
9236
  }
9278
9237
  deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
9279
9238
  this.getObjectEntity().deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
@@ -9671,13 +9630,11 @@ class ExportAllDeclaration extends NodeBase {
9671
9630
  render(code, _options, nodeRenderOptions) {
9672
9631
  code.remove(nodeRenderOptions.start, nodeRenderOptions.end);
9673
9632
  }
9633
+ applyDeoptimizations() { }
9674
9634
  }
9675
9635
  ExportAllDeclaration.prototype.needsBoundaries = true;
9676
9636
 
9677
9637
  class FunctionDeclaration extends FunctionNode {
9678
- include(context, includeChildrenRecursively) {
9679
- super.include(context, includeChildrenRecursively, { includeWithoutParameterDefaults: true });
9680
- }
9681
9638
  initialise() {
9682
9639
  super.initialise();
9683
9640
  if (this.id !== null) {
@@ -9748,6 +9705,7 @@ class ExportDefaultDeclaration extends NodeBase {
9748
9705
  }
9749
9706
  this.declaration.render(code, options);
9750
9707
  }
9708
+ applyDeoptimizations() { }
9751
9709
  renderNamedDeclaration(code, declarationStart, declarationKeyword, endMarker, needsId, options) {
9752
9710
  const { exportNamesByVariable, format, snippets: { getPropertyAccess } } = options;
9753
9711
  const name = this.variable.getName(getPropertyAccess);
@@ -9802,17 +9760,15 @@ class ExportNamedDeclaration extends NodeBase {
9802
9760
  this.declaration.render(code, options, { end, start });
9803
9761
  }
9804
9762
  }
9763
+ applyDeoptimizations() { }
9805
9764
  }
9806
9765
  ExportNamedDeclaration.prototype.needsBoundaries = true;
9807
9766
 
9808
9767
  class ExportSpecifier extends NodeBase {
9768
+ applyDeoptimizations() { }
9809
9769
  }
9810
9770
 
9811
9771
  class ForInStatement extends NodeBase {
9812
- constructor() {
9813
- super(...arguments);
9814
- this.deoptimized = false;
9815
- }
9816
9772
  createScope(parentScope) {
9817
9773
  this.scope = new BlockScope(parentScope);
9818
9774
  }
@@ -9861,10 +9817,6 @@ class ForInStatement extends NodeBase {
9861
9817
  }
9862
9818
 
9863
9819
  class ForOfStatement extends NodeBase {
9864
- constructor() {
9865
- super(...arguments);
9866
- this.deoptimized = false;
9867
- }
9868
9820
  createScope(parentScope) {
9869
9821
  this.scope = new BlockScope(parentScope);
9870
9822
  }
@@ -10059,6 +10011,7 @@ class IfStatement extends NodeBase {
10059
10011
  }
10060
10012
  this.renderHoistedDeclarations(hoistedDeclarations, code, getPropertyAccess);
10061
10013
  }
10014
+ applyDeoptimizations() { }
10062
10015
  getTestValue() {
10063
10016
  if (this.testValue === unset) {
10064
10017
  return (this.testValue = this.test.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this));
@@ -10144,10 +10097,12 @@ class ImportDeclaration extends NodeBase {
10144
10097
  render(code, _options, nodeRenderOptions) {
10145
10098
  code.remove(nodeRenderOptions.start, nodeRenderOptions.end);
10146
10099
  }
10100
+ applyDeoptimizations() { }
10147
10101
  }
10148
10102
  ImportDeclaration.prototype.needsBoundaries = true;
10149
10103
 
10150
10104
  class ImportDefaultSpecifier extends NodeBase {
10105
+ applyDeoptimizations() { }
10151
10106
  }
10152
10107
 
10153
10108
  const INTEROP_DEFAULT_VARIABLE = '_interopDefault';
@@ -10406,6 +10361,7 @@ class ImportExpression extends NodeBase {
10406
10361
  setInternalResolution(inlineNamespace) {
10407
10362
  this.inlineNamespace = inlineNamespace;
10408
10363
  }
10364
+ applyDeoptimizations() { }
10409
10365
  getDynamicImportMechanismAndHelper(resolution, exportMode, { compact, dynamicImportFunction, format, generatedCode: { arrowFunctions }, interop }, { _, getDirectReturnFunction, getDirectReturnIifeLeft }, pluginDriver) {
10410
10366
  const mechanism = pluginDriver.hookFirstSync('renderDynamicImport', [
10411
10367
  {
@@ -10514,9 +10470,11 @@ const accessedImportGlobals = {
10514
10470
  };
10515
10471
 
10516
10472
  class ImportNamespaceSpecifier extends NodeBase {
10473
+ applyDeoptimizations() { }
10517
10474
  }
10518
10475
 
10519
10476
  class ImportSpecifier extends NodeBase {
10477
+ applyDeoptimizations() { }
10520
10478
  }
10521
10479
 
10522
10480
  class LabeledStatement extends NodeBase {
@@ -10562,13 +10520,16 @@ class LogicalExpression extends NodeBase {
10562
10520
  this.usedBranch = null;
10563
10521
  }
10564
10522
  deoptimizeCache() {
10565
- if (this.usedBranch !== null) {
10523
+ if (this.usedBranch) {
10566
10524
  const unusedBranch = this.usedBranch === this.left ? this.right : this.left;
10567
10525
  this.usedBranch = null;
10568
10526
  unusedBranch.deoptimizePath(UNKNOWN_PATH);
10569
10527
  for (const expression of this.expressionsToBeDeoptimized) {
10570
10528
  expression.deoptimizeCache();
10571
10529
  }
10530
+ // Request another pass because we need to ensure "include" runs again if
10531
+ // it is rendered
10532
+ this.context.requestTreeshakingPass();
10572
10533
  }
10573
10534
  }
10574
10535
  deoptimizePath(path) {
@@ -10862,10 +10823,6 @@ const importMetaMechanisms = {
10862
10823
  };
10863
10824
 
10864
10825
  class NewExpression extends NodeBase {
10865
- constructor() {
10866
- super(...arguments);
10867
- this.deoptimized = false;
10868
- }
10869
10826
  hasEffects(context) {
10870
10827
  try {
10871
10828
  for (const argument of this.arguments) {
@@ -10905,6 +10862,10 @@ class NewExpression extends NodeBase {
10905
10862
  withNew: true
10906
10863
  };
10907
10864
  }
10865
+ render(code, options) {
10866
+ this.callee.render(code, options);
10867
+ renderCallArguments(code, options, this);
10868
+ }
10908
10869
  applyDeoptimizations() {
10909
10870
  this.deoptimized = true;
10910
10871
  for (const argument of this.arguments) {
@@ -10952,6 +10913,7 @@ class ObjectExpression extends NodeBase {
10952
10913
  code.prependLeft(this.end, ')');
10953
10914
  }
10954
10915
  }
10916
+ applyDeoptimizations() { }
10955
10917
  getObjectEntity() {
10956
10918
  if (this.objectEntity !== null) {
10957
10919
  return this.objectEntity;
@@ -11028,12 +10990,12 @@ class Program extends NodeBase {
11028
10990
  super.render(code, options);
11029
10991
  }
11030
10992
  }
10993
+ applyDeoptimizations() { }
11031
10994
  }
11032
10995
 
11033
10996
  class Property extends MethodBase {
11034
10997
  constructor() {
11035
10998
  super(...arguments);
11036
- this.deoptimized = false;
11037
10999
  this.declarationInit = null;
11038
11000
  }
11039
11001
  declare(kind, init) {
@@ -11099,6 +11061,7 @@ class PropertyDefinition extends NodeBase {
11099
11061
  hasEffectsWhenCalledAtPath(path, callOptions, context) {
11100
11062
  return !this.value || this.value.hasEffectsWhenCalledAtPath(path, callOptions, context);
11101
11063
  }
11064
+ applyDeoptimizations() { }
11102
11065
  }
11103
11066
 
11104
11067
  class ReturnStatement extends NodeBase {
@@ -11695,10 +11658,6 @@ const unaryOperators = {
11695
11658
  '~': value => ~value
11696
11659
  };
11697
11660
  class UnaryExpression extends NodeBase {
11698
- constructor() {
11699
- super(...arguments);
11700
- this.deoptimized = false;
11701
- }
11702
11661
  getLiteralValueAtPath(path, recursionTracker, origin) {
11703
11662
  if (path.length > 0)
11704
11663
  return UnknownValue;
@@ -11741,10 +11700,6 @@ class UnknownNode extends NodeBase {
11741
11700
  }
11742
11701
 
11743
11702
  class UpdateExpression extends NodeBase {
11744
- constructor() {
11745
- super(...arguments);
11746
- this.deoptimized = false;
11747
- }
11748
11703
  hasEffects(context) {
11749
11704
  if (!this.deoptimized)
11750
11705
  this.applyDeoptimizations();
@@ -11846,6 +11801,7 @@ class VariableDeclaration extends NodeBase {
11846
11801
  this.renderReplacedDeclarations(code, options);
11847
11802
  }
11848
11803
  }
11804
+ applyDeoptimizations() { }
11849
11805
  renderDeclarationEnd(code, separatorString, lastSeparatorPos, actualContentEnd, renderedContentEnd, systemPatternExports, options) {
11850
11806
  if (code.original.charCodeAt(this.end - 1) === 59 /*";"*/) {
11851
11807
  code.remove(this.end - 1, this.end);
@@ -11976,9 +11932,7 @@ class VariableDeclarator extends NodeBase {
11976
11932
  include(context, includeChildrenRecursively) {
11977
11933
  var _a;
11978
11934
  this.included = true;
11979
- (_a = this.init) === null || _a === void 0 ? void 0 : _a.include(context, includeChildrenRecursively, {
11980
- includeWithoutParameterDefaults: true
11981
- });
11935
+ (_a = this.init) === null || _a === void 0 ? void 0 : _a.include(context, includeChildrenRecursively);
11982
11936
  this.id.markDeclarationReached();
11983
11937
  if (includeChildrenRecursively || this.id.shouldBeIncluded(context)) {
11984
11938
  this.id.include(context, includeChildrenRecursively);
@@ -12002,6 +11956,7 @@ class VariableDeclarator extends NodeBase {
12002
11956
  code.appendLeft(this.end, `${_}=${_}void 0`);
12003
11957
  }
12004
11958
  }
11959
+ applyDeoptimizations() { }
12005
11960
  }
12006
11961
 
12007
11962
  class WhileStatement extends NodeBase {
@@ -12028,15 +11983,11 @@ class WhileStatement extends NodeBase {
12028
11983
  }
12029
11984
 
12030
11985
  class YieldExpression extends NodeBase {
12031
- constructor() {
12032
- super(...arguments);
12033
- this.deoptimized = false;
12034
- }
12035
11986
  hasEffects(context) {
12036
11987
  var _a;
12037
11988
  if (!this.deoptimized)
12038
11989
  this.applyDeoptimizations();
12039
- return !context.ignore.returnYield || !!((_a = this.argument) === null || _a === void 0 ? void 0 : _a.hasEffects(context));
11990
+ return !(context.ignore.returnYield && !((_a = this.argument) === null || _a === void 0 ? void 0 : _a.hasEffects(context)));
12040
11991
  }
12041
11992
  render(code, options) {
12042
11993
  if (this.argument) {