Skip to content

Commit 372c7d9

Browse files
authored
infer from usage JSDoc:Don't emit nested comments (microsoft#28161)
* infer from usage JSDoc:Don't emit nested comments Previously, the trivia on a parameter name would show up inside the emitted JSDoc comment. If the trivia contained a C-style comment, the emitted JSDoc comment would be invalid. For example: ```js function call(callback /*oh no*/) { return callback(this) } ``` Emitted this comment: ```js /** * @param {(arg0: any) => void} callback /*oh no*/ */ ``` * Remove misleading comment used for debugging.
1 parent 14c328e commit 372c7d9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/services/codefixes/inferFromUsage.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ namespace ts.codefix {
219219
if (param.initializer || getJSDocType(param) || !isIdentifier(param.name)) return;
220220

221221
const typeNode = inference.type && getTypeNodeIfAccessible(inference.type, param, program, host);
222-
return typeNode && createJSDocParamTag(param.name, !!inference.isOptional, createJSDocTypeExpression(typeNode), "");
222+
const name = getSynthesizedClone(param.name);
223+
setEmitFlags(name, EmitFlags.NoComments | EmitFlags.NoNestedComments);
224+
return typeNode && createJSDocParamTag(name, !!inference.isOptional, createJSDocTypeExpression(typeNode), "");
223225
});
224226
addJSDocTags(changes, sourceFile, signature, paramTags);
225227
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowJs: true
4+
// @checkJs: true
5+
// @noEmit: true
6+
// @noImplicitAny: true
7+
// @Filename: important.js
8+
9+
10+
////function coll(callback /*, name1, name2, ... */) {
11+
//// return callback(this);
12+
////}
13+
14+
verify.codeFix({
15+
description: "Infer parameter types from usage",
16+
index: 2,
17+
newFileContent:
18+
`/**
19+
* @param {(arg0: any) => void} callback
20+
*/
21+
function coll(callback /*, name1, name2, ... */) {
22+
return callback(this);
23+
}`,
24+
});

0 commit comments

Comments
 (0)