rollup 2.75.4 → 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.4
4
- Tue, 31 May 2022 11:26:29 GMT - commit 0409bf0d8859a43f2d5d40dc23ebf74afadb83f7
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.4";
17
+ var version$1 = "2.75.5";
18
18
 
19
19
  var charToInteger = {};
20
20
  var chars$1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
@@ -1593,7 +1593,6 @@ class ExpressionEntity {
1593
1593
  constructor() {
1594
1594
  this.included = false;
1595
1595
  }
1596
- deoptimizeCallParameters() { }
1597
1596
  deoptimizePath(_path) { }
1598
1597
  deoptimizeThisOnEventAtPath(_event, _path, thisParameter, _recursionTracker) {
1599
1598
  thisParameter.deoptimizePath(UNKNOWN_PATH);
@@ -6073,13 +6072,8 @@ class ArrayExpression extends NodeBase {
6073
6072
  if (element) {
6074
6073
  if (hasSpread || element instanceof SpreadElement) {
6075
6074
  hasSpread = true;
6076
- // This also deoptimizes parameter defaults
6077
6075
  element.deoptimizePath(UNKNOWN_PATH);
6078
6076
  }
6079
- else {
6080
- // We do not track parameter defaults in arrays
6081
- element.deoptimizeCallParameters();
6082
- }
6083
6077
  }
6084
6078
  }
6085
6079
  this.context.requestTreeshakingPass();
@@ -6173,10 +6167,6 @@ class LocalVariable extends Variable {
6173
6167
  this.additionalInitializers = null;
6174
6168
  }
6175
6169
  }
6176
- deoptimizeCallParameters() {
6177
- var _a;
6178
- (_a = this.init) === null || _a === void 0 ? void 0 : _a.deoptimizeCallParameters();
6179
- }
6180
6170
  deoptimizePath(path) {
6181
6171
  var _a, _b;
6182
6172
  if (this.isReassigned ||
@@ -6522,641 +6512,87 @@ class ReturnValueScope extends ParameterScope {
6522
6512
  }
6523
6513
  }
6524
6514
 
6525
- class AssignmentPattern extends NodeBase {
6526
- addExportedVariables(variables, exportNamesByVariable) {
6527
- this.left.addExportedVariables(variables, exportNamesByVariable);
6528
- }
6529
- declare(kind, init) {
6530
- return this.left.declare(kind, init);
6531
- }
6532
- deoptimizePath(path) {
6533
- path.length === 0 && this.left.deoptimizePath(path);
6534
- }
6535
- hasEffectsWhenAssignedAtPath(path, context) {
6536
- return path.length > 0 || this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
6537
- }
6538
- // Note that FunctionBase may directly include .left and .right without
6539
- // including the pattern itself. This is how default parameter tree-shaking
6540
- // works at the moment.
6541
- include(context, includeChildrenRecursively) {
6542
- this.included = true;
6543
- this.left.include(context, includeChildrenRecursively);
6544
- this.right.include(context, includeChildrenRecursively);
6545
- }
6546
- markDeclarationReached() {
6547
- this.left.markDeclarationReached();
6548
- }
6549
- render(code, options, { isShorthandProperty } = BLANK) {
6550
- this.left.render(code, options, { isShorthandProperty });
6551
- if (this.right.included) {
6552
- this.right.render(code, options);
6553
- }
6554
- else {
6555
- code.remove(this.left.end, this.end);
6556
- }
6557
- }
6558
- applyDeoptimizations() {
6559
- this.deoptimized = true;
6560
- this.left.deoptimizePath(EMPTY_PATH);
6561
- this.right.deoptimizePath(UNKNOWN_PATH);
6562
- this.context.requestTreeshakingPass();
6563
- }
6564
- }
6515
+ //@ts-check
6516
+ /** @typedef { import('estree').Node} Node */
6517
+ /** @typedef {Node | {
6518
+ * type: 'PropertyDefinition';
6519
+ * computed: boolean;
6520
+ * value: Node
6521
+ * }} NodeWithPropertyDefinition */
6565
6522
 
6566
- function treeshakeNode(node, code, start, end) {
6567
- code.remove(start, end);
6568
- if (node.annotations) {
6569
- for (const annotation of node.annotations) {
6570
- if (annotation.start < start) {
6571
- code.remove(annotation.start, annotation.end);
6572
- }
6573
- else {
6574
- return;
6575
- }
6576
- }
6577
- }
6578
- }
6579
- function removeAnnotations(node, code) {
6580
- if (!node.annotations && node.parent.type === ExpressionStatement$1) {
6581
- node = node.parent;
6582
- }
6583
- if (node.annotations) {
6584
- for (const annotation of node.annotations) {
6585
- code.remove(annotation.start, annotation.end);
6586
- }
6587
- }
6588
- }
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
+ }
6589
6533
 
6590
- const NO_SEMICOLON = { isNoStatement: true };
6591
- // This assumes there are only white-space and comments between start and the string we are looking for
6592
- function findFirstOccurrenceOutsideComment(code, searchString, start = 0) {
6593
- let searchPos, charCodeAfterSlash;
6594
- searchPos = code.indexOf(searchString, start);
6595
- while (true) {
6596
- start = code.indexOf('/', start);
6597
- if (start === -1 || start >= searchPos)
6598
- return searchPos;
6599
- charCodeAfterSlash = code.charCodeAt(++start);
6600
- ++start;
6601
- // With our assumption, '/' always starts a comment. Determine comment type:
6602
- start =
6603
- charCodeAfterSlash === 47 /*"/"*/
6604
- ? code.indexOf('\n', start) + 1
6605
- : code.indexOf('*/', start) + 2;
6606
- if (start > searchPos) {
6607
- searchPos = code.indexOf(searchString, start);
6608
- }
6609
- }
6610
- }
6611
- const NON_WHITESPACE = /\S/g;
6612
- function findNonWhiteSpace(code, index) {
6613
- NON_WHITESPACE.lastIndex = index;
6614
- const result = NON_WHITESPACE.exec(code);
6615
- return result.index;
6616
- }
6617
- // This assumes "code" only contains white-space and comments
6618
- // Returns position of line-comment if applicable
6619
- function findFirstLineBreakOutsideComment(code) {
6620
- let lineBreakPos, charCodeAfterSlash, start = 0;
6621
- lineBreakPos = code.indexOf('\n', start);
6622
- while (true) {
6623
- start = code.indexOf('/', start);
6624
- if (start === -1 || start > lineBreakPos)
6625
- return [lineBreakPos, lineBreakPos + 1];
6626
- // With our assumption, '/' always starts a comment. Determine comment type:
6627
- charCodeAfterSlash = code.charCodeAt(start + 1);
6628
- if (charCodeAfterSlash === 47 /*"/"*/)
6629
- return [start, lineBreakPos + 1];
6630
- start = code.indexOf('*/', start + 3) + 2;
6631
- if (start > lineBreakPos) {
6632
- lineBreakPos = code.indexOf('\n', start);
6633
- }
6634
- }
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;
6635
6564
  }
6636
- function renderStatementList(statements, code, start, end, options) {
6637
- let currentNode, currentNodeStart, currentNodeNeedsBoundaries, nextNodeStart;
6638
- let nextNode = statements[0];
6639
- let nextNodeNeedsBoundaries = !nextNode.included || nextNode.needsBoundaries;
6640
- if (nextNodeNeedsBoundaries) {
6641
- nextNodeStart =
6642
- start + findFirstLineBreakOutsideComment(code.original.slice(start, nextNode.start))[1];
6565
+
6566
+ /* eslint sort-keys: "off" */
6567
+ const ValueProperties = Symbol('Value Properties');
6568
+ const PURE = {
6569
+ hasEffectsWhenCalled() {
6570
+ return false;
6643
6571
  }
6644
- for (let nextIndex = 1; nextIndex <= statements.length; nextIndex++) {
6645
- currentNode = nextNode;
6646
- currentNodeStart = nextNodeStart;
6647
- currentNodeNeedsBoundaries = nextNodeNeedsBoundaries;
6648
- nextNode = statements[nextIndex];
6649
- nextNodeNeedsBoundaries =
6650
- nextNode === undefined ? false : !nextNode.included || nextNode.needsBoundaries;
6651
- if (currentNodeNeedsBoundaries || nextNodeNeedsBoundaries) {
6652
- nextNodeStart =
6653
- currentNode.end +
6654
- findFirstLineBreakOutsideComment(code.original.slice(currentNode.end, nextNode === undefined ? end : nextNode.start))[1];
6655
- if (currentNode.included) {
6656
- currentNodeNeedsBoundaries
6657
- ? currentNode.render(code, options, {
6658
- end: nextNodeStart,
6659
- start: currentNodeStart
6660
- })
6661
- : currentNode.render(code, options);
6662
- }
6663
- else {
6664
- treeshakeNode(currentNode, code, currentNodeStart, nextNodeStart);
6665
- }
6666
- }
6667
- else {
6668
- currentNode.render(code, options);
6669
- }
6572
+ };
6573
+ const IMPURE = {
6574
+ hasEffectsWhenCalled() {
6575
+ return true;
6670
6576
  }
6671
- }
6672
- // This assumes that the first character is not part of the first node
6673
- function getCommaSeparatedNodesWithBoundaries(nodes, code, start, end) {
6674
- const splitUpNodes = [];
6675
- let node, nextNode, nextNodeStart, contentEnd, char;
6676
- let separator = start - 1;
6677
- for (let nextIndex = 0; nextIndex < nodes.length; nextIndex++) {
6678
- nextNode = nodes[nextIndex];
6679
- if (node !== undefined) {
6680
- separator =
6681
- node.end +
6682
- findFirstOccurrenceOutsideComment(code.original.slice(node.end, nextNode.start), ',');
6683
- }
6684
- nextNodeStart = contentEnd =
6685
- separator +
6686
- 1 +
6687
- findFirstLineBreakOutsideComment(code.original.slice(separator + 1, nextNode.start))[1];
6688
- while (((char = code.original.charCodeAt(nextNodeStart)),
6689
- char === 32 /*" "*/ || char === 9 /*"\t"*/ || char === 10 /*"\n"*/ || char === 13) /*"\r"*/)
6690
- nextNodeStart++;
6691
- if (node !== undefined) {
6692
- splitUpNodes.push({
6693
- contentEnd,
6694
- end: nextNodeStart,
6695
- node,
6696
- separator,
6697
- start
6698
- });
6699
- }
6700
- node = nextNode;
6701
- start = nextNodeStart;
6702
- }
6703
- splitUpNodes.push({
6704
- contentEnd: end,
6705
- end,
6706
- node: node,
6707
- separator: null,
6708
- start
6709
- });
6710
- return splitUpNodes;
6711
- }
6712
- // This assumes there are only white-space and comments between start and end
6713
- function removeLineBreaks(code, start, end) {
6714
- while (true) {
6715
- const [removeStart, removeEnd] = findFirstLineBreakOutsideComment(code.original.slice(start, end));
6716
- if (removeStart === -1) {
6717
- break;
6718
- }
6719
- code.remove(start + removeStart, (start += removeEnd));
6720
- }
6721
- }
6722
-
6723
- class BlockScope extends ChildScope {
6724
- addDeclaration(identifier, context, init, isHoisted) {
6725
- if (isHoisted) {
6726
- const variable = this.parent.addDeclaration(identifier, context, init, isHoisted);
6727
- // Necessary to make sure the init is deoptimized for conditional declarations.
6728
- // We cannot call deoptimizePath here.
6729
- variable.markInitializersForDeoptimization();
6730
- return variable;
6731
- }
6732
- else {
6733
- return super.addDeclaration(identifier, context, init, false);
6734
- }
6735
- }
6736
- }
6737
-
6738
- class ExpressionStatement extends NodeBase {
6739
- initialise() {
6740
- if (this.directive &&
6741
- this.directive !== 'use strict' &&
6742
- this.parent.type === Program$1) {
6743
- this.context.warn(
6744
- // This is necessary, because either way (deleting or not) can lead to errors.
6745
- {
6746
- code: 'MODULE_LEVEL_DIRECTIVE',
6747
- message: `Module level directives cause errors when bundled, '${this.directive}' was ignored.`
6748
- }, this.start);
6749
- }
6750
- }
6751
- render(code, options) {
6752
- super.render(code, options);
6753
- if (this.included)
6754
- this.insertSemicolon(code);
6755
- }
6756
- shouldBeIncluded(context) {
6757
- if (this.directive && this.directive !== 'use strict')
6758
- return this.parent.type !== Program$1;
6759
- return super.shouldBeIncluded(context);
6760
- }
6761
- applyDeoptimizations() { }
6762
- }
6763
-
6764
- class BlockStatement extends NodeBase {
6765
- constructor() {
6766
- super(...arguments);
6767
- this.directlyIncluded = false;
6768
- }
6769
- addImplicitReturnExpressionToScope() {
6770
- const lastStatement = this.body[this.body.length - 1];
6771
- if (!lastStatement || lastStatement.type !== ReturnStatement$1) {
6772
- this.scope.addReturnExpression(UNKNOWN_EXPRESSION);
6773
- }
6774
- }
6775
- createScope(parentScope) {
6776
- this.scope = this.parent.preventChildBlockScope
6777
- ? parentScope
6778
- : new BlockScope(parentScope);
6779
- }
6780
- hasEffects(context) {
6781
- if (this.deoptimizeBody)
6782
- return true;
6783
- for (const node of this.body) {
6784
- if (context.brokenFlow)
6785
- break;
6786
- if (node.hasEffects(context))
6787
- return true;
6788
- }
6789
- return false;
6790
- }
6791
- include(context, includeChildrenRecursively) {
6792
- if (!(this.deoptimizeBody && this.directlyIncluded)) {
6793
- this.included = true;
6794
- this.directlyIncluded = true;
6795
- if (this.deoptimizeBody)
6796
- includeChildrenRecursively = true;
6797
- for (const node of this.body) {
6798
- if (includeChildrenRecursively || node.shouldBeIncluded(context))
6799
- node.include(context, includeChildrenRecursively);
6800
- }
6801
- }
6802
- }
6803
- initialise() {
6804
- const firstBodyStatement = this.body[0];
6805
- this.deoptimizeBody =
6806
- firstBodyStatement instanceof ExpressionStatement &&
6807
- firstBodyStatement.directive === 'use asm';
6808
- }
6809
- render(code, options) {
6810
- if (this.body.length) {
6811
- renderStatementList(this.body, code, this.start + 1, this.end - 1, options);
6812
- }
6813
- else {
6814
- super.render(code, options);
6815
- }
6816
- }
6817
- }
6818
-
6819
- class RestElement extends NodeBase {
6820
- constructor() {
6821
- super(...arguments);
6822
- this.declarationInit = null;
6823
- }
6824
- addExportedVariables(variables, exportNamesByVariable) {
6825
- this.argument.addExportedVariables(variables, exportNamesByVariable);
6826
- }
6827
- declare(kind, init) {
6828
- this.declarationInit = init;
6829
- return this.argument.declare(kind, UNKNOWN_EXPRESSION);
6830
- }
6831
- deoptimizePath(path) {
6832
- path.length === 0 && this.argument.deoptimizePath(EMPTY_PATH);
6833
- }
6834
- hasEffectsWhenAssignedAtPath(path, context) {
6835
- return path.length > 0 || this.argument.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
6836
- }
6837
- markDeclarationReached() {
6838
- this.argument.markDeclarationReached();
6839
- }
6840
- applyDeoptimizations() {
6841
- this.deoptimized = true;
6842
- if (this.declarationInit !== null) {
6843
- this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
6844
- this.context.requestTreeshakingPass();
6845
- }
6846
- }
6847
- }
6848
-
6849
- class FunctionBase extends NodeBase {
6850
- constructor() {
6851
- super(...arguments);
6852
- this.objectEntity = null;
6853
- this.deoptimizedReturn = false;
6854
- this.forceIncludeParameters = false;
6855
- }
6856
- deoptimizeCache() {
6857
- this.forceIncludeParameters = true;
6858
- }
6859
- deoptimizeCallParameters() {
6860
- this.forceIncludeParameters = true;
6861
- }
6862
- deoptimizePath(path) {
6863
- this.getObjectEntity().deoptimizePath(path);
6864
- if (path.length === 1 && path[0] === UnknownKey) {
6865
- // A reassignment of UNKNOWN_PATH is considered equivalent to having lost track
6866
- // which means the return expression needs to be reassigned
6867
- this.forceIncludeParameters = true;
6868
- this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH);
6869
- }
6870
- }
6871
- deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
6872
- if (path.length > 0) {
6873
- this.getObjectEntity().deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
6874
- }
6875
- }
6876
- getLiteralValueAtPath(path, recursionTracker, origin) {
6877
- return this.getObjectEntity().getLiteralValueAtPath(path, recursionTracker, origin);
6878
- }
6879
- getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin) {
6880
- if (path.length > 0) {
6881
- return this.getObjectEntity().getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin);
6882
- }
6883
- if (this.async) {
6884
- if (!this.deoptimizedReturn) {
6885
- this.deoptimizedReturn = true;
6886
- this.scope.getReturnExpression().deoptimizePath(UNKNOWN_PATH);
6887
- this.context.requestTreeshakingPass();
6888
- }
6889
- return UNKNOWN_EXPRESSION;
6890
- }
6891
- return this.scope.getReturnExpression();
6892
- }
6893
- hasEffectsWhenAccessedAtPath(path, context) {
6894
- return this.getObjectEntity().hasEffectsWhenAccessedAtPath(path, context);
6895
- }
6896
- hasEffectsWhenAssignedAtPath(path, context) {
6897
- return this.getObjectEntity().hasEffectsWhenAssignedAtPath(path, context);
6898
- }
6899
- hasEffectsWhenCalledAtPath(path, callOptions, context) {
6900
- var _a;
6901
- if (path.length > 0) {
6902
- return this.getObjectEntity().hasEffectsWhenCalledAtPath(path, callOptions, context);
6903
- }
6904
- if (this.async) {
6905
- const { propertyReadSideEffects } = this.context.options
6906
- .treeshake;
6907
- const returnExpression = this.scope.getReturnExpression();
6908
- if (returnExpression.hasEffectsWhenCalledAtPath(['then'], { args: NO_ARGS, thisParam: null, withNew: false }, context) ||
6909
- (propertyReadSideEffects &&
6910
- (propertyReadSideEffects === 'always' ||
6911
- returnExpression.hasEffectsWhenAccessedAtPath(['then'], context)))) {
6912
- return true;
6913
- }
6914
- }
6915
- for (let position = 0; position < this.params.length; position++) {
6916
- const parameter = this.params[position];
6917
- if (parameter instanceof AssignmentPattern) {
6918
- if (parameter.left.hasEffects(context)) {
6919
- return true;
6920
- }
6921
- const argumentValue = (_a = callOptions.args[position]) === null || _a === void 0 ? void 0 : _a.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this);
6922
- if ((argumentValue === undefined || argumentValue === UnknownValue) &&
6923
- parameter.right.hasEffects(context)) {
6924
- return true;
6925
- }
6926
- }
6927
- else if (parameter.hasEffects(context)) {
6928
- return true;
6929
- }
6930
- }
6931
- return false;
6932
- }
6933
- include(context, includeChildrenRecursively, { includeWithoutParameterDefaults } = BLANK) {
6934
- if (!this.deoptimized)
6935
- this.applyDeoptimizations();
6936
- this.included = true;
6937
- const { brokenFlow } = context;
6938
- context.brokenFlow = BROKEN_FLOW_NONE;
6939
- this.body.include(context, includeChildrenRecursively);
6940
- context.brokenFlow = brokenFlow;
6941
- if (!includeWithoutParameterDefaults ||
6942
- includeChildrenRecursively ||
6943
- this.forceIncludeParameters) {
6944
- for (const param of this.params) {
6945
- param.include(context, includeChildrenRecursively);
6946
- }
6947
- }
6948
- }
6949
- includeCallArguments(context, args) {
6950
- var _a;
6951
- for (let position = 0; position < this.params.length; position++) {
6952
- const parameter = this.params[position];
6953
- if (parameter instanceof AssignmentPattern) {
6954
- if (parameter.left.shouldBeIncluded(context)) {
6955
- parameter.left.include(context, false);
6956
- }
6957
- const argumentValue = (_a = args[position]) === null || _a === void 0 ? void 0 : _a.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, this);
6958
- // If argumentValue === UnknownTruthyValue, then we do not need to
6959
- // include the default
6960
- if ((argumentValue === undefined || argumentValue === UnknownValue) &&
6961
- (this.parameterVariables[position].some(variable => variable.included) ||
6962
- parameter.right.shouldBeIncluded(context))) {
6963
- parameter.right.include(context, false);
6964
- }
6965
- }
6966
- else if (parameter.shouldBeIncluded(context)) {
6967
- parameter.include(context, false);
6968
- }
6969
- }
6970
- this.scope.includeCallArguments(context, args);
6971
- }
6972
- initialise() {
6973
- this.parameterVariables = this.params.map(param => param.declare('parameter', UNKNOWN_EXPRESSION));
6974
- this.scope.addParameterVariables(this.parameterVariables, this.params[this.params.length - 1] instanceof RestElement);
6975
- if (this.body instanceof BlockStatement) {
6976
- this.body.addImplicitReturnExpressionToScope();
6977
- }
6978
- else {
6979
- this.scope.addReturnExpression(this.body);
6980
- }
6981
- }
6982
- parseNode(esTreeNode) {
6983
- if (esTreeNode.body.type === BlockStatement$1) {
6984
- this.body = new BlockStatement(esTreeNode.body, this, this.scope.hoistedBodyVarScope);
6985
- }
6986
- super.parseNode(esTreeNode);
6987
- }
6988
- applyDeoptimizations() {
6989
- // We currently do not track deoptimizations of default values, deoptimize them
6990
- // just as we deoptimize call arguments
6991
- for (const param of this.params) {
6992
- if (param instanceof AssignmentPattern) {
6993
- param.right.deoptimizePath(UNKNOWN_PATH);
6994
- }
6995
- }
6996
- }
6997
- }
6998
- FunctionBase.prototype.preventChildBlockScope = true;
6999
-
7000
- class ArrowFunctionExpression extends FunctionBase {
7001
- constructor() {
7002
- super(...arguments);
7003
- this.objectEntity = null;
7004
- }
7005
- createScope(parentScope) {
7006
- this.scope = new ReturnValueScope(parentScope, this.context);
7007
- }
7008
- hasEffects() {
7009
- if (!this.deoptimized)
7010
- this.applyDeoptimizations();
7011
- return false;
7012
- }
7013
- hasEffectsWhenCalledAtPath(path, callOptions, context) {
7014
- if (super.hasEffectsWhenCalledAtPath(path, callOptions, context))
7015
- return true;
7016
- const { ignore, brokenFlow } = context;
7017
- context.ignore = {
7018
- breaks: false,
7019
- continues: false,
7020
- labels: new Set(),
7021
- returnYield: true
7022
- };
7023
- if (this.body.hasEffects(context))
7024
- return true;
7025
- context.ignore = ignore;
7026
- context.brokenFlow = brokenFlow;
7027
- return false;
7028
- }
7029
- getObjectEntity() {
7030
- if (this.objectEntity !== null) {
7031
- return this.objectEntity;
7032
- }
7033
- return (this.objectEntity = new ObjectEntity([], OBJECT_PROTOTYPE));
7034
- }
7035
- }
7036
-
7037
- function getSystemExportStatement(exportedVariables, { exportNamesByVariable, snippets: { _, getObject, getPropertyAccess } }, modifier = '') {
7038
- if (exportedVariables.length === 1 &&
7039
- exportNamesByVariable.get(exportedVariables[0]).length === 1) {
7040
- const variable = exportedVariables[0];
7041
- return `exports('${exportNamesByVariable.get(variable)}',${_}${variable.getName(getPropertyAccess)}${modifier})`;
7042
- }
7043
- else {
7044
- const fields = [];
7045
- for (const variable of exportedVariables) {
7046
- for (const exportName of exportNamesByVariable.get(variable)) {
7047
- fields.push([exportName, variable.getName(getPropertyAccess) + modifier]);
7048
- }
7049
- }
7050
- return `exports(${getObject(fields, { lineBreakIndent: null })})`;
7051
- }
7052
- }
7053
- function renderSystemExportExpression(exportedVariable, expressionStart, expressionEnd, code, { exportNamesByVariable, snippets: { _ } }) {
7054
- code.prependRight(expressionStart, `exports('${exportNamesByVariable.get(exportedVariable)}',${_}`);
7055
- code.appendLeft(expressionEnd, ')');
7056
- }
7057
- function renderSystemExportFunction(exportedVariables, expressionStart, expressionEnd, needsParens, code, options) {
7058
- const { _, getDirectReturnIifeLeft } = options.snippets;
7059
- code.prependRight(expressionStart, getDirectReturnIifeLeft(['v'], `${getSystemExportStatement(exportedVariables, options)},${_}v`, { needsArrowReturnParens: true, needsWrappedFunction: needsParens }));
7060
- code.appendLeft(expressionEnd, ')');
7061
- }
7062
- function renderSystemExportSequenceAfterExpression(exportedVariable, expressionStart, expressionEnd, needsParens, code, options) {
7063
- const { _, getPropertyAccess } = options.snippets;
7064
- code.appendLeft(expressionEnd, `,${_}${getSystemExportStatement([exportedVariable], options)},${_}${exportedVariable.getName(getPropertyAccess)}`);
7065
- if (needsParens) {
7066
- code.prependRight(expressionStart, '(');
7067
- code.appendLeft(expressionEnd, ')');
7068
- }
7069
- }
7070
- function renderSystemExportSequenceBeforeExpression(exportedVariable, expressionStart, expressionEnd, needsParens, code, options, modifier) {
7071
- const { _ } = options.snippets;
7072
- code.prependRight(expressionStart, `${getSystemExportStatement([exportedVariable], options, modifier)},${_}`);
7073
- if (needsParens) {
7074
- code.prependRight(expressionStart, '(');
7075
- code.appendLeft(expressionEnd, ')');
7076
- }
7077
- }
7078
-
7079
- //@ts-check
7080
- /** @typedef { import('estree').Node} Node */
7081
- /** @typedef {Node | {
7082
- * type: 'PropertyDefinition';
7083
- * computed: boolean;
7084
- * value: Node
7085
- * }} NodeWithPropertyDefinition */
7086
-
7087
- /**
7088
- *
7089
- * @param {NodeWithPropertyDefinition} node
7090
- * @param {NodeWithPropertyDefinition} parent
7091
- * @returns boolean
7092
- */
7093
- function is_reference (node, parent) {
7094
- if (node.type === 'MemberExpression') {
7095
- return !node.computed && is_reference(node.object, node);
7096
- }
7097
-
7098
- if (node.type === 'Identifier') {
7099
- if (!parent) return true;
7100
-
7101
- switch (parent.type) {
7102
- // disregard `bar` in `foo.bar`
7103
- case 'MemberExpression': return parent.computed || node === parent.object;
7104
-
7105
- // disregard the `foo` in `class {foo(){}}` but keep it in `class {[foo](){}}`
7106
- case 'MethodDefinition': return parent.computed;
7107
-
7108
- // disregard the `foo` in `class {foo=bar}` but keep it in `class {[foo]=bar}` and `class {bar=foo}`
7109
- case 'PropertyDefinition': return parent.computed || node === parent.value;
7110
-
7111
- // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
7112
- case 'Property': return parent.computed || node === parent.value;
7113
-
7114
- // disregard the `bar` in `export { foo as bar }` or
7115
- // the foo in `import { foo as bar }`
7116
- case 'ExportSpecifier':
7117
- case 'ImportSpecifier': return node === parent.local;
7118
-
7119
- // disregard the `foo` in `foo: while (...) { ... break foo; ... continue foo;}`
7120
- case 'LabeledStatement':
7121
- case 'BreakStatement':
7122
- case 'ContinueStatement': return false;
7123
- default: return true;
7124
- }
7125
- }
7126
-
7127
- return false;
7128
- }
7129
-
7130
- /* eslint sort-keys: "off" */
7131
- const ValueProperties = Symbol('Value Properties');
7132
- const PURE = {
7133
- hasEffectsWhenCalled() {
7134
- return false;
7135
- }
7136
- };
7137
- const IMPURE = {
7138
- hasEffectsWhenCalled() {
7139
- return true;
7140
- }
7141
- };
7142
- // We use shortened variables to reduce file size here
7143
- /* OBJECT */
7144
- const O = {
7145
- __proto__: null,
7146
- [ValueProperties]: IMPURE
7147
- };
7148
- /* PURE FUNCTION */
7149
- const PF = {
7150
- __proto__: null,
7151
- [ValueProperties]: PURE
7152
- };
7153
- /* FUNCTION THAT MUTATES FIRST ARG WITHOUT TRIGGERING ACCESSORS */
7154
- const MUTATES_ARG_WITHOUT_ACCESSOR = {
7155
- __proto__: null,
7156
- [ValueProperties]: {
7157
- hasEffectsWhenCalled(callOptions, context) {
7158
- return (!callOptions.args.length ||
7159
- 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));
7160
6596
  }
7161
6597
  }
7162
6598
  };
@@ -7973,227 +7409,688 @@ const knownGlobals = {
7973
7409
  for (const global of ['window', 'global', 'self', 'globalThis']) {
7974
7410
  knownGlobals[global] = knownGlobals;
7975
7411
  }
7976
- function getGlobalAtPath(path) {
7977
- let currentGlobal = knownGlobals;
7978
- for (const pathSegment of path) {
7979
- if (typeof pathSegment !== 'string') {
7980
- 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);
7981
7699
  }
7982
- currentGlobal = currentGlobal[pathSegment];
7983
- if (!currentGlobal) {
7984
- return null;
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);
7985
7735
  }
7986
7736
  }
7987
- return currentGlobal[ValueProperties];
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
+ }
7988
7787
  }
7989
7788
 
7990
- class GlobalVariable extends Variable {
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;
7797
+ }
7798
+ else {
7799
+ return super.addDeclaration(identifier, context, init, false);
7800
+ }
7801
+ }
7802
+ }
7803
+
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 {
7991
7831
  constructor() {
7992
7832
  super(...arguments);
7993
- // Ensure we use live-bindings for globals as we do not know if they have
7994
- // been reassigned
7995
- this.isReassigned = true;
7833
+ this.directlyIncluded = false;
7996
7834
  }
7997
- getLiteralValueAtPath(path, _recursionTracker, _origin) {
7998
- 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
+ }
7999
7840
  }
8000
- hasEffectsWhenAccessedAtPath(path) {
8001
- if (path.length === 0) {
8002
- // Technically, "undefined" is a global variable of sorts
8003
- 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;
8004
7854
  }
8005
- return !getGlobalAtPath([this.name, ...path].slice(0, -1));
7855
+ return false;
8006
7856
  }
8007
- hasEffectsWhenCalledAtPath(path, callOptions, context) {
8008
- const globalAtPath = getGlobalAtPath([this.name, ...path]);
8009
- 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
+ }
8010
7882
  }
8011
7883
  }
8012
7884
 
8013
- const tdzVariableKinds = {
8014
- __proto__: null,
8015
- class: true,
8016
- const: true,
8017
- let: true,
8018
- var: true
8019
- };
8020
- class Identifier extends NodeBase {
7885
+ class RestElement extends NodeBase {
8021
7886
  constructor() {
8022
7887
  super(...arguments);
8023
- this.variable = null;
8024
- this.isTDZAccess = null;
7888
+ this.declarationInit = null;
7889
+ }
7890
+ addExportedVariables(variables, exportNamesByVariable) {
7891
+ this.argument.addExportedVariables(variables, exportNamesByVariable);
7892
+ }
7893
+ declare(kind, init) {
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);
8025
7899
  }
8026
- addExportedVariables(variables, exportNamesByVariable) {
8027
- if (exportNamesByVariable.has(this.variable)) {
8028
- variables.push(this.variable);
8029
- }
7900
+ hasEffectsWhenAssignedAtPath(path, context) {
7901
+ return path.length > 0 || this.argument.hasEffectsWhenAssignedAtPath(EMPTY_PATH, context);
8030
7902
  }
8031
- bind() {
8032
- if (!this.variable && is_reference(this, this.parent)) {
8033
- this.variable = this.scope.findVariable(this.name);
8034
- this.variable.addReference(this);
8035
- }
7903
+ markDeclarationReached() {
7904
+ this.argument.markDeclarationReached();
8036
7905
  }
8037
- declare(kind, init) {
8038
- let variable;
8039
- const { treeshake } = this.context.options;
8040
- switch (kind) {
8041
- case 'var':
8042
- variable = this.scope.addDeclaration(this, this.context, init, true);
8043
- if (treeshake && treeshake.correctVarValueBeforeDeclaration) {
8044
- // Necessary to make sure the init is deoptimized. We cannot call deoptimizePath here.
8045
- variable.markInitializersForDeoptimization();
8046
- }
8047
- break;
8048
- case 'function':
8049
- // in strict mode, functions are only hoisted within a scope but not across block scopes
8050
- variable = this.scope.addDeclaration(this, this.context, init, false);
8051
- break;
8052
- case 'let':
8053
- case 'const':
8054
- case 'class':
8055
- variable = this.scope.addDeclaration(this, this.context, init, false);
8056
- break;
8057
- case 'parameter':
8058
- variable = this.scope.addParameterDeclaration(this);
8059
- break;
8060
- /* istanbul ignore next */
8061
- default:
8062
- /* istanbul ignore next */
8063
- throw new Error(`Internal Error: Unexpected identifier kind ${kind}.`);
7906
+ applyDeoptimizations() {
7907
+ this.deoptimized = true;
7908
+ if (this.declarationInit !== null) {
7909
+ this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
7910
+ this.context.requestTreeshakingPass();
8064
7911
  }
8065
- variable.kind = kind;
8066
- return [(this.variable = variable)];
8067
7912
  }
8068
- deoptimizeCallParameters() {
8069
- this.variable.deoptimizeCallParameters();
7913
+ }
7914
+
7915
+ class FunctionBase extends NodeBase {
7916
+ constructor() {
7917
+ super(...arguments);
7918
+ this.objectEntity = null;
7919
+ this.deoptimizedReturn = false;
8070
7920
  }
8071
7921
  deoptimizePath(path) {
8072
- var _a;
8073
- if (path.length === 0 && !this.scope.contains(this.name)) {
8074
- 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);
8075
7927
  }
8076
- // We keep conditional chaining because an unknown Node could have an
8077
- // Identifier as property that might be deoptimized by default
8078
- (_a = this.variable) === null || _a === void 0 ? void 0 : _a.deoptimizePath(path);
8079
7928
  }
8080
7929
  deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
8081
- this.variable.deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
7930
+ if (path.length > 0) {
7931
+ this.getObjectEntity().deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
7932
+ }
8082
7933
  }
8083
7934
  getLiteralValueAtPath(path, recursionTracker, origin) {
8084
- return this.getVariableRespectingTDZ().getLiteralValueAtPath(path, recursionTracker, origin);
7935
+ return this.getObjectEntity().getLiteralValueAtPath(path, recursionTracker, origin);
8085
7936
  }
8086
7937
  getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin) {
8087
- return this.getVariableRespectingTDZ().getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin);
8088
- }
8089
- hasEffects() {
8090
- if (!this.deoptimized)
8091
- this.applyDeoptimizations();
8092
- if (this.isPossibleTDZ() && this.variable.kind !== 'var') {
8093
- return true;
7938
+ if (path.length > 0) {
7939
+ return this.getObjectEntity().getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin);
8094
7940
  }
8095
- return (this.context.options.treeshake.unknownGlobalSideEffects &&
8096
- this.variable instanceof GlobalVariable &&
8097
- 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();
8098
7950
  }
8099
7951
  hasEffectsWhenAccessedAtPath(path, context) {
8100
- return (this.variable !== null &&
8101
- this.getVariableRespectingTDZ().hasEffectsWhenAccessedAtPath(path, context));
7952
+ return this.getObjectEntity().hasEffectsWhenAccessedAtPath(path, context);
8102
7953
  }
8103
7954
  hasEffectsWhenAssignedAtPath(path, context) {
8104
- return (path.length > 0 ? this.getVariableRespectingTDZ() : this.variable).hasEffectsWhenAssignedAtPath(path, context);
7955
+ return this.getObjectEntity().hasEffectsWhenAssignedAtPath(path, context);
8105
7956
  }
8106
7957
  hasEffectsWhenCalledAtPath(path, callOptions, context) {
8107
- 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;
8108
7977
  }
8109
- include() {
7978
+ include(context, includeChildrenRecursively) {
8110
7979
  if (!this.deoptimized)
8111
7980
  this.applyDeoptimizations();
8112
- if (!this.included) {
8113
- this.included = true;
8114
- if (this.variable !== null) {
8115
- this.context.includeVariableInModule(this.variable);
8116
- }
8117
- }
7981
+ this.included = true;
7982
+ const { brokenFlow } = context;
7983
+ context.brokenFlow = BROKEN_FLOW_NONE;
7984
+ this.body.include(context, includeChildrenRecursively);
7985
+ context.brokenFlow = brokenFlow;
8118
7986
  }
8119
7987
  includeCallArguments(context, args) {
8120
- this.variable.includeCallArguments(context, args);
7988
+ this.scope.includeCallArguments(context, args);
8121
7989
  }
8122
- isPossibleTDZ() {
8123
- // return cached value to avoid issues with the next tree-shaking pass
8124
- if (this.isTDZAccess !== null)
8125
- return this.isTDZAccess;
8126
- if (!(this.variable instanceof LocalVariable) ||
8127
- !this.variable.kind ||
8128
- !(this.variable.kind in tdzVariableKinds)) {
8129
- return (this.isTDZAccess = false);
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();
8130
7994
  }
8131
- let decl_id;
8132
- if (this.variable.declarations &&
8133
- this.variable.declarations.length === 1 &&
8134
- (decl_id = this.variable.declarations[0]) &&
8135
- this.start < decl_id.start &&
8136
- closestParentFunctionOrProgram(this) === closestParentFunctionOrProgram(decl_id)) {
8137
- // a variable accessed before its declaration
8138
- // in the same function or at top level of module
8139
- return (this.isTDZAccess = true);
7995
+ else {
7996
+ this.scope.addReturnExpression(this.body);
8140
7997
  }
8141
- if (!this.variable.initReached) {
8142
- // Either a const/let TDZ violation or
8143
- // var use before declaration was encountered.
8144
- 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);
8145
8002
  }
8146
- return (this.isTDZAccess = false);
8003
+ super.parseNode(esTreeNode);
8147
8004
  }
8148
- markDeclarationReached() {
8149
- 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;
8150
8013
  }
8151
- render(code, { snippets: { getPropertyAccess } }, { renderedParentType, isCalleeOfRenderedParent, isShorthandProperty } = BLANK) {
8152
- if (this.variable) {
8153
- const name = this.variable.getName(getPropertyAccess);
8154
- if (name !== this.name) {
8155
- code.overwrite(this.start, this.end, name, {
8156
- contentOnly: true,
8157
- storeName: true
8158
- });
8159
- if (isShorthandProperty) {
8160
- code.prependRight(this.start, `${this.name}: `);
8161
- }
8162
- }
8163
- // In strict mode, any variable named "eval" must be the actual "eval" function
8164
- if (name === 'eval' &&
8165
- renderedParentType === CallExpression$1 &&
8166
- isCalleeOfRenderedParent) {
8167
- 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);
8168
8043
  }
8169
8044
  }
8170
8045
  }
8171
- applyDeoptimizations() {
8172
- this.deoptimized = true;
8173
- if (this.variable instanceof LocalVariable) {
8174
- this.variable.consolidateInitializers();
8175
- this.context.requestTreeshakingPass();
8046
+ getObjectEntity() {
8047
+ if (this.objectEntity !== null) {
8048
+ return this.objectEntity;
8176
8049
  }
8050
+ return (this.objectEntity = new ObjectEntity([], OBJECT_PROTOTYPE));
8177
8051
  }
8178
- disallowImportReassignment() {
8179
- return this.context.error({
8180
- code: 'ILLEGAL_REASSIGNMENT',
8181
- message: `Illegal reassignment to import '${this.name}'`
8182
- }, 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})`;
8183
8059
  }
8184
- getVariableRespectingTDZ() {
8185
- if (this.isPossibleTDZ()) {
8186
- 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
+ }
8187
8066
  }
8188
- return this.variable;
8067
+ return `exports(${getObject(fields, { lineBreakIndent: null })})`;
8068
+ }
8069
+ }
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, ')');
8189
8085
  }
8190
8086
  }
8191
- function closestParentFunctionOrProgram(node) {
8192
- while (node && !/^Program|Function/.test(node.type)) {
8193
- node = node.parent;
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, ')');
8194
8093
  }
8195
- // one of: ArrowFunctionExpression, FunctionDeclaration, FunctionExpression or Program
8196
- return node;
8197
8094
  }
8198
8095
 
8199
8096
  class ObjectPattern extends NodeBase {
@@ -8318,6 +8215,46 @@ class AssignmentExpression extends NodeBase {
8318
8215
  }
8319
8216
  }
8320
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
+
8321
8258
  class ArgumentsVariable extends LocalVariable {
8322
8259
  constructor(context) {
8323
8260
  super('arguments', null, UNKNOWN_EXPRESSION, context);
@@ -8456,12 +8393,16 @@ class FunctionNode extends FunctionBase {
8456
8393
  context.ignore = ignore;
8457
8394
  return false;
8458
8395
  }
8459
- include(context, includeChildrenRecursively, { includeWithoutParameterDefaults } = BLANK) {
8396
+ include(context, includeChildrenRecursively) {
8460
8397
  var _a;
8398
+ super.include(context, includeChildrenRecursively);
8461
8399
  (_a = this.id) === null || _a === void 0 ? void 0 : _a.include();
8462
- super.include(context, includeChildrenRecursively, {
8463
- includeWithoutParameterDefaults: includeWithoutParameterDefaults && !this.scope.argumentsVariable.included
8464
- });
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
+ }
8465
8406
  }
8466
8407
  initialise() {
8467
8408
  var _a;
@@ -8590,6 +8531,31 @@ class BreakStatement extends NodeBase {
8590
8531
  }
8591
8532
  }
8592
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
+
8593
8559
  class Literal extends NodeBase {
8594
8560
  deoptimizeThisOnEventAtPath() { }
8595
8561
  getLiteralValueAtPath(path) {
@@ -9053,7 +9019,7 @@ class CallExpression extends CallExpressionBase {
9053
9019
  }
9054
9020
  else {
9055
9021
  this.included = true;
9056
- this.callee.include(context, false, { includeWithoutParameterDefaults: true });
9022
+ this.callee.include(context, false);
9057
9023
  }
9058
9024
  this.callee.includeCallArguments(context, this.arguments);
9059
9025
  const returnExpression = this.getReturnExpression();
@@ -9066,28 +9032,7 @@ class CallExpression extends CallExpressionBase {
9066
9032
  isCalleeOfRenderedParent: true,
9067
9033
  renderedSurroundingElement
9068
9034
  });
9069
- if (this.arguments.length > 0) {
9070
- if (this.arguments[this.arguments.length - 1].included) {
9071
- for (const arg of this.arguments) {
9072
- arg.render(code, options);
9073
- }
9074
- }
9075
- else {
9076
- let lastIncludedIndex = this.arguments.length - 2;
9077
- while (lastIncludedIndex >= 0 && !this.arguments[lastIncludedIndex].included) {
9078
- lastIncludedIndex--;
9079
- }
9080
- if (lastIncludedIndex >= 0) {
9081
- for (let index = 0; index <= lastIncludedIndex; index++) {
9082
- this.arguments[index].render(code, options);
9083
- }
9084
- code.remove(findFirstOccurrenceOutsideComment(code.original, ',', this.arguments[lastIncludedIndex].end), this.end - 1);
9085
- }
9086
- else {
9087
- code.remove(findFirstOccurrenceOutsideComment(code.original, '(', this.callee.end) + 1, this.end - 1);
9088
- }
9089
- }
9090
- }
9035
+ renderCallArguments(code, options, this);
9091
9036
  }
9092
9037
  applyDeoptimizations() {
9093
9038
  this.deoptimized = true;
@@ -9246,41 +9191,6 @@ class MethodDefinition extends MethodBase {
9246
9191
  applyDeoptimizations() { }
9247
9192
  }
9248
9193
 
9249
- class PropertyDefinition extends NodeBase {
9250
- deoptimizePath(path) {
9251
- var _a;
9252
- (_a = this.value) === null || _a === void 0 ? void 0 : _a.deoptimizePath(path);
9253
- }
9254
- deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
9255
- var _a;
9256
- (_a = this.value) === null || _a === void 0 ? void 0 : _a.deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
9257
- }
9258
- getLiteralValueAtPath(path, recursionTracker, origin) {
9259
- return this.value
9260
- ? this.value.getLiteralValueAtPath(path, recursionTracker, origin)
9261
- : UnknownValue;
9262
- }
9263
- getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin) {
9264
- return this.value
9265
- ? this.value.getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin)
9266
- : UNKNOWN_EXPRESSION;
9267
- }
9268
- hasEffects(context) {
9269
- var _a;
9270
- return this.key.hasEffects(context) || (this.static && !!((_a = this.value) === null || _a === void 0 ? void 0 : _a.hasEffects(context)));
9271
- }
9272
- hasEffectsWhenAccessedAtPath(path, context) {
9273
- return !this.value || this.value.hasEffectsWhenAccessedAtPath(path, context);
9274
- }
9275
- hasEffectsWhenAssignedAtPath(path, context) {
9276
- return !this.value || this.value.hasEffectsWhenAssignedAtPath(path, context);
9277
- }
9278
- hasEffectsWhenCalledAtPath(path, callOptions, context) {
9279
- return !this.value || this.value.hasEffectsWhenCalledAtPath(path, callOptions, context);
9280
- }
9281
- applyDeoptimizations() { }
9282
- }
9283
-
9284
9194
  class ObjectMember extends ExpressionEntity {
9285
9195
  constructor(object, key) {
9286
9196
  super();
@@ -9322,14 +9232,7 @@ class ClassNode extends NodeBase {
9322
9232
  this.getObjectEntity().deoptimizeAllProperties();
9323
9233
  }
9324
9234
  deoptimizePath(path) {
9325
- var _a, _b;
9326
9235
  this.getObjectEntity().deoptimizePath(path);
9327
- if (path.length === 1 && path[0] === UnknownKey) {
9328
- // A reassignment of UNKNOWN_PATH is considered equivalent to having lost track
9329
- // which means the constructor needs to be reassigned
9330
- (_a = this.classConstructor) === null || _a === void 0 ? void 0 : _a.deoptimizePath(UNKNOWN_PATH);
9331
- (_b = this.superClass) === null || _b === void 0 ? void 0 : _b.deoptimizePath(UNKNOWN_PATH);
9332
- }
9333
9236
  }
9334
9237
  deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
9335
9238
  this.getObjectEntity().deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
@@ -9391,7 +9294,6 @@ class ClassNode extends NodeBase {
9391
9294
  this.classConstructor = null;
9392
9295
  }
9393
9296
  applyDeoptimizations() {
9394
- var _a, _b;
9395
9297
  this.deoptimized = true;
9396
9298
  for (const definition of this.body.body) {
9397
9299
  if (!(definition.static ||
@@ -9399,11 +9301,7 @@ class ClassNode extends NodeBase {
9399
9301
  // Calls to methods are not tracked, ensure that the return value is deoptimized
9400
9302
  definition.deoptimizePath(UNKNOWN_PATH);
9401
9303
  }
9402
- else if (definition instanceof PropertyDefinition) {
9403
- (_a = definition.value) === null || _a === void 0 ? void 0 : _a.deoptimizeCallParameters();
9404
- }
9405
9304
  }
9406
- (_b = this.superClass) === null || _b === void 0 ? void 0 : _b.deoptimizeCallParameters();
9407
9305
  this.context.requestTreeshakingPass();
9408
9306
  }
9409
9307
  getObjectEntity() {
@@ -9737,9 +9635,6 @@ class ExportAllDeclaration extends NodeBase {
9737
9635
  ExportAllDeclaration.prototype.needsBoundaries = true;
9738
9636
 
9739
9637
  class FunctionDeclaration extends FunctionNode {
9740
- include(context, includeChildrenRecursively) {
9741
- super.include(context, includeChildrenRecursively, { includeWithoutParameterDefaults: true });
9742
- }
9743
9638
  initialise() {
9744
9639
  super.initialise();
9745
9640
  if (this.id !== null) {
@@ -10625,13 +10520,16 @@ class LogicalExpression extends NodeBase {
10625
10520
  this.usedBranch = null;
10626
10521
  }
10627
10522
  deoptimizeCache() {
10628
- if (this.usedBranch !== null) {
10523
+ if (this.usedBranch) {
10629
10524
  const unusedBranch = this.usedBranch === this.left ? this.right : this.left;
10630
10525
  this.usedBranch = null;
10631
10526
  unusedBranch.deoptimizePath(UNKNOWN_PATH);
10632
10527
  for (const expression of this.expressionsToBeDeoptimized) {
10633
10528
  expression.deoptimizeCache();
10634
10529
  }
10530
+ // Request another pass because we need to ensure "include" runs again if
10531
+ // it is rendered
10532
+ this.context.requestTreeshakingPass();
10635
10533
  }
10636
10534
  }
10637
10535
  deoptimizePath(path) {
@@ -10964,6 +10862,10 @@ class NewExpression extends NodeBase {
10964
10862
  withNew: true
10965
10863
  };
10966
10864
  }
10865
+ render(code, options) {
10866
+ this.callee.render(code, options);
10867
+ renderCallArguments(code, options, this);
10868
+ }
10967
10869
  applyDeoptimizations() {
10968
10870
  this.deoptimized = true;
10969
10871
  for (const argument of this.arguments) {
@@ -10974,42 +10876,6 @@ class NewExpression extends NodeBase {
10974
10876
  }
10975
10877
  }
10976
10878
 
10977
- class Property extends MethodBase {
10978
- constructor() {
10979
- super(...arguments);
10980
- this.declarationInit = null;
10981
- }
10982
- declare(kind, init) {
10983
- this.declarationInit = init;
10984
- return this.value.declare(kind, UNKNOWN_EXPRESSION);
10985
- }
10986
- hasEffects(context) {
10987
- if (!this.deoptimized)
10988
- this.applyDeoptimizations();
10989
- const propertyReadSideEffects = this.context.options.treeshake
10990
- .propertyReadSideEffects;
10991
- return ((this.parent.type === 'ObjectPattern' && propertyReadSideEffects === 'always') ||
10992
- this.key.hasEffects(context) ||
10993
- this.value.hasEffects(context));
10994
- }
10995
- markDeclarationReached() {
10996
- this.value.markDeclarationReached();
10997
- }
10998
- render(code, options) {
10999
- if (!this.shorthand) {
11000
- this.key.render(code, options);
11001
- }
11002
- this.value.render(code, options, { isShorthandProperty: this.shorthand });
11003
- }
11004
- applyDeoptimizations() {
11005
- this.deoptimized = true;
11006
- if (this.declarationInit !== null) {
11007
- this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
11008
- this.context.requestTreeshakingPass();
11009
- }
11010
- }
11011
- }
11012
-
11013
10879
  class ObjectExpression extends NodeBase {
11014
10880
  constructor() {
11015
10881
  super(...arguments);
@@ -11047,14 +10913,7 @@ class ObjectExpression extends NodeBase {
11047
10913
  code.prependLeft(this.end, ')');
11048
10914
  }
11049
10915
  }
11050
- applyDeoptimizations() {
11051
- this.deoptimized = true;
11052
- for (const property of this.properties) {
11053
- if (property instanceof Property) {
11054
- property.value.deoptimizeCallParameters();
11055
- }
11056
- }
11057
- }
10916
+ applyDeoptimizations() { }
11058
10917
  getObjectEntity() {
11059
10918
  if (this.objectEntity !== null) {
11060
10919
  return this.objectEntity;
@@ -11134,6 +10993,77 @@ class Program extends NodeBase {
11134
10993
  applyDeoptimizations() { }
11135
10994
  }
11136
10995
 
10996
+ class Property extends MethodBase {
10997
+ constructor() {
10998
+ super(...arguments);
10999
+ this.declarationInit = null;
11000
+ }
11001
+ declare(kind, init) {
11002
+ this.declarationInit = init;
11003
+ return this.value.declare(kind, UNKNOWN_EXPRESSION);
11004
+ }
11005
+ hasEffects(context) {
11006
+ if (!this.deoptimized)
11007
+ this.applyDeoptimizations();
11008
+ const propertyReadSideEffects = this.context.options.treeshake
11009
+ .propertyReadSideEffects;
11010
+ return ((this.parent.type === 'ObjectPattern' && propertyReadSideEffects === 'always') ||
11011
+ this.key.hasEffects(context) ||
11012
+ this.value.hasEffects(context));
11013
+ }
11014
+ markDeclarationReached() {
11015
+ this.value.markDeclarationReached();
11016
+ }
11017
+ render(code, options) {
11018
+ if (!this.shorthand) {
11019
+ this.key.render(code, options);
11020
+ }
11021
+ this.value.render(code, options, { isShorthandProperty: this.shorthand });
11022
+ }
11023
+ applyDeoptimizations() {
11024
+ this.deoptimized = true;
11025
+ if (this.declarationInit !== null) {
11026
+ this.declarationInit.deoptimizePath([UnknownKey, UnknownKey]);
11027
+ this.context.requestTreeshakingPass();
11028
+ }
11029
+ }
11030
+ }
11031
+
11032
+ class PropertyDefinition extends NodeBase {
11033
+ deoptimizePath(path) {
11034
+ var _a;
11035
+ (_a = this.value) === null || _a === void 0 ? void 0 : _a.deoptimizePath(path);
11036
+ }
11037
+ deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker) {
11038
+ var _a;
11039
+ (_a = this.value) === null || _a === void 0 ? void 0 : _a.deoptimizeThisOnEventAtPath(event, path, thisParameter, recursionTracker);
11040
+ }
11041
+ getLiteralValueAtPath(path, recursionTracker, origin) {
11042
+ return this.value
11043
+ ? this.value.getLiteralValueAtPath(path, recursionTracker, origin)
11044
+ : UnknownValue;
11045
+ }
11046
+ getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin) {
11047
+ return this.value
11048
+ ? this.value.getReturnExpressionWhenCalledAtPath(path, callOptions, recursionTracker, origin)
11049
+ : UNKNOWN_EXPRESSION;
11050
+ }
11051
+ hasEffects(context) {
11052
+ var _a;
11053
+ return this.key.hasEffects(context) || (this.static && !!((_a = this.value) === null || _a === void 0 ? void 0 : _a.hasEffects(context)));
11054
+ }
11055
+ hasEffectsWhenAccessedAtPath(path, context) {
11056
+ return !this.value || this.value.hasEffectsWhenAccessedAtPath(path, context);
11057
+ }
11058
+ hasEffectsWhenAssignedAtPath(path, context) {
11059
+ return !this.value || this.value.hasEffectsWhenAssignedAtPath(path, context);
11060
+ }
11061
+ hasEffectsWhenCalledAtPath(path, callOptions, context) {
11062
+ return !this.value || this.value.hasEffectsWhenCalledAtPath(path, callOptions, context);
11063
+ }
11064
+ applyDeoptimizations() { }
11065
+ }
11066
+
11137
11067
  class ReturnStatement extends NodeBase {
11138
11068
  hasEffects(context) {
11139
11069
  var _a;
@@ -12002,9 +11932,7 @@ class VariableDeclarator extends NodeBase {
12002
11932
  include(context, includeChildrenRecursively) {
12003
11933
  var _a;
12004
11934
  this.included = true;
12005
- (_a = this.init) === null || _a === void 0 ? void 0 : _a.include(context, includeChildrenRecursively, {
12006
- includeWithoutParameterDefaults: true
12007
- });
11935
+ (_a = this.init) === null || _a === void 0 ? void 0 : _a.include(context, includeChildrenRecursively);
12008
11936
  this.id.markDeclarationReached();
12009
11937
  if (includeChildrenRecursively || this.id.shouldBeIncluded(context)) {
12010
11938
  this.id.include(context, includeChildrenRecursively);
@@ -12059,7 +11987,7 @@ class YieldExpression extends NodeBase {
12059
11987
  var _a;
12060
11988
  if (!this.deoptimized)
12061
11989
  this.applyDeoptimizations();
12062
- 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)));
12063
11991
  }
12064
11992
  render(code, options) {
12065
11993
  if (this.argument) {