@@ -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();
@@ -10196,6 +10197,7 @@ namespace ts {
1019610197
1019710198 checkCollisionWithCapturedSuperVariable(node, node);
1019810199 checkCollisionWithCapturedThisVariable(node, node);
10200+ checkCollisionWithCapturedNewTargetVariable(node, node);
1019910201 checkNestedBlockScopedBinding(node, symbol);
1020010202
1020110203 const type = getTypeOfSymbol(localOrExportSymbol);
@@ -13720,6 +13722,24 @@ namespace ts {
1372013722 return getNonNullableType(checkExpression(node.expression));
1372113723 }
1372213724
13725+ function checkMetaProperty(node: MetaProperty) {
13726+ checkGrammarMetaProperty(node);
13727+ Debug.assert(node.keywordToken === SyntaxKind.NewKeyword && node.name.text === "target", "Unrecognized meta-property.");
13728+ const container = getNewTargetContainer(node);
13729+ if (!container) {
13730+ error(node, Diagnostics.Meta_property_0_is_only_allowed_in_the_body_of_a_function_declaration_function_expression_or_constructor, "new.target");
13731+ return unknownType;
13732+ }
13733+ else if (container.kind === SyntaxKind.Constructor) {
13734+ const symbol = getSymbolOfNode(container.parent);
13735+ return getTypeOfSymbol(symbol);
13736+ }
13737+ else {
13738+ const symbol = getSymbolOfNode(container);
13739+ return getTypeOfSymbol(symbol);
13740+ }
13741+ }
13742+
1372313743 function getTypeOfParameter(symbol: Symbol) {
1372413744 const type = getTypeOfSymbol(symbol);
1372513745 if (strictNullChecks) {
@@ -14128,6 +14148,7 @@ namespace ts {
1412814148 if (produceDiagnostics && node.kind !== SyntaxKind.MethodDeclaration) {
1412914149 checkCollisionWithCapturedSuperVariable(node, (<FunctionExpression>node).name);
1413014150 checkCollisionWithCapturedThisVariable(node, (<FunctionExpression>node).name);
14151+ checkCollisionWithCapturedNewTargetVariable(node, (<FunctionExpression>node).name);
1413114152 }
1413214153
1413314154 return type;
@@ -15152,6 +15173,8 @@ namespace ts {
1515215173 return checkAssertion(<AssertionExpression>node);
1515315174 case SyntaxKind.NonNullExpression:
1515415175 return checkNonNullAssertion(<NonNullExpression>node);
15176+ case SyntaxKind.MetaProperty:
15177+ return checkMetaProperty(<MetaProperty>node);
1515515178 case SyntaxKind.DeleteExpression:
1515615179 return checkDeleteExpression(<DeleteExpression>node);
1515715180 case SyntaxKind.VoidExpression:
@@ -16552,6 +16575,7 @@ namespace ts {
1655216575
1655316576 checkCollisionWithCapturedSuperVariable(node, node.name);
1655416577 checkCollisionWithCapturedThisVariable(node, node.name);
16578+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
1655516579 checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
1655616580 checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
1655716581 }
@@ -16835,6 +16859,12 @@ namespace ts {
1683516859 }
1683616860 }
1683716861
16862+ function checkCollisionWithCapturedNewTargetVariable(node: Node, name: Identifier): void {
16863+ if (needCollisionCheckForIdentifier(node, name, "_newTarget")) {
16864+ potentialNewTargetCollisions.push(node);
16865+ }
16866+ }
16867+
1683816868 // this function will run after checking the source file so 'CaptureThis' is correct for all nodes
1683916869 function checkIfThisIsCapturedInEnclosingScope(node: Node): void {
1684016870 let current = node;
@@ -16853,6 +16883,23 @@ namespace ts {
1685316883 }
1685416884 }
1685516885
16886+ function checkIfNewTargetIsCapturedInEnclosingScope(node: Node): void {
16887+ let current = node;
16888+ while (current) {
16889+ if (getNodeCheckFlags(current) & NodeCheckFlags.CaptureNewTarget) {
16890+ const isDeclaration = node.kind !== SyntaxKind.Identifier;
16891+ if (isDeclaration) {
16892+ error((<Declaration>node).name, Diagnostics.Duplicate_identifier_newTarget_Compiler_uses_variable_declaration_newTarget_to_capture_new_target_meta_property_reference);
16893+ }
16894+ else {
16895+ error(node, Diagnostics.Expression_resolves_to_variable_declaration_newTarget_that_compiler_uses_to_capture_new_target_meta_property_reference);
16896+ }
16897+ return;
16898+ }
16899+ current = current.parent;
16900+ }
16901+ }
16902+
1685616903 function checkCollisionWithCapturedSuperVariable(node: Node, name: Identifier) {
1685716904 if (!needCollisionCheckForIdentifier(node, name, "_super")) {
1685816905 return;
@@ -17148,6 +17195,7 @@ namespace ts {
1714817195 }
1714917196 checkCollisionWithCapturedSuperVariable(node, <Identifier>node.name);
1715017197 checkCollisionWithCapturedThisVariable(node, <Identifier>node.name);
17198+ checkCollisionWithCapturedNewTargetVariable(node, <Identifier>node.name);
1715117199 checkCollisionWithRequireExportsInGeneratedCode(node, <Identifier>node.name);
1715217200 checkCollisionWithGlobalPromiseInGeneratedCode(node, <Identifier>node.name);
1715317201 }
@@ -17984,6 +18032,7 @@ namespace ts {
1798418032 if (node.name) {
1798518033 checkTypeNameIsReserved(node.name, Diagnostics.Class_name_cannot_be_0);
1798618034 checkCollisionWithCapturedThisVariable(node, node.name);
18035+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
1798718036 checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
1798818037 checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
1798918038 }
@@ -18518,6 +18567,7 @@ namespace ts {
1851818567
1851918568 checkTypeNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0);
1852018569 checkCollisionWithCapturedThisVariable(node, node.name);
18570+ checkCollisionWithCapturedNewTargetVariable(node, node.name);
1852118571 checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
1852218572 checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
1852318573 checkExportsOnMergedDeclarations(node);
@@ -19223,6 +19273,7 @@ namespace ts {
1922319273 checkGrammarSourceFile(node);
1922419274
1922519275 potentialThisCollisions.length = 0;
19276+ potentialNewTargetCollisions.length = 0;
1922619277
1922719278 deferredNodes = [];
1922819279 deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined;
@@ -19251,6 +19302,11 @@ namespace ts {
1925119302 potentialThisCollisions.length = 0;
1925219303 }
1925319304
19305+ if (potentialNewTargetCollisions.length) {
19306+ forEach(potentialNewTargetCollisions, checkIfNewTargetIsCapturedInEnclosingScope)
19307+ potentialNewTargetCollisions.length = 0;
19308+ }
19309+
1925419310 links.flags |= NodeCheckFlags.TypeChecked;
1925519311 }
1925619312 }
@@ -21581,6 +21637,14 @@ namespace ts {
2158121637 }
2158221638 }
2158321639
21640+ function checkGrammarMetaProperty(node: MetaProperty) {
21641+ if (node.keywordToken === SyntaxKind.NewKeyword) {
21642+ if (node.name.text !== "target") {
21643+ 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");
21644+ }
21645+ }
21646+ }
21647+
2158421648 function hasParseDiagnostics(sourceFile: SourceFile): boolean {
2158521649 return sourceFile.parseDiagnostics.length > 0;
2158621650 }
0 commit comments