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
23 changes: 21 additions & 2 deletions tsc/internal/checker/jsdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 23 additions & 3 deletions tsc/internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}

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

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