nebulab / erb-formatter

Format ERB files with speed and precision
MIT License
151 stars 25 forks source link

Integration with ERB Lint / StandardRB / Rubocop? #47

Open thewatts opened 9 months ago

thewatts commented 9 months ago

Hi there! :)

I love that you're working on this - it's something my team has needed for a long long time.

Reaching out because we use erb-lint, as well as standard throughout our project.

The fact that this formatted will autoformat is amazing - but I'm finding that it's clashing with our linting rules.

Is it possible for the formatting, for blocks of Ruby content, to possibly leverage another tool's rules? (ex: for erb-lint, we've configured it to use standardrb).

I suppose practically - is it possible to format based on erb-lint's ruleset?

Thanks for your help!

elia commented 9 months ago

Hey @thewatts, thanks!

Before settling on SyntaxTree I tried a few different formatters, including Rufo and Rubocop (Rubocop was especially slow for this use case hard to use for small snippets).

But if you're ok with slower times I'm happy to accept a PR extracting the ruby formatting code

https://github.com/nebulab/erb-formatter/blob/a0955e056da4dff00ed46afd2584087687e103b0/lib/erb/formatter.rb#L266C5-L273

with a pluggable thing and allow for alternative formatters, e.g.:

SYNTAX_TREE_FORMATTER = ->(code) {

    SyntaxTree::Command.prepend SyntaxTreeCommandPatch

    code = begin
      SyntaxTree.format(code, @line_width)
    rescue SyntaxTree::Parser::ParseError => error
      p RUBY_PARSE_ERROR: error if @debug
      code
    end
}

def initialize(...)
  @ruby_formatter = ruby_formatter || SYNTAX_TREE_FORMATTER

# …

def format_ruby(code, autoclose: false)
  # …
  code = @ruby_formatter.call(code)
  # …
end

I just jotted down this code, no need to take names and organization too seriously, I just wanted to convey the idea 😊

nickpoorman commented 8 months ago

Having the ability to autocrrect the ruby code with something like rubocop would be super helpful.