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