davidbonnet / astring

🌳 Tiny and fast JavaScript code generator from an ESTree-compliant AST.

Home Page:https://david.bonnet.cc/astring/demo/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there is a way to easily jump a line ?

fraxken opened this issue · comments

Hi,

I'm using this lib to generate JavaScript code from a generated AST but i wanted to add some blank line between some of my stmt. Is there is a way to achieve this without having to go in a complicated code that deal with start/end ?

Sorry for the newbie question and thanks for your time !

Best Regards,
Thomas

This can be easily achieved by defining a custom generator.

For example, the following adds blank lines before/after each function declaration:

import { generate, baseGenerator } from 'astring'

generate(ast, {
  generator: {
    ...baseGenerator,
    FunctionDeclaration(node, state) {
      state.write('\n')
      baseGenerator.FunctionDeclaration(node, state)
      state.write('\n')
    },
  },
})