Skip to content
Closed
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
19 changes: 18 additions & 1 deletion tsc/internal/checker/jsdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
25 changes: 23 additions & 2 deletions tsc/internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}

Original file line number Diff line number Diff line change
@@ -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
}

Original file line number Diff line number Diff line change
@@ -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;
}