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.71.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.70.0 lib/rubocop/cop/layout/initial_indentation.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.69.2 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.69.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.69.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.68.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.67.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.66.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.66.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.65.1 lib/rubocop/cop/layout/initial_indentation.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.65.0 lib/rubocop/cop/layout/initial_indentation.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.64.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.63.4 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.63.3 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.63.2 lib/rubocop/cop/layout/initial_indentation.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.63.1 lib/rubocop/cop/layout/initial_indentation.rb