Sha256: c26ec3738e589e878ac91c50149ae0465559d8193da88e8f8d050ba7400b050d

Contents?: true

Size: 1.37 KB

Versions: 151

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # Checks for indentation of the first non-blank non-comment
      # line in a file.
      #
      # @example
      #   # bad
      #      class A
      #        def foo; end
      #      end
      #
      #   # good
      #   class A
      #     def foo; end
      #   end
      #
      class InitialIndentation < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Indentation of first line in file detected.'

        def on_new_investigation
          space_before(first_token) do |space|
            add_offense(first_token.pos) do |corrector|
              corrector.remove(space)
            end
          end
        end

        private

        def first_token
          processed_source.tokens.find { |t| !t.text.start_with?('#') }
        end

        def space_before(token)
          return unless token
          return if token.column.zero?

          space_range = range_with_surrounding_space(token.pos, side: :left, newlines: false)
          # If the file starts with a byte order mark (BOM), the column can be
          # non-zero, but then we find out here if there's no space to the left
          # of the first token.
          return if space_range == token.pos

          yield range_between(space_range.begin_pos, token.begin_pos)
        end
      end
    end
  end
end

Version data entries

151 entries across 150 versions & 15 rubygems

Version Path
rubocop-1.57.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.56.4 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.56.3 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.56.2 lib/rubocop/cop/layout/initial_indentation.rb
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/layout/initial_indentation.rb
synctera_ruby_sdk-1.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/layout/initial_indentation.rb
synctera_ruby_sdk-1.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/layout/initial_indentation.rb
sampero-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.1/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.56.1 lib/rubocop/cop/layout/initial_indentation.rb
tursodb-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/layout/initial_indentation.rb
synctera_ruby_sdk-1.0.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.56.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.55.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.55.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.54.2 lib/rubocop/cop/layout/initial_indentation.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/layout/initial_indentation.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/layout/initial_indentation.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.54.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.54.0 lib/rubocop/cop/layout/initial_indentation.rb