diff --git a/tsc/internal/checker/jsdoc.go b/tsc/internal/checker/jsdoc.go index 901277bc6e12c..36d9661f4c247 100644 --- a/tsc/internal/checker/jsdoc.go +++ b/tsc/internal/checker/jsdoc.go @@ -27,13 +27,32 @@ func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) { parameters := collections.Set[string]{} excludedParameters := collections.Set[int]{} - for i, param := range node.Parameters() { + 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 !hasThisParameterTag && 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..9d9ab7abfc5c3 100644 --- a/tsc/internal/parser/reparser.go +++ b/tsc/internal/parser/reparser.go @@ -651,13 +651,33 @@ func findMatchingParameter(fun *ast.Node, parameterTag *ast.JSDocParameterOrProp } } } - for parameterIndex, parameter := range fun.Parameters() { + 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 !hasThisParameterTag && 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..107166d91ef86 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols @@ -0,0 +1,80 @@ +//// [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)) +} + +=== /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 new file mode 100644 index 0000000000000..ab2860ef3ae76 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types @@ -0,0 +1,84 @@ +//// [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 +} + +=== /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 new file mode 100644 index 0000000000000..5f5e9db19b751 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts @@ -0,0 +1,51 @@ +// @strict: true +// @allowJs: true +// @checkJs: true +// @noEmit: true +// @captureSuggestions: 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; +} + +// @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; +}