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

fix(55796): JSDoc does not support @this when using an arrow function as method in class #55877

Merged
merged 2 commits into from
Oct 25, 2023
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
11 changes: 11 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ import {
JSDocSatisfiesTag,
JSDocSignature,
JSDocTemplateTag,
JSDocThisTag,
JSDocTypeAssertion,
JSDocTypedefTag,
JSDocTypeExpression,
Expand Down Expand Up @@ -41240,6 +41241,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function checkJSDocParameterTag(node: JSDocParameterTag) {
checkSourceElement(node.typeExpression);
}

function checkJSDocPropertyTag(node: JSDocPropertyTag) {
checkSourceElement(node.typeExpression);
}
Expand All @@ -41255,6 +41257,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function checkJSDocThisTag(node: JSDocThisTag) {
const host = getEffectiveJSDocHost(node);
if (host && isArrowFunction(host)) {
error(node.tagName, Diagnostics.An_arrow_function_cannot_have_a_this_parameter);
}
}

function checkJSDocImplementsTag(node: JSDocImplementsTag): void {
const classLike = getEffectiveJSDocHost(node);
if (!classLike || !isClassDeclaration(classLike) && !isClassExpression(classLike)) {
Expand Down Expand Up @@ -45973,6 +45982,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return checkJSDocAccessibilityModifiers(node as JSDocPublicTag | JSDocProtectedTag | JSDocPrivateTag);
case SyntaxKind.JSDocSatisfiesTag:
return checkJSDocSatisfiesTag(node as JSDocSatisfiesTag);
case SyntaxKind.JSDocThisTag:
return checkJSDocThisTag(node as JSDocThisTag);
case SyntaxKind.IndexedAccessType:
return checkIndexedAccessType(node as IndexedAccessTypeNode);
case SyntaxKind.MappedType:
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/thisTag3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/a.js(7,9): error TS2730: An arrow function cannot have a 'this' parameter.
/a.js(10,21): error TS2339: Property 'fn' does not exist on type 'C'.


==== /a.js (2 errors) ====
/**
* @typedef {{fn(a: string): void}} T
*/

class C {
/**
* @this {T}
~~~~
!!! error TS2730: An arrow function cannot have a 'this' parameter.
* @param {string} a
*/
p = (a) => this.fn("" + a);
~~
!!! error TS2339: Property 'fn' does not exist on type 'C'.
}

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

=== /a.js ===
/**
* @typedef {{fn(a: string): void}} T
*/

class C {
>C : Symbol(C, Decl(a.js, 0, 0))

/**
* @this {T}
* @param {string} a
*/
p = (a) => this.fn("" + a);
>p : Symbol(C.p, Decl(a.js, 4, 9))
>a : Symbol(a, Decl(a.js, 9, 9))
>this : Symbol(C, Decl(a.js, 0, 0))
>a : Symbol(a, Decl(a.js, 9, 9))
}

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

=== /a.js ===
/**
* @typedef {{fn(a: string): void}} T
*/

class C {
>C : C

/**
* @this {T}
* @param {string} a
*/
p = (a) => this.fn("" + a);
>p : (this: T, a: string) => any
>(a) => this.fn("" + a) : (this: T, a: string) => any
>a : string
>this.fn("" + a) : any
>this.fn : any
>this : this
>fn : any
>"" + a : string
>"" : ""
>a : string
}

16 changes: 16 additions & 0 deletions tests/cases/conformance/jsdoc/thisTag3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @filename: /a.js

/**
* @typedef {{fn(a: string): void}} T
*/

class C {
/**
* @this {T}
* @param {string} a
*/
p = (a) => this.fn("" + a);
}
Loading