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

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
Oleksandr T 2023-10-26 00:23:04 +03:00 committed by GitHub
parent 781cc19b32
commit 5449489238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 0 deletions

View File

@ -780,6 +780,7 @@ import {
JSDocSatisfiesTag,
JSDocSignature,
JSDocTemplateTag,
JSDocThisTag,
JSDocTypeAssertion,
JSDocTypedefTag,
JSDocTypeExpression,
@ -41240,6 +41241,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function checkJSDocParameterTag(node: JSDocParameterTag) {
checkSourceElement(node.typeExpression);
}
function checkJSDocPropertyTag(node: JSDocPropertyTag) {
checkSourceElement(node.typeExpression);
}
@ -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)) {
@ -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:

View File

@ -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'.
}

View File

@ -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))
}

View File

@ -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
}

View File

@ -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);
}