Rubocop for tabs
Asked Answered
G

6

7

We would like to use Rubocop to validate our ruby is syntaxicaly correct and follow basic code guidelines.

Appart from that we have this rule : we indent using tabs to allow anybody to decided how they want them to be rendered (display them like 2 or 4 spaces)

The problem is that rubocop seems designed to refuse tabs for indentation AT ALL.

How can we override all theses rules to be space complients ?

EDIT: I'm thinking of overriding this module https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/source_parser.rb to replace all tabs from my files by 2 spaces to create the illusion for the gem...

Gilbertogilbertson answered 4/6, 2014 at 19:21 Comment(0)
G
0

I overrid this module https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/source_parser.rb to replace all tabs from my files by 2 spaces to create the illusion for the gem...

Gilbertogilbertson answered 6/6, 2014 at 22:11 Comment(0)
C
11

Add to your .rubocop.yml:

Style/Tab:
  Enabled: false

to disable the tabs rule.

You can write your own custom cop to check proper indentation.

Chloro answered 4/6, 2014 at 19:39 Comment(3)
But for instance if I use a tab in front of "private" I will get an error that there is no indentation in front of it !Gilbertogilbertson
does it mean that I have to rewrite ALL the cop that use indentation ? if yes it's a LOTGilbertogilbertson
Yeah... you are right. Ruby's indentation rules are actually quite accepted - two space indentation - no tabs. Perhaps rubocop is not the tool for you after all...Chloro
T
6

As an evil unrepentant tab user, I ended up hacking it like so. This diff for 0.32.1:

--- processed_source.rb.orig    2015-07-28 05:53:52.199418557 +0100
+++ processed_source.rb 2015-07-28 05:54:04.750420458 +0100
@@ -13,7 +13,7 @@
                 :parser_error, :raw_source

     def self.from_file(path)
-      file = File.read(path)
+      file = File.read(path).gsub(/^(\t+)/) {|m| '  ' * m.size }
       new(file, path)
     rescue
       abort("#{Rainbow('rubocop: No such file or directory').red} -- #{path}")

You should now blend in perfectly. Bwahahaha.

Telangiectasis answered 28/7, 2015 at 5:1 Comment(0)
P
1

For the latest version, add this to your .rubocop.yml file:

Layout/Tab:
  Enabled: false

You can find all default options listed here:

https://github.com/rubocop-hq/rubocop/blob/master/config/default.yml

And, the rule is defined here:

https://github.com/rubocop-hq/rubocop/blob/master/lib/rubocop/cop/layout/tab.rb

Phillada answered 26/11, 2018 at 10:53 Comment(0)
A
1

Patience, my dear colleagues, hopefully the PR into the Rubocop core to enable tabs indentation will be merged soon. For now you can create the following rb file somewhere in your project and require it when running rubocop.

rubocop [...] --require rubocop_tabs.rb

The 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

Also add the following to your .rubocop.yml

Layout/Tab:
  EnforcedStyle: tab
Admiralty answered 11/4, 2020 at 12:10 Comment(0)
G
0

I overrid this module https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/source_parser.rb to replace all tabs from my files by 2 spaces to create the illusion for the gem...

Gilbertogilbertson answered 6/6, 2014 at 22:11 Comment(0)
C
0

Latest (1.50.2) version answer

Use this in your .rubocop.yml:

Layout/IndentationStyle:
    Enabled: false
Cosy answered 26/4, 2023 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.