Sha256: 7ae127d9c0ca06298f377ddfda69a573de9b61bd04edc23606d97bdc49d85881

Contents?: true

Size: 1.74 KB

Versions: 9

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RBS
      module Layout
        # @example default
        #   # bad
        #   class Foo
        #     def foo: () -> void
        #     end
        #
        #   # good
        #   class Foo
        #     def foo: () -> void
        #   end
        class EndAlignment < RuboCop::RBS::CopBase
          extend AutoCorrector

          def on_rbs_new_investigation
            processed_rbs_source.decls.each do |decl|
              check_indentation(decl, expect: 0)
            end
          end

          def check_indentation(decl, expect:)
            if decl.location.key?(:end)
              # module, class and interface
              check(decl, expect: expect)
              decl.members.each do |member|
                check_indentation(member, expect: expect + 2)
              end
            end
          end

          def check(decl, expect:)
            end_loc = decl.location[:end]
            actual = end_loc.start_column
            if actual != expect
              line_start_pos = processed_source.raw_source.rindex(/\R/, end_loc.start_pos) + 1
              range = range_between(end_loc.start_pos, end_loc.end_pos)
              source = decl.location.source.each_line.first.strip
              message = "`end` at #{end_loc.start_line}, #{end_loc.start_column} is not aligned with " \
                        "`#{source}` at #{decl.location.start_line}, #{decl.location.start_column}."
              add_offense(range, message: message) do |corrector|
                whitespace = range_between(line_start_pos, end_loc.start_pos)
                corrector.replace(whitespace, ' ' * expect)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-on-rbs-1.0.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.9.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.8.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.7.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.6.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.5.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.4.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.3.0 lib/rubocop/cop/rbs/layout/end_alignment.rb
rubocop-on-rbs-0.2.0 lib/rubocop/cop/rbs/layout/end_alignment.rb