From b85aaac6835c3ce6073a448dbde2de4b01fb9d02 Mon Sep 17 00:00:00 2001 From: 5 <93879550+tydavid5@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:09:45 +0100 Subject: [PATCH 1/2] Ignore the synthetic this parameter when matching JSDoc params A @this tag inserts a synthetic this parameter at index 0, which shifted positional @param matching by one. The first real parameter was left unmatched, so a destructured parameter got no type and was reported as implicitly any (TS7031), and its valid @param tag was reported as unmatched (TS8024). Both findMatchingParameter and checkUnmatchedJSDocParameters now skip a leading this parameter when computing @param positions. This covers the synthetic parameter from @this as well as an explicitly declared this parameter. Adds a compiler test with the reported case plus the same code without @this and a named parameter with @this as controls. Fixes #65524 --- tsc/internal/checker/jsdoc.go | 12 ++++- tsc/internal/parser/reparser.go | 15 ++++-- ...ocThisTagWithDestructuredParameter.symbols | 44 ++++++++++++++++++ ...sdocThisTagWithDestructuredParameter.types | 46 +++++++++++++++++++ .../jsdocThisTagWithDestructuredParameter.ts | 29 ++++++++++++ 5 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types create mode 100644 tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts diff --git a/tsc/internal/checker/jsdoc.go b/tsc/internal/checker/jsdoc.go index 901277bc6e12c..7c56a43688cb4 100644 --- a/tsc/internal/checker/jsdoc.go +++ b/tsc/internal/checker/jsdoc.go @@ -27,13 +27,21 @@ func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) { parameters := collections.Set[string]{} excludedParameters := collections.Set[int]{} - for i, param := range node.Parameters() { + functionParameters := node.Parameters() + parameterOffset := 0 + if len(functionParameters) > 0 && (functionParameters[0].Name().Kind == ast.KindThisKeyword || ast.IsThisIdentifier(functionParameters[0].Name())) { + parameterOffset = 1 + } + for i, param := range functionParameters { + if i < parameterOffset { + continue + } name := param.AsParameterDeclaration().Name() if ast.IsIdentifier(name) { parameters.Add(name.Text()) } if ast.IsBindingPattern(name) { - excludedParameters.Add(i) + excludedParameters.Add(i - parameterOffset) } } if c.containsArgumentsReference(node) { diff --git a/tsc/internal/parser/reparser.go b/tsc/internal/parser/reparser.go index 676c5865b255e..9b0a4b86cc478 100644 --- a/tsc/internal/parser/reparser.go +++ b/tsc/internal/parser/reparser.go @@ -651,13 +651,22 @@ func findMatchingParameter(fun *ast.Node, parameterTag *ast.JSDocParameterOrProp } } } - for parameterIndex, parameter := range fun.Parameters() { + parameters := fun.Parameters() + parameterOffset := 0 + if len(parameters) > 0 && (parameters[0].Name().Kind == ast.KindThisKeyword || ast.IsThisIdentifier(parameters[0].Name())) { + parameterOffset = 1 + } + for parameterIndex, parameter := range parameters { + if parameterIndex < parameterOffset { + continue + } + parameterPosition := parameterIndex - parameterOffset if parameter.Name().Kind == ast.KindIdentifier { if parameterTag.Name().Kind == ast.KindIdentifier && - ((parameter.Name().Text() == parameterTag.Name().Text()) || (parameterIndex == tagIndex && len(parameterTag.Name().Text()) == 0)) { + ((parameter.Name().Text() == parameterTag.Name().Text()) || (parameterPosition == tagIndex && len(parameterTag.Name().Text()) == 0)) { return parameter.AsParameterDeclaration(), true } - } else if parameterIndex == tagIndex { + } else if parameterPosition == tagIndex { return parameter.AsParameterDeclaration(), true } } diff --git a/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols new file mode 100644 index 0000000000000..9f23eb6702031 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols @@ -0,0 +1,44 @@ +//// [tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts] //// + +=== /a.js === +/** + * @this {{ x: number }} + * @param {{ title: string }} props + */ +export function withThis({ title }) { +>withThis : Symbol(withThis, Decl(a.js, 0, 0)) +>title : Symbol(title, Decl(a.js, 4, 26)) + + return this.x + title; +>this.x : Symbol(x, Decl(a.js, 1, 11)) +>this : Symbol((Missing), Decl(a.js, 1, 4)) +>x : Symbol(x, Decl(a.js, 1, 11)) +>title : Symbol(title, Decl(a.js, 4, 26)) +} + +/** + * @param {{ title: string }} props + */ +export function withoutThis({ title }) { +>withoutThis : Symbol(withoutThis, Decl(a.js, 6, 1)) +>title : Symbol(title, Decl(a.js, 11, 29)) + + return title; +>title : Symbol(title, Decl(a.js, 11, 29)) +} + +/** + * @this {{ x: number }} + * @param {string} name + */ +export function withThisNamed(name) { +>withThisNamed : Symbol(withThisNamed, Decl(a.js, 13, 1)) +>name : Symbol(name, Decl(a.js, 19, 30)) + + return this.x + name; +>this.x : Symbol(x, Decl(a.js, 16, 11)) +>this : Symbol((Missing), Decl(a.js, 16, 4)) +>x : Symbol(x, Decl(a.js, 16, 11)) +>name : Symbol(name, Decl(a.js, 19, 30)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types new file mode 100644 index 0000000000000..760c511ecf9b2 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types @@ -0,0 +1,46 @@ +//// [tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts] //// + +=== /a.js === +/** + * @this {{ x: number }} + * @param {{ title: string }} props + */ +export function withThis({ title }) { +>withThis : (this: { x: number; }, { title }: { title: string; }) => string +>title : string + + return this.x + title; +>this.x + title : string +>this.x : number +>this : { x: number; } +>x : number +>title : string +} + +/** + * @param {{ title: string }} props + */ +export function withoutThis({ title }) { +>withoutThis : ({ title }: { title: string; }) => string +>title : string + + return title; +>title : string +} + +/** + * @this {{ x: number }} + * @param {string} name + */ +export function withThisNamed(name) { +>withThisNamed : (this: { x: number; }, name: string) => string +>name : string + + return this.x + name; +>this.x + name : string +>this.x : number +>this : { x: number; } +>x : number +>name : string +} + diff --git a/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts b/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts new file mode 100644 index 0000000000000..96cab4763a72b --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts @@ -0,0 +1,29 @@ +// @strict: true +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @filename: /a.js + +/** + * @this {{ x: number }} + * @param {{ title: string }} props + */ +export function withThis({ title }) { + return this.x + title; +} + +/** + * @param {{ title: string }} props + */ +export function withoutThis({ title }) { + return title; +} + +/** + * @this {{ x: number }} + * @param {string} name + */ +export function withThisNamed(name) { + return this.x + name; +} From 76b49a8f641f2a6eeea7c08ddf50bf01d7628962 Mon Sep 17 00:00:00 2001 From: 5 <93879550+tydavid5@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:40:41 +0100 Subject: [PATCH 2/2] Fix false TS8024 for documented explicit this receivers Copilot review on PR #65576 caught a case the first version of this fix missed. The leading this parameter was skipped unconditionally, which was right when the parameter is synthetic, inserted from a @this tag, because the @param tags are written without counting it. But when the receiver is documented with a @param this tag, the tags are written counting it, so skipping it misaligned everything: the following @param tag was reported as unmatched with a false TS8024, and in JavaScript files the receiver type was applied to the wrong parameter. Both findMatchingParameter and checkUnmatchedJSDocParameters now apply the offset only when the receiver has no corresponding @param tag. If any @param tag names this, the existing name and position matching is kept. A regression test covers the documented receiver in both forms, a JavaScript file with @this plus @param this, and a TypeScript file with an explicitly declared this receiver, with suggestion capture on so the TypeScript case is checked too. Fixes #65524 --- tsc/internal/checker/jsdoc.go | 13 ++++++- tsc/internal/parser/reparser.go | 13 ++++++- ...ocThisTagWithDestructuredParameter.symbols | 36 ++++++++++++++++++ ...sdocThisTagWithDestructuredParameter.types | 38 +++++++++++++++++++ .../jsdocThisTagWithDestructuredParameter.ts | 22 +++++++++++ 5 files changed, 120 insertions(+), 2 deletions(-) diff --git a/tsc/internal/checker/jsdoc.go b/tsc/internal/checker/jsdoc.go index 7c56a43688cb4..36d9661f4c247 100644 --- a/tsc/internal/checker/jsdoc.go +++ b/tsc/internal/checker/jsdoc.go @@ -28,8 +28,19 @@ func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) { excludedParameters := collections.Set[int]{} functionParameters := node.Parameters() + // A @param tag naming this means the tags are written counting the + // receiver, so keep the existing matching and do not shift positions. + // Without such a tag, a leading this parameter is the synthetic one + // from a @this tag and the tags are written without counting it. + hasThisParameterTag := false + for _, tag := range jsdocParameters { + if ast.IsThisIdentifier(tag.AsJSDocParameterOrPropertyTag().Name()) { + hasThisParameterTag = true + break + } + } parameterOffset := 0 - if len(functionParameters) > 0 && (functionParameters[0].Name().Kind == ast.KindThisKeyword || ast.IsThisIdentifier(functionParameters[0].Name())) { + if !hasThisParameterTag && len(functionParameters) > 0 && (functionParameters[0].Name().Kind == ast.KindThisKeyword || ast.IsThisIdentifier(functionParameters[0].Name())) { parameterOffset = 1 } for i, param := range functionParameters { diff --git a/tsc/internal/parser/reparser.go b/tsc/internal/parser/reparser.go index 9b0a4b86cc478..9d9ab7abfc5c3 100644 --- a/tsc/internal/parser/reparser.go +++ b/tsc/internal/parser/reparser.go @@ -652,8 +652,19 @@ func findMatchingParameter(fun *ast.Node, parameterTag *ast.JSDocParameterOrProp } } parameters := fun.Parameters() + // A @param tag naming this means the tags are written counting the + // receiver, so keep the existing matching and do not shift positions. + // Without such a tag, a leading this parameter is the synthetic one + // from a @this tag and the tags are written without counting it. + hasThisParameterTag := false + for _, tag := range jsDoc.AsJSDoc().Tags.Nodes { + if tag.Kind == ast.KindJSDocParameterTag && ast.IsThisIdentifier(tag.AsJSDocParameterOrPropertyTag().Name()) { + hasThisParameterTag = true + break + } + } parameterOffset := 0 - if len(parameters) > 0 && (parameters[0].Name().Kind == ast.KindThisKeyword || ast.IsThisIdentifier(parameters[0].Name())) { + if !hasThisParameterTag && len(parameters) > 0 && (parameters[0].Name().Kind == ast.KindThisKeyword || ast.IsThisIdentifier(parameters[0].Name())) { parameterOffset = 1 } for parameterIndex, parameter := range parameters { diff --git a/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols index 9f23eb6702031..107166d91ef86 100644 --- a/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols @@ -42,3 +42,39 @@ export function withThisNamed(name) { >name : Symbol(name, Decl(a.js, 19, 30)) } +=== /b.js === +/** + * @this {{ x: number }} + * @param {{ x: number }} this + * @param {{ title: string }} props + */ +export function withDocumentedThis({ title }) { +>withDocumentedThis : Symbol(withDocumentedThis, Decl(b.js, 0, 0)) +>title : Symbol(title, Decl(b.js, 5, 36)) + + return this.x + title; +>this.x : Symbol(x, Decl(b.js, 1, 11)) +>this : Symbol((Missing), Decl(b.js, 1, 4)) +>x : Symbol(x, Decl(b.js, 1, 11)) +>title : Symbol(title, Decl(b.js, 5, 36)) +} + +=== /c.ts === +/** + * @param {{ x: number }} this + * @param {{ title: string }} props + */ +export function withDocumentedThisReceiver(this: { x: number }, { title }: { title: string }) { +>withDocumentedThisReceiver : Symbol(withDocumentedThisReceiver, Decl(c.ts, 0, 0)) +>this : Symbol(this, Decl(c.ts, 4, 43)) +>x : Symbol(x, Decl(c.ts, 4, 50)) +>title : Symbol(title, Decl(c.ts, 4, 65)) +>title : Symbol(title, Decl(c.ts, 4, 76)) + + return this.x + title; +>this.x : Symbol(x, Decl(c.ts, 4, 50)) +>this : Symbol(this, Decl(c.ts, 4, 43)) +>x : Symbol(x, Decl(c.ts, 4, 50)) +>title : Symbol(title, Decl(c.ts, 4, 65)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types index 760c511ecf9b2..ab2860ef3ae76 100644 --- a/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types @@ -44,3 +44,41 @@ export function withThisNamed(name) { >name : string } +=== /b.js === +/** + * @this {{ x: number }} + * @param {{ x: number }} this + * @param {{ title: string }} props + */ +export function withDocumentedThis({ title }) { +>withDocumentedThis : (this: { x: number; }, { title }: { title: string; }) => string +>title : string + + return this.x + title; +>this.x + title : string +>this.x : number +>this : { x: number; } +>x : number +>title : string +} + +=== /c.ts === +/** + * @param {{ x: number }} this + * @param {{ title: string }} props + */ +export function withDocumentedThisReceiver(this: { x: number }, { title }: { title: string }) { +>withDocumentedThisReceiver : (this: { x: number; }, { title }: { title: string; }) => string +>this : { x: number; } +>x : number +>title : string +>title : string + + return this.x + title; +>this.x + title : string +>this.x : number +>this : { x: number; } +>x : number +>title : string +} + diff --git a/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts b/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts index 96cab4763a72b..5f5e9db19b751 100644 --- a/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts +++ b/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts @@ -2,6 +2,7 @@ // @allowJs: true // @checkJs: true // @noEmit: true +// @captureSuggestions: true // @filename: /a.js @@ -27,3 +28,24 @@ export function withoutThis({ title }) { export function withThisNamed(name) { return this.x + name; } + +// @filename: /b.js + +/** + * @this {{ x: number }} + * @param {{ x: number }} this + * @param {{ title: string }} props + */ +export function withDocumentedThis({ title }) { + return this.x + title; +} + +// @filename: /c.ts + +/** + * @param {{ x: number }} this + * @param {{ title: string }} props + */ +export function withDocumentedThisReceiver(this: { x: number }, { title }: { title: string }) { + return this.x + title; +}