Skip to content

Commit 9b2d487

Browse files
authored
Fix this-parameter emit for JSDocFunction types (microsoft#39814)
* Fix this parameter emit for JSDocFunction types Previously, parameters with names that were not `new` were treated like rest parameters. This is incorrect: parameters with the name `this` should emit a `this` parameter. Fixes microsoft#38550 * ❤️ quote style
1 parent f2d1531 commit 9b2d487

7 files changed

+79
-4
lines changed

src/compiler/checker.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -5777,7 +5777,7 @@ namespace ts {
57775777
/*decorators*/ undefined,
57785778
/*modifiers*/ undefined,
57795779
getEffectiveDotDotDotForParameter(p),
5780-
p.name || getEffectiveDotDotDotForParameter(p) ? `args` : `arg${i}`,
5780+
getNameForJSDocFunctionParameter(p, i),
57815781
p.questionToken,
57825782
visitNode(p.type, visitExistingNodeTreeSymbols),
57835783
/*initializer*/ undefined
@@ -5792,7 +5792,7 @@ namespace ts {
57925792
/*decorators*/ undefined,
57935793
/*modifiers*/ undefined,
57945794
getEffectiveDotDotDotForParameter(p),
5795-
p.name || getEffectiveDotDotDotForParameter(p) ? `args` : `arg${i}`,
5795+
getNameForJSDocFunctionParameter(p, i),
57965796
p.questionToken,
57975797
visitNode(p.type, visitExistingNodeTreeSymbols),
57985798
/*initializer*/ undefined
@@ -5847,6 +5847,13 @@ namespace ts {
58475847
return p.dotDotDotToken || (p.type && isJSDocVariadicType(p.type) ? factory.createToken(SyntaxKind.DotDotDotToken) : undefined);
58485848
}
58495849

5850+
/** Note that `new:T` parameters are not handled, but should be before calling this function. */
5851+
function getNameForJSDocFunctionParameter(p: ParameterDeclaration, index: number) {
5852+
return p.name && isIdentifier(p.name) && p.name.escapedText === "this" ? "this"
5853+
: getEffectiveDotDotDotForParameter(p) ? `args`
5854+
: `arg${index}`;
5855+
}
5856+
58505857
function rewriteModuleSpecifier(parent: ImportTypeNode, lit: StringLiteral) {
58515858
if (bundled) {
58525859
if (context.tracker && context.tracker.moduleResolverHost) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [bug38550.js]
2+
export class Clazz {
3+
/**
4+
* @param {function(this:Object, ...*):*} functionDeclaration
5+
*/
6+
method(functionDeclaration) {}
7+
}
8+
9+
10+
//// [bug38550.js]
11+
"use strict";
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.Clazz = void 0;
14+
var Clazz = /** @class */ (function () {
15+
function Clazz() {
16+
}
17+
/**
18+
* @param {function(this:Object, ...*):*} functionDeclaration
19+
*/
20+
Clazz.prototype.method = function (functionDeclaration) { };
21+
return Clazz;
22+
}());
23+
exports.Clazz = Clazz;
24+
25+
26+
//// [bug38550.d.ts]
27+
export class Clazz {
28+
/**
29+
* @param {function(this:Object, ...*):*} functionDeclaration
30+
*/
31+
method(functionDeclaration: (this: any, ...args: any[]) => any): void;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/jsdoc/declarations/bug38550.js ===
2+
export class Clazz {
3+
>Clazz : Symbol(Clazz, Decl(bug38550.js, 0, 0))
4+
5+
/**
6+
* @param {function(this:Object, ...*):*} functionDeclaration
7+
*/
8+
method(functionDeclaration) {}
9+
>method : Symbol(Clazz.method, Decl(bug38550.js, 0, 20))
10+
>functionDeclaration : Symbol(functionDeclaration, Decl(bug38550.js, 4, 9))
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/jsdoc/declarations/bug38550.js ===
2+
export class Clazz {
3+
>Clazz : Clazz
4+
5+
/**
6+
* @param {function(this:Object, ...*):*} functionDeclaration
7+
*/
8+
method(functionDeclaration) {}
9+
>method : (functionDeclaration: (this: any, ...args: any[]) => any) => void
10+
>functionDeclaration : (this: any, ...arg1: any[]) => any
11+
}
12+

tests/baselines/reference/jsdocDisallowedInTypescript.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function hof(ctor: function(new: number, string)) {
3535
>'hi' : "hi"
3636
}
3737
function hof2(f: function(this: number, string): string) {
38-
>hof2 : (f: (args: number, arg1: string) => string) => string
38+
>hof2 : (f: (this: number, arg1: string) => string) => string
3939
>f : (this: number, arg1: string) => string
4040
>this : number
4141

tests/baselines/reference/jsdocFunctionType.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @return {function(this: string, number): number}
55
*/
66
function id1(c) {
7-
>id1 : (c: (args: string, arg1: number) => number) => (args: string, arg1: number) => number
7+
>id1 : (c: (this: string, arg1: number) => number) => (this: string, arg1: number) => number
88
>c : (this: string, arg1: number) => number
99

1010
return c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @target: es5
4+
// @outDir: ./out
5+
// @declaration: true
6+
// @filename: bug38550.js
7+
export class Clazz {
8+
/**
9+
* @param {function(this:Object, ...*):*} functionDeclaration
10+
*/
11+
method(functionDeclaration) {}
12+
}

0 commit comments

Comments
 (0)