prettier-ruby icon indicating copy to clipboard operation
prettier-ruby copied to clipboard

Improve class method printing

Open AlanFoster opened this issue 7 years ago • 3 comments

Rails in particular has a lot of class methods within its API. I'm not massively opinionated here, but perhaps the convention should be to drop brackets on method calls within class bodies

Input

class MyClass
  helper :all
  before_action :hello_world
  include FooBarBaz

  def self.foo
    puts "foo"
  end
  private_class_method :foo

  private_class_method def self.bar
     puts "bar"
  end
end

Current output

class MyClass
  helper(:all)
  before_action(:hello_world)
  include(FooBarBaz)
  def self.foo
    puts 'foo'
  end
  private_class_method(:foo)
  private_class_method(def self.bar
    puts 'bar'
  end)
end

Expected Output

Brackets are dropped from method/command calls within the class body

class MyClass
  helper :all
  before_action :hello_world
  include FooBarBaz

  def self.foo
    puts 'foo'
  end
  private_class_method :foo

  private_class_method def self.bar
    puts 'bar'
  end
end

AlanFoster avatar Jun 05 '18 22:06 AlanFoster

Hey, we do drop parens when we should like for puts etc. we can add more method names where parens are not required, or we can default to no paren and add it as an option.

iamsolankiamit avatar Jun 06 '18 08:06 iamsolankiamit

Yip, all sound good - I'm sure we'll get the right balance here 🤞

There's also a bunch of nice examples within Rubocop too - https://github.com/rubocop-hq/ruby-style-guide#classes--modules

AlanFoster avatar Jun 06 '18 19:06 AlanFoster

How about printing parens if the call breaks, if there is a nested call, or if there are 3 or more parameters?

j-f1 avatar Jun 06 '18 19:06 j-f1