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(42238): JSDoc '@this' not emitted in declaration file #46511

Merged
merged 1 commit into from
Nov 17, 2021
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
23 changes: 21 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5580,8 +5580,8 @@ namespace ts {
const expandedParams = getExpandedParameters(signature, /*skipUnionExpanding*/ true)[0];
// If the expanded parameter list had a variadic in a non-trailing position, don't expand it
const parameters = (some(expandedParams, p => p !== expandedParams[expandedParams.length - 1] && !!(getCheckFlags(p) & CheckFlags.RestParameter)) ? signature.parameters : expandedParams).map(parameter => symbolToParameterDeclaration(parameter, context, kind === SyntaxKind.Constructor, options?.privateSymbolVisitor, options?.bundledImports));
if (signature.thisParameter) {
const thisParameter = symbolToParameterDeclaration(signature.thisParameter, context);
const thisParameter = tryGetThisParameterDeclaration(signature, context);
if (thisParameter) {
parameters.unshift(thisParameter);
}

Expand Down Expand Up @@ -5636,6 +5636,25 @@ namespace ts {
return node;
}

function tryGetThisParameterDeclaration(signature: Signature, context: NodeBuilderContext) {
if (signature.thisParameter) {
return symbolToParameterDeclaration(signature.thisParameter, context);
}
if (signature.declaration) {
const thisTag = getJSDocThisTag(signature.declaration);
if (thisTag && thisTag.typeExpression) {
return factory.createParameterDeclaration(
/* decorators */ undefined,
/* modifiers */ undefined,
/* dotDotDotToken */ undefined,
"this",
/* questionToken */ undefined,
typeToTypeNodeHelper(getTypeFromTypeNode(thisTag.typeExpression), context)
);
}
}
}

function typeParameterToDeclarationWithConstraint(type: TypeParameter, context: NodeBuilderContext, constraintNode: TypeNode | undefined): TypeParameterDeclaration {
const savedContextFlags = context.flags;
context.flags &= ~NodeBuilderFlags.WriteTypeParametersInQualifiedName; // Avoids potential infinite loop when building for a claimspace with a generic
Expand Down
14 changes: 7 additions & 7 deletions tests/baselines/reference/thisTag1.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @return {number}
*/
function f(s) {
>f : (s: string) => number
>f : (this: { n: number; }, s: string) => number
>s : string

return this.n + s.length
Expand All @@ -18,20 +18,20 @@ function f(s) {
}

const o = {
>o : { f: (s: string) => number; n: number; }
>{ f, n: 1} : { f: (s: string) => number; n: number; }
>o : { f: (this: { n: number; }, s: string) => number; n: number; }
>{ f, n: 1} : { f: (this: { n: number; }, s: string) => number; n: number; }

f,
>f : (s: string) => number
>f : (this: { n: number; }, s: string) => number

n: 1
>n : number
>1 : 1
}
o.f('hi')
>o.f('hi') : number
>o.f : (s: string) => number
>o : { f: (s: string) => number; n: number; }
>f : (s: string) => number
>o.f : (this: { n: number; }, s: string) => number
>o : { f: (this: { n: number; }, s: string) => number; n: number; }
>f : (this: { n: number; }, s: string) => number
>'hi' : "hi"

15 changes: 15 additions & 0 deletions tests/baselines/reference/thisTag2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [a.js]
/** @this {string} */
export function f1() {}

/** @this */
export function f2() {}




//// [a.d.ts]
/** @this {string} */
export function f1(this: string): void;
/** @this */
export function f2(this: any): void;
9 changes: 9 additions & 0 deletions tests/baselines/reference/thisTag2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/jsdoc/a.js ===
/** @this {string} */
export function f1() {}
>f1 : Symbol(f1, Decl(a.js, 0, 0))

/** @this */
export function f2() {}
>f2 : Symbol(f2, Decl(a.js, 1, 23))

9 changes: 9 additions & 0 deletions tests/baselines/reference/thisTag2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/jsdoc/a.js ===
/** @this {string} */
export function f1() {}
>f1 : (this: string) => void

/** @this */
export function f2() {}
>f2 : (this: any) => void

11 changes: 11 additions & 0 deletions tests/cases/conformance/jsdoc/thisTag2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @target: esnext
// @allowJs: true
// @declaration: true
// @emitDeclarationOnly: true
// @filename: a.js

/** @this {string} */
export function f1() {}

/** @this */
export function f2() {}