Skip to content

Commit

Permalink
fix(42238): emit this parameter in function declaration (#46511)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored Nov 17, 2021
1 parent f11f14b commit a75f26e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 9 deletions.
23 changes: 21 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5657,8 +5657,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 @@ -5713,6 +5713,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() {}

0 comments on commit a75f26e

Please sign in to comment.