rollup 4.3.1 → 4.4.1

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.1
4
+ Tue, 14 Nov 2023 05:24:21 GMT - commit 01d8c9d1b68919c2c429427ae7e60f503a8bb5f4
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.1";
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,41 @@ 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
+ - in function scopes, function and var can redeclare function and var
5785
+ - var is hoisted across scopes, function remains in the scope it is declared
5786
+ - var and function can redeclare function parameters, but parameters cannot redeclare parameters
5787
+ - function cannot redeclare catch scope parameters
5788
+ - var can redeclare catch scope parameters in a way
5789
+ - if the parameter is an identifier and not a pattern
5790
+ - then the variable is still declared in the hoisted outer scope, but the initializer is assigned to the parameter
5791
+ - const, let, class, and function except in the cases above cannot redeclare anything
5792
+ */
5793
+ addDeclaration(identifier, context, init, kind) {
5783
5794
  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);
5795
+ const existingVariable = this.hoistedVariables?.get(name) || this.variables.get(name);
5796
+ if (existingVariable) {
5797
+ const existingKind = existingVariable.kind;
5798
+ if (kind === "var" /* VariableKind.var */ && existingKind === "var" /* VariableKind.var */) {
5799
+ existingVariable.addDeclaration(identifier, init);
5800
+ return existingVariable;
5801
+ }
5802
+ context.error(logRedeclarationError(name), identifier.start);
5791
5803
  }
5792
- return variable;
5804
+ const newVariable = new LocalVariable(identifier.name, identifier, init, context, kind);
5805
+ this.variables.set(name, newVariable);
5806
+ return newVariable;
5807
+ }
5808
+ addHoistedVariable(name, variable) {
5809
+ (this.hoistedVariables ||= new Map()).set(name, variable);
5793
5810
  }
5794
5811
  contains(name) {
5795
5812
  return this.variables.has(name);
5796
5813
  }
5797
5814
  findVariable(_name) {
5815
+ /* istanbul ignore next */
5798
5816
  throw new Error('Internal Error: findVariable needs to be implemented by a subclass');
5799
5817
  }
5800
5818
  }
@@ -5802,9 +5820,9 @@ class Scope {
5802
5820
  class ChildScope extends Scope {
5803
5821
  constructor(parent, context) {
5804
5822
  super();
5805
- this.accessedOutsideVariables = new Map();
5806
5823
  this.parent = parent;
5807
5824
  this.context = context;
5825
+ this.accessedOutsideVariables = new Map();
5808
5826
  parent.children.push(this);
5809
5827
  }
5810
5828
  addAccessedDynamicImport(importExpression) {
@@ -5882,26 +5900,110 @@ class ChildScope extends Scope {
5882
5900
  }
5883
5901
  }
5884
5902
 
5885
- class ParameterScope extends ChildScope {
5903
+ class CatchBodyScope extends ChildScope {
5886
5904
  constructor(parent, context) {
5905
+ super(parent, context);
5906
+ this.parent = parent;
5907
+ this.context = context;
5908
+ }
5909
+ addDeclaration(identifier, context, init, kind) {
5910
+ if (kind === "var" /* VariableKind.var */) {
5911
+ const name = identifier.name;
5912
+ const existingVariable = this.hoistedVariables?.get(name) || this.variables.get(name);
5913
+ if (existingVariable) {
5914
+ const existingKind = existingVariable.kind;
5915
+ if (existingKind === "parameter" /* VariableKind.parameter */ &&
5916
+ // If this is a destructured parameter, it is forbidden to redeclare
5917
+ existingVariable.declarations[0].parent.type === CatchClause$1) {
5918
+ // If this is a var with the same name as the catch scope parameter,
5919
+ // the assignment actually goes to the parameter and the var is
5920
+ // hoisted without assignment. Locally, it is shadowed by the
5921
+ // parameter
5922
+ const declaredVariable = this.parent.parent.addDeclaration(identifier, context, UNDEFINED_EXPRESSION, kind);
5923
+ // To avoid the need to rewrite the declaration, we link the variable
5924
+ // names. If we ever implement a logic that splits initialization and
5925
+ // assignment for hoisted vars, the "renderLikeHoisted" logic can be
5926
+ // removed again.
5927
+ // We do not need to check whether there already is a linked
5928
+ // variable because then declaredVariable would be that linked
5929
+ // variable.
5930
+ existingVariable.renderLikeHoisted(declaredVariable);
5931
+ this.addHoistedVariable(name, declaredVariable);
5932
+ return declaredVariable;
5933
+ }
5934
+ if (existingKind === "var" /* VariableKind.var */) {
5935
+ existingVariable.addDeclaration(identifier, init);
5936
+ return existingVariable;
5937
+ }
5938
+ return context.error(logRedeclarationError(name), identifier.start);
5939
+ }
5940
+ // We only add parameters to parameter scopes
5941
+ const declaredVariable = this.parent.parent.addDeclaration(identifier, context, init, kind);
5942
+ // Necessary to make sure the init is deoptimized for conditional declarations.
5943
+ // We cannot call deoptimizePath here.
5944
+ declaredVariable.markInitializersForDeoptimization();
5945
+ // We add the variable to this and all parent scopes to reliably detect conflicts
5946
+ this.addHoistedVariable(name, declaredVariable);
5947
+ return declaredVariable;
5948
+ }
5949
+ return super.addDeclaration(identifier, context, init, kind);
5950
+ }
5951
+ }
5952
+
5953
+ class FunctionBodyScope extends ChildScope {
5954
+ constructor(parent, context) {
5955
+ super(parent, context);
5956
+ this.parent = parent;
5957
+ this.context = context;
5958
+ }
5959
+ // There is stuff that is only allowed in function scopes, i.e. functions can
5960
+ // be redeclared, functions and var can redeclare each other
5961
+ addDeclaration(identifier, context, init, kind) {
5962
+ const name = identifier.name;
5963
+ const existingVariable = this.hoistedVariables?.get(name) || this.variables.get(name);
5964
+ if (existingVariable) {
5965
+ const existingKind = existingVariable.kind;
5966
+ if ((kind === "var" /* VariableKind.var */ || kind === "function" /* VariableKind.function */) &&
5967
+ (existingKind === "var" /* VariableKind.var */ ||
5968
+ existingKind === "function" /* VariableKind.function */ ||
5969
+ existingKind === "parameter" /* VariableKind.parameter */)) {
5970
+ existingVariable.addDeclaration(identifier, init);
5971
+ return existingVariable;
5972
+ }
5973
+ context.error(logRedeclarationError(name), identifier.start);
5974
+ }
5975
+ const newVariable = new LocalVariable(identifier.name, identifier, init, context, kind);
5976
+ this.variables.set(name, newVariable);
5977
+ return newVariable;
5978
+ }
5979
+ }
5980
+
5981
+ class ParameterScope extends ChildScope {
5982
+ constructor(parent, context, isCatchScope) {
5887
5983
  super(parent, context);
5888
5984
  this.parameters = [];
5889
5985
  this.hasRest = false;
5890
- this.hoistedBodyVarScope = new ChildScope(this, context);
5986
+ this.bodyScope = isCatchScope
5987
+ ? new CatchBodyScope(this, context)
5988
+ : new FunctionBodyScope(this, context);
5891
5989
  }
5892
5990
  /**
5893
5991
  * Adds a parameter to this scope. Parameters must be added in the correct
5894
5992
  * order, i.e. from left to right.
5895
5993
  */
5896
5994
  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);
5995
+ const { name, start } = identifier;
5996
+ const existingParameter = this.variables.get(name);
5997
+ if (existingParameter) {
5998
+ return this.context.error(logDuplicateArgumentNameError(name), start);
5903
5999
  }
6000
+ const variable = new ParameterVariable(name, identifier, this.context);
5904
6001
  this.variables.set(name, variable);
6002
+ // We also add it to the body scope to detect name conflicts with local
6003
+ // variables. We still need the intermediate scope, though, as parameter
6004
+ // defaults are NOT taken from the body scope but from the parameters or
6005
+ // outside scope.
6006
+ this.bodyScope.addHoistedVariable(name, variable);
5905
6007
  return variable;
5906
6008
  }
5907
6009
  addParameterVariables(parameters, hasRest) {
@@ -7087,26 +7189,26 @@ class Identifier extends NodeBase {
7087
7189
  let variable;
7088
7190
  const { treeshake } = this.scope.context.options;
7089
7191
  switch (kind) {
7090
- case 'var': {
7091
- variable = this.scope.addDeclaration(this, this.scope.context, init, true);
7192
+ case "var" /* VariableKind.var */: {
7193
+ variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
7092
7194
  if (treeshake && treeshake.correctVarValueBeforeDeclaration) {
7093
7195
  // Necessary to make sure the init is deoptimized. We cannot call deoptimizePath here.
7094
7196
  variable.markInitializersForDeoptimization();
7095
7197
  }
7096
7198
  break;
7097
7199
  }
7098
- case 'function': {
7200
+ case "function" /* VariableKind.function */: {
7099
7201
  // 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);
7202
+ variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
7101
7203
  break;
7102
7204
  }
7103
- case 'let':
7104
- case 'const':
7105
- case 'class': {
7106
- variable = this.scope.addDeclaration(this, this.scope.context, init, false);
7205
+ case "let" /* VariableKind.let */:
7206
+ case "const" /* VariableKind.const */:
7207
+ case "class" /* VariableKind.class */: {
7208
+ variable = this.scope.addDeclaration(this, this.scope.context, init, kind);
7107
7209
  break;
7108
7210
  }
7109
- case 'parameter': {
7211
+ case "parameter" /* VariableKind.parameter */: {
7110
7212
  variable = this.scope.addParameterDeclaration(this);
7111
7213
  break;
7112
7214
  }
@@ -7116,13 +7218,15 @@ class Identifier extends NodeBase {
7116
7218
  throw new Error(`Internal Error: Unexpected identifier kind ${kind}.`);
7117
7219
  }
7118
7220
  }
7119
- variable.kind = kind;
7120
7221
  return [(this.variable = variable)];
7121
7222
  }
7122
7223
  deoptimizeArgumentsOnInteractionAtPath(interaction, path, recursionTracker) {
7123
7224
  this.variable.deoptimizeArgumentsOnInteractionAtPath(interaction, path, recursionTracker);
7124
7225
  }
7125
7226
  deoptimizePath(path) {
7227
+ if (path.length === 0 && !this.scope.contains(this.name)) {
7228
+ this.disallowImportReassignment();
7229
+ }
7126
7230
  // We keep conditional chaining because an unknown Node could have an
7127
7231
  // Identifier as property that might be deoptimized by default
7128
7232
  this.variable?.deoptimizePath(path);
@@ -7137,7 +7241,7 @@ class Identifier extends NodeBase {
7137
7241
  hasEffects(context) {
7138
7242
  if (!this.deoptimized)
7139
7243
  this.applyDeoptimizations();
7140
- if (this.isPossibleTDZ() && this.variable.kind !== 'var') {
7244
+ if (this.isPossibleTDZ() && this.variable.kind !== "var" /* VariableKind.var */) {
7141
7245
  return true;
7142
7246
  }
7143
7247
  return (this.scope.context.options.treeshake
@@ -7228,6 +7332,9 @@ class Identifier extends NodeBase {
7228
7332
  }
7229
7333
  }
7230
7334
  }
7335
+ disallowImportReassignment() {
7336
+ return this.scope.context.error(logIllegalImportReassignment(this.name, this.scope.context.module.id), this.start);
7337
+ }
7231
7338
  applyDeoptimizations() {
7232
7339
  this.deoptimized = true;
7233
7340
  if (this.variable instanceof LocalVariable) {
@@ -7403,17 +7510,26 @@ function removeLineBreaks(code, start, end) {
7403
7510
  }
7404
7511
 
7405
7512
  class BlockScope extends ChildScope {
7406
- addDeclaration(identifier, context, init, isHoisted) {
7407
- if (isHoisted) {
7408
- const variable = this.parent.addDeclaration(identifier, context, init, isHoisted);
7513
+ addDeclaration(identifier, context, init, kind) {
7514
+ if (kind === "var" /* VariableKind.var */) {
7515
+ const name = identifier.name;
7516
+ const existingVariable = this.hoistedVariables?.get(name) || this.variables.get(name);
7517
+ if (existingVariable) {
7518
+ if (existingVariable.kind === "var" /* VariableKind.var */) {
7519
+ existingVariable.addDeclaration(identifier, init);
7520
+ return existingVariable;
7521
+ }
7522
+ return context.error(logRedeclarationError(name), identifier.start);
7523
+ }
7524
+ const declaredVariable = this.parent.addDeclaration(identifier, context, init, kind);
7409
7525
  // Necessary to make sure the init is deoptimized for conditional declarations.
7410
7526
  // We cannot call deoptimizePath here.
7411
- variable.markInitializersForDeoptimization();
7412
- return variable;
7413
- }
7414
- else {
7415
- return super.addDeclaration(identifier, context, init, false);
7527
+ declaredVariable.markInitializersForDeoptimization();
7528
+ // We add the variable to this and all parent scopes to reliably detect conflicts
7529
+ this.addHoistedVariable(name, declaredVariable);
7530
+ return declaredVariable;
7416
7531
  }
7532
+ return super.addDeclaration(identifier, context, init, kind);
7417
7533
  }
7418
7534
  }
7419
7535
 
@@ -7651,7 +7767,6 @@ class FunctionBase extends NodeBase {
7651
7767
  this.scope.includeCallArguments(context, parameters);
7652
7768
  }
7653
7769
  initialise() {
7654
- this.scope.addParameterVariables(this.params.map(parameter => parameter.declare('parameter', UNKNOWN_EXPRESSION)), this.params[this.params.length - 1] instanceof RestElement);
7655
7770
  if (this.body instanceof BlockStatement) {
7656
7771
  this.body.addImplicitReturnExpressionToScope();
7657
7772
  }
@@ -7660,9 +7775,18 @@ class FunctionBase extends NodeBase {
7660
7775
  }
7661
7776
  }
7662
7777
  parseNode(esTreeNode) {
7663
- if (esTreeNode.body.type === BlockStatement$1) {
7664
- this.body = new BlockStatement(esTreeNode.body, this, this.scope.hoistedBodyVarScope);
7665
- }
7778
+ const { body, params } = esTreeNode;
7779
+ const parameters = (this.params = []);
7780
+ const { scope } = this;
7781
+ const { bodyScope, context } = scope;
7782
+ // We need to ensure that parameters are declared before the body is parsed
7783
+ // so that the scope already knows all parameters and can detect conflicts
7784
+ // when parsing the body.
7785
+ for (const parameter of params) {
7786
+ parameters.push(new (context.getNodeConstructor(parameter.type))(parameter, this, scope, false));
7787
+ }
7788
+ scope.addParameterVariables(parameters.map(parameter => parameter.declare("parameter" /* VariableKind.parameter */, UNKNOWN_EXPRESSION)), parameters[parameters.length - 1] instanceof RestElement);
7789
+ this.body = new (context.getNodeConstructor(body.type))(body, this, bodyScope);
7666
7790
  super.parseNode(esTreeNode);
7667
7791
  }
7668
7792
  addArgumentToBeDeoptimized(_argument) { }
@@ -7676,7 +7800,7 @@ class ArrowFunctionExpression extends FunctionBase {
7676
7800
  this.objectEntity = null;
7677
7801
  }
7678
7802
  createScope(parentScope) {
7679
- this.scope = new ReturnValueScope(parentScope, this.scope.context);
7803
+ this.scope = new ReturnValueScope(parentScope, this.scope.context, false);
7680
7804
  }
7681
7805
  hasEffects() {
7682
7806
  if (!this.deoptimized)
@@ -7920,7 +8044,7 @@ class AssignmentPattern extends NodeBase {
7920
8044
 
7921
8045
  class ArgumentsVariable extends LocalVariable {
7922
8046
  constructor(context) {
7923
- super('arguments', null, UNKNOWN_EXPRESSION, context);
8047
+ super('arguments', null, UNKNOWN_EXPRESSION, context, "other" /* VariableKind.other */);
7924
8048
  this.deoptimizedArguments = [];
7925
8049
  }
7926
8050
  addArgumentToBeDeoptimized(argument) {
@@ -7954,7 +8078,7 @@ class ThisVariable extends ParameterVariable {
7954
8078
 
7955
8079
  class FunctionScope extends ReturnValueScope {
7956
8080
  constructor(parent, context) {
7957
- super(parent, context);
8081
+ super(parent, context, false);
7958
8082
  this.variables.set('arguments', (this.argumentsVariable = new ArgumentsVariable(context)));
7959
8083
  this.variables.set('this', (this.thisVariable = new ThisVariable(context)));
7960
8084
  }
@@ -8042,7 +8166,7 @@ class FunctionNode extends FunctionBase {
8042
8166
  }
8043
8167
  initialise() {
8044
8168
  super.initialise();
8045
- this.id?.declare('function', this);
8169
+ this.id?.declare("function" /* VariableKind.function */, this);
8046
8170
  }
8047
8171
  addArgumentToBeDeoptimized(argument) {
8048
8172
  this.scope.argumentsVariable.addArgumentToBeDeoptimized(argument);
@@ -8767,37 +8891,22 @@ class CallExpression extends CallExpressionBase {
8767
8891
  }
8768
8892
  }
8769
8893
 
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
8894
  class CatchClause extends NodeBase {
8786
8895
  createScope(parentScope) {
8787
- this.scope = new CatchScope(parentScope, this.scope.context);
8896
+ this.scope = new ParameterScope(parentScope, this.scope.context, true);
8788
8897
  }
8789
8898
  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;
8899
+ const { body, param, type } = esTreeNode;
8900
+ this.type = type;
8794
8901
  if (param) {
8795
8902
  this.param = new (this.scope.context.getNodeConstructor(param.type))(param, this, this.scope);
8796
- this.param.declare('parameter', UNKNOWN_EXPRESSION);
8903
+ this.param.declare("parameter" /* VariableKind.parameter */, UNKNOWN_EXPRESSION);
8797
8904
  }
8905
+ this.body = new BlockStatement(body, this, this.scope.bodyScope);
8798
8906
  super.parseNode(esTreeNode);
8799
8907
  }
8800
8908
  }
8909
+ CatchClause.prototype.preventChildBlockScope = true;
8801
8910
 
8802
8911
  class ChainExpression extends NodeBase {
8803
8912
  // deoptimizations are not relevant as we are not caching values
@@ -8820,7 +8929,7 @@ class ChainExpression extends NodeBase {
8820
8929
  class ClassBodyScope extends ChildScope {
8821
8930
  constructor(parent, classNode, context) {
8822
8931
  super(parent, context);
8823
- this.variables.set('this', (this.thisVariable = new LocalVariable('this', null, classNode, context)));
8932
+ this.variables.set('this', (this.thisVariable = new LocalVariable('this', null, classNode, context, "other" /* VariableKind.other */)));
8824
8933
  this.instanceScope = new ChildScope(this, context);
8825
8934
  this.instanceScope.variables.set('this', new ThisVariable(context));
8826
8935
  }
@@ -9004,7 +9113,7 @@ class ClassNode extends NodeBase {
9004
9113
  }
9005
9114
  }
9006
9115
  initialise() {
9007
- this.id?.declare('class', this);
9116
+ this.id?.declare("class" /* VariableKind.class */, this);
9008
9117
  for (const method of this.body.body) {
9009
9118
  if (method instanceof MethodDefinition && method.kind === 'constructor') {
9010
9119
  this.classConstructor = method;
@@ -9644,9 +9753,9 @@ class TrackingScope extends BlockScope {
9644
9753
  super(...arguments);
9645
9754
  this.hoistedDeclarations = [];
9646
9755
  }
9647
- addDeclaration(identifier, context, init, isHoisted) {
9756
+ addDeclaration(identifier, context, init, kind) {
9648
9757
  this.hoistedDeclarations.push(identifier);
9649
- return super.addDeclaration(identifier, context, init, isHoisted);
9758
+ return super.addDeclaration(identifier, context, init, kind);
9650
9759
  }
9651
9760
  }
9652
9761
 
@@ -11372,7 +11481,7 @@ class UndefinedVariable extends Variable {
11372
11481
 
11373
11482
  class ExportDefaultVariable extends LocalVariable {
11374
11483
  constructor(name, exportDefaultDeclaration, context) {
11375
- super(name, exportDefaultDeclaration, exportDefaultDeclaration.declaration, context);
11484
+ super(name, exportDefaultDeclaration, exportDefaultDeclaration.declaration, context, "other" /* VariableKind.other */);
11376
11485
  this.hasId = false;
11377
11486
  this.originalId = null;
11378
11487
  this.originalVariable = null;
@@ -11443,7 +11552,13 @@ class ExportDefaultVariable extends LocalVariable {
11443
11552
  class ModuleScope extends ChildScope {
11444
11553
  constructor(parent, context) {
11445
11554
  super(parent, context);
11446
- this.variables.set('this', new LocalVariable('this', null, UNDEFINED_EXPRESSION, context));
11555
+ this.variables.set('this', new LocalVariable('this', null, UNDEFINED_EXPRESSION, context, "other" /* VariableKind.other */));
11556
+ }
11557
+ addDeclaration(identifier, context, init, kind) {
11558
+ if (this.context.module.importDescriptions.has(identifier.name)) {
11559
+ context.error(logRedeclarationError(identifier.name), identifier.start);
11560
+ }
11561
+ return super.addDeclaration(identifier, context, init, kind);
11447
11562
  }
11448
11563
  addExportDefaultDeclaration(name, exportDefaultDeclaration, context) {
11449
11564
  const variable = new ExportDefaultVariable(name, exportDefaultDeclaration, context);
@@ -12040,7 +12155,7 @@ class NamespaceVariable extends Variable {
12040
12155
  memberVariable.hasEffectsOnInteractionAtPath(path.slice(1), interaction, context));
12041
12156
  }
12042
12157
  include() {
12043
- this.included = true;
12158
+ super.include();
12044
12159
  this.context.includeAllExports();
12045
12160
  }
12046
12161
  prepare(accessedGlobalsByScope) {
@@ -12134,7 +12249,7 @@ class SyntheticNamedExportVariable extends Variable {
12134
12249
  return `${this.syntheticNamespace.getName(getPropertyAccess)}${getPropertyAccess(this.name)}`;
12135
12250
  }
12136
12251
  include() {
12137
- this.included = true;
12252
+ super.include();
12138
12253
  this.context.includeVariableInModule(this.syntheticNamespace);
12139
12254
  }
12140
12255
  setRenderNames(baseName, name) {
@@ -12743,7 +12858,7 @@ class Module {
12743
12858
  const removedExports = [];
12744
12859
  for (const exportName of this.exports.keys()) {
12745
12860
  const [variable] = this.getVariableForExportName(exportName);
12746
- (variable && variable.included ? renderedExports : removedExports).push(exportName);
12861
+ (variable?.included ? renderedExports : removedExports).push(exportName);
12747
12862
  }
12748
12863
  return { removedExports, renderedExports };
12749
12864
  }
@@ -12840,6 +12955,9 @@ class Module {
12840
12955
  for (const exportName of this.exports.keys()) {
12841
12956
  if (includeNamespaceMembers || exportName !== this.info.syntheticNamedExports) {
12842
12957
  const variable = this.getVariableForExportName(exportName)[0];
12958
+ if (!variable) {
12959
+ return error(logMissingEntryExport(exportName, this.id));
12960
+ }
12843
12961
  variable.deoptimizePath(UNKNOWN_PATH);
12844
12962
  if (!variable.included) {
12845
12963
  this.includeVariable(variable);
@@ -13077,9 +13195,15 @@ class Module {
13077
13195
  }
13078
13196
  this.dynamicImports.push({ argument, id: null, node, resolution: null });
13079
13197
  }
13198
+ assertUniqueExportName(name, nodeStart) {
13199
+ if (this.exports.has(name) || this.reexportDescriptions.has(name)) {
13200
+ this.error(logDuplicateExportError(name), nodeStart);
13201
+ }
13202
+ }
13080
13203
  addExport(node) {
13081
13204
  if (node instanceof ExportDefaultDeclaration) {
13082
13205
  // export default foo;
13206
+ this.assertUniqueExportName('default', node.start);
13083
13207
  this.exports.set('default', {
13084
13208
  identifier: node.variable.getAssignedVariableName(),
13085
13209
  localName: 'default'
@@ -13091,6 +13215,7 @@ class Module {
13091
13215
  if (node.exported) {
13092
13216
  // export * as name from './other'
13093
13217
  const name = node.exported.name;
13218
+ this.assertUniqueExportName(name, node.exported.start);
13094
13219
  this.reexportDescriptions.set(name, {
13095
13220
  localName: '*',
13096
13221
  module: null,
@@ -13109,6 +13234,7 @@ class Module {
13109
13234
  this.addSource(source, node);
13110
13235
  for (const { exported, local, start } of node.specifiers) {
13111
13236
  const name = exported instanceof Literal ? exported.value : exported.name;
13237
+ this.assertUniqueExportName(name, start);
13112
13238
  this.reexportDescriptions.set(name, {
13113
13239
  localName: local instanceof Literal ? local.value : local.name,
13114
13240
  module: null,
@@ -13124,6 +13250,7 @@ class Module {
13124
13250
  // export var foo = 1, bar = 2;
13125
13251
  for (const declarator of declaration.declarations) {
13126
13252
  for (const localName of extractAssignedNames(declarator.id)) {
13253
+ this.assertUniqueExportName(localName, declarator.id.start);
13127
13254
  this.exports.set(localName, { identifier: null, localName });
13128
13255
  }
13129
13256
  }
@@ -13131,6 +13258,7 @@ class Module {
13131
13258
  else {
13132
13259
  // export function foo () {}
13133
13260
  const localName = declaration.id.name;
13261
+ this.assertUniqueExportName(localName, declaration.id.start);
13134
13262
  this.exports.set(localName, { identifier: null, localName });
13135
13263
  }
13136
13264
  }
@@ -13140,6 +13268,7 @@ class Module {
13140
13268
  // except for reexports, local must be an Identifier
13141
13269
  const localName = local.name;
13142
13270
  const exportedName = exported instanceof Identifier ? exported.name : exported.value;
13271
+ this.assertUniqueExportName(exportedName, exported.start);
13143
13272
  this.exports.set(exportedName, { identifier: null, localName });
13144
13273
  }
13145
13274
  }
@@ -13148,6 +13277,10 @@ class Module {
13148
13277
  const source = node.source.value;
13149
13278
  this.addSource(source, node);
13150
13279
  for (const specifier of node.specifiers) {
13280
+ const localName = specifier.local.name;
13281
+ if (this.scope.variables.has(localName) || this.importDescriptions.has(localName)) {
13282
+ this.error(logRedeclarationError(localName), specifier.local.start);
13283
+ }
13151
13284
  const name = specifier instanceof ImportDefaultSpecifier
13152
13285
  ? 'default'
13153
13286
  : specifier instanceof ImportNamespaceSpecifier
@@ -13155,7 +13288,7 @@ class Module {
13155
13288
  : specifier.imported instanceof Identifier
13156
13289
  ? specifier.imported.name
13157
13290
  : specifier.imported.value;
13158
- this.importDescriptions.set(specifier.local.name, {
13291
+ this.importDescriptions.set(localName, {
13159
13292
  module: null,
13160
13293
  name,
13161
13294
  source,