Sha256: b41d07315e0ebbab7584f51619448b0ae8be79512d74a7a744125924a6a32975

Contents?: true

Size: 1.48 KB

Versions: 35

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop 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.find_token { |t| !t.text.start_with?('#') }
        end

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

          space_range =
            range_with_surrounding_space(range: 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

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/initial_indentation.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/initial_indentation.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/initial_indentation.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/initial_indentation.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/initial_indentation.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.12.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.12.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.11.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.10.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.9.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.9.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.8.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.8.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.7.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.6.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.6.0 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.5.2 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.5.1 lib/rubocop/cop/layout/initial_indentation.rb
rubocop-1.5.0 lib/rubocop/cop/layout/initial_indentation.rb