-
Notifications
You must be signed in to change notification settings - Fork 652
feat: handle unmatched jsdoc parameters #1256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sandersn
merged 12 commits into
microsoft:main
from
a-tarasyuk:fix/unmatched-jsdoc-params
Jun 25, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3e7f08e
feat: handle unmatched jsdoc parameters
a-tarasyuk 1411bd0
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk 7afd24d
restrict containsArgumentsReference to decls that have body
a-tarasyuk 4217eb5
Merge branch 'main' into fix/unmatched-jsdoc-params
a-tarasyuk b04cd84
use signature links
a-tarasyuk 00422a2
cleanup
a-tarasyuk 502b4d2
use map to cache containsArgumentsReference results
a-tarasyuk ac2c379
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk 235e424
handle array type
a-tarasyuk 707015d
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk 46a2453
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk a4da181
Merge branch 'main' into fix/unmatched-jsdoc-params
a-tarasyuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package checker | ||
|
||
import ( | ||
"github.com/microsoft/typescript-go/internal/ast" | ||
"github.com/microsoft/typescript-go/internal/collections" | ||
"github.com/microsoft/typescript-go/internal/diagnostics" | ||
) | ||
|
||
func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) { | ||
var jsdocParameters []*ast.Node | ||
for _, tag := range getAllJSDocTags(node) { | ||
if tag.Kind == ast.KindJSDocParameterTag { | ||
name := tag.AsJSDocParameterOrPropertyTag().Name() | ||
if ast.IsIdentifier(name) && len(name.Text()) == 0 { | ||
continue | ||
} | ||
jsdocParameters = append(jsdocParameters, tag) | ||
} | ||
} | ||
|
||
if len(jsdocParameters) == 0 { | ||
return | ||
} | ||
|
||
isJs := ast.IsInJSFile(node) | ||
parameters := collections.Set[string]{} | ||
excludedParameters := collections.Set[int]{} | ||
|
||
for i, param := range node.Parameters() { | ||
name := param.AsParameterDeclaration().Name() | ||
if ast.IsIdentifier(name) { | ||
parameters.Add(name.Text()) | ||
} | ||
if ast.IsBindingPattern(name) { | ||
excludedParameters.Add(i) | ||
} | ||
} | ||
if c.containsArgumentsReference(node) { | ||
if isJs { | ||
lastJSDocParamIndex := len(jsdocParameters) - 1 | ||
lastJSDocParam := jsdocParameters[lastJSDocParamIndex].AsJSDocParameterOrPropertyTag() | ||
if lastJSDocParam == nil || !ast.IsIdentifier(lastJSDocParam.Name()) { | ||
return | ||
} | ||
if excludedParameters.Has(lastJSDocParamIndex) || parameters.Has(lastJSDocParam.Name().Text()) { | ||
return | ||
} | ||
if lastJSDocParam.TypeExpression == nil || lastJSDocParam.TypeExpression.Type() == nil { | ||
return | ||
} | ||
if c.isArrayType(c.getTypeFromTypeNode(lastJSDocParam.TypeExpression.Type())) { | ||
return | ||
} | ||
c.error(lastJSDocParam.Name(), diagnostics.JSDoc_param_tag_has_name_0_but_there_is_no_parameter_with_that_name_It_would_match_arguments_if_it_had_an_array_type, lastJSDocParam.Name().Text()) | ||
} | ||
} else { | ||
for index, tag := range jsdocParameters { | ||
name := tag.AsJSDocParameterOrPropertyTag().Name() | ||
isNameFirst := tag.AsJSDocParameterOrPropertyTag().IsNameFirst | ||
|
||
if excludedParameters.Has(index) || (ast.IsIdentifier(name) && parameters.Has(name.Text())) { | ||
continue | ||
} | ||
|
||
if ast.IsQualifiedName(name) { | ||
if isJs { | ||
c.error(name, diagnostics.Qualified_name_0_is_not_allowed_without_a_leading_param_object_1, | ||
entityNameToString(name), | ||
entityNameToString(name.AsQualifiedName().Left), | ||
) | ||
} | ||
} else { | ||
if !isNameFirst { | ||
c.errorOrSuggestion(isJs, name, | ||
diagnostics.JSDoc_param_tag_has_name_0_but_there_is_no_parameter_with_that_name, | ||
name.Text(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func getAllJSDocTags(node *ast.Node) []*ast.Node { | ||
if node == nil { | ||
return nil | ||
} | ||
jsdocs := node.JSDoc(nil) | ||
if len(jsdocs) == 0 { | ||
return nil | ||
} | ||
lastJSDoc := jsdocs[len(jsdocs)-1].AsJSDoc() | ||
if lastJSDoc.Tags == nil { | ||
return nil | ||
} | ||
return lastJSDoc.Tags.Nodes | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
testdata/baselines/reference/submodule/compiler/jsdocParamTagInvalid.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/a.js(1,21): error TS8024: JSDoc '@param' tag has name 'colour', but there is no parameter with that name. | ||
|
||
|
||
==== /a.js (1 errors) ==== | ||
/** @param {string} colour */ | ||
~~~~~~ | ||
!!! error TS8024: JSDoc '@param' tag has name 'colour', but there is no parameter with that name. | ||
function f(color) {} | ||
|
80 changes: 80 additions & 0 deletions
80
testdata/baselines/reference/submodule/conformance/jsdocParamTag2.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
0.js(70,20): error TS8024: JSDoc '@param' tag has name 'y', but there is no parameter with that name. | ||
|
||
|
||
==== 0.js (1 errors) ==== | ||
// Object literal syntax | ||
/** | ||
* @param {{a: string, b: string}} obj | ||
* @param {string} x | ||
*/ | ||
function good1({a, b}, x) {} | ||
/** | ||
* @param {{a: string, b: string}} obj | ||
* @param {{c: number, d: number}} OBJECTION | ||
*/ | ||
function good2({a, b}, {c, d}) {} | ||
/** | ||
* @param {number} x | ||
* @param {{a: string, b: string}} obj | ||
* @param {string} y | ||
*/ | ||
function good3(x, {a, b}, y) {} | ||
/** | ||
* @param {{a: string, b: string}} obj | ||
*/ | ||
function good4({a, b}) {} | ||
|
||
// nested object syntax | ||
/** | ||
* @param {Object} obj | ||
* @param {string} obj.a - this is like the saddest way to specify a type | ||
* @param {string} obj.b - but it sure does allow a lot of documentation | ||
* @param {string} x | ||
*/ | ||
function good5({a, b}, x) {} | ||
/** | ||
* @param {Object} obj | ||
* @param {string} obj.a | ||
* @param {string} obj.b - but it sure does allow a lot of documentation | ||
* @param {Object} OBJECTION - documentation here too | ||
* @param {string} OBJECTION.c | ||
* @param {string} OBJECTION.d - meh | ||
*/ | ||
function good6({a, b}, {c, d}) {} | ||
/** | ||
* @param {number} x | ||
* @param {Object} obj | ||
* @param {string} obj.a | ||
* @param {string} obj.b | ||
* @param {string} y | ||
*/ | ||
function good7(x, {a, b}, y) {} | ||
/** | ||
* @param {Object} obj | ||
* @param {string} obj.a | ||
* @param {string} obj.b | ||
*/ | ||
function good8({a, b}) {} | ||
|
||
/** | ||
* @param {{ a: string }} argument | ||
*/ | ||
function good9({ a }) { | ||
console.log(arguments, a); | ||
} | ||
|
||
/** | ||
* @param {object} obj - this type gets ignored | ||
* @param {string} obj.a | ||
* @param {string} obj.b - and x's type gets used for both parameters | ||
* @param {string} x | ||
*/ | ||
function bad1(x, {a, b}) {} | ||
/** | ||
* @param {string} y - here, y's type gets ignored but obj's is fine | ||
~ | ||
!!! error TS8024: JSDoc '@param' tag has name 'y', but there is no parameter with that name. | ||
* @param {{a: string, b: string}} obj | ||
*/ | ||
function bad2(x, {a, b}) {} | ||
|
78 changes: 78 additions & 0 deletions
78
testdata/baselines/reference/submodule/conformance/jsdocParamTagTypeLiteral.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
0.js(3,20): error TS8024: JSDoc '@param' tag has name 'unrelated', but there is no parameter with that name. | ||
|
||
|
||
==== 0.js (1 errors) ==== | ||
/** | ||
* @param {Object} notSpecial | ||
* @param {string} unrelated - not actually related because it's not notSpecial.unrelated | ||
~~~~~~~~~ | ||
!!! error TS8024: JSDoc '@param' tag has name 'unrelated', but there is no parameter with that name. | ||
*/ | ||
function normal(notSpecial) { | ||
notSpecial; // should just be 'Object' | ||
} | ||
normal(12); | ||
|
||
/** | ||
* @param {Object} opts1 doc1 | ||
* @param {string} opts1.x doc2 | ||
* @param {string=} opts1.y doc3 | ||
* @param {string} [opts1.z] doc4 | ||
* @param {string} [opts1.w="hi"] doc5 | ||
*/ | ||
function foo1(opts1) { | ||
opts1.x; | ||
} | ||
|
||
foo1({x: 'abc'}); | ||
|
||
/** | ||
* @param {Object[]} opts2 | ||
* @param {string} opts2[].anotherX | ||
* @param {string=} opts2[].anotherY | ||
*/ | ||
function foo2(/** @param opts2 bad idea theatre! */opts2) { | ||
opts2[0].anotherX; | ||
} | ||
|
||
foo2([{anotherX: "world"}]); | ||
|
||
/** | ||
* @param {object} opts3 | ||
* @param {string} opts3.x | ||
*/ | ||
function foo3(opts3) { | ||
opts3.x; | ||
} | ||
foo3({x: 'abc'}); | ||
|
||
/** | ||
* @param {object[]} opts4 | ||
* @param {string} opts4[].x | ||
* @param {string=} opts4[].y | ||
* @param {string} [opts4[].z] | ||
* @param {string} [opts4[].w="hi"] | ||
*/ | ||
function foo4(opts4) { | ||
opts4[0].x; | ||
} | ||
|
||
foo4([{ x: 'hi' }]); | ||
|
||
/** | ||
* @param {object[]} opts5 - Let's test out some multiple nesting levels | ||
* @param {string} opts5[].help - (This one is just normal) | ||
* @param {object} opts5[].what - Look at us go! Here's the first nest! | ||
* @param {string} opts5[].what.a - (Another normal one) | ||
* @param {Object[]} opts5[].what.bad - Now we're nesting inside a nested type | ||
* @param {string} opts5[].what.bad[].idea - I don't think you can get back out of this level... | ||
* @param {boolean} opts5[].what.bad[].oh - Oh ... that's how you do it. | ||
* @param {number} opts5[].unnest - Here we are almost all the way back at the beginning. | ||
*/ | ||
function foo5(opts5) { | ||
opts5[0].what.bad[0].idea; | ||
opts5[0].unnest; | ||
} | ||
|
||
foo5([{ help: "help", what: { a: 'a', bad: [{ idea: 'idea', oh: false }] }, unnest: 1 }]); | ||
|
12 changes: 12 additions & 0 deletions
12
.../baselines/reference/submodule/conformance/paramTagNestedWithoutTopLevelObject.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
paramTagNestedWithoutTopLevelObject.js(2,20): error TS8032: Qualified name 'xyz.p' is not allowed without a leading '@param {object} xyz'. | ||
|
||
|
||
==== paramTagNestedWithoutTopLevelObject.js (1 errors) ==== | ||
/** | ||
* @param {number} xyz.p | ||
~~~~~ | ||
!!! error TS8032: Qualified name 'xyz.p' is not allowed without a leading '@param {object} xyz'. | ||
*/ | ||
function g(xyz) { | ||
return xyz.p; | ||
} |
13 changes: 13 additions & 0 deletions
13
...baselines/reference/submodule/conformance/paramTagNestedWithoutTopLevelObject2.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
paramTagNestedWithoutTopLevelObject2.js(2,20): error TS8032: Qualified name 'xyz.bar' is not allowed without a leading '@param {object} xyz'. | ||
|
||
|
||
==== paramTagNestedWithoutTopLevelObject2.js (1 errors) ==== | ||
/** | ||
* @param {object} xyz.bar | ||
~~~~~~~ | ||
!!! error TS8032: Qualified name 'xyz.bar' is not allowed without a leading '@param {object} xyz'. | ||
* @param {number} xyz.bar.p | ||
*/ | ||
function g(xyz) { | ||
return xyz.bar.p; | ||
} |
5 changes: 4 additions & 1 deletion
5
...baselines/reference/submodule/conformance/paramTagNestedWithoutTopLevelObject3.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.