Skip to content

Commit 8ca4412

Browse files
author
Andy Hanson
committed
Don't store @template constraint in a TypeParameterDeclaration node
1 parent 1a05f13 commit 8ca4412

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

src/compiler/checker.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -5948,7 +5948,8 @@ namespace ts {
59485948

59495949
/** A type parameter is thisless if its contraint is thisless, or if it has no constraint. */
59505950
function isThislessTypeParameter(node: TypeParameterDeclaration) {
5951-
return !node.constraint || isThislessType(node.constraint);
5951+
const constraint = getEffectiveConstraintOfTypeParameter(node);
5952+
return !constraint || isThislessType(constraint);
59525953
}
59535954

59545955
/**
@@ -6735,7 +6736,7 @@ namespace ts {
67356736
}
67366737

67376738
function getConstraintDeclarationForMappedType(type: MappedType) {
6738-
return type.declaration.typeParameter.constraint;
6739+
return getEffectiveConstraintOfTypeParameter(type.declaration.typeParameter);
67396740
}
67406741

67416742
function isMappedTypeWithKeyofConstraintDeclaration(type: MappedType) {
@@ -7872,7 +7873,7 @@ namespace ts {
78727873

78737874
function getConstraintDeclaration(type: TypeParameter) {
78747875
const decl = type.symbol && getDeclarationOfKind<TypeParameterDeclaration>(type.symbol, SyntaxKind.TypeParameter);
7875-
return decl && decl.constraint;
7876+
return decl && getEffectiveConstraintOfTypeParameter(decl);
78767877
}
78777878

78787879
function getInferredTypeParameterConstraint(typeParameter: TypeParameter) {
@@ -22016,7 +22017,7 @@ namespace ts {
2201622017
checkSourceElement(node.default);
2201722018
const typeParameter = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node));
2201822019
if (!hasNonCircularBaseConstraint(typeParameter)) {
22019-
error(node.constraint, Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter));
22020+
error(getEffectiveConstraintOfTypeParameter(node), Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter));
2202022021
}
2202122022
if (!hasNonCircularTypeParameterDefault(typeParameter)) {
2202222023
error(node.default, Diagnostics.Type_parameter_0_has_a_circular_default, typeToString(typeParameter));
@@ -22749,7 +22750,7 @@ namespace ts {
2274922750

2275022751
const type = <MappedType>getTypeFromMappedTypeNode(node);
2275122752
const constraintType = getConstraintTypeFromMappedType(type);
22752-
checkTypeAssignableTo(constraintType, keyofConstraintType, node.typeParameter.constraint);
22753+
checkTypeAssignableTo(constraintType, keyofConstraintType, getEffectiveConstraintOfTypeParameter(node.typeParameter));
2275322754
}
2275422755

2275522756
function checkThisType(node: ThisTypeNode) {
@@ -23640,6 +23641,13 @@ namespace ts {
2364023641
checkSourceElement(node.typeExpression);
2364123642
}
2364223643

23644+
function checkJSDocTemplateTag(node: JSDocTemplateTag): void {
23645+
checkSourceElement(node.constraint);
23646+
for (const tp of node.typeParameters) {
23647+
checkSourceElement(tp);
23648+
}
23649+
}
23650+
2364323651
function checkJSDocTypeTag(node: JSDocTypeTag) {
2364423652
checkSourceElement(node.typeExpression);
2364523653
}
@@ -25427,7 +25435,8 @@ namespace ts {
2542725435

2542825436
// If the type parameter node does not have an identical constraint as the resolved
2542925437
// type parameter at this position, we report an error.
25430-
const sourceConstraint = source.constraint && getTypeFromTypeNode(source.constraint);
25438+
const constraint = getEffectiveConstraintOfTypeParameter(source);
25439+
const sourceConstraint = constraint && getTypeFromTypeNode(constraint);
2543125440
const targetConstraint = getConstraintOfTypeParameter(target);
2543225441
if (sourceConstraint) {
2543325442
// relax check if later interface augmentation has no constraint
@@ -26647,6 +26656,8 @@ namespace ts {
2664726656
case SyntaxKind.JSDocTypedefTag:
2664826657
case SyntaxKind.JSDocCallbackTag:
2664926658
return checkJSDocTypeAliasTag(node as JSDocTypedefTag);
26659+
case SyntaxKind.JSDocTemplateTag:
26660+
return checkJSDocTemplateTag(node as JSDocTemplateTag);
2665026661
case SyntaxKind.JSDocTypeTag:
2665126662
return checkJSDocTypeTag(node as JSDocTypeTag);
2665226663
case SyntaxKind.JSDocParameterTag:
@@ -29763,6 +29774,13 @@ namespace ts {
2976329774
}
2976429775
return false;
2976529776
}
29777+
29778+
function getEffectiveConstraintOfTypeParameter(node: TypeParameterDeclaration): TypeNode | undefined {
29779+
return node.constraint ? node.constraint
29780+
: isJSDocTemplateTag(node.parent) && node === node.parent.typeParameters[0]
29781+
? node.parent.constraint
29782+
: undefined;
29783+
}
2976629784
}
2976729785

2976829786
/** Like 'isDeclarationName', but returns true for LHS of `import { x as y }` or `export { x as y }`. */

src/compiler/parser.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ namespace ts {
475475
case SyntaxKind.JSDocAugmentsTag:
476476
return visitNode(cbNode, (<JSDocAugmentsTag>node).class);
477477
case SyntaxKind.JSDocTemplateTag:
478-
return visitNodes(cbNode, cbNodes, (<JSDocTemplateTag>node).typeParameters);
478+
return visitNode(cbNode, (<JSDocTemplateTag>node).constraint) || visitNodes(cbNode, cbNodes, (<JSDocTemplateTag>node).typeParameters);
479479
case SyntaxKind.JSDocTypedefTag:
480480
if ((node as JSDocTypedefTag).typeExpression &&
481481
(node as JSDocTypedefTag).typeExpression!.kind === SyntaxKind.JSDocTypeExpression) {
@@ -7049,13 +7049,10 @@ namespace ts {
70497049
typeParameters.push(typeParameter);
70507050
} while (parseOptionalJsdoc(SyntaxKind.CommaToken));
70517051

7052-
if (constraint) {
7053-
first(typeParameters).constraint = constraint.type;
7054-
}
7055-
70567052
const result = <JSDocTemplateTag>createNode(SyntaxKind.JSDocTemplateTag, atToken.pos);
70577053
result.atToken = atToken;
70587054
result.tagName = tagName;
7055+
result.constraint = constraint;
70597056
result.typeParameters = createNodeArray(typeParameters, typeParametersPos);
70607057
finishNode(result);
70617058
return result;

src/compiler/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ namespace ts {
759759
kind: SyntaxKind.TypeParameter;
760760
parent: DeclarationWithTypeParameterChildren | InferTypeNode;
761761
name: Identifier;
762+
// Note: Consider calling `getEffectiveConstraintOfTypeParameter`
762763
constraint?: TypeNode;
763764
default?: TypeNode;
764765

@@ -2363,6 +2364,7 @@ namespace ts {
23632364

23642365
export interface JSDocTemplateTag extends JSDocTag {
23652366
kind: SyntaxKind.JSDocTemplateTag;
2367+
constraint: TypeNode | undefined;
23662368
typeParameters: NodeArray<TypeParameterDeclaration>;
23672369
}
23682370

src/compiler/utilities.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,8 @@ namespace ts {
10161016
return !isExpressionWithTypeArgumentsInClassExtendsClause(parent);
10171017
case SyntaxKind.TypeParameter:
10181018
return node === (<TypeParameterDeclaration>parent).constraint;
1019+
case SyntaxKind.JSDocTemplateTag:
1020+
return node === (<JSDocTemplateTag>parent).constraint;
10191021
case SyntaxKind.PropertyDeclaration:
10201022
case SyntaxKind.PropertySignature:
10211023
case SyntaxKind.Parameter:

tests/baselines/reference/jsdocTemplateTag3.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ tests/cases/conformance/jsdoc/a.js(14,29): error TS2339: Property 'a' does not e
22
tests/cases/conformance/jsdoc/a.js(14,35): error TS2339: Property 'b' does not exist on type 'U'.
33
tests/cases/conformance/jsdoc/a.js(21,3): error TS2345: Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; b: string; }'.
44
Property 'b' is missing in type '{ a: number; }'.
5+
tests/cases/conformance/jsdoc/a.js(24,15): error TS2304: Cannot find name 'NoLongerAllowed'.
56
tests/cases/conformance/jsdoc/a.js(25,2): error TS1069: Unexpected token. A type parameter name was expected without curly braces.
67

78

8-
==== tests/cases/conformance/jsdoc/a.js (4 errors) ====
9+
==== tests/cases/conformance/jsdoc/a.js (5 errors) ====
910
/**
1011
* @template {{ a: number, b: string }} T,U A Comment
1112
* @template {{ c: boolean }} V uh ... are comments even supported??
@@ -37,6 +38,8 @@ tests/cases/conformance/jsdoc/a.js(25,2): error TS1069: Unexpected token. A type
3738

3839
/**
3940
* @template {NoLongerAllowed}
41+
~~~~~~~~~~~~~~~
42+
!!! error TS2304: Cannot find name 'NoLongerAllowed'.
4043
* @template T preceding line's syntax is no longer allowed
4144
~
4245
!!! error TS1069: Unexpected token. A type parameter name was expected without curly braces.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////**
4+
//// * @template {/**/
5+
//// */
6+
////function f() {}
7+
8+
goTo.marker("");
9+
edit.insert("n");
10+
edit.insert("u");

0 commit comments

Comments
 (0)