耐心,我亲爱的同事们,希望能很快合并到 Rubocop 核心中以启用制表符缩进的 PR 。现在,您可以在项目的某处创建以下 rb 文件,并在运行 rubocop 时需要它。
rubocop [...] --require rubocop_tabs.rb
rubocop_tabs.rb:
# frozen_string_literal: true
require 'rubocop'
require 'set'
RuboCop::ConfigLoader.default_configuration['Layout/Tab']['SupportedStyles'] = %w[tab space]
RuboCop::ConfigLoader.default_configuration['Layout/Tab']['EnforcedStyle'] = :space
module RuboCop
module Cop
module Layout
class Tab < Cop
include Alignment
include RangeHelp
include ConfigurableEnforcedStyle
MSG = '%<type>s detected.'
def investigate(processed_source)
str_ranges = string_literal_ranges(processed_source.ast)
processed_source.lines.each.with_index(1) do |line, lineno|
match = style == :tab ? line.match(/\A\s* +/) : line.match(/\A\t+/)
next unless match
range = source_range(processed_source.buffer, lineno, match.begin(0)...match.end(0))
next if in_string_literal?(str_ranges, range)
add_offense(range, location: range)
end
end
def autocorrect(range)
if style == :tab
lambda do |corrector|
corrector.replace(range, range.source.gsub(/\A\s+/) do |i|
"\t" * i.size
end)
end
else
lambda do |corrector|
spaces = ' ' * configured_indentation_width
corrector.replace(range, range.source.gsub(/\t/, spaces))
end
end
end
private
def message(_node)
format(MSG, type: style == :tab ? 'Space' : 'Tab')
end
end
end
end
end
还将以下内容添加到您的.rubocop.yml
Layout/Tab:
EnforcedStyle: tab