@@ -241,6 +241,7 @@ namespace ts {
241
241
const visitedFlowNodes: FlowNode[] = [];
242
242
const visitedFlowTypes: FlowType[] = [];
243
243
const potentialThisCollisions: Node[] = [];
244
+ const potentialNewTargetCollisions: Node[] = [];
244
245
const awaitedTypeStack: number[] = [];
245
246
246
247
const diagnostics = createDiagnosticCollection();
@@ -10331,6 +10332,7 @@ namespace ts {
10331
10332
10332
10333
checkCollisionWithCapturedSuperVariable(node, node);
10333
10334
checkCollisionWithCapturedThisVariable(node, node);
10335
+ checkCollisionWithCapturedNewTargetVariable(node, node);
10334
10336
checkNestedBlockScopedBinding(node, symbol);
10335
10337
10336
10338
const type = getTypeOfSymbol(localOrExportSymbol);
@@ -13857,6 +13859,24 @@ namespace ts {
13857
13859
return getNonNullableType(checkExpression(node.expression));
13858
13860
}
13859
13861
13862
+ function checkMetaProperty(node: MetaProperty) {
13863
+ checkGrammarMetaProperty(node);
13864
+ Debug.assert(node.keywordToken === SyntaxKind.NewKeyword && node.name.text === "target", "Unrecognized meta-property.");
13865
+ const container = getNewTargetContainer(node);
13866
+ if (!container) {
13867
+ error(node, Diagnostics.Meta_property_0_is_only_allowed_in_the_body_of_a_function_declaration_function_expression_or_constructor, "new.target");
13868
+ return unknownType;
13869
+ }
13870
+ else if (container.kind === SyntaxKind.Constructor) {
13871
+ const symbol = getSymbolOfNode(container.parent);
13872
+ return getTypeOfSymbol(symbol);
13873
+ }
13874
+ else {
13875
+ const symbol = getSymbolOfNode(container);
13876
+ return getTypeOfSymbol(symbol);
13877
+ }
13878
+ }
13879
+
13860
13880
function getTypeOfParameter(symbol: Symbol) {
13861
13881
const type = getTypeOfSymbol(symbol);
13862
13882
if (strictNullChecks) {
@@ -14265,6 +14285,7 @@ namespace ts {
14265
14285
if (produceDiagnostics && node.kind !== SyntaxKind.MethodDeclaration) {
14266
14286
checkCollisionWithCapturedSuperVariable(node, (<FunctionExpression>node).name);
14267
14287
checkCollisionWithCapturedThisVariable(node, (<FunctionExpression>node).name);
14288
+ checkCollisionWithCapturedNewTargetVariable(node, (<FunctionExpression>node).name);
14268
14289
}
14269
14290
14270
14291
return type;
@@ -15299,6 +15320,8 @@ namespace ts {
15299
15320
return checkAssertion(<AssertionExpression>node);
15300
15321
case SyntaxKind.NonNullExpression:
15301
15322
return checkNonNullAssertion(<NonNullExpression>node);
15323
+ case SyntaxKind.MetaProperty:
15324
+ return checkMetaProperty(<MetaProperty>node);
15302
15325
case SyntaxKind.DeleteExpression:
15303
15326
return checkDeleteExpression(<DeleteExpression>node);
15304
15327
case SyntaxKind.VoidExpression:
@@ -16706,6 +16729,7 @@ namespace ts {
16706
16729
16707
16730
checkCollisionWithCapturedSuperVariable(node, node.name);
16708
16731
checkCollisionWithCapturedThisVariable(node, node.name);
16732
+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
16709
16733
checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
16710
16734
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
16711
16735
}
@@ -17000,6 +17024,12 @@ namespace ts {
17000
17024
}
17001
17025
}
17002
17026
17027
+ function checkCollisionWithCapturedNewTargetVariable(node: Node, name: Identifier): void {
17028
+ if (needCollisionCheckForIdentifier(node, name, "_newTarget")) {
17029
+ potentialNewTargetCollisions.push(node);
17030
+ }
17031
+ }
17032
+
17003
17033
// this function will run after checking the source file so 'CaptureThis' is correct for all nodes
17004
17034
function checkIfThisIsCapturedInEnclosingScope(node: Node): void {
17005
17035
let current = node;
@@ -17018,6 +17048,23 @@ namespace ts {
17018
17048
}
17019
17049
}
17020
17050
17051
+ function checkIfNewTargetIsCapturedInEnclosingScope(node: Node): void {
17052
+ let current = node;
17053
+ while (current) {
17054
+ if (getNodeCheckFlags(current) & NodeCheckFlags.CaptureNewTarget) {
17055
+ const isDeclaration = node.kind !== SyntaxKind.Identifier;
17056
+ if (isDeclaration) {
17057
+ error((<Declaration>node).name, Diagnostics.Duplicate_identifier_newTarget_Compiler_uses_variable_declaration_newTarget_to_capture_new_target_meta_property_reference);
17058
+ }
17059
+ else {
17060
+ error(node, Diagnostics.Expression_resolves_to_variable_declaration_newTarget_that_compiler_uses_to_capture_new_target_meta_property_reference);
17061
+ }
17062
+ return;
17063
+ }
17064
+ current = current.parent;
17065
+ }
17066
+ }
17067
+
17021
17068
function checkCollisionWithCapturedSuperVariable(node: Node, name: Identifier) {
17022
17069
if (!needCollisionCheckForIdentifier(node, name, "_super")) {
17023
17070
return;
@@ -17314,6 +17361,7 @@ namespace ts {
17314
17361
}
17315
17362
checkCollisionWithCapturedSuperVariable(node, <Identifier>node.name);
17316
17363
checkCollisionWithCapturedThisVariable(node, <Identifier>node.name);
17364
+ checkCollisionWithCapturedNewTargetVariable(node, <Identifier>node.name);
17317
17365
checkCollisionWithRequireExportsInGeneratedCode(node, <Identifier>node.name);
17318
17366
checkCollisionWithGlobalPromiseInGeneratedCode(node, <Identifier>node.name);
17319
17367
}
@@ -18150,6 +18198,7 @@ namespace ts {
18150
18198
if (node.name) {
18151
18199
checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0);
18152
18200
checkCollisionWithCapturedThisVariable(node, node.name);
18201
+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
18153
18202
checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
18154
18203
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
18155
18204
}
@@ -18685,6 +18734,7 @@ namespace ts {
18685
18734
18686
18735
checkTypeNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0);
18687
18736
checkCollisionWithCapturedThisVariable(node, node.name);
18737
+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
18688
18738
checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
18689
18739
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
18690
18740
checkExportsOnMergedDeclarations(node);
@@ -19396,6 +19446,7 @@ namespace ts {
19396
19446
checkGrammarSourceFile(node);
19397
19447
19398
19448
potentialThisCollisions.length = 0;
19449
+ potentialNewTargetCollisions.length = 0;
19399
19450
19400
19451
deferredNodes = [];
19401
19452
deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined;
@@ -19424,6 +19475,11 @@ namespace ts {
19424
19475
potentialThisCollisions.length = 0;
19425
19476
}
19426
19477
19478
+ if (potentialNewTargetCollisions.length) {
19479
+ forEach(potentialNewTargetCollisions, checkIfNewTargetIsCapturedInEnclosingScope)
19480
+ potentialNewTargetCollisions.length = 0;
19481
+ }
19482
+
19427
19483
links.flags |= NodeCheckFlags.TypeChecked;
19428
19484
}
19429
19485
}
@@ -21754,6 +21810,14 @@ namespace ts {
21754
21810
}
21755
21811
}
21756
21812
21813
+ function checkGrammarMetaProperty(node: MetaProperty) {
21814
+ if (node.keywordToken === SyntaxKind.NewKeyword) {
21815
+ if (node.name.text !== "target") {
21816
+ return grammarErrorOnNode(node.name, Diagnostics._0_is_not_a_valid_meta_property_for_keyword_1_Did_you_mean_0, node.name.text, tokenToString(node.keywordToken), "target");
21817
+ }
21818
+ }
21819
+ }
21820
+
21757
21821
function hasParseDiagnostics(sourceFile: SourceFile): boolean {
21758
21822
return sourceFile.parseDiagnostics.length > 0;
21759
21823
}
0 commit comments