Sha256: 1b384e92d2bd5cb2ecd379b01336e718b49f16cb0ff69360add4b7c5eff639bc

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'set'

module RuboCop
  module Cop
    module Layout
      # 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

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/layout/tab.rb
rubocop-0.49.0 lib/rubocop/cop/layout/tab.rb