diff --git a/tsc/internal/checker/jsdoc.go b/tsc/internal/checker/jsdoc.go index da014a5c9f3b5..b32cbfacc2b76 100644 --- a/tsc/internal/checker/jsdoc.go +++ b/tsc/internal/checker/jsdoc.go @@ -9,6 +9,7 @@ import ( func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) { var jsdocParameters []*ast.Node + hasThisTag := false for _, tag := range getAllJSDocTags(node) { if tag.Kind == ast.KindJSDocParameterTag { name := tag.AsJSDocParameterOrPropertyTag().Name() @@ -17,6 +18,9 @@ func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) { } jsdocParameters = append(jsdocParameters, tag) } + if tag.Kind == ast.KindJSDocThisTag { + hasThisTag = true + } } if len(jsdocParameters) == 0 { @@ -27,13 +31,26 @@ func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) { parameters := collections.Set[string]{} excludedParameters := collections.Set[int]{} + // A @this tag inserts a synthetic `this` parameter at index 0. It is not + // described by a @param tag, so ignore it when matching by position. + paramOffset := 0 + if hasThisTag { + params := node.Parameters() + if len(params) > 0 && ast.IsThisIdentifier(params[0].Name()) { + paramOffset = 1 + } + } + for i, param := range node.Parameters() { + if i < paramOffset { + continue + } name := param.AsParameterDeclaration().Name() if ast.IsIdentifier(name) { parameters.Add(name.Text()) } if ast.IsBindingPattern(name) { - excludedParameters.Add(i) + excludedParameters.Add(i - paramOffset) } } if c.containsArgumentsReference(node) { diff --git a/tsc/internal/parser/reparser.go b/tsc/internal/parser/reparser.go index ef420f7e19971..a0c5700cf1f8c 100644 --- a/tsc/internal/parser/reparser.go +++ b/tsc/internal/parser/reparser.go @@ -626,6 +626,15 @@ func (p *Parser) makeQuestionIfOptional(parameter *ast.JSDocParameterOrPropertyT return questionToken } +func hasJSDocThisTag(jsDoc *ast.Node) bool { + for _, tag := range jsDoc.AsJSDoc().Tags.Nodes { + if tag.Kind == ast.KindJSDocThisTag { + return true + } + } + return false +} + func findMatchingParameter(fun *ast.Node, parameterTag *ast.JSDocParameterOrPropertyTag, jsDoc *ast.Node) (*ast.ParameterDeclaration, bool) { tagIndex := -1 paramCount := -1 @@ -638,13 +647,25 @@ func findMatchingParameter(fun *ast.Node, parameterTag *ast.JSDocParameterOrProp } } } + // A @this tag inserts a synthetic `this` parameter at index 0. It is not + // described by a @param tag, so ignore it when matching by position. + paramOffset := 0 + if hasJSDocThisTag(jsDoc) { + params := fun.Parameters() + if len(params) > 0 && ast.IsThisIdentifier(params[0].Name()) { + paramOffset = 1 + } + } for parameterIndex, parameter := range fun.Parameters() { + if parameterIndex < paramOffset { + continue + } 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()) || (parameterIndex-paramOffset == tagIndex && len(parameterTag.Name().Text()) == 0)) { return parameter.AsParameterDeclaration(), true } - } else if parameterIndex == tagIndex { + } else if parameterIndex-paramOffset == 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..a7d2163671369 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.symbols @@ -0,0 +1,18 @@ +//// [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)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types new file mode 100644 index 0000000000000..928c7065ced95 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsdocThisTagWithDestructuredParameter.types @@ -0,0 +1,19 @@ +//// [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 +} + diff --git a/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts b/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts new file mode 100644 index 0000000000000..51a6f77e28668 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsdocThisTagWithDestructuredParameter.ts @@ -0,0 +1,14 @@ +// @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; +}