Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3796,6 +3796,16 @@ namespace ts {
}
baseType = getReturnTypeOfSignature(constructors[0]);
}

// In a JS file, you can use the @augments jsdoc tag to specify a base type with type parameters
const valueDecl = type.symbol.valueDeclaration;
if (valueDecl && isInJavaScriptFile(valueDecl)) {
const augTag = getJSDocAugmentsTag(type.symbol.valueDeclaration);
if (augTag) {
baseType = getTypeFromTypeNode(augTag.typeExpression.type);
}
}

if (baseType === unknownType) {
return;
}
Expand All @@ -3804,7 +3814,7 @@ namespace ts {
return;
}
if (type === baseType || hasBaseType(<InterfaceType>baseType, type)) {
error(type.symbol.valueDeclaration, Diagnostics.Type_0_recursively_references_itself_as_a_base_type,
error(valueDecl, Diagnostics.Type_0_recursively_references_itself_as_a_base_type,
typeToString(type, /*enclosingDeclaration*/ undefined, TypeFormatFlags.WriteArrayAsGenericType));
return;
}
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ namespace ts {
return visitNode(cbNode, (<JSDocReturnTag>node).typeExpression);
case SyntaxKind.JSDocTypeTag:
return visitNode(cbNode, (<JSDocTypeTag>node).typeExpression);
case SyntaxKind.JSDocAugmentsTag:
return visitNode(cbNode, (<JSDocAugmentsTag>node).typeExpression);
case SyntaxKind.JSDocTemplateTag:
return visitNodes(cbNodes, (<JSDocTemplateTag>node).typeParameters);
case SyntaxKind.JSDocTypedefTag:
Expand Down Expand Up @@ -6426,6 +6428,9 @@ namespace ts {
let tag: JSDocTag;
if (tagName) {
switch (tagName.text) {
case "augments":
tag = parseAugmentsTag(atToken, tagName);
break;
case "param":
tag = parseParamTag(atToken, tagName);
break;
Expand Down Expand Up @@ -6642,6 +6647,16 @@ namespace ts {
return finishNode(result);
}

function parseAugmentsTag(atToken: AtToken, tagName: Identifier): JSDocAugmentsTag {
const typeExpression = tryParseTypeExpression();

const result = <JSDocAugmentsTag>createNode(SyntaxKind.JSDocAugmentsTag, atToken.pos);
result.atToken = atToken;
result.tagName = tagName;
result.typeExpression = typeExpression;
return finishNode(result);
}

function parseTypedefTag(atToken: AtToken, tagName: Identifier): JSDocTypedefTag {
const typeExpression = tryParseTypeExpression();
skipWhitespace();
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ namespace ts {
JSDocThisType,
JSDocComment,
JSDocTag,
JSDocAugmentsTag,
JSDocParameterTag,
JSDocReturnTag,
JSDocTypeTag,
Expand Down Expand Up @@ -1989,6 +1990,11 @@ namespace ts {
kind: SyntaxKind.JSDocTag;
}

export interface JSDocAugmentsTag extends JSDocTag {
kind: SyntaxKind.JSDocAugmentsTag;
typeExpression: JSDocTypeExpression;
}

export interface JSDocTemplateTag extends JSDocTag {
kind: SyntaxKind.JSDocTemplateTag;
typeParameters: NodeArray<TypeParameterDeclaration>;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,10 @@ namespace ts {
return tag && tag.typeExpression && tag.typeExpression.type;
}

export function getJSDocAugmentsTag(node: Node): JSDocAugmentsTag {
return getFirstJSDocTag(node, SyntaxKind.JSDocAugmentsTag) as JSDocAugmentsTag;
}

export function getJSDocReturnTag(node: Node): JSDocReturnTag {
return getFirstJSDocTag(node, SyntaxKind.JSDocReturnTag) as JSDocReturnTag;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/cases/fourslash/jsDocAugments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
///<reference path="fourslash.ts" />

// @allowJs: true
// @Filename: dummy.js

//// /**
//// * @augments {Thing<string>}
//// */
//// class MyStringThing extends Thing {
//// constructor() {
//// var x = this.mine;
//// x/**/;
//// }
//// }

// @Filename: declarations.d.ts
//// declare class Thing<T> {
//// mine: T;
//// }

goTo.marker();
verify.quickInfoIs("(local var) x: string");