如何告诉鲁伯警察忽略一个特定的目录或文件

我的项目是从第三方 gem 扩展开放源码类,我们不希望使用与我们自己的代码相同的编码标准。重构 gem 代码不是一个可行的选择。我们只是想让鲁伯警察忽略复制的代码。

我怎样才能指示鲁伯警察完全忽略一个文件或目录?

61744 次浏览

As per orde's comment with the link to the manual I found .rubocop.yml and added the following:

AllCops:
Exclude:
- 'path/to/excluded/file.rb'

where the path is relative to .rubocop.yml

For your convenience, here is the .rubocop.yml I frequently used.

See formal explanation of .rubocop.yml here.

AllCops:
Exclude:
- Berksfile
- recipes/basic.rb
- attributes/*.rb


# Customize rules
Metrics/LineLength:
Max: 95


MethodLength:
Max: 35


Metrics/AbcSize:
Enabled: false


BlockLength:
Max: 70

I constantly bump by rubocop errors and warning. Thus I've published this post.

Common Rubocop Errors: Improve Your Ruby Code Quality

From rubocop/default.yml:

AllCops:
Exclude:
- 'node_modules/**/*'
- 'vendor/**/*'

Can also consider comments for a single file. Great for ignoring the linter for a quick and dirty temporary task.

# rubocop:disable all


module TempTask
...
end


# rubocop:enable all

When using Rubocop through Visual Studio I could not get the former answers to work.

I eventually noticed that Rubocop was not in fact executing from the root of my project file but from my own root directory and so using:

# Doesn't work :(
AllCops:
Exclude:
- db/schema.rb

was failing because db was not in the same directory that Rubocop was looking for it.

I then changed it to

# Works :)
AllCops:
Exclude:
- */schema.rb

and it works fine. Note there are no quotation marks around the file name.

I'm using a newer version and every time I add Exclude rubocop will hang. Turned out I need to tell how to merge Exclude. This might work for you

require:
- rubocop-rails
inherit_mode:
merge:
- Exclude
AllCops:
Exclude:
- 'node_modules/**/*'
- 'vendor/**/*'
NewCops: disable