-
Notifications
You must be signed in to change notification settings - Fork 12.9k
feat(11378): Check @param names in JSDoc method documentation #47257
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
Merged
Changes from all commits
Commits
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
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,104 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const deleteUnmatchedParameter = "deleteUnmatchedParameter"; | ||
const renameUnmatchedParameter = "renameUnmatchedParameter"; | ||
|
||
const errorCodes = [ | ||
Diagnostics.JSDoc_param_tag_has_name_0_but_there_is_no_parameter_with_that_name.code, | ||
]; | ||
|
||
registerCodeFix({ | ||
fixIds: [deleteUnmatchedParameter, renameUnmatchedParameter], | ||
errorCodes, | ||
getCodeActions: function getCodeActionsToFixUnmatchedParameter(context) { | ||
const { sourceFile, span } = context; | ||
const actions: CodeFixAction[] = []; | ||
const info = getInfo(sourceFile, span.start); | ||
if (info) { | ||
append(actions, getDeleteAction(context, info)); | ||
append(actions, getRenameAction(context, info)); | ||
return actions; | ||
} | ||
return undefined; | ||
}, | ||
getAllCodeActions: function getAllCodeActionsToFixUnmatchedParameter(context) { | ||
const tagsToSignature = new Map<SignatureDeclaration, JSDocTag[]>(); | ||
return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => { | ||
eachDiagnostic(context, errorCodes, ({ file, start }) => { | ||
const info = getInfo(file, start); | ||
if (info) { | ||
tagsToSignature.set(info.signature, append(tagsToSignature.get(info.signature), info.jsDocParameterTag)); | ||
} | ||
}); | ||
|
||
tagsToSignature.forEach((tags, signature) => { | ||
if (context.fixId === deleteUnmatchedParameter) { | ||
const tagsSet = new Set(tags); | ||
changes.filterJSDocTags(signature.getSourceFile(), signature, t => !tagsSet.has(t)); | ||
} | ||
}); | ||
})); | ||
} | ||
}); | ||
|
||
function getDeleteAction(context: CodeFixContext, { name, signature, jsDocParameterTag }: Info) { | ||
const changes = textChanges.ChangeTracker.with(context, changeTracker => | ||
changeTracker.filterJSDocTags(context.sourceFile, signature, t => t !== jsDocParameterTag)); | ||
return createCodeFixAction( | ||
deleteUnmatchedParameter, | ||
changes, | ||
[Diagnostics.Delete_unused_param_tag_0, name.getText(context.sourceFile)], | ||
deleteUnmatchedParameter, | ||
Diagnostics.Delete_all_unused_param_tags | ||
); | ||
} | ||
|
||
function getRenameAction(context: CodeFixContext, { name, signature, jsDocParameterTag }: Info) { | ||
if (!length(signature.parameters)) return undefined; | ||
|
||
const sourceFile = context.sourceFile; | ||
const tags = getJSDocTags(signature); | ||
const names = new Set<__String>(); | ||
for (const tag of tags) { | ||
if (isJSDocParameterTag(tag) && isIdentifier(tag.name)) { | ||
names.add(tag.name.escapedText); | ||
} | ||
} | ||
// @todo - match to all available names instead to the first parameter name | ||
// @see /codeFixRenameUnmatchedParameter3.ts | ||
const parameterName = firstDefined(signature.parameters, p => | ||
isIdentifier(p.name) && !names.has(p.name.escapedText) ? p.name.getText(sourceFile) : undefined); | ||
if (parameterName === undefined) return undefined; | ||
|
||
const newJSDocParameterTag = factory.updateJSDocParameterTag( | ||
jsDocParameterTag, | ||
jsDocParameterTag.tagName, | ||
factory.createIdentifier(parameterName), | ||
jsDocParameterTag.isBracketed, | ||
jsDocParameterTag.typeExpression, | ||
jsDocParameterTag.isNameFirst, | ||
jsDocParameterTag.comment | ||
); | ||
const changes = textChanges.ChangeTracker.with(context, changeTracker => | ||
changeTracker.replaceJSDocComment(sourceFile, signature, map(tags, t => t === jsDocParameterTag ? newJSDocParameterTag : t))); | ||
return createCodeFixActionWithoutFixAll(renameUnmatchedParameter, changes, [Diagnostics.Rename_param_tag_name_0_to_1, name.getText(sourceFile), parameterName]); | ||
} | ||
|
||
interface Info { | ||
readonly signature: SignatureDeclaration; | ||
readonly jsDocParameterTag: JSDocParameterTag; | ||
readonly name: Identifier; | ||
} | ||
|
||
function getInfo(sourceFile: SourceFile, pos: number): Info | undefined { | ||
const token = getTokenAtPosition(sourceFile, pos); | ||
if (token.parent && isJSDocParameterTag(token.parent) && isIdentifier(token.parent.name)) { | ||
const jsDocParameterTag = token.parent; | ||
const signature = getHostSignatureFromJSDoc(jsDocParameterTag); | ||
if (signature) { | ||
return { signature, name: token.parent.name, jsDocParameterTag }; | ||
} | ||
} | ||
return undefined; | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
tests/baselines/reference/checkJsdocParamOnVariableDeclaredFunctionExpression.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,23 @@ | ||
tests/cases/conformance/jsdoc/0.js(14,20): error TS8024: JSDoc '@param' tag has name 's', but there is no parameter with that name. | ||
|
||
|
||
==== tests/cases/conformance/jsdoc/0.js (1 errors) ==== | ||
// @ts-check | ||
/** | ||
* @param {number=} n | ||
* @param {string} [s] | ||
*/ | ||
var x = function foo(n, s) {} | ||
var y; | ||
/** | ||
* @param {boolean!} b | ||
*/ | ||
y = function bar(b) {} | ||
|
||
/** | ||
* @param {string} s | ||
~ | ||
!!! error TS8024: JSDoc '@param' tag has name 's', but there is no parameter with that name. | ||
*/ | ||
var one = function (s) { }, two = function (untyped) { }; | ||
|
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,23 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {number} b | ||
//// */ | ||
////function foo() {} | ||
|
||
verify.codeFixAvailable([ | ||
{ description: "Delete unused '@param' tag 'a'" }, | ||
{ description: "Delete unused '@param' tag 'b'" }, | ||
]); | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Delete_unused_param_tag_0.message, "a"], | ||
index: 0, | ||
newFileContent: | ||
`/** | ||
* @param {number} b | ||
*/ | ||
function foo() {}`, | ||
}); |
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,26 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {string} b | ||
//// */ | ||
////function foo(a: number) { | ||
//// a; | ||
////} | ||
|
||
verify.codeFixAvailable([ | ||
{ description: "Delete unused '@param' tag 'b'" }, | ||
]); | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Delete_unused_param_tag_0.message, "b"], | ||
index: 0, | ||
newFileContent: | ||
`/** | ||
* @param {number} a | ||
*/ | ||
function foo(a: number) { | ||
a; | ||
}` | ||
}); |
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,30 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {string} b | ||
//// * @param {number} c | ||
//// */ | ||
////function foo(a: number, c: number) { | ||
//// a; | ||
//// c; | ||
////} | ||
|
||
verify.codeFixAvailable([ | ||
{ description: "Delete unused '@param' tag 'b'" }, | ||
]); | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Delete_unused_param_tag_0.message, "b"], | ||
index: 0, | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
newFileContent: | ||
`/** | ||
* @param {number} a | ||
* @param {number} c | ||
*/ | ||
function foo(a: number, c: number) { | ||
a; | ||
c; | ||
}` | ||
}); |
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,19 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// */ | ||
////function foo() {} | ||
|
||
verify.codeFixAvailable([ | ||
{ description: "Delete unused '@param' tag 'a'" }, | ||
]); | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Delete_unused_param_tag_0.message, "a"], | ||
index: 0, | ||
newFileContent: | ||
`/** */ | ||
function foo() {}` | ||
}); |
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.