Skip to content

Commit b89019d

Browse files
CopilotjakebaileyDanielRosenwasser
authored
Fix JSDoc comment preservation on default export in declaration emit (#2324)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jakebailey <[email protected]> Co-authored-by: DanielRosenwasser <[email protected]>
1 parent 41455a6 commit b89019d

15 files changed

+366
-30
lines changed

internal/transformers/declarations/transform.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,11 +1131,8 @@ func (tx *DeclarationTransformer) tryGetResolutionModeOverride(node *ast.Node) *
11311131
}
11321132

11331133
func (tx *DeclarationTransformer) preserveJsDoc(updated *ast.Node, original *ast.Node) {
1134-
// !!! TODO: JSDoc comment support
1135-
// if (hasJSDocNodes(updated) && hasJSDocNodes(original)) {
1136-
// updated.jsDoc = original.jsDoc;
1137-
// }
1138-
// return setCommentRange(updated, getCommentRange(original));
1134+
// Copy comment range from original to updated node so JSDoc comments are preserved
1135+
tx.EmitContext().AssignCommentRange(updated, original)
11391136
}
11401137

11411138
func (tx *DeclarationTransformer) removeAllComments(node *ast.Node) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//// [tests/cases/compiler/jsdocCommentDefaultExport.ts] ////
2+
3+
//// [exportDefaultObject.ts]
4+
/** Object comment */
5+
export default {
6+
fn() {}
7+
}
8+
9+
//// [exportDefaultFunction.ts]
10+
/** Function comment */
11+
export default function() {
12+
return 42;
13+
}
14+
15+
//// [exportDefaultClass.ts]
16+
/** Class comment */
17+
export default class {
18+
method() {}
19+
}
20+
21+
//// [exportDefaultLiteral.ts]
22+
/** Literal comment */
23+
export default 42;
24+
25+
//// [exportDefaultNull.ts]
26+
/** Null comment */
27+
export default null;
28+
29+
30+
//// [exportDefaultObject.js]
31+
"use strict";
32+
Object.defineProperty(exports, "__esModule", { value: true });
33+
/** Object comment */
34+
exports.default = {
35+
fn() { }
36+
};
37+
//// [exportDefaultFunction.js]
38+
"use strict";
39+
Object.defineProperty(exports, "__esModule", { value: true });
40+
exports.default = default_1;
41+
/** Function comment */
42+
function default_1() {
43+
return 42;
44+
}
45+
//// [exportDefaultClass.js]
46+
"use strict";
47+
Object.defineProperty(exports, "__esModule", { value: true });
48+
/** Class comment */
49+
class default_1 {
50+
method() { }
51+
}
52+
exports.default = default_1;
53+
//// [exportDefaultLiteral.js]
54+
"use strict";
55+
Object.defineProperty(exports, "__esModule", { value: true });
56+
/** Literal comment */
57+
exports.default = 42;
58+
//// [exportDefaultNull.js]
59+
"use strict";
60+
Object.defineProperty(exports, "__esModule", { value: true });
61+
/** Null comment */
62+
exports.default = null;
63+
64+
65+
//// [exportDefaultObject.d.ts]
66+
/** Object comment */
67+
declare const _default: {
68+
fn(): void;
69+
};
70+
export default _default;
71+
//// [exportDefaultFunction.d.ts]
72+
/** Function comment */
73+
export default function (): number;
74+
//// [exportDefaultClass.d.ts]
75+
/** Class comment */
76+
export default class {
77+
method(): void;
78+
}
79+
//// [exportDefaultLiteral.d.ts]
80+
/** Literal comment */
81+
declare const _default: number;
82+
export default _default;
83+
//// [exportDefaultNull.d.ts]
84+
/** Null comment */
85+
declare const _default: null;
86+
export default _default;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/jsdocCommentDefaultExport.ts] ////
2+
3+
=== exportDefaultObject.ts ===
4+
/** Object comment */
5+
export default {
6+
fn() {}
7+
>fn : Symbol(fn, Decl(exportDefaultObject.ts, 1, 16))
8+
}
9+
10+
=== exportDefaultFunction.ts ===
11+
12+
/** Function comment */
13+
export default function() {
14+
return 42;
15+
}
16+
17+
=== exportDefaultClass.ts ===
18+
/** Class comment */
19+
export default class {
20+
method() {}
21+
>method : Symbol(default.method, Decl(exportDefaultClass.ts, 1, 22))
22+
}
23+
24+
=== exportDefaultLiteral.ts ===
25+
26+
/** Literal comment */
27+
export default 42;
28+
29+
=== exportDefaultNull.ts ===
30+
31+
/** Null comment */
32+
export default null;
33+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/compiler/jsdocCommentDefaultExport.ts] ////
2+
3+
=== exportDefaultObject.ts ===
4+
/** Object comment */
5+
export default {
6+
>{ fn() {}} : { fn(): void; }
7+
8+
fn() {}
9+
>fn : () => void
10+
}
11+
12+
=== exportDefaultFunction.ts ===
13+
/** Function comment */
14+
export default function() {
15+
return 42;
16+
>42 : 42
17+
}
18+
19+
=== exportDefaultClass.ts ===
20+
/** Class comment */
21+
export default class {
22+
method() {}
23+
>method : () => void
24+
}
25+
26+
=== exportDefaultLiteral.ts ===
27+
28+
/** Literal comment */
29+
export default 42;
30+
31+
=== exportDefaultNull.ts ===
32+
33+
/** Null comment */
34+
export default null;
35+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//// [tests/cases/compiler/jsdocCommentDefaultExport.ts] ////
2+
3+
//// [exportDefaultObject.ts]
4+
/** Object comment */
5+
export default {
6+
fn() {}
7+
}
8+
9+
//// [exportDefaultFunction.ts]
10+
/** Function comment */
11+
export default function() {
12+
return 42;
13+
}
14+
15+
//// [exportDefaultClass.ts]
16+
/** Class comment */
17+
export default class {
18+
method() {}
19+
}
20+
21+
//// [exportDefaultLiteral.ts]
22+
/** Literal comment */
23+
export default 42;
24+
25+
//// [exportDefaultNull.ts]
26+
/** Null comment */
27+
export default null;
28+
29+
30+
//// [exportDefaultObject.js]
31+
/** Object comment */
32+
export default {
33+
fn() { }
34+
};
35+
//// [exportDefaultFunction.js]
36+
/** Function comment */
37+
export default function () {
38+
return 42;
39+
}
40+
//// [exportDefaultClass.js]
41+
/** Class comment */
42+
export default class {
43+
method() { }
44+
}
45+
//// [exportDefaultLiteral.js]
46+
/** Literal comment */
47+
export default 42;
48+
//// [exportDefaultNull.js]
49+
/** Null comment */
50+
export default null;
51+
52+
53+
//// [exportDefaultObject.d.ts]
54+
/** Object comment */
55+
declare const _default: {
56+
fn(): void;
57+
};
58+
export default _default;
59+
//// [exportDefaultFunction.d.ts]
60+
/** Function comment */
61+
export default function (): number;
62+
//// [exportDefaultClass.d.ts]
63+
/** Class comment */
64+
export default class {
65+
method(): void;
66+
}
67+
//// [exportDefaultLiteral.d.ts]
68+
/** Literal comment */
69+
declare const _default: number;
70+
export default _default;
71+
//// [exportDefaultNull.d.ts]
72+
/** Null comment */
73+
declare const _default: null;
74+
export default _default;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/jsdocCommentDefaultExport.ts] ////
2+
3+
=== exportDefaultObject.ts ===
4+
/** Object comment */
5+
export default {
6+
fn() {}
7+
>fn : Symbol(fn, Decl(exportDefaultObject.ts, 1, 16))
8+
}
9+
10+
=== exportDefaultFunction.ts ===
11+
12+
/** Function comment */
13+
export default function() {
14+
return 42;
15+
}
16+
17+
=== exportDefaultClass.ts ===
18+
/** Class comment */
19+
export default class {
20+
method() {}
21+
>method : Symbol(default.method, Decl(exportDefaultClass.ts, 1, 22))
22+
}
23+
24+
=== exportDefaultLiteral.ts ===
25+
26+
/** Literal comment */
27+
export default 42;
28+
29+
=== exportDefaultNull.ts ===
30+
31+
/** Null comment */
32+
export default null;
33+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/compiler/jsdocCommentDefaultExport.ts] ////
2+
3+
=== exportDefaultObject.ts ===
4+
/** Object comment */
5+
export default {
6+
>{ fn() {}} : { fn(): void; }
7+
8+
fn() {}
9+
>fn : () => void
10+
}
11+
12+
=== exportDefaultFunction.ts ===
13+
/** Function comment */
14+
export default function() {
15+
return 42;
16+
>42 : 42
17+
}
18+
19+
=== exportDefaultClass.ts ===
20+
/** Class comment */
21+
export default class {
22+
method() {}
23+
>method : () => void
24+
}
25+
26+
=== exportDefaultLiteral.ts ===
27+
28+
/** Literal comment */
29+
export default 42;
30+
31+
=== exportDefaultNull.ts ===
32+
33+
/** Null comment */
34+
export default null;
35+

testdata/baselines/reference/submodule/compiler/jsDeclarationEmitExportAssignedFunctionWithExtraTypedefsMembers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ module.exports = function loader(options) { };
3232
export type Options = {
3333
opt: string;
3434
};
35+
/**
36+
* @param {Options} options
37+
*/
3538
declare const _default: (options: Options) => void;
3639
export = _default;
3740

3841

3942
//// [DtsFileErrors]
4043

4144

42-
out/index.d.ts(9,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
45+
out/index.d.ts(12,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
4346

4447

4548
==== out/index.d.ts (1 errors) ====
@@ -50,6 +53,9 @@ out/index.d.ts(9,1): error TS2309: An export assignment cannot be used in a modu
5053
export type Options = {
5154
opt: string;
5255
};
56+
/**
57+
* @param {Options} options
58+
*/
5359
declare const _default: (options: Options) => void;
5460
export = _default;
5561
~~~~~~~~~~~~~~~~~~

testdata/baselines/reference/submodule/compiler/jsDeclarationEmitExportAssignedFunctionWithExtraTypedefsMembers.js.diff

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
+export type Options = {
1818
opt: string;
1919
};
20+
+/**
21+
+ * @param {Options} options
22+
+ */
2023
+declare const _default: (options: Options) => void;
2124
+export = _default;
2225
+
2326
+
2427
+//// [DtsFileErrors]
2528
+
2629
+
27-
+out/index.d.ts(9,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
30+
+out/index.d.ts(12,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
2831
+
2932
+
3033
+==== out/index.d.ts (1 errors) ====
@@ -35,6 +38,9 @@
3538
+ export type Options = {
3639
+ opt: string;
3740
+ };
41+
+ /**
42+
+ * @param {Options} options
43+
+ */
3844
+ declare const _default: (options: Options) => void;
3945
+ export = _default;
4046
+ ~~~~~~~~~~~~~~~~~~

testdata/baselines/reference/submodule/conformance/exportDefaultExpressionComments.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ exports.default = null;
1717

1818

1919
//// [exportDefaultExpressionComments.d.ts]
20+
/**
21+
* JSDoc Comments
22+
*/
2023
declare const _default: any;
2124
export default _default;

0 commit comments

Comments
 (0)