Skip to content

Commit b4838c8

Browse files
authored
Use comments from host variable declaration when exporting a signature in js declarations (microsoft#37594)
1 parent 5a79169 commit b4838c8

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6132,7 +6132,8 @@ namespace ts {
61326132
// Each overload becomes a separate function declaration, in order
61336133
const decl = signatureToSignatureDeclarationHelper(sig, SyntaxKind.FunctionDeclaration, context, includePrivateSymbol, bundled) as FunctionDeclaration;
61346134
decl.name = createIdentifier(localName);
6135-
addResult(setTextRange(decl, sig.declaration), modifierFlags);
6135+
// for expressions assigned to `var`s, use the `var` as the text range
6136+
addResult(setTextRange(decl, sig.declaration && isVariableDeclaration(sig.declaration.parent) && sig.declaration.parent.parent || sig.declaration), modifierFlags);
61366137
}
61376138
// Module symbol emit will take care of module-y members, provided it has exports
61386139
if (!(symbol.flags & (SymbolFlags.ValueModule | SymbolFlags.NamespaceModule) && !!symbol.exports && !!symbol.exports.size)) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//// [index1.js]
2+
/**
3+
* const doc comment
4+
*/
5+
const x = (a) => {
6+
return '';
7+
};
8+
9+
/**
10+
* function doc comment
11+
*/
12+
function b() {
13+
return 0;
14+
}
15+
16+
module.exports = {x, b}
17+
18+
//// [index1.js]
19+
/**
20+
* const doc comment
21+
*/
22+
var x = function (a) {
23+
return '';
24+
};
25+
/**
26+
* function doc comment
27+
*/
28+
function b() {
29+
return 0;
30+
}
31+
module.exports = { x: x, b: b };
32+
33+
34+
//// [index1.d.ts]
35+
/**
36+
* const doc comment
37+
*/
38+
export function x(a: any): string;
39+
/**
40+
* function doc comment
41+
*/
42+
export function b(): number;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/conformance/jsdoc/declarations/index1.js ===
2+
/**
3+
* const doc comment
4+
*/
5+
const x = (a) => {
6+
>x : Symbol(x, Decl(index1.js, 3, 5))
7+
>a : Symbol(a, Decl(index1.js, 3, 11))
8+
9+
return '';
10+
};
11+
12+
/**
13+
* function doc comment
14+
*/
15+
function b() {
16+
>b : Symbol(b, Decl(index1.js, 5, 2))
17+
18+
return 0;
19+
}
20+
21+
module.exports = {x, b}
22+
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/index1", Decl(index1.js, 0, 0))
23+
>module : Symbol(export=, Decl(index1.js, 12, 1))
24+
>exports : Symbol(export=, Decl(index1.js, 12, 1))
25+
>x : Symbol(x, Decl(index1.js, 14, 18))
26+
>b : Symbol(b, Decl(index1.js, 14, 20))
27+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/conformance/jsdoc/declarations/index1.js ===
2+
/**
3+
* const doc comment
4+
*/
5+
const x = (a) => {
6+
>x : (a: any) => string
7+
>(a) => { return '';} : (a: any) => string
8+
>a : any
9+
10+
return '';
11+
>'' : ""
12+
13+
};
14+
15+
/**
16+
* function doc comment
17+
*/
18+
function b() {
19+
>b : () => number
20+
21+
return 0;
22+
>0 : 0
23+
}
24+
25+
module.exports = {x, b}
26+
>module.exports = {x, b} : { x: (a: any) => string; b: () => number; }
27+
>module.exports : { x: (a: any) => string; b: () => number; }
28+
>module : { "\"tests/cases/conformance/jsdoc/declarations/index1\"": { x: (a: any) => string; b: () => number; }; }
29+
>exports : { x: (a: any) => string; b: () => number; }
30+
>{x, b} : { x: (a: any) => string; b: () => number; }
31+
>x : (a: any) => string
32+
>b : () => number
33+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @target: es5
4+
// @outDir: ./out
5+
// @declaration: true
6+
// @filename: index1.js
7+
/**
8+
* const doc comment
9+
*/
10+
const x = (a) => {
11+
return '';
12+
};
13+
14+
/**
15+
* function doc comment
16+
*/
17+
function b() {
18+
return 0;
19+
}
20+
21+
module.exports = {x, b}

0 commit comments

Comments
 (0)