Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertAfter a CommentBlock or CommentLine #1341

Open
mbroadst opened this issue Jul 8, 2023 · 0 comments
Open

insertAfter a CommentBlock or CommentLine #1341

mbroadst opened this issue Jul 8, 2023 · 0 comments

Comments

@mbroadst
Copy link

mbroadst commented Jul 8, 2023

Hi, I'm trying to figure out how to insert an import statement after a comment block at the top of a file. The sample input is:

/**
 * This is a code block I want to add an import statement after
 */

// something else 
const thar = 'that';

and I'd like to insert:

import {SomeClass} from 'my-module';

after the first block to produce:

/**
 * This is a code block I want to add an import statement after
 */
import {SomeClass} from 'my-module';

// something else 
const thar = 'that';

I wrote a visitor look for comments and append my import declaration, but it seems like when I run that code it doesn't change the output at all. Here's the transformer I came up with:

export default function transformer(code, { recast, parsers }) {
  const ast = recast.parse(code, { parser: parsers.esprima });
  const b = recast.types.builders;
  
  recast.visit(ast, {
    visitComment: function (comment) {
      comment.insertAfter(
        b.importDeclaration(
          [b.importSpecifier(b.identifier("SomeClass"))],
          b.literal("my-module")
        )
      );

      this.traverse(comment);
    }
  });

  return recast.print(ast).code;
}

(view in astexplorer)

what's more curious is that if I reduce the input script to just the top comment block, then running this transformer actually removes all the code in the input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant