@@ -241,6 +241,7 @@ namespace ts {
241241 const visitedFlowNodes: FlowNode[] = [];
242242 const visitedFlowTypes: FlowType[] = [];
243243 const potentialThisCollisions: Node[] = [];
244+ const potentialNewTargetCollisions: Node[] = [];
244245 const awaitedTypeStack: number[] = [];
245246
246247 const diagnostics = createDiagnosticCollection();
@@ -10368,6 +10369,7 @@ namespace ts {
1036810369
1036910370 checkCollisionWithCapturedSuperVariable(node, node);
1037010371 checkCollisionWithCapturedThisVariable(node, node);
10372+ checkCollisionWithCapturedNewTargetVariable(node, node);
1037110373 checkNestedBlockScopedBinding(node, symbol);
1037210374
1037310375 const type = getTypeOfSymbol(localOrExportSymbol);
@@ -10409,7 +10411,7 @@ namespace ts {
1040910411 // the entire control flow graph from the variable's declaration (i.e. when the flow container and
1041010412 // declaration container are the same).
1041110413 const assumeInitialized = isParameter || isOuterVariable ||
10412- type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0) ||
10414+ type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0 || isInTypeQuery(node) ) ||
1041310415 isInAmbientContext(declaration);
1041410416 const flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer);
1041510417 // A variable is considered uninitialized when it is possible to analyze the entire control flow graph
@@ -13893,6 +13895,24 @@ namespace ts {
1389313895 return getNonNullableType(checkExpression(node.expression));
1389413896 }
1389513897
13898+ function checkMetaProperty(node: MetaProperty) {
13899+ checkGrammarMetaProperty(node);
13900+ Debug.assert(node.keywordToken === SyntaxKind.NewKeyword && node.name.text === "target", "Unrecognized meta-property.");
13901+ const container = getNewTargetContainer(node);
13902+ if (!container) {
13903+ error(node, Diagnostics.Meta_property_0_is_only_allowed_in_the_body_of_a_function_declaration_function_expression_or_constructor, "new.target");
13904+ return unknownType;
13905+ }
13906+ else if (container.kind === SyntaxKind.Constructor) {
13907+ const symbol = getSymbolOfNode(container.parent);
13908+ return getTypeOfSymbol(symbol);
13909+ }
13910+ else {
13911+ const symbol = getSymbolOfNode(container);
13912+ return getTypeOfSymbol(symbol);
13913+ }
13914+ }
13915+
1389613916 function getTypeOfParameter(symbol: Symbol) {
1389713917 const type = getTypeOfSymbol(symbol);
1389813918 if (strictNullChecks) {
@@ -14301,6 +14321,7 @@ namespace ts {
1430114321 if (produceDiagnostics && node.kind !== SyntaxKind.MethodDeclaration) {
1430214322 checkCollisionWithCapturedSuperVariable(node, (<FunctionExpression>node).name);
1430314323 checkCollisionWithCapturedThisVariable(node, (<FunctionExpression>node).name);
14324+ checkCollisionWithCapturedNewTargetVariable(node, (<FunctionExpression>node).name);
1430414325 }
1430514326
1430614327 return type;
@@ -15335,6 +15356,8 @@ namespace ts {
1533515356 return checkAssertion(<AssertionExpression>node);
1533615357 case SyntaxKind.NonNullExpression:
1533715358 return checkNonNullAssertion(<NonNullExpression>node);
15359+ case SyntaxKind.MetaProperty:
15360+ return checkMetaProperty(<MetaProperty>node);
1533815361 case SyntaxKind.DeleteExpression:
1533915362 return checkDeleteExpression(<DeleteExpression>node);
1534015363 case SyntaxKind.VoidExpression:
@@ -16742,6 +16765,7 @@ namespace ts {
1674216765
1674316766 checkCollisionWithCapturedSuperVariable(node, node.name);
1674416767 checkCollisionWithCapturedThisVariable(node, node.name);
16768+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
1674516769 checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
1674616770 checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
1674716771 }
@@ -17034,6 +17058,12 @@ namespace ts {
1703417058 }
1703517059 }
1703617060
17061+ function checkCollisionWithCapturedNewTargetVariable(node: Node, name: Identifier): void {
17062+ if (needCollisionCheckForIdentifier(node, name, "_newTarget")) {
17063+ potentialNewTargetCollisions.push(node);
17064+ }
17065+ }
17066+
1703717067 // this function will run after checking the source file so 'CaptureThis' is correct for all nodes
1703817068 function checkIfThisIsCapturedInEnclosingScope(node: Node): void {
1703917069 let current = node;
@@ -17052,6 +17082,23 @@ namespace ts {
1705217082 }
1705317083 }
1705417084
17085+ function checkIfNewTargetIsCapturedInEnclosingScope(node: Node): void {
17086+ let current = node;
17087+ while (current) {
17088+ if (getNodeCheckFlags(current) & NodeCheckFlags.CaptureNewTarget) {
17089+ const isDeclaration = node.kind !== SyntaxKind.Identifier;
17090+ if (isDeclaration) {
17091+ error((<Declaration>node).name, Diagnostics.Duplicate_identifier_newTarget_Compiler_uses_variable_declaration_newTarget_to_capture_new_target_meta_property_reference);
17092+ }
17093+ else {
17094+ error(node, Diagnostics.Expression_resolves_to_variable_declaration_newTarget_that_compiler_uses_to_capture_new_target_meta_property_reference);
17095+ }
17096+ return;
17097+ }
17098+ current = current.parent;
17099+ }
17100+ }
17101+
1705517102 function checkCollisionWithCapturedSuperVariable(node: Node, name: Identifier) {
1705617103 if (!needCollisionCheckForIdentifier(node, name, "_super")) {
1705717104 return;
@@ -17348,6 +17395,7 @@ namespace ts {
1734817395 }
1734917396 checkCollisionWithCapturedSuperVariable(node, <Identifier>node.name);
1735017397 checkCollisionWithCapturedThisVariable(node, <Identifier>node.name);
17398+ checkCollisionWithCapturedNewTargetVariable(node, <Identifier>node.name);
1735117399 checkCollisionWithRequireExportsInGeneratedCode(node, <Identifier>node.name);
1735217400 checkCollisionWithGlobalPromiseInGeneratedCode(node, <Identifier>node.name);
1735317401 }
@@ -18184,6 +18232,7 @@ namespace ts {
1818418232 if (node.name) {
1818518233 checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0);
1818618234 checkCollisionWithCapturedThisVariable(node, node.name);
18235+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
1818718236 checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
1818818237 checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
1818918238 }
@@ -18719,6 +18768,7 @@ namespace ts {
1871918768
1872018769 checkTypeNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0);
1872118770 checkCollisionWithCapturedThisVariable(node, node.name);
18771+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
1872218772 checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
1872318773 checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
1872418774 checkExportsOnMergedDeclarations(node);
@@ -19424,6 +19474,7 @@ namespace ts {
1942419474 checkGrammarSourceFile(node);
1942519475
1942619476 potentialThisCollisions.length = 0;
19477+ potentialNewTargetCollisions.length = 0;
1942719478
1942819479 deferredNodes = [];
1942919480 deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined;
@@ -19452,6 +19503,11 @@ namespace ts {
1945219503 potentialThisCollisions.length = 0;
1945319504 }
1945419505
19506+ if (potentialNewTargetCollisions.length) {
19507+ forEach(potentialNewTargetCollisions, checkIfNewTargetIsCapturedInEnclosingScope)
19508+ potentialNewTargetCollisions.length = 0;
19509+ }
19510+
1945519511 links.flags |= NodeCheckFlags.TypeChecked;
1945619512 }
1945719513 }
@@ -21780,6 +21836,14 @@ namespace ts {
2178021836 }
2178121837 }
2178221838
21839+ function checkGrammarMetaProperty(node: MetaProperty) {
21840+ if (node.keywordToken === SyntaxKind.NewKeyword) {
21841+ if (node.name.text !== "target") {
21842+ 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");
21843+ }
21844+ }
21845+ }
21846+
2178321847 function hasParseDiagnostics(sourceFile: SourceFile): boolean {
2178421848 return sourceFile.parseDiagnostics.length > 0;
2178521849 }
0 commit comments