Skip to content

support @extends in jsdoc #18706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 9, 2017
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
14 changes: 10 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20009,14 +20009,20 @@ namespace ts {
}

function checkJSDocAugmentsTag(node: JSDocAugmentsTag): void {
const cls = getJSDocHost(node);
if (!isClassDeclaration(cls) && !isClassExpression(cls)) {
error(cls, Diagnostics.JSDoc_augments_is_not_attached_to_a_class_declaration);
const classLike = getJSDocHost(node);
if (!isClassDeclaration(classLike) && !isClassExpression(classLike)) {
error(classLike, Diagnostics.JSDoc_augments_is_not_attached_to_a_class_declaration);
return;
}

const augmentsTags = getAllJSDocTagsOfKind(classLike, SyntaxKind.JSDocAugmentsTag);
Debug.assert(augmentsTags.length > 0);
if (augmentsTags.length > 1) {
error(augmentsTags[1], Diagnostics.Class_declarations_cannot_have_more_than_one_augments_or_extends_tag);
}

const name = getIdentifierFromEntityNameExpression(node.class.expression);
const extend = getClassExtendsHeritageClauseElement(cls);
const extend = getClassExtendsHeritageClauseElement(classLike);
if (extend) {
const className = getIdentifierFromEntityNameExpression(extend.expression);
if (className && name.escapedText !== className.escapedText) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3527,6 +3527,10 @@
"category": "Error",
"code": 8024
},
"Class declarations cannot have more than one `@augments` or `@extends` tag.": {
"category": "Error",
"code": 8025
},
"Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clause.": {
"category": "Error",
"code": 9002
Expand Down
1 change: 1 addition & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6373,6 +6373,7 @@ namespace ts {
if (tagName) {
switch (tagName.escapedText) {
case "augments":
case "extends":
tag = parseAugmentsTag(atToken, tagName);
break;
case "class":
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,10 @@ namespace ts {
kind: SyntaxKind.JSDocTag;
}

/**
* Note that `@extends` is a synonym of `@augments`.
* Both tags are represented by this interface.
*/
export interface JSDocAugmentsTag extends JSDocTag {
kind: SyntaxKind.JSDocAugmentsTag;
class: ExpressionWithTypeArguments & { expression: Identifier | PropertyAccessEntityNameExpression };
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4247,6 +4247,12 @@ namespace ts {
return find(tags, doc => doc.kind === kind);
}

/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
export function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag> | undefined {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove 'All'

const tags = getJSDocTags(node);
return filter(tags, doc => doc.kind === kind);
}

}

// Simple node tests of the form `node.kind === SyntaxKind.Foo`.
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,10 @@ declare namespace ts {
interface JSDocUnknownTag extends JSDocTag {
kind: SyntaxKind.JSDocTag;
}
/**
* Note that `@extends` is a synonym of `@augments`.
* Both tags are represented by this interface.
*/
interface JSDocAugmentsTag extends JSDocTag {
kind: SyntaxKind.JSDocAugmentsTag;
class: ExpressionWithTypeArguments & {
Expand Down Expand Up @@ -2866,6 +2870,8 @@ declare namespace ts {
function getJSDocReturnType(node: Node): TypeNode | undefined;
/** Get all JSDoc tags related to a node, including those on parent nodes. */
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag> | undefined;
/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag> | undefined;
}
declare namespace ts {
function isNumericLiteral(node: Node): node is NumericLiteral;
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,10 @@ declare namespace ts {
interface JSDocUnknownTag extends JSDocTag {
kind: SyntaxKind.JSDocTag;
}
/**
* Note that `@extends` is a synonym of `@augments`.
* Both tags are represented by this interface.
*/
interface JSDocAugmentsTag extends JSDocTag {
kind: SyntaxKind.JSDocAugmentsTag;
class: ExpressionWithTypeArguments & {
Expand Down Expand Up @@ -2921,6 +2925,8 @@ declare namespace ts {
function getJSDocReturnType(node: Node): TypeNode | undefined;
/** Get all JSDoc tags related to a node, including those on parent nodes. */
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag> | undefined;
/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag> | undefined;
}
declare namespace ts {
function isNumericLiteral(node: Node): node is NumericLiteral;
Expand Down
1 change: 0 additions & 1 deletion tests/cases/fourslash/jsDocAugments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@

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

37 changes: 37 additions & 0 deletions tests/cases/fourslash/jsDocAugmentsAndExtends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
///<reference path="fourslash.ts" />

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

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

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

// if more than one tag is present, report an error and take the type of the first entry.

goTo.marker();
verify.quickInfoIs("(local var) x: number");
verify.getSemanticDiagnostics(
`[
{
"message": "Class declarations cannot have more than one \`@augments\` or \`@extends\` tag.",
"start": 36,
"length": 24,
"category": "error",
"code": 8025
}
]`);
22 changes: 22 additions & 0 deletions tests/cases/fourslash/jsDocExtends.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
///<reference path="fourslash.ts" />

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

//// /**
//// * @extends {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");