Skip to content
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

Improve @implements error #58904

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42919,7 +42919,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

const name = getIdentifierFromEntityNameExpression(node.class.expression);
const extend = getClassExtendsHeritageElement(classLike);
if (extend) {
if (name && extend) {
const className = getIdentifierFromEntityNameExpression(extend.expression);
if (className && name.escapedText !== className.escapedText) {
error(name, Diagnostics.JSDoc_0_1_does_not_match_the_extends_2_clause, idText(node.tagName), idText(name), idText(className));
Expand Down
28 changes: 12 additions & 16 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ import {
PrefixUnaryOperator,
PrimaryExpression,
PrivateIdentifier,
PropertyAccessEntityNameExpression,
PropertyAccessExpression,
PropertyAssignment,
PropertyDeclaration,
Expand Down Expand Up @@ -9474,12 +9473,12 @@ namespace Parser {
}

function parseImplementsTag(start: number, tagName: Identifier, margin: number, indentText: string): JSDocImplementsTag {
const className = parseExpressionWithTypeArgumentsForAugments();
const className = parseExpressionWithTypeArgumentsForJSDoc();
return finishNode(factory.createJSDocImplementsTag(tagName, className, parseTrailingTagComments(start, getNodePos(), margin, indentText)), start);
}

function parseAugmentsTag(start: number, tagName: Identifier, margin: number, indentText: string): JSDocAugmentsTag {
const className = parseExpressionWithTypeArgumentsForAugments();
const className = parseExpressionWithTypeArgumentsForJSDoc();
return finishNode(factory.createJSDocAugmentsTag(tagName, className, parseTrailingTagComments(start, getNodePos(), margin, indentText)), start);
}

Expand All @@ -9505,31 +9504,28 @@ namespace Parser {
return finishNode(factory.createJSDocImportTag(tagName, importClause, moduleSpecifier, attributes, comments), start);
}

function parseExpressionWithTypeArgumentsForAugments(): ExpressionWithTypeArguments & { expression: Identifier | PropertyAccessEntityNameExpression; } {
function parseExpressionWithTypeArgumentsForJSDoc(): ExpressionWithTypeArguments {
const usedBrace = parseOptional(SyntaxKind.OpenBraceToken);
const pos = getNodePos();
const expression = parsePropertyAccessEntityNameExpression();
const saveParsingContext = parsingContext;
parsingContext |= 1 << ParsingContext.HeritageClauseElement;
const expression = parseLeftHandSideExpressionOrHigher();
parsingContext = saveParsingContext;
if (expression.kind === SyntaxKind.ExpressionWithTypeArguments) {
parseExpected(SyntaxKind.CloseBraceToken);
return expression as ExpressionWithTypeArguments;
}
scanner.setSkipJsDocLeadingAsterisks(true);
const typeArguments = tryParseTypeArguments();
scanner.setSkipJsDocLeadingAsterisks(false);
const node = factory.createExpressionWithTypeArguments(expression, typeArguments) as ExpressionWithTypeArguments & { expression: Identifier | PropertyAccessEntityNameExpression; };
const node = factory.createExpressionWithTypeArguments(expression, typeArguments);
const res = finishNode(node, pos);
if (usedBrace) {
parseExpected(SyntaxKind.CloseBraceToken);
}
return res;
}

function parsePropertyAccessEntityNameExpression() {
const pos = getNodePos();
let node: Identifier | PropertyAccessEntityNameExpression = parseJSDocIdentifierName();
while (parseOptional(SyntaxKind.DotToken)) {
const name = parseJSDocIdentifierName();
node = finishNode(factoryCreatePropertyAccessExpression(node, name), pos) as PropertyAccessEntityNameExpression;
}
return node;
}

function parseSimpleTag(start: number, createTag: (tagName: Identifier | undefined, comment?: string | NodeArray<JSDocComment>) => JSDocTag, tagName: Identifier, margin: number, indentText: string): JSDocTag {
return finishNode(createTag(tagName, parseTrailingTagComments(start, getNodePos(), margin, indentText)), start);
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3949,12 +3949,12 @@ export interface JSDocUnknownTag extends JSDocTag {
*/
export interface JSDocAugmentsTag extends JSDocTag {
readonly kind: SyntaxKind.JSDocAugmentsTag;
readonly class: ExpressionWithTypeArguments & { readonly expression: Identifier | PropertyAccessEntityNameExpression; };
readonly class: ExpressionWithTypeArguments;
Copy link
Member

Choose a reason for hiding this comment

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

Are these types wrong and that's why they're changing?

}

export interface JSDocImplementsTag extends JSDocTag {
readonly kind: SyntaxKind.JSDocImplementsTag;
readonly class: ExpressionWithTypeArguments & { readonly expression: Identifier | PropertyAccessEntityNameExpression; };
readonly class: ExpressionWithTypeArguments;
}

export interface JSDocAuthorTag extends JSDocTag {
Expand Down
8 changes: 2 additions & 6 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5737,15 +5737,11 @@ declare namespace ts {
*/
interface JSDocAugmentsTag extends JSDocTag {
readonly kind: SyntaxKind.JSDocAugmentsTag;
readonly class: ExpressionWithTypeArguments & {
readonly expression: Identifier | PropertyAccessEntityNameExpression;
};
readonly class: ExpressionWithTypeArguments;
}
interface JSDocImplementsTag extends JSDocTag {
readonly kind: SyntaxKind.JSDocImplementsTag;
readonly class: ExpressionWithTypeArguments & {
readonly expression: Identifier | PropertyAccessEntityNameExpression;
};
readonly class: ExpressionWithTypeArguments;
}
interface JSDocAuthorTag extends JSDocTag {
readonly kind: SyntaxKind.JSDocAuthorTag;
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/jsdocAugmentsMissingType.errors.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/a.js(2,14): error TS1003: Identifier expected.
/a.js(2,14): error TS8023: JSDoc '@augments ' does not match the 'extends A' clause.
/a.js(2,14): error TS1109: Expression expected.
/a.js(5,14): error TS2339: Property 'x' does not exist on type 'B'.


==== /a.js (3 errors) ====
class A { constructor() { this.x = 0; } }
/** @augments */

!!! error TS1003: Identifier expected.

!!! error TS8023: JSDoc '@augments ' does not match the 'extends A' clause.
~
!!! error TS1109: Expression expected.
class B extends A {
m() {
this.x
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/jsdocImplements_importType.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/a.js(1,18): error TS2500: A class can only implement an identifier/qualified-name with optional type arguments.


==== /a.js (1 errors) ====
/** @implements {import('./b').B} */
~~~~~~~~~~~~~~~
!!! error TS2500: A class can only implement an identifier/qualified-name with optional type arguments.
class A {
/** @return {number} */
method() { throw new Error(); }
}
==== /b.ts (0 errors) ====
export interface B {
method(): number
}

26 changes: 26 additions & 0 deletions tests/baselines/reference/jsdocImplements_importType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/conformance/jsdoc/jsdocImplements_importType.ts] ////

//// [a.js]
/** @implements {import('./b').B} */
class A {
/** @return {number} */
method() { throw new Error(); }
}
//// [b.ts]
export interface B {
method(): number
}




//// [b.d.ts]
export interface B {
method(): number;
}
//// [a.d.ts]
/** @implements {import('./b').B} */
declare class A implements import('./b').B {
/** @return {number} */
method(): number;
}
20 changes: 20 additions & 0 deletions tests/baselines/reference/jsdocImplements_importType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/conformance/jsdoc/jsdocImplements_importType.ts] ////

=== /a.js ===
/** @implements {import('./b').B} */
class A {
>A : Symbol(A, Decl(a.js, 0, 0))

/** @return {number} */
method() { throw new Error(); }
>method : Symbol(A.method, Decl(a.js, 1, 9))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}
=== /b.ts ===
export interface B {
>B : Symbol(B, Decl(b.ts, 0, 0))

method(): number
>method : Symbol(B.method, Decl(b.ts, 0, 21))
}

24 changes: 24 additions & 0 deletions tests/baselines/reference/jsdocImplements_importType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/conformance/jsdoc/jsdocImplements_importType.ts] ////

=== /a.js ===
/** @implements {import('./b').B} */
class A {
>A : A
> : ^

/** @return {number} */
method() { throw new Error(); }
>method : () => number
> : ^^^^^^
>new Error() : Error
> : ^^^^^
>Error : ErrorConstructor
> : ^^^^^^^^^^^^^^^^
}
=== /b.ts ===
export interface B {
method(): number
>method : () => number
> : ^^^^^^
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/a.js(2,16): error TS1003: Identifier expected.
/a.js(2,16): error TS1109: Expression expected.


==== /a.js (1 errors) ====
class A { constructor() { this.x = 0; } }
/** @implements */

!!! error TS1003: Identifier expected.
~
!!! error TS1109: Expression expected.
class B {
}

16 changes: 16 additions & 0 deletions tests/cases/conformance/jsdoc/jsdocImplements_importType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @allowJs: true
// @checkJs: true
// @declaration: true
// @emitDeclarationOnly: true
// @outDir: ./out

// @Filename: /a.js
/** @implements {import('./b').B} */
class A {
/** @return {number} */
method() { throw new Error(); }
}
// @Filename: /b.ts
export interface B {
method(): number
}