Sha256: 50b4617098332fee1accaa1dc6721dcb9d3d5e734a3c63b79996617db0133af2

Contents?: true

Size: 1.81 KB

Versions: 12

Compression:

Stored size: 1.81 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 IndentationWidth < RuboCop::RBS::CopBase
          extend AutoCorrector

          def on_rbs_new_investigation
            @first_char_columns = processed_source.raw_source.each_line.map do |line|
              line.index(/[^[:space:]]/) || 0
            end
            processed_rbs_source.decls.each do |decl|
              check_indentation(decl, expect: 0)
            end
          end

          def check_indentation(decl, expect:)
            if decl.respond_to?(:members)
              check(decl, expect: expect)
              decl.members.each do |member|
                check_indentation(member, expect: expect + 2)
              end
            else
              check(decl, expect: expect)
            end
          end

          def check(decl, expect:)
            line_start_pos = line_start_pos(decl)
            actual = @first_char_columns[decl.location.start_line - 1]
            if actual != expect
              range = range_between(line_start_pos, line_start_pos + actual)
              message = "Use #{expect} (not #{actual}) spaces for indentation."
              add_offense(range, message: message) do |corrector|
                corrector.replace(range, ' ' * expect)
              end
            end
          end

          def line_start_pos(decl)
            rindex = processed_source.raw_source.rindex(/\R/, decl.location.start_pos)
            if rindex
              rindex + 1
            else
              0
            end
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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