Skip to content

Commit db888b8

Browse files
committed
Retain synthetic comments on exported variables.
Variables that do not have a local variable created get transformed into a single exports assignment expression. TypeScript previously just created a new expression and set the text range to retain original comments, but for synthetic comments, merging the emit nodes by setting the original node is required.
1 parent d92c26d commit db888b8

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/compiler/transformers/module/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ namespace ts {
11401140
}
11411141

11421142
if (expressions) {
1143-
statements = append(statements, setTextRange(createExpressionStatement(inlineExpressions(expressions)), node));
1143+
statements = append(statements, setOriginalNode(setTextRange(createExpressionStatement(inlineExpressions(expressions)), node), node));
11441144
}
11451145
}
11461146
else {

src/testRunner/unittests/transform.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,36 @@ namespace ts {
296296
}
297297
}
298298
});
299+
300+
// https://github.com/Microsoft/TypeScript/issues/17594
301+
testBaseline("transformAddCommentToExportedVar", () => {
302+
return transpileModule(`export const exportedDirectly = 1;
303+
const exportedSeparately = 2;
304+
export {exportedSeparately};
305+
`, {
306+
transformers: {
307+
before: [addSyntheticComment],
308+
},
309+
compilerOptions: {
310+
target: ScriptTarget.ES5,
311+
newLine: NewLineKind.CarriageReturnLineFeed,
312+
}
313+
}).outputText;
314+
315+
function addSyntheticComment(context: TransformationContext) {
316+
return (sourceFile: SourceFile): SourceFile => {
317+
return visitNode(sourceFile, rootTransform, isSourceFile);
318+
};
319+
function rootTransform<T extends Node>(node: T): VisitResult<T> {
320+
if (isVariableStatement(node)) {
321+
setEmitFlags(node, EmitFlags.NoLeadingComments);
322+
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "* @type {number} ", pos: -1, end: -1, hasTrailingNewLine: true }]);
323+
return node;
324+
}
325+
return visitEachChild(node, rootTransform, context);
326+
}
327+
}
328+
});
299329
});
300330
}
301331

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
/** @type {number} */
4+
exports.exportedDirectly = 1;
5+
/** @type {number} */
6+
var exportedSeparately = 2;
7+
exports.exportedSeparately = exportedSeparately;

0 commit comments

Comments
 (0)