Skip to content

Commit f92be3f

Browse files
committed
Skip asterisks after newline when printing JSDoc types
1 parent fdfd939 commit f92be3f

File tree

5 files changed

+119
-12
lines changed

5 files changed

+119
-12
lines changed

src/compiler/utilities.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,29 @@ namespace ts {
490490
return getTextOfNodeFromSourceText(sourceFile.text, node, includeTrivia);
491491
}
492492

493+
function isJSDocTypeExpressionOrChild(node: Node): boolean {
494+
if (node.kind === SyntaxKind.JSDocTypeExpression) {
495+
return true;
496+
}
497+
if (node.parent) {
498+
return isJSDocTypeExpressionOrChild(node.parent);
499+
}
500+
return false;
501+
}
502+
493503
export function getTextOfNodeFromSourceText(sourceText: string, node: Node, includeTrivia = false): string {
494504
if (nodeIsMissing(node)) {
495505
return "";
496506
}
497507

498-
return sourceText.substring(includeTrivia ? node.pos : skipTrivia(sourceText, node.pos), node.end);
508+
let text = sourceText.substring(includeTrivia ? node.pos : skipTrivia(sourceText, node.pos), node.end);
509+
510+
if (isJSDocTypeExpressionOrChild(node)) {
511+
// strip space + asterisk at line start
512+
text = text.replace(/(^|\r?\n|\r)\s*\*\s*/g, "$1");
513+
}
514+
515+
return text;
499516
}
500517

501518
export function getTextOfNode(node: Node, includeTrivia = false): string {

tests/baselines/reference/typedefTagWrapping.symbols

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
=== tests/cases/conformance/jsdoc/mod1.js ===
22
/**
33
* @typedef {function(string): boolean}
4-
* MyType
4+
* Type1
55
*/
66

77
/**
88
* Tries to use a type whose name is on a different
99
* line than the typedef tag.
10-
* @param {MyType} func The function to call.
10+
* @param {Type1} func The function to call.
1111
* @param {string} arg The argument to call it with.
1212
* @returns {boolean} The return.
1313
*/
@@ -21,3 +21,33 @@ function callIt(func, arg) {
2121
>arg : Symbol(arg, Decl(mod1.js, 12, 21))
2222
}
2323

24+
=== tests/cases/conformance/jsdoc/mod2.js ===
25+
/**
26+
* @typedef {{
27+
* num: number,
28+
* str: string,
29+
* boo: boolean
30+
* }} Type2
31+
*/
32+
33+
/**
34+
* Makes use of a type with a multiline type expression.
35+
* @param {Type2} obj The object.
36+
* @returns {string|number} The return.
37+
*/
38+
function check(obj) {
39+
>check : Symbol(check, Decl(mod2.js, 0, 0))
40+
>obj : Symbol(obj, Decl(mod2.js, 13, 15))
41+
42+
return obj.boo ? obj.num : obj.str;
43+
>obj.boo : Symbol(boo, Decl(mod2.js, 3, 17))
44+
>obj : Symbol(obj, Decl(mod2.js, 13, 15))
45+
>boo : Symbol(boo, Decl(mod2.js, 3, 17))
46+
>obj.num : Symbol(num, Decl(mod2.js, 1, 14))
47+
>obj : Symbol(obj, Decl(mod2.js, 13, 15))
48+
>num : Symbol(num, Decl(mod2.js, 1, 14))
49+
>obj.str : Symbol(str, Decl(mod2.js, 2, 17))
50+
>obj : Symbol(obj, Decl(mod2.js, 13, 15))
51+
>str : Symbol(str, Decl(mod2.js, 2, 17))
52+
}
53+

tests/baselines/reference/typedefTagWrapping.types

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
=== tests/cases/conformance/jsdoc/mod1.js ===
22
/**
33
* @typedef {function(string): boolean}
4-
* MyType
4+
* Type1
55
*/
66

77
/**
88
* Tries to use a type whose name is on a different
99
* line than the typedef tag.
10-
* @param {MyType} func The function to call.
10+
* @param {Type1} func The function to call.
1111
* @param {string} arg The argument to call it with.
1212
* @returns {boolean} The return.
1313
*/
@@ -22,3 +22,34 @@ function callIt(func, arg) {
2222
>arg : string
2323
}
2424

25+
=== tests/cases/conformance/jsdoc/mod2.js ===
26+
/**
27+
* @typedef {{
28+
* num: number,
29+
* str: string,
30+
* boo: boolean
31+
* }} Type2
32+
*/
33+
34+
/**
35+
* Makes use of a type with a multiline type expression.
36+
* @param {Type2} obj The object.
37+
* @returns {string|number} The return.
38+
*/
39+
function check(obj) {
40+
>check : (obj: { num: number; str: string; boo: boolean; }) => string | number
41+
>obj : { num: number; str: string; boo: boolean; }
42+
43+
return obj.boo ? obj.num : obj.str;
44+
>obj.boo ? obj.num : obj.str : string | number
45+
>obj.boo : boolean
46+
>obj : { num: number; str: string; boo: boolean; }
47+
>boo : boolean
48+
>obj.num : number
49+
>obj : { num: number; str: string; boo: boolean; }
50+
>num : number
51+
>obj.str : string
52+
>obj : { num: number; str: string; boo: boolean; }
53+
>str : string
54+
}
55+
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
// @noEmit: true
22
// @allowJs: true
33
// @checkJs: true
4+
45
// @Filename: mod1.js
56

67
/**
78
* @typedef {function(string): boolean}
8-
* MyType
9+
* Type1
910
*/
1011

1112
/**
1213
* Tries to use a type whose name is on a different
1314
* line than the typedef tag.
14-
* @param {MyType} func The function to call.
15+
* @param {Type1} func The function to call.
1516
* @param {string} arg The argument to call it with.
1617
* @returns {boolean} The return.
1718
*/
1819
function callIt(func, arg) {
1920
return func(arg);
2021
}
22+
23+
// @Filename: mod2.js
24+
25+
/**
26+
* @typedef {{
27+
* num: number,
28+
* str: string,
29+
* boo: boolean
30+
* }} Type2
31+
*/
32+
33+
/**
34+
* Makes use of a type with a multiline type expression.
35+
* @param {Type2} obj The object.
36+
* @returns {string|number} The return.
37+
*/
38+
function check(obj) {
39+
return obj.boo ? obj.num : obj.str;
40+
}
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
21
///<reference path="fourslash.ts" />
2+
33
// @allowJs: true
4-
// @Filename: Foo.js
4+
// @Filename: jsDocFunctionSignatures.js
5+
56
/////**
6-
//// * @param {{ stringProp: string,
7-
//// * numProp: number }} o
7+
//// * @param {{
8+
//// * stringProp: string,
9+
//// * numProp: number,
10+
//// * boolProp: boolean
11+
//// * }} o
812
//// */
913
////function f1(o) {
1014
//// o/**/;
1115
////}
16+
1217
goTo.marker();
13-
verify.quickInfoIs("(parameter) o: any");
18+
verify.quickInfoIs(`(parameter) o: {
19+
stringProp: string;
20+
numProp: number;
21+
boolProp: boolean;
22+
}`);

0 commit comments

Comments
 (0)