Skip to content

Fix this-parameter emit for JSDocFunction types #39814

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 2 commits into from
Jul 29, 2020
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: 9 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5777,7 +5777,7 @@ namespace ts {
/*decorators*/ undefined,
/*modifiers*/ undefined,
getEffectiveDotDotDotForParameter(p),
p.name || getEffectiveDotDotDotForParameter(p) ? `args` : `arg${i}`,
getNameForJSDocFunctionParameter(p, i),
p.questionToken,
visitNode(p.type, visitExistingNodeTreeSymbols),
/*initializer*/ undefined
Expand All @@ -5792,7 +5792,7 @@ namespace ts {
/*decorators*/ undefined,
/*modifiers*/ undefined,
getEffectiveDotDotDotForParameter(p),
p.name || getEffectiveDotDotDotForParameter(p) ? `args` : `arg${i}`,
getNameForJSDocFunctionParameter(p, i),
p.questionToken,
visitNode(p.type, visitExistingNodeTreeSymbols),
/*initializer*/ undefined
Expand Down Expand Up @@ -5847,6 +5847,13 @@ namespace ts {
return p.dotDotDotToken || (p.type && isJSDocVariadicType(p.type) ? factory.createToken(SyntaxKind.DotDotDotToken) : undefined);
}

/** Note that `new:T` parameters are not handled, but should be before calling this function. */
function getNameForJSDocFunctionParameter(p: ParameterDeclaration, index: number) {
return p.name && isIdentifier(p.name) && p.name.escapedText === "this" ? "this"
: getEffectiveDotDotDotForParameter(p) ? `args`
: `arg${index}`;
}

function rewriteModuleSpecifier(parent: ImportTypeNode, lit: StringLiteral) {
if (bundled) {
if (context.tracker && context.tracker.moduleResolverHost) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [bug38550.js]
export class Clazz {
/**
* @param {function(this:Object, ...*):*} functionDeclaration
*/
method(functionDeclaration) {}
}


//// [bug38550.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Clazz = void 0;
var Clazz = /** @class */ (function () {
function Clazz() {
}
/**
* @param {function(this:Object, ...*):*} functionDeclaration
*/
Clazz.prototype.method = function (functionDeclaration) { };
return Clazz;
}());
exports.Clazz = Clazz;


//// [bug38550.d.ts]
export class Clazz {
/**
* @param {function(this:Object, ...*):*} functionDeclaration
*/
method(functionDeclaration: (this: any, ...args: any[]) => any): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/jsdoc/declarations/bug38550.js ===
export class Clazz {
>Clazz : Symbol(Clazz, Decl(bug38550.js, 0, 0))

/**
* @param {function(this:Object, ...*):*} functionDeclaration
*/
method(functionDeclaration) {}
>method : Symbol(Clazz.method, Decl(bug38550.js, 0, 20))
>functionDeclaration : Symbol(functionDeclaration, Decl(bug38550.js, 4, 9))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/jsdoc/declarations/bug38550.js ===
export class Clazz {
>Clazz : Clazz

/**
* @param {function(this:Object, ...*):*} functionDeclaration
*/
method(functionDeclaration) {}
>method : (functionDeclaration: (this: any, ...args: any[]) => any) => void
>functionDeclaration : (this: any, ...arg1: any[]) => any
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function hof(ctor: function(new: number, string)) {
>'hi' : "hi"
}
function hof2(f: function(this: number, string): string) {
>hof2 : (f: (args: number, arg1: string) => string) => string
>hof2 : (f: (this: number, arg1: string) => string) => string
>f : (this: number, arg1: string) => string
>this : number

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/jsdocFunctionType.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @return {function(this: string, number): number}
*/
function id1(c) {
>id1 : (c: (args: string, arg1: number) => number) => (args: string, arg1: number) => number
>id1 : (c: (this: string, arg1: number) => number) => (this: string, arg1: number) => number
>c : (this: string, arg1: number) => number

return c
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @allowJs: true
// @checkJs: true
// @target: es5
// @outDir: ./out
// @declaration: true
// @filename: bug38550.js
export class Clazz {
/**
* @param {function(this:Object, ...*):*} functionDeclaration
*/
method(functionDeclaration) {}
}