glimmerjs/glimmer-vm

Glimmer Transform - Preserve spacing after transform

robert-j-webb opened this issue · 2 comments

I'm using the glimmer-vm to edit and then save some handlebars files for a refactoring project I'm working on. This has the unintended consequence of eating all the nice whitespace that makes the template more readable - see this for an example: Transform eats white space. This also happens with component hashes: here.

I've been trying to push new line characters and tabs at the right spot in the transform, but it gets really tricky really fast. Is there anyway to preserve whitespace after a transform?

For reference, I'm using this code to edit and print the handlebars files:

  const glimmer = require('@glimmer/syntax');
  const ast = glimmer.preprocess(file);
  glimmer.traverse(ast, {
    ElementNode(node) {
     //transorm code goes here
    },
  });
  return glimmer.print(ast);

You might want to look into using https://github.com/q2ebanking/ember-template-rewrite (which handles white space quite a bit better). I don’t have a feature by feature comparison though.

Thank you so much @rwjblue! I was able to preserve whitespace by using that repo.