rollup 4.3.1 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v4.3.1
4
- Sat, 11 Nov 2023 07:57:25 GMT - commit 52c55bb1e17154ae6d01fb40e0e4a3589bc20a8f
3
+ Rollup.js v4.4.0
4
+ Sun, 12 Nov 2023 07:49:26 GMT - commit 53d636051ac60da9b302c4bd6b7eaaccb4871f4b
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
8
8
  Released under the MIT License.
9
9
  */
10
- import { normalize, getImportPath, LOGLEVEL_WARN, logUnusedExternalImports, ANNOTATION_KEY, INVALID_ANNOTATION_KEY, logInvalidAnnotation, logModuleLevelDirective, logIllegalImportReassignment, logMissingExport, logCannotCallNamespace, logEval, LOGLEVEL_INFO, logFirstSideEffect, locate, logThisIsUndefined, logImportAttributeIsInvalid, logImportOptionsAreInvalid, error, logSyntheticNamedExportsNeedNamespaceExport, logInvalidFormatForTopLevelAwait, logInvalidSourcemapForError, augmentCodeLocation, logInconsistentImportAttributes, logNamespaceConflict, logAmbiguousExternalNamespaces, logShimmedExport, parseAst, logModuleParseError, parseAstAsync, logCircularReexport, logMissingNodeBuiltins, logIllegalIdentifierAsName, logMissingNameOptionForIifeExport, logMissingNameOptionForUmdExport, logAddonNotGenerated, logIncompatibleExportOptionValue, logMixedExport, logFailedValidation, isPathFragment, logCyclicCrossChunkReexport, getAliasName, logUnexpectedNamedImport, isAbsolute as isAbsolute$1, relative as relative$1, logUnexpectedNamespaceReexport, logEmptyChunk, logMissingGlobalName, logOptimizeChunkStatus, logSourcemapBroken, logConflictingSourcemapSources, logChunkInvalid, logInvalidOption, URL_OUTPUT_FORMAT, URL_OUTPUT_DIR, URL_OUTPUT_SOURCEMAPFILE, URL_OUTPUT_AMD_ID, logCannotAssignModuleToChunk, logAnonymousPluginCache, logDuplicatePluginName, LOGLEVEL_ERROR, logLevelPriority, relativeId, LOGLEVEL_DEBUG, logUnknownOption, printQuotedStringList, logInvalidSetAssetSourceCall, logPluginError, logNoTransformMapOrAstWithoutCode, logBadLoader, logExternalModulesCannotBeTransformedToModules, logInternalIdCannotBeExternal, isRelative, logUnresolvedImport, logUnresolvedImportTreatedAsExternal, logExternalSyntheticExports, logUnresolvedEntry, logUnresolvedImplicitDependant, logExternalModulesCannotBeIncludedInManualChunks, logEntryCannotBeExternal, logImplicitDependantCannotBeExternal, logNoAssetSourceSet, logFileReferenceIdNotFoundForFilename, logAssetReferenceIdNotFoundForSetSource, logAssetSourceAlreadySet, logInvalidRollupPhaseForChunkEmission, logFileNameConflict, logAssetNotFinalisedForFileName, logChunkNotGeneratedForFileName, logInvalidLogPosition, logInvalidRollupPhaseForAddWatchFile, logInputHookInOutputPlugin, logInvalidFunctionPluginHook, logInvalidAddonPluginHook, logImplicitDependantIsNotIncluded, logCircularDependency, URL_TREESHAKE_MODULESIDEEFFECTS, URL_TREESHAKE, URL_OUTPUT_INLINEDYNAMICIMPORTS, URL_PRESERVEENTRYSIGNATURES, URL_OUTPUT_AMD_BASEPATH, logInvalidExportOptionValue, warnDeprecation, URL_OUTPUT_INTEROP, URL_OUTPUT_MANUALCHUNKS, isValidUrl, addTrailingSlashIfMissed, URL_OUTPUT_SOURCEMAPBASEURL, URL_OUTPUT_GENERATEDCODE, URL_OUTPUT_EXTERNALIMPORTATTRIBUTES, logAlreadyClosed, logMissingFileOrDirOption, logCannotEmitFromOptionsHook, URL_WATCH } from './parseAst.js';
10
+ import { normalize, getImportPath, LOGLEVEL_WARN, logUnusedExternalImports, ANNOTATION_KEY, INVALID_ANNOTATION_KEY, logInvalidAnnotation, logRedeclarationError, logDuplicateArgumentNameError, logIllegalImportReassignment, logModuleLevelDirective, logMissingExport, logCannotCallNamespace, logEval, LOGLEVEL_INFO, logFirstSideEffect, locate, logThisIsUndefined, logImportAttributeIsInvalid, logImportOptionsAreInvalid, error, logSyntheticNamedExportsNeedNamespaceExport, logMissingEntryExport, logInvalidFormatForTopLevelAwait, logDuplicateExportError, logInvalidSourcemapForError, augmentCodeLocation, logInconsistentImportAttributes, logNamespaceConflict, logAmbiguousExternalNamespaces, logShimmedExport, parseAst, logModuleParseError, parseAstAsync, logCircularReexport, logMissingNodeBuiltins, logIllegalIdentifierAsName, logMissingNameOptionForIifeExport, logMissingNameOptionForUmdExport, logAddonNotGenerated, logIncompatibleExportOptionValue, logMixedExport, logFailedValidation, isPathFragment, logCyclicCrossChunkReexport, getAliasName, logUnexpectedNamedImport, isAbsolute as isAbsolute$1, relative as relative$1, logUnexpectedNamespaceReexport, logEmptyChunk, logMissingGlobalName, logOptimizeChunkStatus, logSourcemapBroken, logConflictingSourcemapSources, logChunkInvalid, logInvalidOption, URL_OUTPUT_FORMAT, URL_OUTPUT_DIR, URL_OUTPUT_SOURCEMAPFILE, URL_OUTPUT_AMD_ID, logCannotAssignModuleToChunk, logAnonymousPluginCache, logDuplicatePluginName, LOGLEVEL_ERROR, logLevelPriority, relativeId, LOGLEVEL_DEBUG, logUnknownOption, printQuotedStringList, logInvalidSetAssetSourceCall, logPluginError, logNoTransformMapOrAstWithoutCode, logBadLoader, logExternalModulesCannotBeTransformedToModules, logInternalIdCannotBeExternal, isRelative, logUnresolvedImport, logUnresolvedImportTreatedAsExternal, logExternalSyntheticExports, logUnresolvedEntry, logUnresolvedImplicitDependant, logExternalModulesCannotBeIncludedInManualChunks, logEntryCannotBeExternal, logImplicitDependantCannotBeExternal, logNoAssetSourceSet, logFileReferenceIdNotFoundForFilename, logAssetReferenceIdNotFoundForSetSource, logAssetSourceAlreadySet, logInvalidRollupPhaseForChunkEmission, logFileNameConflict, logAssetNotFinalisedForFileName, logChunkNotGeneratedForFileName, logInvalidLogPosition, logInvalidRollupPhaseForAddWatchFile, logInputHookInOutputPlugin, logInvalidFunctionPluginHook, logInvalidAddonPluginHook, logImplicitDependantIsNotIncluded, logCircularDependency, URL_TREESHAKE_MODULESIDEEFFECTS, URL_TREESHAKE, URL_OUTPUT_INLINEDYNAMICIMPORTS, URL_PRESERVEENTRYSIGNATURES, URL_OUTPUT_AMD_BASEPATH, logInvalidExportOptionValue, warnDeprecation, URL_OUTPUT_INTEROP, URL_OUTPUT_MANUALCHUNKS, isValidUrl, addTrailingSlashIfMissed, URL_OUTPUT_SOURCEMAPBASEURL, URL_OUTPUT_GENERATEDCODE, URL_OUTPUT_EXTERNALIMPORTATTRIBUTES, logAlreadyClosed, logMissingFileOrDirOption, logCannotEmitFromOptionsHook, URL_WATCH } from './parseAst.js';
11
11
  import { relative, dirname, basename, extname, resolve as resolve$1 } from 'node:path';
12
12
  import require$$0$1, { win32, posix, isAbsolute, resolve } from 'path';
13
13
  import process$1, { env as env$1 } from 'node:process';
@@ -16,7 +16,7 @@ import { xxhashBase64Url } from '../../native.js';
16
16
  import { lstat, realpath, readdir, readFile, mkdir, writeFile } from 'node:fs/promises';
17
17
  import * as tty from 'tty';
18
18
 
19
- var version = "4.3.1";
19
+ var version = "4.4.0";
20
20
 
21
21
  const comma = ','.charCodeAt(0);
22
22
  const semicolon = ';'.charCodeAt(0);
@@ -1917,12 +1917,18 @@ class Variable extends ExpressionEntity {
1917
1917
  (this.forbiddenNames ||= new Set()).add(name);
1918
1918
  }
1919
1919
  getBaseVariableName() {
1920
- return this.renderBaseName || this.renderName || this.name;
1920
+ return (this.renderedLikeHoisted?.getBaseVariableName() ||
1921
+ this.renderBaseName ||
1922
+ this.renderName ||
1923
+ this.name);
1921
1924
  }
1922
1925
  getName(getPropertyAccess, useOriginalName) {
1923
1926
  if (useOriginalName?.(this)) {
1924
1927
  return this.name;
1925
1928
  }
1929
+ if (this.renderedLikeHoisted) {
1930
+ return this.renderedLikeHoisted.getName(getPropertyAccess, useOriginalName);
1931
+ }
1926
1932
  const name = this.renderName || this.name;
1927
1933
  return this.renderBaseName ? `${this.renderBaseName}${getPropertyAccess(name)}` : name;
1928
1934
  }
@@ -1937,6 +1943,14 @@ class Variable extends ExpressionEntity {
1937
1943
  */
1938
1944
  include() {
1939
1945
  this.included = true;
1946
+ this.renderedLikeHoisted?.include();
1947
+ }
1948
+ /**
1949
+ * Links the rendered name of this variable to another variable and includes
1950
+ * this variable if the other variable is included.
1951
+ */
1952
+ renderLikeHoisted(variable) {
1953
+ this.renderedLikeHoisted = variable;
1940
1954
  }
1941
1955
  markCalledFromTryStatement() { }
1942
1956
  setRenderNames(baseName, name) {
@@ -1962,10 +1976,8 @@ class ExternalVariable extends Variable {
1962
1976
  return type !== INTERACTION_ACCESSED || path.length > (this.isNamespace ? 1 : 0);
1963
1977
  }
1964
1978
  include() {
1965
- if (!this.included) {
1966
- this.included = true;
1967
- this.module.used = true;
1968
- }
1979
+ super.include();
1980
+ this.module.used = true;
1969
1981
  }
1970
1982
  }
1971
1983
 
@@ -5495,6 +5507,7 @@ class ArrayPattern extends NodeBase {
5495
5507
  const ArrowFunctionExpression$1 = 'ArrowFunctionExpression';
5496
5508
  const BlockStatement$1 = 'BlockStatement';
5497
5509
  const CallExpression$1 = 'CallExpression';
5510
+ const CatchClause$1 = 'CatchClause';
5498
5511
  const ExpressionStatement$1 = 'ExpressionStatement';
5499
5512
  const Identifier$1 = 'Identifier';
5500
5513
  const Program$1 = 'Program';
@@ -5502,7 +5515,7 @@ const Property$1 = 'Property';
5502
5515
  const ReturnStatement$1 = 'ReturnStatement';
5503
5516
 
5504
5517
  class LocalVariable extends Variable {
5505
- constructor(name, declarator, init, context) {
5518
+ constructor(name, declarator, init, context, kind) {
5506
5519
  super(name);
5507
5520
  this.init = init;
5508
5521
  this.calledFromTryStatement = false;
@@ -5511,6 +5524,7 @@ class LocalVariable extends Variable {
5511
5524
  this.declarations = declarator ? [declarator] : [];
5512
5525
  this.deoptimizationTracker = context.deoptimizationTracker;
5513
5526
  this.module = context.module;
5527
+ this.kind = kind;
5514
5528
  }
5515
5529
  addDeclaration(identifier, init) {
5516
5530
  this.declarations.push(identifier);
@@ -5537,15 +5551,13 @@ class LocalVariable extends Variable {
5537
5551
  return;
5538
5552
  }
5539
5553
  if (path.length === 0) {
5540
- if (!this.isReassigned) {
5541
- this.isReassigned = true;
5542
- const expressionsToBeDeoptimized = this.expressionsToBeDeoptimized;
5543
- this.expressionsToBeDeoptimized = EMPTY_ARRAY;
5544
- for (const expression of expressionsToBeDeoptimized) {
5545
- expression.deoptimizeCache();
5546
- }
5547
- this.init.deoptimizePath(UNKNOWN_PATH);
5554
+ this.isReassigned = true;
5555
+ const expressionsToBeDeoptimized = this.expressionsToBeDeoptimized;
5556
+ this.expressionsToBeDeoptimized = EMPTY_ARRAY;
5557
+ for (const expression of expressionsToBeDeoptimized) {
5558
+ expression.deoptimizeCache();
5548
5559
  }
5560
+ this.init.deoptimizePath(UNKNOWN_PATH);
5549
5561
  }
5550
5562
  else {
5551
5563
  this.init.deoptimizePath(path);
@@ -5597,7 +5609,7 @@ class LocalVariable extends Variable {
5597
5609
  }
5598
5610
  include() {
5599
5611
  if (!this.included) {
5600
- this.included = true;
5612
+ super.include();
5601
5613
  for (const declaration of this.declarations) {
5602
5614
  // If node is a default export, it can save a tree-shaking run to include the full declaration now
5603
5615
  if (!declaration.included)
@@ -5637,19 +5649,6 @@ class LocalVariable extends Variable {
5637
5649
  }
5638
5650
  return this.additionalInitializers;
5639
5651
  }
5640
- mergeDeclarations(variable) {
5641
- const { declarations } = this;
5642
- for (const declaration of variable.declarations) {
5643
- declarations.push(declaration);
5644
- }
5645
- const additionalInitializers = this.markInitializersForDeoptimization();
5646
- additionalInitializers.push(variable.init);
5647
- if (variable.additionalInitializers) {
5648
- for (const initializer of variable.additionalInitializers) {
5649
- additionalInitializers.push(initializer);
5650
- }
5651
- }
5652
- }
5653
5652
  }
5654
5653
 
5655
5654
  const MAX_TRACKED_INTERACTIONS = 20;
@@ -5659,7 +5658,7 @@ const EMPTY_PATH_TRACKER = new PathTracker();
5659
5658
  const UNKNOWN_DEOPTIMIZED_ENTITY = new Set([UNKNOWN_EXPRESSION]);
5660
5659
  class ParameterVariable extends LocalVariable {
5661
5660
  constructor(name, declarator, context) {
5662
- super(name, declarator, UNKNOWN_EXPRESSION, context);
5661
+ super(name, declarator, UNKNOWN_EXPRESSION, context, "parameter" /* VariableKind.parameter */);
5663
5662
  this.deoptimizationInteractions = [];
5664
5663
  this.deoptimizations = new PathTracker();
5665
5664
  this.deoptimizedFields = new Set();
@@ -5779,22 +5778,42 @@ class Scope {
5779
5778
  this.children = [];
5780
5779
  this.variables = new Map();
5781
5780
  }
5782
- addDeclaration(identifier, context, init, _isHoisted) {
5781
+ /*
5782
+ Redeclaration rules:
5783
+ - var can redeclare var
5784
+ - var is hoisted across scopes, function remains in the scope it is declared
5785
+ - var and function can redeclare function parameters, but parameters cannot redeclare parameters
5786
+ - function cannot redeclare catch scope parameters
5787
+ - var can redeclare catch scope parameters in a way
5788
+ - if the parameter is an identifier and not a pattern
5789
+ - then the variable is still declared in the hoisted outer scope, but the initializer is assigned to the parameter
5790
+ - const, let, class, and function except in the cases above cannot redeclare anything
5791
+ */
5792
+ addDeclaration(identifier, context, init, kind) {
5783
5793
  const name = identifier.name;
5784
- let variable = this.variables.get(name);
5785
- if (variable) {
5786
- variable.addDeclaration(identifier, init);
5787
- }
5788
- else {
5789
- variable = new LocalVariable(identifier.name, identifier, init || UNDEFINED_EXPRESSION, context);
5790
- this.variables.set(name, variable);
5794
+ const existingVariable = this.hoistedVariables?.get(name) || this.variables.get(name);
5795
+ if (existingVariable) {
5796
+ const existingKind = existingVariable.kind;
5797
+ if ((kind === "var" /* VariableKind.var */ &&
5798
+ (existingKind === "var" /* VariableKind.var */ || existingKind === "parameter" /* VariableKind.parameter */)) ||
5799
+ (kind === "function" /* VariableKind.function */ && existingKind === "parameter" /* VariableKind.parameter */)) {
5800
+ existingVariable.addDeclaration(identifier, init);
5801
+ return existingVariable;
5802
+ }
5803
+ context.error(logRedeclarationError(name), identifier.start);
5791
5804
  }
5792
- return variable;
5805
+ const newVariable = new LocalVariable(identifier.name, identifier, init, context, kind);
5806
+ this.variables.set(name, newVariable);
5807
+ return newVariable;
5808
+ }
5809
+ addHoistedVariable(name, variable) {
5810
+ (this.hoistedVariables ||= new Map()).set(name, variable);
5793
5811
  }
5794
5812
  contains(name) {
5795
5813
  return this.variables.has(name);
5796
5814
  }
5797
5815
  findVariable(_name) {
5816
+ /* istanbul ignore next */
5798
5817
  throw new Error('Internal Error: findVariable needs to be implemented by a subclass');
5799
5818
  }
5800
5819
  }
@@ -5802,9 +5821,9 @@ class Scope {
5802
5821
  class ChildScope extends Scope {
5803
5822
  constructor(parent, context) {
5804
5823
  super();
5805
- this.accessedOutsideVariables = new Map();
5806
5824
  this.parent = parent;
5807
5825
  this.context = context;
5826
+ this.accessedOutsideVariables = new Map();
5808
5827
  parent.children.push(this);
5809
5828
  }
5810
5829
  addAccessedDynamicImport(importExpression) {
@@ -5882,26 +5901,89 @@ class ChildScope extends Scope {
5882
5901
  }
5883
5902
  }
5884
5903
 
5885
- class ParameterScope extends ChildScope {
5904
+ class CatchBodyScope extends ChildScope {
5886
5905
  constructor(parent, context) {
5906
+ super(parent, context);
5907
+ this.parent = parent;
5908
+ this.context = context;
5909
+ }
5910
+ addDeclaration(identifier, context, init, kind) {
5911
+ if (kind === "var" /* VariableKind.var */) {
5912
+ const name = identifier.name;
5913
+ const existingVariable = this.hoistedVariables?.get(name) || this.variables.get(name);
5914
+ if (existingVariable) {
5915
+ const existingKind = existingVariable.kind;
5916
+ if (existingKind === "parameter" /* VariableKind.parameter */ &&
5917
+ // If this is a destructured parameter, it is forbidden to redeclare
5918
+ existingVariable.declarations[0].parent.type === CatchClause$1) {
5919
+ // If this is a var with the same name as the catch scope parameter,
5920
+ // the assignment actually goes to the parameter and the var is
5921
+ // hoisted without assignment. Locally, it is shadowed by the
5922
+ // parameter
5923
+ const declaredVariable = this.parent.parent.addDeclaration(identifier, context, UNDEFINED_EXPRESSION, kind);
5924
+ // To avoid the need to rewrite the declaration, we link the variable
5925
+ // names. If we ever implement a logic that splits initialization and
5926
+ // assignment for hoisted vars, the "renderLikeHoisted" logic can be
5927
+ // removed again.
5928
+ // We do not need to check whether there already is a linked
5929
+ // variable because then declaredVariable would be that linked
5930
+ // variable.
5931
+ existingVariable.renderLikeHoisted(declaredVariable);
5932
+ this.addHoistedVariable(name, declaredVariable);
5933
+ return declaredVariable;
5934
+ }
5935
+ if (existingKind === "var" /* VariableKind.var */) {
5936
+ existingVariable.addDeclaration(identifier, init);
5937
+ return existingVariable;
5938
+ }
5939
+ return context.error(logRedeclarationError(name), identifier.start);
5940
+ }
5941
+ // We only add parameters to parameter scopes
5942
+ const declaredVariable = this.parent.parent.addDeclaration(identifier, context, init, kind);
5943
+ // Necessary to make sure the init is deoptimized for conditional declarations.
5944
+ // We cannot call deoptimizePath here.
5945
+ declaredVariable.markInitializersForDeoptimization();
5946
+ // We add the variable to this and all parent scopes to reliably detect conflicts
5947
+ this.addHoistedVariable(name, declaredVariable);
5948
+ return declaredVariable;
5949
+ }
5950
+ // Functions can never re-declare catch parameters
5951
+ if (kind === "function" /* VariableKind.function */) {
5952
+ const name = identifier.name;
5953
+ if (this.hoistedVariables?.get(name)) {
5954
+ context.error(logRedeclarationError(name), identifier.start);
5955
+ }
5956
+ }
5957
+ return super.addDeclaration(identifier, context, init, kind);
5958
+ }
5959
+ }
5960
+
5961
+ class ParameterScope extends ChildScope {
5962
+ constructor(parent, context, isCatchScope) {
5887
5963
  super(parent, context);
5888
5964
  this.parameters = [];
5889
5965
  this.hasRest = false;
5890
- this.hoistedBodyVarScope = new ChildScope(this, context);
5966
+ this.bodyScope = isCatchScope
5967
+ ? new CatchBodyScope(this, context)
5968
+ : new ChildScope(this, context);
5891
5969
  }
5892
5970
  /**
5893
5971
  * Adds a parameter to this scope. Parameters must be added in the correct
5894
5972
  * order, i.e. from left to right.
5895
5973
  */
5896
5974
  addParameterDeclaration(identifier) {
5897
- const { name } = identifier;
5898
- const variable = new ParameterVariable(name, identifier, this.context);
5899
- const localVariable = this.hoistedBodyVarScope.variables.get(name);
5900
- if (localVariable) {
5901
- this.hoistedBodyVarScope.variables.set(name, variable);
5902
- variable.mergeDeclarations(localVariable);
5975
+ const { name, start } = identifier;
5976
+ const existingParameter = this.variables.get(name);
5977
+ if (existingParameter) {
5978
+ return this.context.error(logDuplicateArgumentNameError(name), start);
5903
5979
  }
5980
+ const variable = new ParameterVariable(name, identifier, this.context);
5904
5981
  this.variables.set(name, variable);
5982
+ // We also add it to the body scope to detect name conflicts with local
5983
+ // variables. We still need the intermediate scope, though, as parameter
5984
+ // defaults are NOT taken from the body scope but from the parameters or
5985
+ // outside scope.
5986
+ this.bodyScope.addHoistedVariable(name, variable);
5905
5987
  return variable;
5906
5988
  }
5907
5989
  addParameterVariables(parameters, hasRest) {
@@ -7087,26 +7169,26 @@ class Identifier extends NodeBase {
7087
7169
  let variable;
7088
7170
  const { treeshake } = this.scope.context.options;
7089
7171
  switch (kind) {
7090
- case 'var': {
7091
- variable = this.scope.addDeclaration(this, this.scope.context, init, true);
7172
+ case "var" /* VariableKind.var */: {
7173
+ variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
7092
7174
  if (treeshake && treeshake.correctVarValueBeforeDeclaration) {
7093
7175
  // Necessary to make sure the init is deoptimized. We cannot call deoptimizePath here.
7094
7176
  variable.markInitializersForDeoptimization();
7095
7177
  }
7096
7178
  break;
7097
7179
  }
7098
- case 'function': {
7180
+ case "function" /* VariableKind.function */: {
7099
7181
  // in strict mode, functions are only hoisted within a scope but not across block scopes
7100
- variable = this.scope.addDeclaration(this, this.scope.context, init, false);
7182
+ variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
7101
7183
  break;
7102
7184
  }
7103
- case 'let':
7104
- case 'const':
7105
- case 'class': {
7106
- variable = this.scope.addDeclaration(this, this.scope.context, init, false);
7185
+ case "let" /* VariableKind.let */:
7186
+ case "const" /* VariableKind.const */:
7187
+ case "class" /* VariableKind.class */: {
7188
+ variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
7107
7189
  break;
7108
7190
  }
7109
- case 'parameter': {
7191
+ case "parameter" /* VariableKind.parameter */: {
7110
7192
  variable = this.scope.addParameterDeclaration(this);
7111
7193
  break;
7112
7194
  }
@@ -7116,13 +7198,15 @@ class Identifier extends NodeBase {
7116
7198
  throw new Error(`Internal Error: Unexpected identifier kind ${kind}.`);
7117
7199
  }
7118
7200
  }
7119
- variable.kind = kind;
7120
7201
  return [(this.variable = variable)];
7121
7202
  }
7122
7203
  deoptimizeArgumentsOnInteractionAtPath(interaction, path, recursionTracker) {
7123
7204
  this.variable.deoptimizeArgumentsOnInteractionAtPath(interaction, path, recursionTracker);
7124
7205
  }
7125
7206
  deoptimizePath(path) {
7207
+ if (path.length === 0 && !this.scope.contains(this.name)) {
7208
+ this.disallowImportReassignment();
7209
+ }
7126
7210
  // We keep conditional chaining because an unknown Node could have an
7127
7211
  // Identifier as property that might be deoptimized by default
7128
7212
  this.variable?.deoptimizePath(path);
@@ -7137,7 +7221,7 @@ class Identifier extends NodeBase {
7137
7221
  hasEffects(context) {
7138
7222
  if (!this.deoptimized)
7139
7223
  this.applyDeoptimizations();
7140
- if (this.isPossibleTDZ() && this.variable.kind !== 'var') {
7224
+ if (this.isPossibleTDZ() && this.variable.kind !== "var" /* VariableKind.var */) {
7141
7225
  return true;
7142
7226
  }
7143
7227
  return (this.scope.context.options.treeshake
@@ -7228,6 +7312,9 @@ class Identifier extends NodeBase {
7228
7312
  }
7229
7313
  }
7230
7314
  }
7315
+ disallowImportReassignment() {
7316
+ return this.scope.context.error(logIllegalImportReassignment(this.name, this.scope.context.module.id), this.start);
7317
+ }
7231
7318
  applyDeoptimizations() {
7232
7319
  this.deoptimized = true;
7233
7320
  if (this.variable instanceof LocalVariable) {
@@ -7403,17 +7490,26 @@ function removeLineBreaks(code, start, end) {
7403
7490
  }
7404
7491
 
7405
7492
  class BlockScope extends ChildScope {
7406
- addDeclaration(identifier, context, init, isHoisted) {
7407
- if (isHoisted) {
7408
- const variable = this.parent.addDeclaration(identifier, context, init, isHoisted);
7493
+ addDeclaration(identifier, context, init, kind) {
7494
+ if (kind === "var" /* VariableKind.var */) {
7495
+ const name = identifier.name;
7496
+ const existingVariable = this.hoistedVariables?.get(name) || this.variables.get(name);
7497
+ if (existingVariable) {
7498
+ if (existingVariable.kind === "var" /* VariableKind.var */) {
7499
+ existingVariable.addDeclaration(identifier, init);
7500
+ return existingVariable;
7501
+ }
7502
+ return context.error(logRedeclarationError(name), identifier.start);
7503
+ }
7504
+ const declaredVariable = this.parent.addDeclaration(identifier, context, init, kind);
7409
7505
  // Necessary to make sure the init is deoptimized for conditional declarations.
7410
7506
  // We cannot call deoptimizePath here.
7411
- variable.markInitializersForDeoptimization();
7412
- return variable;
7413
- }
7414
- else {
7415
- return super.addDeclaration(identifier, context, init, false);
7507
+ declaredVariable.markInitializersForDeoptimization();
7508
+ // We add the variable to this and all parent scopes to reliably detect conflicts
7509
+ this.addHoistedVariable(name, declaredVariable);
7510
+ return declaredVariable;
7416
7511
  }
7512
+ return super.addDeclaration(identifier, context, init, kind);
7417
7513
  }
7418
7514
  }
7419
7515
 
@@ -7651,7 +7747,6 @@ class FunctionBase extends NodeBase {
7651
7747
  this.scope.includeCallArguments(context, parameters);
7652
7748
  }
7653
7749
  initialise() {
7654
- this.scope.addParameterVariables(this.params.map(parameter => parameter.declare('parameter', UNKNOWN_EXPRESSION)), this.params[this.params.length - 1] instanceof RestElement);
7655
7750
  if (this.body instanceof BlockStatement) {
7656
7751
  this.body.addImplicitReturnExpressionToScope();
7657
7752
  }
@@ -7660,9 +7755,18 @@ class FunctionBase extends NodeBase {
7660
7755
  }
7661
7756
  }
7662
7757
  parseNode(esTreeNode) {
7663
- if (esTreeNode.body.type === BlockStatement$1) {
7664
- this.body = new BlockStatement(esTreeNode.body, this, this.scope.hoistedBodyVarScope);
7665
- }
7758
+ const { body, params } = esTreeNode;
7759
+ const parameters = (this.params = []);
7760
+ const { scope } = this;
7761
+ const { bodyScope, context } = scope;
7762
+ // We need to ensure that parameters are declared before the body is parsed
7763
+ // so that the scope already knows all parameters and can detect conflicts
7764
+ // when parsing the body.
7765
+ for (const parameter of params) {
7766
+ parameters.push(new (context.getNodeConstructor(parameter.type))(parameter, this, scope, false));
7767
+ }
7768
+ scope.addParameterVariables(parameters.map(parameter => parameter.declare("parameter" /* VariableKind.parameter */, UNKNOWN_EXPRESSION)), parameters[parameters.length - 1] instanceof RestElement);
7769
+ this.body = new (context.getNodeConstructor(body.type))(body, this, bodyScope);
7666
7770
  super.parseNode(esTreeNode);
7667
7771
  }
7668
7772
  addArgumentToBeDeoptimized(_argument) { }
@@ -7676,7 +7780,7 @@ class ArrowFunctionExpression extends FunctionBase {
7676
7780
  this.objectEntity = null;
7677
7781
  }
7678
7782
  createScope(parentScope) {
7679
- this.scope = new ReturnValueScope(parentScope, this.scope.context);
7783
+ this.scope = new ReturnValueScope(parentScope, this.scope.context, false);
7680
7784
  }
7681
7785
  hasEffects() {
7682
7786
  if (!this.deoptimized)
@@ -7920,7 +8024,7 @@ class AssignmentPattern extends NodeBase {
7920
8024
 
7921
8025
  class ArgumentsVariable extends LocalVariable {
7922
8026
  constructor(context) {
7923
- super('arguments', null, UNKNOWN_EXPRESSION, context);
8027
+ super('arguments', null, UNKNOWN_EXPRESSION, context, "other" /* VariableKind.other */);
7924
8028
  this.deoptimizedArguments = [];
7925
8029
  }
7926
8030
  addArgumentToBeDeoptimized(argument) {
@@ -7954,7 +8058,7 @@ class ThisVariable extends ParameterVariable {
7954
8058
 
7955
8059
  class FunctionScope extends ReturnValueScope {
7956
8060
  constructor(parent, context) {
7957
- super(parent, context);
8061
+ super(parent, context, false);
7958
8062
  this.variables.set('arguments', (this.argumentsVariable = new ArgumentsVariable(context)));
7959
8063
  this.variables.set('this', (this.thisVariable = new ThisVariable(context)));
7960
8064
  }
@@ -8042,7 +8146,7 @@ class FunctionNode extends FunctionBase {
8042
8146
  }
8043
8147
  initialise() {
8044
8148
  super.initialise();
8045
- this.id?.declare('function', this);
8149
+ this.id?.declare("function" /* VariableKind.function */, this);
8046
8150
  }
8047
8151
  addArgumentToBeDeoptimized(argument) {
8048
8152
  this.scope.argumentsVariable.addArgumentToBeDeoptimized(argument);
@@ -8767,37 +8871,22 @@ class CallExpression extends CallExpressionBase {
8767
8871
  }
8768
8872
  }
8769
8873
 
8770
- class CatchScope extends ParameterScope {
8771
- addDeclaration(identifier, context, init, isHoisted) {
8772
- const existingParameter = this.variables.get(identifier.name);
8773
- if (existingParameter) {
8774
- // While we still create a hoisted declaration, the initializer goes to
8775
- // the parameter. Note that technically, the declaration now belongs to
8776
- // two variables, which is not correct but should not cause issues.
8777
- this.parent.addDeclaration(identifier, context, UNDEFINED_EXPRESSION, isHoisted);
8778
- existingParameter.addDeclaration(identifier, init);
8779
- return existingParameter;
8780
- }
8781
- return this.parent.addDeclaration(identifier, context, init, isHoisted);
8782
- }
8783
- }
8784
-
8785
8874
  class CatchClause extends NodeBase {
8786
8875
  createScope(parentScope) {
8787
- this.scope = new CatchScope(parentScope, this.scope.context);
8876
+ this.scope = new ParameterScope(parentScope, this.scope.context, true);
8788
8877
  }
8789
8878
  parseNode(esTreeNode) {
8790
- // Parameters need to be declared first as the logic is that initializers
8791
- // of hoisted body variables are associated with parameters of the same
8792
- // name instead of the variable
8793
- const { param } = esTreeNode;
8879
+ const { body, param, type } = esTreeNode;
8880
+ this.type = type;
8794
8881
  if (param) {
8795
8882
  this.param = new (this.scope.context.getNodeConstructor(param.type))(param, this, this.scope);
8796
- this.param.declare('parameter', UNKNOWN_EXPRESSION);
8883
+ this.param.declare("parameter" /* VariableKind.parameter */, UNKNOWN_EXPRESSION);
8797
8884
  }
8885
+ this.body = new BlockStatement(body, this, this.scope.bodyScope);
8798
8886
  super.parseNode(esTreeNode);
8799
8887
  }
8800
8888
  }
8889
+ CatchClause.prototype.preventChildBlockScope = true;
8801
8890
 
8802
8891
  class ChainExpression extends NodeBase {
8803
8892
  // deoptimizations are not relevant as we are not caching values
@@ -8820,7 +8909,7 @@ class ChainExpression extends NodeBase {
8820
8909
  class ClassBodyScope extends ChildScope {
8821
8910
  constructor(parent, classNode, context) {
8822
8911
  super(parent, context);
8823
- this.variables.set('this', (this.thisVariable = new LocalVariable('this', null, classNode, context)));
8912
+ this.variables.set('this', (this.thisVariable = new LocalVariable('this', null, classNode, context, "other" /* VariableKind.other */)));
8824
8913
  this.instanceScope = new ChildScope(this, context);
8825
8914
  this.instanceScope.variables.set('this', new ThisVariable(context));
8826
8915
  }
@@ -9004,7 +9093,7 @@ class ClassNode extends NodeBase {
9004
9093
  }
9005
9094
  }
9006
9095
  initialise() {
9007
- this.id?.declare('class', this);
9096
+ this.id?.declare("class" /* VariableKind.class */, this);
9008
9097
  for (const method of this.body.body) {
9009
9098
  if (method instanceof MethodDefinition && method.kind === 'constructor') {
9010
9099
  this.classConstructor = method;
@@ -9644,9 +9733,9 @@ class TrackingScope extends BlockScope {
9644
9733
  super(...arguments);
9645
9734
  this.hoistedDeclarations = [];
9646
9735
  }
9647
- addDeclaration(identifier, context, init, isHoisted) {
9736
+ addDeclaration(identifier, context, init, kind) {
9648
9737
  this.hoistedDeclarations.push(identifier);
9649
- return super.addDeclaration(identifier, context, init, isHoisted);
9738
+ return super.addDeclaration(identifier, context, init, kind);
9650
9739
  }
9651
9740
  }
9652
9741
 
@@ -11372,7 +11461,7 @@ class UndefinedVariable extends Variable {
11372
11461
 
11373
11462
  class ExportDefaultVariable extends LocalVariable {
11374
11463
  constructor(name, exportDefaultDeclaration, context) {
11375
- super(name, exportDefaultDeclaration, exportDefaultDeclaration.declaration, context);
11464
+ super(name, exportDefaultDeclaration, exportDefaultDeclaration.declaration, context, "other" /* VariableKind.other */);
11376
11465
  this.hasId = false;
11377
11466
  this.originalId = null;
11378
11467
  this.originalVariable = null;
@@ -11443,7 +11532,13 @@ class ExportDefaultVariable extends LocalVariable {
11443
11532
  class ModuleScope extends ChildScope {
11444
11533
  constructor(parent, context) {
11445
11534
  super(parent, context);
11446
- this.variables.set('this', new LocalVariable('this', null, UNDEFINED_EXPRESSION, context));
11535
+ this.variables.set('this', new LocalVariable('this', null, UNDEFINED_EXPRESSION, context, "other" /* VariableKind.other */));
11536
+ }
11537
+ addDeclaration(identifier, context, init, kind) {
11538
+ if (this.context.module.importDescriptions.has(identifier.name)) {
11539
+ context.error(logRedeclarationError(identifier.name), identifier.start);
11540
+ }
11541
+ return super.addDeclaration(identifier, context, init, kind);
11447
11542
  }
11448
11543
  addExportDefaultDeclaration(name, exportDefaultDeclaration, context) {
11449
11544
  const variable = new ExportDefaultVariable(name, exportDefaultDeclaration, context);
@@ -12040,7 +12135,7 @@ class NamespaceVariable extends Variable {
12040
12135
  memberVariable.hasEffectsOnInteractionAtPath(path.slice(1), interaction, context));
12041
12136
  }
12042
12137
  include() {
12043
- this.included = true;
12138
+ super.include();
12044
12139
  this.context.includeAllExports();
12045
12140
  }
12046
12141
  prepare(accessedGlobalsByScope) {
@@ -12134,7 +12229,7 @@ class SyntheticNamedExportVariable extends Variable {
12134
12229
  return `${this.syntheticNamespace.getName(getPropertyAccess)}${getPropertyAccess(this.name)}`;
12135
12230
  }
12136
12231
  include() {
12137
- this.included = true;
12232
+ super.include();
12138
12233
  this.context.includeVariableInModule(this.syntheticNamespace);
12139
12234
  }
12140
12235
  setRenderNames(baseName, name) {
@@ -12743,7 +12838,7 @@ class Module {
12743
12838
  const removedExports = [];
12744
12839
  for (const exportName of this.exports.keys()) {
12745
12840
  const [variable] = this.getVariableForExportName(exportName);
12746
- (variable && variable.included ? renderedExports : removedExports).push(exportName);
12841
+ (variable?.included ? renderedExports : removedExports).push(exportName);
12747
12842
  }
12748
12843
  return { removedExports, renderedExports };
12749
12844
  }
@@ -12840,6 +12935,9 @@ class Module {
12840
12935
  for (const exportName of this.exports.keys()) {
12841
12936
  if (includeNamespaceMembers || exportName !== this.info.syntheticNamedExports) {
12842
12937
  const variable = this.getVariableForExportName(exportName)[0];
12938
+ if (!variable) {
12939
+ return error(logMissingEntryExport(exportName, this.id));
12940
+ }
12843
12941
  variable.deoptimizePath(UNKNOWN_PATH);
12844
12942
  if (!variable.included) {
12845
12943
  this.includeVariable(variable);
@@ -13077,9 +13175,15 @@ class Module {
13077
13175
  }
13078
13176
  this.dynamicImports.push({ argument, id: null, node, resolution: null });
13079
13177
  }
13178
+ assertUniqueExportName(name, nodeStart) {
13179
+ if (this.exports.has(name) || this.reexportDescriptions.has(name)) {
13180
+ this.error(logDuplicateExportError(name), nodeStart);
13181
+ }
13182
+ }
13080
13183
  addExport(node) {
13081
13184
  if (node instanceof ExportDefaultDeclaration) {
13082
13185
  // export default foo;
13186
+ this.assertUniqueExportName('default', node.start);
13083
13187
  this.exports.set('default', {
13084
13188
  identifier: node.variable.getAssignedVariableName(),
13085
13189
  localName: 'default'
@@ -13091,6 +13195,7 @@ class Module {
13091
13195
  if (node.exported) {
13092
13196
  // export * as name from './other'
13093
13197
  const name = node.exported.name;
13198
+ this.assertUniqueExportName(name, node.exported.start);
13094
13199
  this.reexportDescriptions.set(name, {
13095
13200
  localName: '*',
13096
13201
  module: null,
@@ -13109,6 +13214,7 @@ class Module {
13109
13214
  this.addSource(source, node);
13110
13215
  for (const { exported, local, start } of node.specifiers) {
13111
13216
  const name = exported instanceof Literal ? exported.value : exported.name;
13217
+ this.assertUniqueExportName(name, start);
13112
13218
  this.reexportDescriptions.set(name, {
13113
13219
  localName: local instanceof Literal ? local.value : local.name,
13114
13220
  module: null,
@@ -13124,6 +13230,7 @@ class Module {
13124
13230
  // export var foo = 1, bar = 2;
13125
13231
  for (const declarator of declaration.declarations) {
13126
13232
  for (const localName of extractAssignedNames(declarator.id)) {
13233
+ this.assertUniqueExportName(localName, declarator.id.start);
13127
13234
  this.exports.set(localName, { identifier: null, localName });
13128
13235
  }
13129
13236
  }
@@ -13131,6 +13238,7 @@ class Module {
13131
13238
  else {
13132
13239
  // export function foo () {}
13133
13240
  const localName = declaration.id.name;
13241
+ this.assertUniqueExportName(localName, declaration.id.start);
13134
13242
  this.exports.set(localName, { identifier: null, localName });
13135
13243
  }
13136
13244
  }
@@ -13140,6 +13248,7 @@ class Module {
13140
13248
  // except for reexports, local must be an Identifier
13141
13249
  const localName = local.name;
13142
13250
  const exportedName = exported instanceof Identifier ? exported.name : exported.value;
13251
+ this.assertUniqueExportName(exportedName, exported.start);
13143
13252
  this.exports.set(exportedName, { identifier: null, localName });
13144
13253
  }
13145
13254
  }
@@ -13148,6 +13257,10 @@ class Module {
13148
13257
  const source = node.source.value;
13149
13258
  this.addSource(source, node);
13150
13259
  for (const specifier of node.specifiers) {
13260
+ const localName = specifier.local.name;
13261
+ if (this.scope.variables.has(localName) || this.importDescriptions.has(localName)) {
13262
+ this.error(logRedeclarationError(localName), specifier.local.start);
13263
+ }
13151
13264
  const name = specifier instanceof ImportDefaultSpecifier
13152
13265
  ? 'default'
13153
13266
  : specifier instanceof ImportNamespaceSpecifier
@@ -13155,7 +13268,7 @@ class Module {
13155
13268
  : specifier.imported instanceof Identifier
13156
13269
  ? specifier.imported.name
13157
13270
  : specifier.imported.value;
13158
- this.importDescriptions.set(specifier.local.name, {
13271
+ this.importDescriptions.set(localName, {
13159
13272
  module: null,
13160
13273
  name,
13161
13274
  source,