Skip to content

Commit e1aef1e

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

9 files changed

+70
-13
lines changed

src/compiler/checker.ts

+27-7
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) {
@@ -7936,7 +7937,9 @@ namespace ts {
79367937
}
79377938

79387939
function getParentSymbolOfTypeParameter(typeParameter: TypeParameter): Symbol | undefined {
7939-
return getSymbolOfNode(getDeclarationOfKind(typeParameter.symbol, SyntaxKind.TypeParameter)!.parent);
7940+
const tp = getDeclarationOfKind<TypeParameterDeclaration>(typeParameter.symbol, SyntaxKind.TypeParameter)!;
7941+
const host = isJSDocTemplateTag(tp.parent) ? getHostSignatureFromJSDoc(tp.parent) : tp.parent;
7942+
return host && getSymbolOfNode(host);
79407943
}
79417944

79427945
function getTypeListId(types: ReadonlyArray<Type> | undefined) {
@@ -22016,7 +22019,7 @@ namespace ts {
2201622019
checkSourceElement(node.default);
2201722020
const typeParameter = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node));
2201822021
if (!hasNonCircularBaseConstraint(typeParameter)) {
22019-
error(node.constraint, Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter));
22022+
error(getEffectiveConstraintOfTypeParameter(node), Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter));
2202022023
}
2202122024
if (!hasNonCircularTypeParameterDefault(typeParameter)) {
2202222025
error(node.default, Diagnostics.Type_parameter_0_has_a_circular_default, typeToString(typeParameter));
@@ -22749,7 +22752,7 @@ namespace ts {
2274922752

2275022753
const type = <MappedType>getTypeFromMappedTypeNode(node);
2275122754
const constraintType = getConstraintTypeFromMappedType(type);
22752-
checkTypeAssignableTo(constraintType, keyofConstraintType, node.typeParameter.constraint);
22755+
checkTypeAssignableTo(constraintType, keyofConstraintType, getEffectiveConstraintOfTypeParameter(node.typeParameter));
2275322756
}
2275422757

2275522758
function checkThisType(node: ThisTypeNode) {
@@ -23640,6 +23643,13 @@ namespace ts {
2364023643
checkSourceElement(node.typeExpression);
2364123644
}
2364223645

23646+
function checkJSDocTemplateTag(node: JSDocTemplateTag): void {
23647+
checkSourceElement(node.constraint);
23648+
for (const tp of node.typeParameters) {
23649+
checkSourceElement(tp);
23650+
}
23651+
}
23652+
2364323653
function checkJSDocTypeTag(node: JSDocTypeTag) {
2364423654
checkSourceElement(node.typeExpression);
2364523655
}
@@ -25427,7 +25437,8 @@ namespace ts {
2542725437

2542825438
// If the type parameter node does not have an identical constraint as the resolved
2542925439
// type parameter at this position, we report an error.
25430-
const sourceConstraint = source.constraint && getTypeFromTypeNode(source.constraint);
25440+
const constraint = getEffectiveConstraintOfTypeParameter(source);
25441+
const sourceConstraint = constraint && getTypeFromTypeNode(constraint);
2543125442
const targetConstraint = getConstraintOfTypeParameter(target);
2543225443
if (sourceConstraint) {
2543325444
// relax check if later interface augmentation has no constraint
@@ -26647,6 +26658,8 @@ namespace ts {
2664726658
case SyntaxKind.JSDocTypedefTag:
2664826659
case SyntaxKind.JSDocCallbackTag:
2664926660
return checkJSDocTypeAliasTag(node as JSDocTypedefTag);
26661+
case SyntaxKind.JSDocTemplateTag:
26662+
return checkJSDocTemplateTag(node as JSDocTemplateTag);
2665026663
case SyntaxKind.JSDocTypeTag:
2665126664
return checkJSDocTypeTag(node as JSDocTypeTag);
2665226665
case SyntaxKind.JSDocParameterTag:
@@ -29763,6 +29776,13 @@ namespace ts {
2976329776
}
2976429777
return false;
2976529778
}
29779+
29780+
function getEffectiveConstraintOfTypeParameter(node: TypeParameterDeclaration): TypeNode | undefined {
29781+
return node.constraint ? node.constraint
29782+
: isJSDocTemplateTag(node.parent) && node === node.parent.typeParameters[0]
29783+
? node.parent.constraint
29784+
: undefined;
29785+
}
2976629786
}
2976729787

2976829788
/** 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/api/tsserverlibrary.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,7 @@ declare namespace ts {
15681568
}
15691569
interface JSDocTemplateTag extends JSDocTag {
15701570
kind: SyntaxKind.JSDocTemplateTag;
1571+
constraint: TypeNode | undefined;
15711572
typeParameters: NodeArray<TypeParameterDeclaration>;
15721573
}
15731574
interface JSDocReturnTag extends JSDocTag {

tests/baselines/reference/api/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,7 @@ declare namespace ts {
15681568
}
15691569
interface JSDocTemplateTag extends JSDocTag {
15701570
kind: SyntaxKind.JSDocTemplateTag;
1571+
constraint: TypeNode | undefined;
15711572
typeParameters: NodeArray<TypeParameterDeclaration>;
15721573
}
15731574
interface JSDocReturnTag extends JSDocTag {

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");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @allowJs: true
4+
// @checkJs: true
5+
// @Filename: /foo.js
6+
7+
/////**
8+
//// * Doc
9+
//// * @template {new (...args: any[]) => any} T
10+
//// * @param {T} cls
11+
//// */
12+
////function /**/myMixin(cls) {
13+
//// return class extends cls {}
14+
////}
15+
16+
verify.quickInfoAt("",
17+
`function myMixin<T extends new (...args: any[]) => any>(cls: T): {
18+
new (...args: any[]): (Anonymous class);
19+
prototype: myMixin<any>.(Anonymous class);
20+
} & T`,
21+
"Doc");

0 commit comments

Comments
 (0)