Repository: prettier/plugin-ruby

Issue #50: Incorrect argument formatting for HEREDOC
Created: 2019-02-08T04:37:17Z
I have an easily reproducible error case for you, hope you dont mind 😄
With this input:
require 'set'

module Delegation
  class DelegationError < NoMethodError; end

  RUBY_RESERVED_KEYWORDS = %w[alias and BEGIN begin break case class def defined? do
                              else elsif END end ensure false for if in module next nil not or redo rescue retry
                              return self super then true undef unless until when while yield].freeze
  DELEGATION_RESERVED_KEYWORDS = %w[_ arg args block].freeze
  DELEGATION_RESERVED_METHOD_NAMES = Set.new(
    RUBY_RESERVED_KEYWORDS + DELEGATION_RESERVED_KEYWORDS
  ).freeze

  def delegate_missing_to(target)
    target = target.to_s
    target = "self.#{target}" if DELEGATION_RESERVED_METHOD_NAMES.include?(target)

    module_eval <<-RUBY, __FILE__, __LINE__ + 1
      def respond_to_missing?(name, include_private = false)
        # It may look like an oversight, but we deliberately do not pass
        # +include_private+, because they do not get delegated.

        #{target}.respond_to?(name) || super
      end

      def method_missing(method, *args, &block)
        if #{target}.respond_to?(method)
          #{target}.public_send(method, *args, &block)
        else
          begin
            super
          rescue NoMethodError
            if #{target}.nil?
              raise DelegationError, "\#{method} delegated to #{target}, but #{target} is nil"
            else
              raise
            end
          end
        end
      end
    RUBY
  end
end
prettier-ruby creates syntactically incorrect output:
# frozen_string_literal: true

require 'set'

module Delegation
  class DelegationError < NoMethodError; end

  RUBY_RESERVED_KEYWORDS = %w[
    alias
    and
    BEGIN
    begin
    break
    case
    class
    def
    defined?
    do
    else
    elsif
    END
    end
    ensure
    false
    for
    if
    in
    module
    next
    nil
    not
    or
    redo
    rescue
    retry
    return
    self
    super
    then
    true
    undef
    unless
    until
    when
    while
    yield
  ]
    .freeze
  DELEGATION_RESERVED_KEYWORDS = %w[_ arg args block].freeze
  DELEGATION_RESERVED_METHOD_NAMES =
    Set.new(RUBY_RESERVED_KEYWORDS + DELEGATION_RESERVED_KEYWORDS).freeze

  def delegate_missing_to(target)
    target = target.to_s
    if DELEGATION_RESERVED_METHOD_NAMES.include?(target)
      target = "self.#{target}"
    end

    module_eval <<-RUBY
                      def respond_to_missing?(name, include_private = false)
        # It may look like an oversight, but we deliberately do not pass
        # +include_private+, because they do not get delegated.

        #{target}.respond_to?(name) || super
      end

      def method_missing(method, *args, &block)
        if #{target}.respond_to?(method)
          #{target}.public_send(method, *args, &block)
        else
          begin
            super
          rescue NoMethodError
            if #{target}.nil?
              raise DelegationError, "\#{method} delegated to #{target}, but #{target} is nil"
            else
              raise
            end
          end
        end
      end
    RUBY,
                __FILE__,
                __LINE__ + 1
  end
end

Duplicate issue #141: heredoc when used as an argument gets formatted incorrectly
Created: 2019-02-19T20:11:47Z
Metadata

Ruby version: 2.6.1
@prettier/plugin-ruby version: 0.6.3

Input
Person.where(<<~WHERE, arg1)
  some sql
WHERE
Current output
Person.where(
  <<~WHERE
  some sql
WHERE,
  arg1
)

It's putting the comma in the wrong place.
Expected output
I think the original way looks the cleanest, but it doesn't support multiple heredocs, so I guess something like this would work:
Person.where(
  <<~WHERE,
  some sql
  WHERE
  arg1
)

Closing comment (2019-02-19T20:17:00Z): Closing this as it's a duplicate of #50