Sha256: 2b03ac1f690e160e1f1d8b6dc3653dfb66dfc0e7c5b9df6434257405c05a50cc

Contents?: true

Size: 1.58 KB

Versions: 14

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

require 'set'

module RuboCop
  module Cop
    module Style
      # This cop checks for tabs inside the source code.
      class Tab < Cop
        MSG = 'Tab detected.'.freeze

        def investigate(processed_source)
          str_lines = string_literal_lines(processed_source.ast)

          processed_source.lines.each_with_index do |line, index|
            match = line.match(/^( *)[\t ]*\t/)
            next unless match
            next if str_lines.include?(index + 1)

            spaces = match.captures[0]
            range = source_range(processed_source.buffer,
                                 index + 1,
                                 (spaces.length)...(match.end(0)))

            add_offense(range, range, MSG)
          end
        end

        private

        def autocorrect(range)
          lambda do |corrector|
            corrector.replace(range, range.source.gsub(/\t/, '  '))
          end
        end

        def string_literal_lines(ast)
          # which lines start inside a string literal?
          return [] if ast.nil?

          ast.each_node(:str, :dstr).each_with_object(Set.new) do |str, lines|
            loc = str.location

            str_lines = if loc.is_a?(Parser::Source::Map::Heredoc)
                          body = loc.heredoc_body
                          (body.first_line)..(body.last_line)
                        else
                          (loc.first_line + 1)..(loc.last_line)
                        end

            lines.merge(str_lines.to_a)
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/tab.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/tab.rb
rubocop-0.43.0 lib/rubocop/cop/style/tab.rb
rubocop-0.42.0 lib/rubocop/cop/style/tab.rb
rubocop-0.41.2 lib/rubocop/cop/style/tab.rb
rubocop-0.41.1 lib/rubocop/cop/style/tab.rb
rubocop-0.41.0 lib/rubocop/cop/style/tab.rb
rubocop-0.40.0 lib/rubocop/cop/style/tab.rb
rubocop-0.39.0 lib/rubocop/cop/style/tab.rb
rubocop-0.38.0 lib/rubocop/cop/style/tab.rb
rubocop-0.37.2 lib/rubocop/cop/style/tab.rb
rubocop-0.37.1 lib/rubocop/cop/style/tab.rb
rubocop-0.37.0 lib/rubocop/cop/style/tab.rb
rubocop-0.36.0 lib/rubocop/cop/style/tab.rb