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.
This commit is contained in:
Martin Probst
2018-07-12 12:23:24 +02:00
parent d92c26db69
commit db888b8670
3 changed files with 38 additions and 1 deletions

View File

@@ -296,6 +296,36 @@ namespace ts {
}
}
});
// https://github.com/Microsoft/TypeScript/issues/17594
testBaseline("transformAddCommentToExportedVar", () => {
return transpileModule(`export const exportedDirectly = 1;
const exportedSeparately = 2;
export {exportedSeparately};
`, {
transformers: {
before: [addSyntheticComment],
},
compilerOptions: {
target: ScriptTarget.ES5,
newLine: NewLineKind.CarriageReturnLineFeed,
}
}).outputText;
function addSyntheticComment(context: TransformationContext) {
return (sourceFile: SourceFile): SourceFile => {
return visitNode(sourceFile, rootTransform, isSourceFile);
};
function rootTransform<T extends Node>(node: T): VisitResult<T> {
if (isVariableStatement(node)) {
setEmitFlags(node, EmitFlags.NoLeadingComments);
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "* @type {number} ", pos: -1, end: -1, hasTrailingNewLine: true }]);
return node;
}
return visitEachChild(node, rootTransform, context);
}
}
});
});
}