-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(11378): check param names in JSDoc
- Loading branch information
1 parent
b7b6483
commit 0f5cc8f
Showing
20 changed files
with
497 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 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 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,89 @@ | ||
/* @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) => { | ||
const tagsSet = new Set(tags); | ||
if (context.fixId === deleteUnmatchedParameter) { | ||
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.Remove_unused_parameter_0, name.getText()], deleteUnmatchedParameter, Diagnostics.Remove_all_unused_parameters); | ||
} | ||
|
||
function getRenameAction(context: CodeFixContext, { name, signature, jsDocParameterTag }: Info) { | ||
const sourceFile = context.sourceFile; | ||
if (length(signature.parameters)) { | ||
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); | ||
} | ||
} | ||
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(context.sourceFile, signature, map(tags, t => t === jsDocParameterTag ? newJSDocParameterTag : t))); | ||
return createCodeFixActionWithoutFixAll(renameUnmatchedParameter, changes, [Diagnostics.Rename_parameter_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 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 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 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 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,18 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {number} b | ||
//// */ | ||
////function foo() {} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Remove_unused_parameter_0.message, "a"], | ||
index: 0, | ||
newFileContent: | ||
`/** | ||
* @param {number} b | ||
*/ | ||
function foo() {}`, | ||
}); |
This file contains 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,18 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {string} b | ||
//// */ | ||
////function foo(a: number) {} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Remove_unused_parameter_0.message, "b"], | ||
index: 0, | ||
newFileContent: | ||
`/** | ||
* @param {number} a | ||
*/ | ||
function foo(a: number) {}` | ||
}); |
This file contains 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,20 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {string} b | ||
//// * @param {number} c | ||
//// */ | ||
////function foo(a: number, c: number) {} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Remove_unused_parameter_0.message, "b"], | ||
index: 0, | ||
newFileContent: | ||
`/** | ||
* @param {number} a | ||
* @param {number} c | ||
*/ | ||
function foo(a: number, c: number) {}` | ||
}); |
This file contains 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,15 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @filename: a.ts | ||
/////** | ||
//// * @param {number} a | ||
//// */ | ||
////function foo() {} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Remove_unused_parameter_0.message, "a"], | ||
index: 0, | ||
newFileContent: | ||
`/** */ | ||
function foo() {}` | ||
}); |
20 changes: 20 additions & 0 deletions
20
tests/cases/fourslash/codeFixDeleteUnmatchedParameterJS1.ts
This file contains 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,20 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @allowJs: true | ||
// @checkJs: true | ||
// @filename: /a.js | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {number} b | ||
//// */ | ||
////function foo() {} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Remove_unused_parameter_0.message, "a"], | ||
index: 0, | ||
newFileContent: | ||
`/** | ||
* @param {number} b | ||
*/ | ||
function foo() {}`, | ||
}); |
20 changes: 20 additions & 0 deletions
20
tests/cases/fourslash/codeFixDeleteUnmatchedParameterJS2.ts
This file contains 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,20 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @allowJs: true | ||
// @checkJs: true | ||
// @filename: /a.js | ||
/////** | ||
//// * @param {number} a | ||
//// * @param {string} b | ||
//// */ | ||
////function foo(a) {} | ||
|
||
verify.codeFix({ | ||
description: [ts.Diagnostics.Remove_unused_parameter_0.message, "b"], | ||
index: 0, | ||
newFileContent: | ||
`/** | ||
* @param {number} a | ||
*/ | ||
function foo(a) {}` | ||
}); |
Oops, something went wrong.