typescript 4.6.0-dev.20220113 → 4.6.0-dev.20220114

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.
@@ -294,7 +294,7 @@ var ts;
294
294
  // The following is baselined as a literal template type without intervention
295
295
  /** The version of the TypeScript compiler release */
296
296
  // eslint-disable-next-line @typescript-eslint/no-inferrable-types
297
- ts.version = ts.versionMajorMinor + ".0-dev.20220113";
297
+ ts.version = ts.versionMajorMinor + ".0-dev.20220114";
298
298
  /* @internal */
299
299
  var Comparison;
300
300
  (function (Comparison) {
@@ -8482,7 +8482,7 @@ var ts;
8482
8482
  Parameter_0_cannot_reference_identifier_1_declared_after_it: diag(2373, ts.DiagnosticCategory.Error, "Parameter_0_cannot_reference_identifier_1_declared_after_it_2373", "Parameter '{0}' cannot reference identifier '{1}' declared after it."),
8483
8483
  Duplicate_index_signature_for_type_0: diag(2374, ts.DiagnosticCategory.Error, "Duplicate_index_signature_for_type_0_2374", "Duplicate index signature for type '{0}'."),
8484
8484
  Type_0_is_not_assignable_to_type_1_with_exactOptionalPropertyTypes_Colon_true_Consider_adding_undefined_to_the_types_of_the_target_s_properties: diag(2375, ts.DiagnosticCategory.Error, "Type_0_is_not_assignable_to_type_1_with_exactOptionalPropertyTypes_Colon_true_Consider_adding_undefi_2375", "Type '{0}' is not assignable to type '{1}' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties."),
8485
- A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_parameter_properties_or_private_identifiers: diag(2376, ts.DiagnosticCategory.Error, "A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_proper_2376", "A 'super' call must be the first statement in the constructor when a class contains initialized properties, parameter properties, or private identifiers."),
8485
+ A_super_call_must_be_the_first_statement_in_the_constructor_to_refer_to_super_or_this_when_a_derived_class_contains_initialized_properties_parameter_properties_or_private_identifiers: diag(2376, ts.DiagnosticCategory.Error, "A_super_call_must_be_the_first_statement_in_the_constructor_to_refer_to_super_or_this_when_a_derived_2376", "A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers."),
8486
8486
  Constructors_for_derived_classes_must_contain_a_super_call: diag(2377, ts.DiagnosticCategory.Error, "Constructors_for_derived_classes_must_contain_a_super_call_2377", "Constructors for derived classes must contain a 'super' call."),
8487
8487
  A_get_accessor_must_return_a_value: diag(2378, ts.DiagnosticCategory.Error, "A_get_accessor_must_return_a_value_2378", "A 'get' accessor must return a value."),
8488
8488
  Argument_of_type_0_is_not_assignable_to_parameter_of_type_1_with_exactOptionalPropertyTypes_Colon_true_Consider_adding_undefined_to_the_types_of_the_target_s_properties: diag(2379, ts.DiagnosticCategory.Error, "Argument_of_type_0_is_not_assignable_to_parameter_of_type_1_with_exactOptionalPropertyTypes_Colon_tr_2379", "Argument of type '{0}' is not assignable to parameter of type '{1}' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties."),
@@ -8505,6 +8505,7 @@ var ts;
8505
8505
  constructor_cannot_be_used_as_a_parameter_property_name: diag(2398, ts.DiagnosticCategory.Error, "constructor_cannot_be_used_as_a_parameter_property_name_2398", "'constructor' cannot be used as a parameter property name."),
8506
8506
  Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference: diag(2399, ts.DiagnosticCategory.Error, "Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference_2399", "Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference."),
8507
8507
  Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference: diag(2400, ts.DiagnosticCategory.Error, "Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference_2400", "Expression resolves to variable declaration '_this' that compiler uses to capture 'this' reference."),
8508
+ A_super_call_must_be_a_root_level_statement_within_a_constructor_of_a_derived_class_that_contains_initialized_properties_parameter_properties_or_private_identifiers: diag(2401, ts.DiagnosticCategory.Error, "A_super_call_must_be_a_root_level_statement_within_a_constructor_of_a_derived_class_that_contains_in_2401", "A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers."),
8508
8509
  Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference: diag(2402, ts.DiagnosticCategory.Error, "Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference_2402", "Expression resolves to '_super' that compiler uses to capture base class reference."),
8509
8510
  Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2: diag(2403, ts.DiagnosticCategory.Error, "Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_t_2403", "Subsequent variable declarations must have the same type. Variable '{0}' must be of type '{1}', but here has type '{2}'."),
8510
8511
  The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation: diag(2404, ts.DiagnosticCategory.Error, "The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation_2404", "The left-hand side of a 'for...in' statement cannot use a type annotation."),
@@ -15665,6 +15666,34 @@ var ts;
15665
15666
  }
15666
15667
  }
15667
15668
  ts.getThisContainer = getThisContainer;
15669
+ /**
15670
+ * @returns Whether the node creates a new 'this' scope for its children.
15671
+ */
15672
+ function isThisContainerOrFunctionBlock(node) {
15673
+ switch (node.kind) {
15674
+ // Arrow functions use the same scope, but may do so in a "delayed" manner
15675
+ // For example, `const getThis = () => this` may be before a super() call in a derived constructor
15676
+ case 213 /* ArrowFunction */:
15677
+ case 255 /* FunctionDeclaration */:
15678
+ case 212 /* FunctionExpression */:
15679
+ case 166 /* PropertyDeclaration */:
15680
+ return true;
15681
+ case 234 /* Block */:
15682
+ switch (node.parent.kind) {
15683
+ case 170 /* Constructor */:
15684
+ case 168 /* MethodDeclaration */:
15685
+ case 171 /* GetAccessor */:
15686
+ case 172 /* SetAccessor */:
15687
+ // Object properties can have computed names; only method-like bodies start a new scope
15688
+ return true;
15689
+ default:
15690
+ return false;
15691
+ }
15692
+ default:
15693
+ return false;
15694
+ }
15695
+ }
15696
+ ts.isThisContainerOrFunctionBlock = isThisContainerOrFunctionBlock;
15668
15697
  function isInTopLevelContext(node) {
15669
15698
  // The name of a class or function declaration is a BindingIdentifier in its surrounding scope.
15670
15699
  if (ts.isIdentifier(node) && (ts.isClassDeclaration(node.parent) || ts.isFunctionDeclaration(node.parent)) && node.parent.name === node) {
@@ -26251,7 +26280,7 @@ var ts;
26251
26280
  * @param visitor Optional callback used to visit any custom prologue directives.
26252
26281
  */
26253
26282
  function copyPrologue(source, target, ensureUseStrict, visitor) {
26254
- var offset = copyStandardPrologue(source, target, ensureUseStrict);
26283
+ var offset = copyStandardPrologue(source, target, 0, ensureUseStrict);
26255
26284
  return copyCustomPrologue(source, target, offset, visitor);
26256
26285
  }
26257
26286
  function isUseStrictPrologue(node) {
@@ -26264,12 +26293,14 @@ var ts;
26264
26293
  * Copies only the standard (string-expression) prologue-directives into the target statement-array.
26265
26294
  * @param source origin statements array
26266
26295
  * @param target result statements array
26296
+ * @param statementOffset The offset at which to begin the copy.
26267
26297
  * @param ensureUseStrict boolean determining whether the function need to add prologue-directives
26298
+ * @returns Count of how many directive statements were copied.
26268
26299
  */
26269
- function copyStandardPrologue(source, target, ensureUseStrict) {
26300
+ function copyStandardPrologue(source, target, statementOffset, ensureUseStrict) {
26301
+ if (statementOffset === void 0) { statementOffset = 0; }
26270
26302
  ts.Debug.assert(target.length === 0, "Prologue directives should be at the first statement in the target statements array");
26271
26303
  var foundUseStrict = false;
26272
- var statementOffset = 0;
26273
26304
  var numStatements = source.length;
26274
26305
  while (statementOffset < numStatements) {
26275
26306
  var statement = source[statementOffset];
@@ -27692,7 +27723,7 @@ var ts;
27692
27723
  importName: "__createBinding",
27693
27724
  scoped: false,
27694
27725
  priority: 1,
27695
- text: "\n var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });\n }) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n }));"
27726
+ text: "\n var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n }) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n }));"
27696
27727
  };
27697
27728
  ts.setModuleDefaultHelper = {
27698
27729
  name: "typescript:commonjscreatevalue",
@@ -41327,7 +41358,7 @@ var ts;
41327
41358
  ? 1 /* Recursive */ : 0 /* None */
41328
41359
  };
41329
41360
  }
41330
- if (ts.isImplicitGlob(spec)) {
41361
+ if (ts.isImplicitGlob(spec.substring(spec.lastIndexOf(ts.directorySeparator) + 1))) {
41331
41362
  return {
41332
41363
  key: useCaseSensitiveFileNames ? spec : ts.toFileNameLowerCase(spec),
41333
41364
  flags: 1 /* Recursive */
@@ -78712,32 +78743,38 @@ var ts;
78712
78743
  if (classExtendsNull) {
78713
78744
  error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
78714
78745
  }
78715
- // The first statement in the body of a constructor (excluding prologue directives) must be a super call
78716
- // if both of the following are true:
78746
+ // A super call must be root-level in a constructor if both of the following are true:
78717
78747
  // - The containing class is a derived class.
78718
78748
  // - The constructor declares parameter properties
78719
78749
  // or the containing class declares instance member variables with initializers.
78720
- var superCallShouldBeFirst = (ts.getEmitScriptTarget(compilerOptions) !== 99 /* ESNext */ || !useDefineForClassFields) &&
78750
+ var superCallShouldBeRootLevel = (ts.getEmitScriptTarget(compilerOptions) !== 99 /* ESNext */ || !useDefineForClassFields) &&
78721
78751
  (ts.some(node.parent.members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
78722
78752
  ts.some(node.parameters, function (p) { return ts.hasSyntacticModifier(p, 16476 /* ParameterPropertyModifier */); }));
78723
- // Skip past any prologue directives to find the first statement
78724
- // to ensure that it was a super call.
78725
- if (superCallShouldBeFirst) {
78726
- var statements = node.body.statements;
78727
- var superCallStatement = void 0;
78728
- for (var _i = 0, statements_4 = statements; _i < statements_4.length; _i++) {
78729
- var statement = statements_4[_i];
78730
- if (statement.kind === 237 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) {
78731
- superCallStatement = statement;
78732
- break;
78753
+ if (superCallShouldBeRootLevel) {
78754
+ // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional
78755
+ // See GH #8277
78756
+ if (!superCallIsRootLevelInConstructor(superCall, node.body)) {
78757
+ error(superCall, ts.Diagnostics.A_super_call_must_be_a_root_level_statement_within_a_constructor_of_a_derived_class_that_contains_initialized_properties_parameter_properties_or_private_identifiers);
78758
+ }
78759
+ // Skip past any prologue directives to check statements for referring to 'super' or 'this' before a super call
78760
+ else {
78761
+ var superCallStatement = void 0;
78762
+ for (var _i = 0, _a = node.body.statements; _i < _a.length; _i++) {
78763
+ var statement = _a[_i];
78764
+ if (ts.isExpressionStatement(statement) && ts.isSuperCall(ts.skipOuterExpressions(statement.expression))) {
78765
+ superCallStatement = statement;
78766
+ break;
78767
+ }
78768
+ if (!ts.isPrologueDirective(statement) && nodeImmediatelyReferencesSuperOrThis(statement)) {
78769
+ break;
78770
+ }
78733
78771
  }
78734
- if (!ts.isPrologueDirective(statement)) {
78735
- break;
78772
+ // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional
78773
+ // See GH #8277
78774
+ if (superCallStatement === undefined) {
78775
+ error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_to_refer_to_super_or_this_when_a_derived_class_contains_initialized_properties_parameter_properties_or_private_identifiers);
78736
78776
  }
78737
78777
  }
78738
- if (!superCallStatement) {
78739
- error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_parameter_properties_or_private_identifiers);
78740
- }
78741
78778
  }
78742
78779
  }
78743
78780
  else if (!classExtendsNull) {
@@ -78745,6 +78782,19 @@ var ts;
78745
78782
  }
78746
78783
  }
78747
78784
  }
78785
+ function superCallIsRootLevelInConstructor(superCall, body) {
78786
+ var superCallParent = ts.walkUpParenthesizedExpressions(superCall.parent);
78787
+ return ts.isExpressionStatement(superCallParent) && superCallParent.parent === body;
78788
+ }
78789
+ function nodeImmediatelyReferencesSuperOrThis(node) {
78790
+ if (node.kind === 106 /* SuperKeyword */ || node.kind === 108 /* ThisKeyword */) {
78791
+ return true;
78792
+ }
78793
+ if (ts.isThisContainerOrFunctionBlock(node)) {
78794
+ return false;
78795
+ }
78796
+ return !!ts.forEachChild(node, nodeImmediatelyReferencesSuperOrThis);
78797
+ }
78748
78798
  function checkAccessorDeclaration(node) {
78749
78799
  if (produceDiagnostics) {
78750
78800
  // Grammar checking accessors
@@ -88867,34 +88917,31 @@ var ts;
88867
88917
  }
88868
88918
  ts.getNonAssignmentOperatorForCompoundAssignment = getNonAssignmentOperatorForCompoundAssignment;
88869
88919
  /**
88870
- * Adds super call and preceding prologue directives into the list of statements.
88871
- *
88872
- * @param ctor The constructor node.
88873
- * @param result The list of statements.
88874
- * @param visitor The visitor to apply to each node added to the result array.
88875
- * @returns index of the statement that follows super call
88920
+ * @returns Contained super() call from descending into the statement ignoring parentheses, if that call exists.
88921
+ */
88922
+ function getSuperCallFromStatement(statement) {
88923
+ if (!ts.isExpressionStatement(statement)) {
88924
+ return undefined;
88925
+ }
88926
+ var expression = ts.skipParentheses(statement.expression);
88927
+ return ts.isSuperCall(expression)
88928
+ ? expression
88929
+ : undefined;
88930
+ }
88931
+ ts.getSuperCallFromStatement = getSuperCallFromStatement;
88932
+ /**
88933
+ * @returns The index (after prologue statements) of a super call, or -1 if not found.
88876
88934
  */
88877
- function addPrologueDirectivesAndInitialSuperCall(factory, ctor, result, visitor) {
88878
- if (ctor.body) {
88879
- var statements = ctor.body.statements;
88880
- // add prologue directives to the list (if any)
88881
- var index = factory.copyPrologue(statements, result, /*ensureUseStrict*/ false, visitor);
88882
- if (index === statements.length) {
88883
- // list contains nothing but prologue directives (or empty) - exit
88884
- return index;
88885
- }
88886
- var superIndex = ts.findIndex(statements, function (s) { return ts.isExpressionStatement(s) && ts.isSuperCall(s.expression); }, index);
88887
- if (superIndex > -1) {
88888
- for (var i = index; i <= superIndex; i++) {
88889
- result.push(ts.visitNode(statements[i], visitor, ts.isStatement));
88890
- }
88891
- return superIndex + 1;
88935
+ function findSuperStatementIndex(statements, indexAfterLastPrologueStatement) {
88936
+ for (var i = indexAfterLastPrologueStatement; i < statements.length; i += 1) {
88937
+ var statement = statements[i];
88938
+ if (getSuperCallFromStatement(statement)) {
88939
+ return i;
88892
88940
  }
88893
- return index;
88894
88941
  }
88895
- return 0;
88942
+ return -1;
88896
88943
  }
88897
- ts.addPrologueDirectivesAndInitialSuperCall = addPrologueDirectivesAndInitialSuperCall;
88944
+ ts.findSuperStatementIndex = findSuperStatementIndex;
88898
88945
  function getProperties(node, requireInitializer, isStatic) {
88899
88946
  return ts.filter(node.members, function (m) { return isInitializedOrStaticProperty(m, requireInitializer, isStatic); });
88900
88947
  }
@@ -91132,10 +91179,14 @@ var ts;
91132
91179
  return ts.visitFunctionBody(body, visitor, context);
91133
91180
  }
91134
91181
  var statements = [];
91135
- var indexOfFirstStatement = 0;
91136
91182
  resumeLexicalEnvironment();
91137
- indexOfFirstStatement = ts.addPrologueDirectivesAndInitialSuperCall(factory, constructor, statements, visitor);
91138
- // Add parameters with property assignments. Transforms this:
91183
+ var indexAfterLastPrologueStatement = factory.copyPrologue(body.statements, statements, /*ensureUseStrict*/ false, visitor);
91184
+ var superStatementIndex = ts.findSuperStatementIndex(body.statements, indexAfterLastPrologueStatement);
91185
+ // If there was a super call, visit existing statements up to and including it
91186
+ if (superStatementIndex >= 0) {
91187
+ ts.addRange(statements, ts.visitNodes(body.statements, visitor, ts.isStatement, indexAfterLastPrologueStatement, superStatementIndex + 1 - indexAfterLastPrologueStatement));
91188
+ }
91189
+ // Transform parameters into property assignments. Transforms this:
91139
91190
  //
91140
91191
  // constructor (public x, public y) {
91141
91192
  // }
@@ -91147,9 +91198,17 @@ var ts;
91147
91198
  // this.y = y;
91148
91199
  // }
91149
91200
  //
91150
- ts.addRange(statements, ts.map(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment));
91151
- // Add the existing statements, skipping the initial super call.
91152
- ts.addRange(statements, ts.visitNodes(body.statements, visitor, ts.isStatement, indexOfFirstStatement));
91201
+ var parameterPropertyAssignments = ts.mapDefined(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment);
91202
+ // If there is a super() call, the parameter properties go immediately after it
91203
+ if (superStatementIndex >= 0) {
91204
+ ts.addRange(statements, parameterPropertyAssignments);
91205
+ }
91206
+ // Since there was no super() call, parameter properties are the first statements in the constructor
91207
+ else {
91208
+ statements = ts.addRange(parameterPropertyAssignments, statements);
91209
+ }
91210
+ // Add remaining statements from the body, skipping the super() call if it was found
91211
+ ts.addRange(statements, ts.visitNodes(body.statements, visitor, ts.isStatement, superStatementIndex + 1));
91153
91212
  // End the lexical environment.
91154
91213
  statements = factory.mergeLexicalEnvironment(statements, endLexicalEnvironment());
91155
91214
  var block = factory.createBlock(ts.setTextRange(factory.createNodeArray(statements), body.statements), /*multiLine*/ true);
@@ -93047,6 +93106,7 @@ var ts;
93047
93106
  /*modifiers*/ undefined, parameters !== null && parameters !== void 0 ? parameters : [], body), constructor || node), constructor));
93048
93107
  }
93049
93108
  function transformConstructorBody(node, constructor, isDerivedClass) {
93109
+ var _a;
93050
93110
  var properties = ts.getProperties(node, /*requireInitializer*/ false, /*isStatic*/ false);
93051
93111
  if (!useDefineForClassFields) {
93052
93112
  properties = ts.filter(properties, function (property) { return !!property.initializer || ts.isPrivateIdentifier(property.name); });
@@ -93058,9 +93118,21 @@ var ts;
93058
93118
  return ts.visitFunctionBody(/*node*/ undefined, visitor, context);
93059
93119
  }
93060
93120
  resumeLexicalEnvironment();
93061
- var indexOfFirstStatement = 0;
93121
+ var needsSyntheticConstructor = !constructor && isDerivedClass;
93122
+ var indexOfFirstStatementAfterSuper = 0;
93123
+ var prologueStatementCount = 0;
93124
+ var superStatementIndex = -1;
93062
93125
  var statements = [];
93063
- if (!constructor && isDerivedClass) {
93126
+ if ((_a = constructor === null || constructor === void 0 ? void 0 : constructor.body) === null || _a === void 0 ? void 0 : _a.statements) {
93127
+ prologueStatementCount = factory.copyPrologue(constructor.body.statements, statements, /*ensureUseStrict*/ false, visitor);
93128
+ superStatementIndex = ts.findSuperStatementIndex(constructor.body.statements, prologueStatementCount);
93129
+ // If there was a super call, visit existing statements up to and including it
93130
+ if (superStatementIndex >= 0) {
93131
+ indexOfFirstStatementAfterSuper = superStatementIndex + 1;
93132
+ statements = __spreadArray(__spreadArray(__spreadArray([], statements.slice(0, prologueStatementCount), true), ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, prologueStatementCount, indexOfFirstStatementAfterSuper - prologueStatementCount), true), statements.slice(prologueStatementCount), true);
93133
+ }
93134
+ }
93135
+ if (needsSyntheticConstructor) {
93064
93136
  // Add a synthetic `super` call:
93065
93137
  //
93066
93138
  // super(...arguments);
@@ -93068,9 +93140,6 @@ var ts;
93068
93140
  statements.push(factory.createExpressionStatement(factory.createCallExpression(factory.createSuper(),
93069
93141
  /*typeArguments*/ undefined, [factory.createSpreadElement(factory.createIdentifier("arguments"))])));
93070
93142
  }
93071
- if (constructor) {
93072
- indexOfFirstStatement = ts.addPrologueDirectivesAndInitialSuperCall(factory, constructor, statements, visitor);
93073
- }
93074
93143
  // Add the property initializers. Transforms this:
93075
93144
  //
93076
93145
  // public x = 1;
@@ -93081,31 +93150,59 @@ var ts;
93081
93150
  // this.x = 1;
93082
93151
  // }
93083
93152
  //
93153
+ // If we do useDefineForClassFields, they'll be converted elsewhere.
93154
+ // We instead *remove* them from the transformed output at this stage.
93155
+ var parameterPropertyDeclarationCount = 0;
93084
93156
  if (constructor === null || constructor === void 0 ? void 0 : constructor.body) {
93085
- var afterParameterProperties = ts.findIndex(constructor.body.statements, function (s) { return !ts.isParameterPropertyDeclaration(ts.getOriginalNode(s), constructor); }, indexOfFirstStatement);
93086
- if (afterParameterProperties === -1) {
93087
- afterParameterProperties = constructor.body.statements.length;
93157
+ if (useDefineForClassFields) {
93158
+ statements = statements.filter(function (statement) { return !ts.isParameterPropertyDeclaration(ts.getOriginalNode(statement), constructor); });
93088
93159
  }
93089
- if (afterParameterProperties > indexOfFirstStatement) {
93090
- if (!useDefineForClassFields) {
93091
- ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, indexOfFirstStatement, afterParameterProperties - indexOfFirstStatement));
93160
+ else {
93161
+ for (var _i = 0, _b = constructor.body.statements; _i < _b.length; _i++) {
93162
+ var statement = _b[_i];
93163
+ if (ts.isParameterPropertyDeclaration(ts.getOriginalNode(statement), constructor)) {
93164
+ parameterPropertyDeclarationCount++;
93165
+ }
93166
+ }
93167
+ if (parameterPropertyDeclarationCount > 0) {
93168
+ var parameterProperties = ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount);
93169
+ // If there was a super() call found, add parameter properties immediately after it
93170
+ if (superStatementIndex >= 0) {
93171
+ ts.addRange(statements, parameterProperties);
93172
+ }
93173
+ // If a synthetic super() call was added, add them just after it
93174
+ else if (needsSyntheticConstructor) {
93175
+ statements = __spreadArray(__spreadArray([
93176
+ statements[0]
93177
+ ], parameterProperties, true), statements.slice(1), true);
93178
+ }
93179
+ // Since there wasn't a super() call, add them to the top of the constructor
93180
+ else {
93181
+ statements = __spreadArray(__spreadArray([], parameterProperties, true), statements, true);
93182
+ }
93183
+ indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount;
93092
93184
  }
93093
- indexOfFirstStatement = afterParameterProperties;
93094
93185
  }
93095
93186
  }
93096
93187
  var receiver = factory.createThis();
93097
93188
  // private methods can be called in property initializers, they should execute first.
93098
93189
  addMethodStatements(statements, privateMethodsAndAccessors, receiver);
93099
93190
  addPropertyOrClassStaticBlockStatements(statements, properties, receiver);
93100
- // Add existing statements, skipping the initial super call.
93191
+ // Add existing statements after the initial prologues and super call
93101
93192
  if (constructor) {
93102
- ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, indexOfFirstStatement));
93193
+ ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitBodyStatement, ts.isStatement, indexOfFirstStatementAfterSuper + prologueStatementCount));
93103
93194
  }
93104
93195
  statements = factory.mergeLexicalEnvironment(statements, endLexicalEnvironment());
93105
93196
  return ts.setTextRange(factory.createBlock(ts.setTextRange(factory.createNodeArray(statements),
93106
93197
  /*location*/ constructor ? constructor.body.statements : node.members),
93107
93198
  /*multiLine*/ true),
93108
93199
  /*location*/ constructor ? constructor.body : undefined);
93200
+ function visitBodyStatement(statement) {
93201
+ if (useDefineForClassFields && ts.isParameterPropertyDeclaration(ts.getOriginalNode(statement), constructor)) {
93202
+ return undefined;
93203
+ }
93204
+ return visitor(statement);
93205
+ }
93109
93206
  }
93110
93207
  /**
93111
93208
  * Generates assignment statements for property initializers.
@@ -96897,37 +96994,39 @@ var ts;
96897
96994
  var prologue = [];
96898
96995
  var statements = [];
96899
96996
  resumeLexicalEnvironment();
96997
+ // In derived classes, there may be code before the necessary super() call
96998
+ // We'll remove pre-super statements to be tacked on after the rest of the body
96999
+ var existingPrologue = ts.takeWhile(constructor.body.statements, ts.isPrologueDirective);
97000
+ var _a = findSuperCallAndStatementIndex(constructor.body.statements, existingPrologue), superCall = _a.superCall, superStatementIndex = _a.superStatementIndex;
97001
+ var postSuperStatementsStart = superStatementIndex === -1 ? existingPrologue.length : superStatementIndex + 1;
96900
97002
  // If a super call has already been synthesized,
96901
97003
  // we're going to assume that we should just transform everything after that.
96902
97004
  // The assumption is that no prior step in the pipeline has added any prologue directives.
96903
- var statementOffset = 0;
97005
+ var statementOffset = postSuperStatementsStart;
96904
97006
  if (!hasSynthesizedSuper)
96905
- statementOffset = factory.copyStandardPrologue(constructor.body.statements, prologue, /*ensureUseStrict*/ false);
96906
- addDefaultValueAssignmentsIfNeeded(statements, constructor);
96907
- addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper);
97007
+ statementOffset = factory.copyStandardPrologue(constructor.body.statements, prologue, statementOffset, /*ensureUseStrict*/ false);
96908
97008
  if (!hasSynthesizedSuper)
96909
- statementOffset = factory.copyCustomPrologue(constructor.body.statements, statements, statementOffset, visitor);
96910
- // If the first statement is a call to `super()`, visit the statement directly
97009
+ statementOffset = factory.copyCustomPrologue(constructor.body.statements, statements, statementOffset, visitor, /*filter*/ undefined);
97010
+ // If there already exists a call to `super()`, visit the statement directly
96911
97011
  var superCallExpression;
96912
97012
  if (hasSynthesizedSuper) {
96913
97013
  superCallExpression = createDefaultSuperCallOrThis();
96914
97014
  }
96915
- else if (isDerivedClass && statementOffset < constructor.body.statements.length) {
96916
- var firstStatement = constructor.body.statements[statementOffset];
96917
- if (ts.isExpressionStatement(firstStatement) && ts.isSuperCall(firstStatement.expression)) {
96918
- superCallExpression = visitImmediateSuperCallInBody(firstStatement.expression);
96919
- }
97015
+ else if (superCall) {
97016
+ superCallExpression = visitSuperCallInBody(superCall);
96920
97017
  }
96921
97018
  if (superCallExpression) {
96922
97019
  hierarchyFacts |= 8192 /* ConstructorWithCapturedSuper */;
96923
- statementOffset++; // skip this statement, we will add it after visiting the rest of the body.
96924
97020
  }
97021
+ // Add parameter defaults at the beginning of the output, with prologue statements
97022
+ addDefaultValueAssignmentsIfNeeded(prologue, constructor);
97023
+ addRestParameterIfNeeded(prologue, constructor, hasSynthesizedSuper);
96925
97024
  // visit the remaining statements
96926
97025
  ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, /*start*/ statementOffset));
96927
97026
  factory.mergeLexicalEnvironment(prologue, endLexicalEnvironment());
96928
97027
  insertCaptureNewTargetIfNeeded(prologue, constructor, /*copyOnWrite*/ false);
96929
- if (isDerivedClass) {
96930
- if (superCallExpression && statementOffset === constructor.body.statements.length && !(constructor.body.transformFlags & 8192 /* ContainsLexicalThis */)) {
97028
+ if (isDerivedClass || superCallExpression) {
97029
+ if (superCallExpression && postSuperStatementsStart === constructor.body.statements.length && !(constructor.body.transformFlags & 8192 /* ContainsLexicalThis */)) {
96931
97030
  // If the subclass constructor does *not* contain `this` and *ends* with a `super()` call, we will use the
96932
97031
  // following representation:
96933
97032
  //
@@ -96947,10 +97046,10 @@ var ts;
96947
97046
  // return C;
96948
97047
  // })(Base);
96949
97048
  // ```
96950
- var superCall = ts.cast(ts.cast(superCallExpression, ts.isBinaryExpression).left, ts.isCallExpression);
97049
+ var superCall_1 = ts.cast(ts.cast(superCallExpression, ts.isBinaryExpression).left, ts.isCallExpression);
96951
97050
  var returnStatement = factory.createReturnStatement(superCallExpression);
96952
- ts.setCommentRange(returnStatement, ts.getCommentRange(superCall));
96953
- ts.setEmitFlags(superCall, 1536 /* NoComments */);
97051
+ ts.setCommentRange(returnStatement, ts.getCommentRange(superCall_1));
97052
+ ts.setEmitFlags(superCall_1, 1536 /* NoComments */);
96954
97053
  statements.push(returnStatement);
96955
97054
  }
96956
97055
  else {
@@ -96975,9 +97074,19 @@ var ts;
96975
97074
  // return C;
96976
97075
  // })(Base);
96977
97076
  // ```
96978
- // Since the `super()` call was the first statement, we insert the `this` capturing call to
96979
- // `super()` at the top of the list of `statements` (after any pre-existing custom prologues).
96980
- insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis());
97077
+ // If the super() call is the first statement, we can directly create and assign its result to `_this`
97078
+ if (superStatementIndex <= existingPrologue.length) {
97079
+ insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis());
97080
+ }
97081
+ // Since the `super()` call isn't the first statement, it's split across 1-2 statements:
97082
+ // * A prologue `var _this = this;`, in case the constructor accesses this before super()
97083
+ // * If it exists, a reassignment to that `_this` of the super() call
97084
+ else {
97085
+ insertCaptureThisForNode(prologue, constructor, createActualThis());
97086
+ if (superCallExpression) {
97087
+ insertSuperThisCaptureThisForNode(statements, superCallExpression);
97088
+ }
97089
+ }
96981
97090
  if (!isSufficientlyCoveredByReturnStatements(constructor.body)) {
96982
97091
  statements.push(factory.createReturnStatement(factory.createUniqueName("_this", 16 /* Optimistic */ | 32 /* FileLevel */)));
96983
97092
  }
@@ -97000,11 +97109,27 @@ var ts;
97000
97109
  // ```
97001
97110
  insertCaptureThisForNodeIfNeeded(prologue, constructor);
97002
97111
  }
97003
- var block = factory.createBlock(ts.setTextRange(factory.createNodeArray(ts.concatenate(prologue, statements)),
97112
+ var body = factory.createBlock(ts.setTextRange(factory.createNodeArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], existingPrologue, true), prologue, true), (superStatementIndex <= existingPrologue.length ? ts.emptyArray : ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, existingPrologue.length, superStatementIndex)), true), statements, true)),
97004
97113
  /*location*/ constructor.body.statements),
97005
97114
  /*multiLine*/ true);
97006
- ts.setTextRange(block, constructor.body);
97007
- return block;
97115
+ ts.setTextRange(body, constructor.body);
97116
+ return body;
97117
+ }
97118
+ function findSuperCallAndStatementIndex(originalBodyStatements, existingPrologue) {
97119
+ for (var i = existingPrologue.length; i < originalBodyStatements.length; i += 1) {
97120
+ var superCall = ts.getSuperCallFromStatement(originalBodyStatements[i]);
97121
+ if (superCall) {
97122
+ // With a super() call, split the statements into pre-super() and 'body' (post-super())
97123
+ return {
97124
+ superCall: superCall,
97125
+ superStatementIndex: i,
97126
+ };
97127
+ }
97128
+ }
97129
+ // Since there was no super() call found, consider all statements to be in the main 'body' (post-super())
97130
+ return {
97131
+ superStatementIndex: -1,
97132
+ };
97008
97133
  }
97009
97134
  /**
97010
97135
  * We want to try to avoid emitting a return statement in certain cases if a user already returned something.
@@ -97233,6 +97358,18 @@ var ts;
97233
97358
  }
97234
97359
  return false;
97235
97360
  }
97361
+ /**
97362
+ * Assigns the `this` in a constructor to the result of its `super()` call.
97363
+ *
97364
+ * @param statements Statements in the constructor body.
97365
+ * @param superExpression Existing `super()` call for the constructor.
97366
+ */
97367
+ function insertSuperThisCaptureThisForNode(statements, superExpression) {
97368
+ enableSubstitutionsForCapturedThis();
97369
+ var assignSuperExpression = factory.createExpressionStatement(factory.createBinaryExpression(factory.createThis(), 63 /* EqualsToken */, superExpression));
97370
+ ts.insertStatementAfterCustomPrologue(statements, assignSuperExpression);
97371
+ ts.setCommentRange(assignSuperExpression, ts.getOriginalNode(superExpression).parent);
97372
+ }
97236
97373
  function insertCaptureThisForNode(statements, node, initializer) {
97237
97374
  enableSubstitutionsForCapturedThis();
97238
97375
  var captureThisStatement = factory.createVariableStatement(
@@ -97542,7 +97679,7 @@ var ts;
97542
97679
  if (ts.isBlock(body)) {
97543
97680
  // ensureUseStrict is false because no new prologue-directive should be added.
97544
97681
  // addStandardPrologue will put already-existing directives at the beginning of the target statement-array
97545
- statementOffset = factory.copyStandardPrologue(body.statements, prologue, /*ensureUseStrict*/ false);
97682
+ statementOffset = factory.copyStandardPrologue(body.statements, prologue, 0, /*ensureUseStrict*/ false);
97546
97683
  statementOffset = factory.copyCustomPrologue(body.statements, statements, statementOffset, visitor, ts.isHoistedFunction);
97547
97684
  statementOffset = factory.copyCustomPrologue(body.statements, statements, statementOffset, visitor, ts.isHoistedVariableStatement);
97548
97685
  }
@@ -98913,7 +99050,7 @@ var ts;
98913
99050
  /*type*/ undefined, factory.updateBlock(func.body, statements))),
98914
99051
  /*typeArguments*/ undefined, call.arguments))));
98915
99052
  }
98916
- function visitImmediateSuperCallInBody(node) {
99053
+ function visitSuperCallInBody(node) {
98917
99054
  return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ false);
98918
99055
  }
98919
99056
  function visitCallExpressionWithPotentialCapturedThisAssignment(node, assignToCapturedThis) {
@@ -146842,20 +146979,31 @@ var ts;
146842
146979
  codefix.registerCodeFix({
146843
146980
  errorCodes: errorCodes,
146844
146981
  getCodeActions: function getCodeActionsToAddConvertToUnknownForNonOverlappingTypes(context) {
146845
- var changes = ts.textChanges.ChangeTracker.with(context, function (t) { return makeChange(t, context.sourceFile, context.span.start); });
146982
+ var assertion = getAssertion(context.sourceFile, context.span.start);
146983
+ if (assertion === undefined)
146984
+ return undefined;
146985
+ var changes = ts.textChanges.ChangeTracker.with(context, function (t) { return makeChange(t, context.sourceFile, assertion); });
146846
146986
  return [codefix.createCodeFixAction(fixId, changes, ts.Diagnostics.Add_unknown_conversion_for_non_overlapping_types, fixId, ts.Diagnostics.Add_unknown_to_all_conversions_of_non_overlapping_types)];
146847
146987
  },
146848
146988
  fixIds: [fixId],
146849
- getAllCodeActions: function (context) { return codefix.codeFixAll(context, errorCodes, function (changes, diag) { return makeChange(changes, diag.file, diag.start); }); },
146989
+ getAllCodeActions: function (context) { return codefix.codeFixAll(context, errorCodes, function (changes, diag) {
146990
+ var assertion = getAssertion(diag.file, diag.start);
146991
+ if (assertion) {
146992
+ makeChange(changes, diag.file, assertion);
146993
+ }
146994
+ }); },
146850
146995
  });
146851
- function makeChange(changeTracker, sourceFile, pos) {
146852
- var token = ts.getTokenAtPosition(sourceFile, pos);
146853
- var assertion = ts.Debug.checkDefined(ts.findAncestor(token, function (n) { return ts.isAsExpression(n) || ts.isTypeAssertionExpression(n); }), "Expected to find an assertion expression");
146996
+ function makeChange(changeTracker, sourceFile, assertion) {
146854
146997
  var replacement = ts.isAsExpression(assertion)
146855
146998
  ? ts.factory.createAsExpression(assertion.expression, ts.factory.createKeywordTypeNode(154 /* UnknownKeyword */))
146856
146999
  : ts.factory.createTypeAssertion(ts.factory.createKeywordTypeNode(154 /* UnknownKeyword */), assertion.expression);
146857
147000
  changeTracker.replaceNode(sourceFile, assertion.expression, replacement);
146858
147001
  }
147002
+ function getAssertion(sourceFile, pos) {
147003
+ if (ts.isInJSFile(sourceFile))
147004
+ return undefined;
147005
+ return ts.findAncestor(ts.getTokenAtPosition(sourceFile, pos), function (n) { return ts.isAsExpression(n) || ts.isTypeAssertionExpression(n); });
147006
+ }
146859
147007
  })(codefix = ts.codefix || (ts.codefix = {}));
146860
147008
  })(ts || (ts = {}));
146861
147009
  /* @internal */