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.
@@ -89,7 +89,7 @@ var ts;
89
89
  // The following is baselined as a literal template type without intervention
90
90
  /** The version of the TypeScript compiler release */
91
91
  // eslint-disable-next-line @typescript-eslint/no-inferrable-types
92
- ts.version = ts.versionMajorMinor + ".0-dev.20220113";
92
+ ts.version = ts.versionMajorMinor + ".0-dev.20220114";
93
93
  /* @internal */
94
94
  var Comparison;
95
95
  (function (Comparison) {
@@ -8277,7 +8277,7 @@ var ts;
8277
8277
  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."),
8278
8278
  Duplicate_index_signature_for_type_0: diag(2374, ts.DiagnosticCategory.Error, "Duplicate_index_signature_for_type_0_2374", "Duplicate index signature for type '{0}'."),
8279
8279
  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."),
8280
- 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."),
8280
+ 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."),
8281
8281
  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."),
8282
8282
  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."),
8283
8283
  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."),
@@ -8300,6 +8300,7 @@ var ts;
8300
8300
  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."),
8301
8301
  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."),
8302
8302
  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."),
8303
+ 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."),
8303
8304
  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."),
8304
8305
  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}'."),
8305
8306
  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."),
@@ -15460,6 +15461,34 @@ var ts;
15460
15461
  }
15461
15462
  }
15462
15463
  ts.getThisContainer = getThisContainer;
15464
+ /**
15465
+ * @returns Whether the node creates a new 'this' scope for its children.
15466
+ */
15467
+ function isThisContainerOrFunctionBlock(node) {
15468
+ switch (node.kind) {
15469
+ // Arrow functions use the same scope, but may do so in a "delayed" manner
15470
+ // For example, `const getThis = () => this` may be before a super() call in a derived constructor
15471
+ case 213 /* ArrowFunction */:
15472
+ case 255 /* FunctionDeclaration */:
15473
+ case 212 /* FunctionExpression */:
15474
+ case 166 /* PropertyDeclaration */:
15475
+ return true;
15476
+ case 234 /* Block */:
15477
+ switch (node.parent.kind) {
15478
+ case 170 /* Constructor */:
15479
+ case 168 /* MethodDeclaration */:
15480
+ case 171 /* GetAccessor */:
15481
+ case 172 /* SetAccessor */:
15482
+ // Object properties can have computed names; only method-like bodies start a new scope
15483
+ return true;
15484
+ default:
15485
+ return false;
15486
+ }
15487
+ default:
15488
+ return false;
15489
+ }
15490
+ }
15491
+ ts.isThisContainerOrFunctionBlock = isThisContainerOrFunctionBlock;
15463
15492
  function isInTopLevelContext(node) {
15464
15493
  // The name of a class or function declaration is a BindingIdentifier in its surrounding scope.
15465
15494
  if (ts.isIdentifier(node) && (ts.isClassDeclaration(node.parent) || ts.isFunctionDeclaration(node.parent)) && node.parent.name === node) {
@@ -26046,7 +26075,7 @@ var ts;
26046
26075
  * @param visitor Optional callback used to visit any custom prologue directives.
26047
26076
  */
26048
26077
  function copyPrologue(source, target, ensureUseStrict, visitor) {
26049
- var offset = copyStandardPrologue(source, target, ensureUseStrict);
26078
+ var offset = copyStandardPrologue(source, target, 0, ensureUseStrict);
26050
26079
  return copyCustomPrologue(source, target, offset, visitor);
26051
26080
  }
26052
26081
  function isUseStrictPrologue(node) {
@@ -26059,12 +26088,14 @@ var ts;
26059
26088
  * Copies only the standard (string-expression) prologue-directives into the target statement-array.
26060
26089
  * @param source origin statements array
26061
26090
  * @param target result statements array
26091
+ * @param statementOffset The offset at which to begin the copy.
26062
26092
  * @param ensureUseStrict boolean determining whether the function need to add prologue-directives
26093
+ * @returns Count of how many directive statements were copied.
26063
26094
  */
26064
- function copyStandardPrologue(source, target, ensureUseStrict) {
26095
+ function copyStandardPrologue(source, target, statementOffset, ensureUseStrict) {
26096
+ if (statementOffset === void 0) { statementOffset = 0; }
26065
26097
  ts.Debug.assert(target.length === 0, "Prologue directives should be at the first statement in the target statements array");
26066
26098
  var foundUseStrict = false;
26067
- var statementOffset = 0;
26068
26099
  var numStatements = source.length;
26069
26100
  while (statementOffset < numStatements) {
26070
26101
  var statement = source[statementOffset];
@@ -27487,7 +27518,7 @@ var ts;
27487
27518
  importName: "__createBinding",
27488
27519
  scoped: false,
27489
27520
  priority: 1,
27490
- 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 }));"
27521
+ 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 }));"
27491
27522
  };
27492
27523
  ts.setModuleDefaultHelper = {
27493
27524
  name: "typescript:commonjscreatevalue",
@@ -41122,7 +41153,7 @@ var ts;
41122
41153
  ? 1 /* Recursive */ : 0 /* None */
41123
41154
  };
41124
41155
  }
41125
- if (ts.isImplicitGlob(spec)) {
41156
+ if (ts.isImplicitGlob(spec.substring(spec.lastIndexOf(ts.directorySeparator) + 1))) {
41126
41157
  return {
41127
41158
  key: useCaseSensitiveFileNames ? spec : ts.toFileNameLowerCase(spec),
41128
41159
  flags: 1 /* Recursive */
@@ -78507,32 +78538,38 @@ var ts;
78507
78538
  if (classExtendsNull) {
78508
78539
  error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
78509
78540
  }
78510
- // The first statement in the body of a constructor (excluding prologue directives) must be a super call
78511
- // if both of the following are true:
78541
+ // A super call must be root-level in a constructor if both of the following are true:
78512
78542
  // - The containing class is a derived class.
78513
78543
  // - The constructor declares parameter properties
78514
78544
  // or the containing class declares instance member variables with initializers.
78515
- var superCallShouldBeFirst = (ts.getEmitScriptTarget(compilerOptions) !== 99 /* ESNext */ || !useDefineForClassFields) &&
78545
+ var superCallShouldBeRootLevel = (ts.getEmitScriptTarget(compilerOptions) !== 99 /* ESNext */ || !useDefineForClassFields) &&
78516
78546
  (ts.some(node.parent.members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
78517
78547
  ts.some(node.parameters, function (p) { return ts.hasSyntacticModifier(p, 16476 /* ParameterPropertyModifier */); }));
78518
- // Skip past any prologue directives to find the first statement
78519
- // to ensure that it was a super call.
78520
- if (superCallShouldBeFirst) {
78521
- var statements = node.body.statements;
78522
- var superCallStatement = void 0;
78523
- for (var _i = 0, statements_4 = statements; _i < statements_4.length; _i++) {
78524
- var statement = statements_4[_i];
78525
- if (statement.kind === 237 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) {
78526
- superCallStatement = statement;
78527
- break;
78548
+ if (superCallShouldBeRootLevel) {
78549
+ // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional
78550
+ // See GH #8277
78551
+ if (!superCallIsRootLevelInConstructor(superCall, node.body)) {
78552
+ 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);
78553
+ }
78554
+ // Skip past any prologue directives to check statements for referring to 'super' or 'this' before a super call
78555
+ else {
78556
+ var superCallStatement = void 0;
78557
+ for (var _i = 0, _a = node.body.statements; _i < _a.length; _i++) {
78558
+ var statement = _a[_i];
78559
+ if (ts.isExpressionStatement(statement) && ts.isSuperCall(ts.skipOuterExpressions(statement.expression))) {
78560
+ superCallStatement = statement;
78561
+ break;
78562
+ }
78563
+ if (!ts.isPrologueDirective(statement) && nodeImmediatelyReferencesSuperOrThis(statement)) {
78564
+ break;
78565
+ }
78528
78566
  }
78529
- if (!ts.isPrologueDirective(statement)) {
78530
- break;
78567
+ // Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional
78568
+ // See GH #8277
78569
+ if (superCallStatement === undefined) {
78570
+ 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);
78531
78571
  }
78532
78572
  }
78533
- if (!superCallStatement) {
78534
- 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);
78535
- }
78536
78573
  }
78537
78574
  }
78538
78575
  else if (!classExtendsNull) {
@@ -78540,6 +78577,19 @@ var ts;
78540
78577
  }
78541
78578
  }
78542
78579
  }
78580
+ function superCallIsRootLevelInConstructor(superCall, body) {
78581
+ var superCallParent = ts.walkUpParenthesizedExpressions(superCall.parent);
78582
+ return ts.isExpressionStatement(superCallParent) && superCallParent.parent === body;
78583
+ }
78584
+ function nodeImmediatelyReferencesSuperOrThis(node) {
78585
+ if (node.kind === 106 /* SuperKeyword */ || node.kind === 108 /* ThisKeyword */) {
78586
+ return true;
78587
+ }
78588
+ if (ts.isThisContainerOrFunctionBlock(node)) {
78589
+ return false;
78590
+ }
78591
+ return !!ts.forEachChild(node, nodeImmediatelyReferencesSuperOrThis);
78592
+ }
78543
78593
  function checkAccessorDeclaration(node) {
78544
78594
  if (produceDiagnostics) {
78545
78595
  // Grammar checking accessors
@@ -88662,34 +88712,31 @@ var ts;
88662
88712
  }
88663
88713
  ts.getNonAssignmentOperatorForCompoundAssignment = getNonAssignmentOperatorForCompoundAssignment;
88664
88714
  /**
88665
- * Adds super call and preceding prologue directives into the list of statements.
88666
- *
88667
- * @param ctor The constructor node.
88668
- * @param result The list of statements.
88669
- * @param visitor The visitor to apply to each node added to the result array.
88670
- * @returns index of the statement that follows super call
88715
+ * @returns Contained super() call from descending into the statement ignoring parentheses, if that call exists.
88716
+ */
88717
+ function getSuperCallFromStatement(statement) {
88718
+ if (!ts.isExpressionStatement(statement)) {
88719
+ return undefined;
88720
+ }
88721
+ var expression = ts.skipParentheses(statement.expression);
88722
+ return ts.isSuperCall(expression)
88723
+ ? expression
88724
+ : undefined;
88725
+ }
88726
+ ts.getSuperCallFromStatement = getSuperCallFromStatement;
88727
+ /**
88728
+ * @returns The index (after prologue statements) of a super call, or -1 if not found.
88671
88729
  */
88672
- function addPrologueDirectivesAndInitialSuperCall(factory, ctor, result, visitor) {
88673
- if (ctor.body) {
88674
- var statements = ctor.body.statements;
88675
- // add prologue directives to the list (if any)
88676
- var index = factory.copyPrologue(statements, result, /*ensureUseStrict*/ false, visitor);
88677
- if (index === statements.length) {
88678
- // list contains nothing but prologue directives (or empty) - exit
88679
- return index;
88680
- }
88681
- var superIndex = ts.findIndex(statements, function (s) { return ts.isExpressionStatement(s) && ts.isSuperCall(s.expression); }, index);
88682
- if (superIndex > -1) {
88683
- for (var i = index; i <= superIndex; i++) {
88684
- result.push(ts.visitNode(statements[i], visitor, ts.isStatement));
88685
- }
88686
- return superIndex + 1;
88730
+ function findSuperStatementIndex(statements, indexAfterLastPrologueStatement) {
88731
+ for (var i = indexAfterLastPrologueStatement; i < statements.length; i += 1) {
88732
+ var statement = statements[i];
88733
+ if (getSuperCallFromStatement(statement)) {
88734
+ return i;
88687
88735
  }
88688
- return index;
88689
88736
  }
88690
- return 0;
88737
+ return -1;
88691
88738
  }
88692
- ts.addPrologueDirectivesAndInitialSuperCall = addPrologueDirectivesAndInitialSuperCall;
88739
+ ts.findSuperStatementIndex = findSuperStatementIndex;
88693
88740
  function getProperties(node, requireInitializer, isStatic) {
88694
88741
  return ts.filter(node.members, function (m) { return isInitializedOrStaticProperty(m, requireInitializer, isStatic); });
88695
88742
  }
@@ -90927,10 +90974,14 @@ var ts;
90927
90974
  return ts.visitFunctionBody(body, visitor, context);
90928
90975
  }
90929
90976
  var statements = [];
90930
- var indexOfFirstStatement = 0;
90931
90977
  resumeLexicalEnvironment();
90932
- indexOfFirstStatement = ts.addPrologueDirectivesAndInitialSuperCall(factory, constructor, statements, visitor);
90933
- // Add parameters with property assignments. Transforms this:
90978
+ var indexAfterLastPrologueStatement = factory.copyPrologue(body.statements, statements, /*ensureUseStrict*/ false, visitor);
90979
+ var superStatementIndex = ts.findSuperStatementIndex(body.statements, indexAfterLastPrologueStatement);
90980
+ // If there was a super call, visit existing statements up to and including it
90981
+ if (superStatementIndex >= 0) {
90982
+ ts.addRange(statements, ts.visitNodes(body.statements, visitor, ts.isStatement, indexAfterLastPrologueStatement, superStatementIndex + 1 - indexAfterLastPrologueStatement));
90983
+ }
90984
+ // Transform parameters into property assignments. Transforms this:
90934
90985
  //
90935
90986
  // constructor (public x, public y) {
90936
90987
  // }
@@ -90942,9 +90993,17 @@ var ts;
90942
90993
  // this.y = y;
90943
90994
  // }
90944
90995
  //
90945
- ts.addRange(statements, ts.map(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment));
90946
- // Add the existing statements, skipping the initial super call.
90947
- ts.addRange(statements, ts.visitNodes(body.statements, visitor, ts.isStatement, indexOfFirstStatement));
90996
+ var parameterPropertyAssignments = ts.mapDefined(parametersWithPropertyAssignments, transformParameterWithPropertyAssignment);
90997
+ // If there is a super() call, the parameter properties go immediately after it
90998
+ if (superStatementIndex >= 0) {
90999
+ ts.addRange(statements, parameterPropertyAssignments);
91000
+ }
91001
+ // Since there was no super() call, parameter properties are the first statements in the constructor
91002
+ else {
91003
+ statements = ts.addRange(parameterPropertyAssignments, statements);
91004
+ }
91005
+ // Add remaining statements from the body, skipping the super() call if it was found
91006
+ ts.addRange(statements, ts.visitNodes(body.statements, visitor, ts.isStatement, superStatementIndex + 1));
90948
91007
  // End the lexical environment.
90949
91008
  statements = factory.mergeLexicalEnvironment(statements, endLexicalEnvironment());
90950
91009
  var block = factory.createBlock(ts.setTextRange(factory.createNodeArray(statements), body.statements), /*multiLine*/ true);
@@ -92842,6 +92901,7 @@ var ts;
92842
92901
  /*modifiers*/ undefined, parameters !== null && parameters !== void 0 ? parameters : [], body), constructor || node), constructor));
92843
92902
  }
92844
92903
  function transformConstructorBody(node, constructor, isDerivedClass) {
92904
+ var _a;
92845
92905
  var properties = ts.getProperties(node, /*requireInitializer*/ false, /*isStatic*/ false);
92846
92906
  if (!useDefineForClassFields) {
92847
92907
  properties = ts.filter(properties, function (property) { return !!property.initializer || ts.isPrivateIdentifier(property.name); });
@@ -92853,9 +92913,21 @@ var ts;
92853
92913
  return ts.visitFunctionBody(/*node*/ undefined, visitor, context);
92854
92914
  }
92855
92915
  resumeLexicalEnvironment();
92856
- var indexOfFirstStatement = 0;
92916
+ var needsSyntheticConstructor = !constructor && isDerivedClass;
92917
+ var indexOfFirstStatementAfterSuper = 0;
92918
+ var prologueStatementCount = 0;
92919
+ var superStatementIndex = -1;
92857
92920
  var statements = [];
92858
- if (!constructor && isDerivedClass) {
92921
+ if ((_a = constructor === null || constructor === void 0 ? void 0 : constructor.body) === null || _a === void 0 ? void 0 : _a.statements) {
92922
+ prologueStatementCount = factory.copyPrologue(constructor.body.statements, statements, /*ensureUseStrict*/ false, visitor);
92923
+ superStatementIndex = ts.findSuperStatementIndex(constructor.body.statements, prologueStatementCount);
92924
+ // If there was a super call, visit existing statements up to and including it
92925
+ if (superStatementIndex >= 0) {
92926
+ indexOfFirstStatementAfterSuper = superStatementIndex + 1;
92927
+ 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);
92928
+ }
92929
+ }
92930
+ if (needsSyntheticConstructor) {
92859
92931
  // Add a synthetic `super` call:
92860
92932
  //
92861
92933
  // super(...arguments);
@@ -92863,9 +92935,6 @@ var ts;
92863
92935
  statements.push(factory.createExpressionStatement(factory.createCallExpression(factory.createSuper(),
92864
92936
  /*typeArguments*/ undefined, [factory.createSpreadElement(factory.createIdentifier("arguments"))])));
92865
92937
  }
92866
- if (constructor) {
92867
- indexOfFirstStatement = ts.addPrologueDirectivesAndInitialSuperCall(factory, constructor, statements, visitor);
92868
- }
92869
92938
  // Add the property initializers. Transforms this:
92870
92939
  //
92871
92940
  // public x = 1;
@@ -92876,31 +92945,59 @@ var ts;
92876
92945
  // this.x = 1;
92877
92946
  // }
92878
92947
  //
92948
+ // If we do useDefineForClassFields, they'll be converted elsewhere.
92949
+ // We instead *remove* them from the transformed output at this stage.
92950
+ var parameterPropertyDeclarationCount = 0;
92879
92951
  if (constructor === null || constructor === void 0 ? void 0 : constructor.body) {
92880
- var afterParameterProperties = ts.findIndex(constructor.body.statements, function (s) { return !ts.isParameterPropertyDeclaration(ts.getOriginalNode(s), constructor); }, indexOfFirstStatement);
92881
- if (afterParameterProperties === -1) {
92882
- afterParameterProperties = constructor.body.statements.length;
92952
+ if (useDefineForClassFields) {
92953
+ statements = statements.filter(function (statement) { return !ts.isParameterPropertyDeclaration(ts.getOriginalNode(statement), constructor); });
92883
92954
  }
92884
- if (afterParameterProperties > indexOfFirstStatement) {
92885
- if (!useDefineForClassFields) {
92886
- ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, indexOfFirstStatement, afterParameterProperties - indexOfFirstStatement));
92955
+ else {
92956
+ for (var _i = 0, _b = constructor.body.statements; _i < _b.length; _i++) {
92957
+ var statement = _b[_i];
92958
+ if (ts.isParameterPropertyDeclaration(ts.getOriginalNode(statement), constructor)) {
92959
+ parameterPropertyDeclarationCount++;
92960
+ }
92961
+ }
92962
+ if (parameterPropertyDeclarationCount > 0) {
92963
+ var parameterProperties = ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, indexOfFirstStatementAfterSuper, parameterPropertyDeclarationCount);
92964
+ // If there was a super() call found, add parameter properties immediately after it
92965
+ if (superStatementIndex >= 0) {
92966
+ ts.addRange(statements, parameterProperties);
92967
+ }
92968
+ // If a synthetic super() call was added, add them just after it
92969
+ else if (needsSyntheticConstructor) {
92970
+ statements = __spreadArray(__spreadArray([
92971
+ statements[0]
92972
+ ], parameterProperties, true), statements.slice(1), true);
92973
+ }
92974
+ // Since there wasn't a super() call, add them to the top of the constructor
92975
+ else {
92976
+ statements = __spreadArray(__spreadArray([], parameterProperties, true), statements, true);
92977
+ }
92978
+ indexOfFirstStatementAfterSuper += parameterPropertyDeclarationCount;
92887
92979
  }
92888
- indexOfFirstStatement = afterParameterProperties;
92889
92980
  }
92890
92981
  }
92891
92982
  var receiver = factory.createThis();
92892
92983
  // private methods can be called in property initializers, they should execute first.
92893
92984
  addMethodStatements(statements, privateMethodsAndAccessors, receiver);
92894
92985
  addPropertyOrClassStaticBlockStatements(statements, properties, receiver);
92895
- // Add existing statements, skipping the initial super call.
92986
+ // Add existing statements after the initial prologues and super call
92896
92987
  if (constructor) {
92897
- ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, indexOfFirstStatement));
92988
+ ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitBodyStatement, ts.isStatement, indexOfFirstStatementAfterSuper + prologueStatementCount));
92898
92989
  }
92899
92990
  statements = factory.mergeLexicalEnvironment(statements, endLexicalEnvironment());
92900
92991
  return ts.setTextRange(factory.createBlock(ts.setTextRange(factory.createNodeArray(statements),
92901
92992
  /*location*/ constructor ? constructor.body.statements : node.members),
92902
92993
  /*multiLine*/ true),
92903
92994
  /*location*/ constructor ? constructor.body : undefined);
92995
+ function visitBodyStatement(statement) {
92996
+ if (useDefineForClassFields && ts.isParameterPropertyDeclaration(ts.getOriginalNode(statement), constructor)) {
92997
+ return undefined;
92998
+ }
92999
+ return visitor(statement);
93000
+ }
92904
93001
  }
92905
93002
  /**
92906
93003
  * Generates assignment statements for property initializers.
@@ -96692,37 +96789,39 @@ var ts;
96692
96789
  var prologue = [];
96693
96790
  var statements = [];
96694
96791
  resumeLexicalEnvironment();
96792
+ // In derived classes, there may be code before the necessary super() call
96793
+ // We'll remove pre-super statements to be tacked on after the rest of the body
96794
+ var existingPrologue = ts.takeWhile(constructor.body.statements, ts.isPrologueDirective);
96795
+ var _a = findSuperCallAndStatementIndex(constructor.body.statements, existingPrologue), superCall = _a.superCall, superStatementIndex = _a.superStatementIndex;
96796
+ var postSuperStatementsStart = superStatementIndex === -1 ? existingPrologue.length : superStatementIndex + 1;
96695
96797
  // If a super call has already been synthesized,
96696
96798
  // we're going to assume that we should just transform everything after that.
96697
96799
  // The assumption is that no prior step in the pipeline has added any prologue directives.
96698
- var statementOffset = 0;
96800
+ var statementOffset = postSuperStatementsStart;
96699
96801
  if (!hasSynthesizedSuper)
96700
- statementOffset = factory.copyStandardPrologue(constructor.body.statements, prologue, /*ensureUseStrict*/ false);
96701
- addDefaultValueAssignmentsIfNeeded(statements, constructor);
96702
- addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper);
96802
+ statementOffset = factory.copyStandardPrologue(constructor.body.statements, prologue, statementOffset, /*ensureUseStrict*/ false);
96703
96803
  if (!hasSynthesizedSuper)
96704
- statementOffset = factory.copyCustomPrologue(constructor.body.statements, statements, statementOffset, visitor);
96705
- // If the first statement is a call to `super()`, visit the statement directly
96804
+ statementOffset = factory.copyCustomPrologue(constructor.body.statements, statements, statementOffset, visitor, /*filter*/ undefined);
96805
+ // If there already exists a call to `super()`, visit the statement directly
96706
96806
  var superCallExpression;
96707
96807
  if (hasSynthesizedSuper) {
96708
96808
  superCallExpression = createDefaultSuperCallOrThis();
96709
96809
  }
96710
- else if (isDerivedClass && statementOffset < constructor.body.statements.length) {
96711
- var firstStatement = constructor.body.statements[statementOffset];
96712
- if (ts.isExpressionStatement(firstStatement) && ts.isSuperCall(firstStatement.expression)) {
96713
- superCallExpression = visitImmediateSuperCallInBody(firstStatement.expression);
96714
- }
96810
+ else if (superCall) {
96811
+ superCallExpression = visitSuperCallInBody(superCall);
96715
96812
  }
96716
96813
  if (superCallExpression) {
96717
96814
  hierarchyFacts |= 8192 /* ConstructorWithCapturedSuper */;
96718
- statementOffset++; // skip this statement, we will add it after visiting the rest of the body.
96719
96815
  }
96816
+ // Add parameter defaults at the beginning of the output, with prologue statements
96817
+ addDefaultValueAssignmentsIfNeeded(prologue, constructor);
96818
+ addRestParameterIfNeeded(prologue, constructor, hasSynthesizedSuper);
96720
96819
  // visit the remaining statements
96721
96820
  ts.addRange(statements, ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, /*start*/ statementOffset));
96722
96821
  factory.mergeLexicalEnvironment(prologue, endLexicalEnvironment());
96723
96822
  insertCaptureNewTargetIfNeeded(prologue, constructor, /*copyOnWrite*/ false);
96724
- if (isDerivedClass) {
96725
- if (superCallExpression && statementOffset === constructor.body.statements.length && !(constructor.body.transformFlags & 8192 /* ContainsLexicalThis */)) {
96823
+ if (isDerivedClass || superCallExpression) {
96824
+ if (superCallExpression && postSuperStatementsStart === constructor.body.statements.length && !(constructor.body.transformFlags & 8192 /* ContainsLexicalThis */)) {
96726
96825
  // If the subclass constructor does *not* contain `this` and *ends* with a `super()` call, we will use the
96727
96826
  // following representation:
96728
96827
  //
@@ -96742,10 +96841,10 @@ var ts;
96742
96841
  // return C;
96743
96842
  // })(Base);
96744
96843
  // ```
96745
- var superCall = ts.cast(ts.cast(superCallExpression, ts.isBinaryExpression).left, ts.isCallExpression);
96844
+ var superCall_1 = ts.cast(ts.cast(superCallExpression, ts.isBinaryExpression).left, ts.isCallExpression);
96746
96845
  var returnStatement = factory.createReturnStatement(superCallExpression);
96747
- ts.setCommentRange(returnStatement, ts.getCommentRange(superCall));
96748
- ts.setEmitFlags(superCall, 1536 /* NoComments */);
96846
+ ts.setCommentRange(returnStatement, ts.getCommentRange(superCall_1));
96847
+ ts.setEmitFlags(superCall_1, 1536 /* NoComments */);
96749
96848
  statements.push(returnStatement);
96750
96849
  }
96751
96850
  else {
@@ -96770,9 +96869,19 @@ var ts;
96770
96869
  // return C;
96771
96870
  // })(Base);
96772
96871
  // ```
96773
- // Since the `super()` call was the first statement, we insert the `this` capturing call to
96774
- // `super()` at the top of the list of `statements` (after any pre-existing custom prologues).
96775
- insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis());
96872
+ // If the super() call is the first statement, we can directly create and assign its result to `_this`
96873
+ if (superStatementIndex <= existingPrologue.length) {
96874
+ insertCaptureThisForNode(statements, constructor, superCallExpression || createActualThis());
96875
+ }
96876
+ // Since the `super()` call isn't the first statement, it's split across 1-2 statements:
96877
+ // * A prologue `var _this = this;`, in case the constructor accesses this before super()
96878
+ // * If it exists, a reassignment to that `_this` of the super() call
96879
+ else {
96880
+ insertCaptureThisForNode(prologue, constructor, createActualThis());
96881
+ if (superCallExpression) {
96882
+ insertSuperThisCaptureThisForNode(statements, superCallExpression);
96883
+ }
96884
+ }
96776
96885
  if (!isSufficientlyCoveredByReturnStatements(constructor.body)) {
96777
96886
  statements.push(factory.createReturnStatement(factory.createUniqueName("_this", 16 /* Optimistic */ | 32 /* FileLevel */)));
96778
96887
  }
@@ -96795,11 +96904,27 @@ var ts;
96795
96904
  // ```
96796
96905
  insertCaptureThisForNodeIfNeeded(prologue, constructor);
96797
96906
  }
96798
- var block = factory.createBlock(ts.setTextRange(factory.createNodeArray(ts.concatenate(prologue, statements)),
96907
+ 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)),
96799
96908
  /*location*/ constructor.body.statements),
96800
96909
  /*multiLine*/ true);
96801
- ts.setTextRange(block, constructor.body);
96802
- return block;
96910
+ ts.setTextRange(body, constructor.body);
96911
+ return body;
96912
+ }
96913
+ function findSuperCallAndStatementIndex(originalBodyStatements, existingPrologue) {
96914
+ for (var i = existingPrologue.length; i < originalBodyStatements.length; i += 1) {
96915
+ var superCall = ts.getSuperCallFromStatement(originalBodyStatements[i]);
96916
+ if (superCall) {
96917
+ // With a super() call, split the statements into pre-super() and 'body' (post-super())
96918
+ return {
96919
+ superCall: superCall,
96920
+ superStatementIndex: i,
96921
+ };
96922
+ }
96923
+ }
96924
+ // Since there was no super() call found, consider all statements to be in the main 'body' (post-super())
96925
+ return {
96926
+ superStatementIndex: -1,
96927
+ };
96803
96928
  }
96804
96929
  /**
96805
96930
  * We want to try to avoid emitting a return statement in certain cases if a user already returned something.
@@ -97028,6 +97153,18 @@ var ts;
97028
97153
  }
97029
97154
  return false;
97030
97155
  }
97156
+ /**
97157
+ * Assigns the `this` in a constructor to the result of its `super()` call.
97158
+ *
97159
+ * @param statements Statements in the constructor body.
97160
+ * @param superExpression Existing `super()` call for the constructor.
97161
+ */
97162
+ function insertSuperThisCaptureThisForNode(statements, superExpression) {
97163
+ enableSubstitutionsForCapturedThis();
97164
+ var assignSuperExpression = factory.createExpressionStatement(factory.createBinaryExpression(factory.createThis(), 63 /* EqualsToken */, superExpression));
97165
+ ts.insertStatementAfterCustomPrologue(statements, assignSuperExpression);
97166
+ ts.setCommentRange(assignSuperExpression, ts.getOriginalNode(superExpression).parent);
97167
+ }
97031
97168
  function insertCaptureThisForNode(statements, node, initializer) {
97032
97169
  enableSubstitutionsForCapturedThis();
97033
97170
  var captureThisStatement = factory.createVariableStatement(
@@ -97337,7 +97474,7 @@ var ts;
97337
97474
  if (ts.isBlock(body)) {
97338
97475
  // ensureUseStrict is false because no new prologue-directive should be added.
97339
97476
  // addStandardPrologue will put already-existing directives at the beginning of the target statement-array
97340
- statementOffset = factory.copyStandardPrologue(body.statements, prologue, /*ensureUseStrict*/ false);
97477
+ statementOffset = factory.copyStandardPrologue(body.statements, prologue, 0, /*ensureUseStrict*/ false);
97341
97478
  statementOffset = factory.copyCustomPrologue(body.statements, statements, statementOffset, visitor, ts.isHoistedFunction);
97342
97479
  statementOffset = factory.copyCustomPrologue(body.statements, statements, statementOffset, visitor, ts.isHoistedVariableStatement);
97343
97480
  }
@@ -98708,7 +98845,7 @@ var ts;
98708
98845
  /*type*/ undefined, factory.updateBlock(func.body, statements))),
98709
98846
  /*typeArguments*/ undefined, call.arguments))));
98710
98847
  }
98711
- function visitImmediateSuperCallInBody(node) {
98848
+ function visitSuperCallInBody(node) {
98712
98849
  return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ false);
98713
98850
  }
98714
98851
  function visitCallExpressionWithPotentialCapturedThisAssignment(node, assignToCapturedThis) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "typescript",
3
3
  "author": "Microsoft Corp.",
4
4
  "homepage": "https://www.typescriptlang.org/",
5
- "version": "4.6.0-dev.20220113",
5
+ "version": "4.6.0-dev.20220114",
6
6
  "license": "Apache-2.0",
7
7
  "description": "TypeScript is a language for application scale JavaScript development",
8
8
  "keywords": [