Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------- Input -------
- import { objectType } from "nexus";
- // Original comment
- export const Toto = objectType({
- name: "Query",
- definition(t) {
- t.string("hello");
- }
- });
- // ------- Expected output -------
- import { objectType } from "nexus";
- //Improved comment
- export const Toto = objectType({
- name: "Query",
- definition(t) {
- t.string("hello");
- }
- });
- // ------- Actual output -------
- import { objectType } from "nexus";
- //Improved comment
- // Original comment
- export const Toto = objectType({
- name: "Query",
- definition(t) {
- t.string("hello");
- }
- });
- // ------- Implementation -------
- const transformFactoryStack = (context: ts.TransformationContext) => (
- rootNode: ts.SourceFile
- ): ts.SourceFile => {
- const visit = (node: ts.Node) => {
- node = ts.visitEachChild(node, visit, context);
- if (ts.isVariableStatement(node)) {
- let sourceFileText = node.getSourceFile().text;
- const existingComments = ts.getLeadingCommentRanges(
- sourceFileText,
- node.pos
- );
- if (existingComments) {
- // Log existing comments just for fun
- for (const comment of existingComments) {
- console.log({
- existingComment: sourceFileText.substring(comment.pos, comment.end)
- });
- }
- ts.setTextRange(node, { pos: node.getStart(), end: node.getEnd() });
- ts.setSyntheticLeadingComments(node, [
- {
- pos: -1,
- end: -1,
- hasTrailingNewLine: false,
- text: "Improved comment",
- kind: ts.SyntaxKind.SingleLineCommentTrivia
- }
- ]);
- }
- }
- return node;
- };
- return ts.visitNode(rootNode, visit);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement