RuboCop:行太长 ← 如何忽略?

93

我刚把RuboCop添加到一个rails项目中,并安装了Sublime插件,以便在编辑器中查看RuboCop的建议。我正在尝试找出如何将最大行长度从80个字符更改为其他值,或者完全忽略此规则。

当前使用的工具:

4个回答

139

在你的代码中,你可以这样禁用一堆行:

# rubocop:disable Layout/LineLength
puts "This line is lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng"
# rubocop:enable Layout/LineLength
将以下内容添加到您的 .rubocop.yml 文件中,以增加最大长度:
Layout/LineLength:
  Max: 100

1
我应该把这个放在哪里? - Abram
1
所以我复制了这个文件 https://github.com/bbatsov/rubocop/blob/master/config/default.yml ,做出了更改并重新启动sublime,但仍然看到了问题。 - Abram
3
啊,我知道我错在哪里了。我忘记在.rubocop.yml中加上 . 了。现在它可以工作了,谢谢! - Abram
如果您喜欢在 .yml 文件中进行更改而不是本地更改,我更喜欢使用“排除:”选项而不是“最大值:”选项。因为“最大值”会全局更改规则,而“排除”允许您管理少量的特殊情况。当这些情况超过几个时,我认为需要进行重构。如果重构无法解决问题,那么我会考虑编辑“最大值:”选项。 - SMAG

75

创建一个.rubocop.yml文件(注意文件名中的初始.)并将其放置在项目根目录下,您将有一堆选项(检查注释以查看您使用的Rubocop版本作为处理LineLength的方法已更改):

Metrics/LineLength: # for Rubocop < 0.78.0
Layout/LineLength: # for Rubocop >= 0.78.0
  # This will disable the rule completely, regardless what other options you put
  Enabled: false
  # Change the default 80 chars limit value
  Max: 120
  # If you want the rule only apply to a specific folder/file
  Include:
    - 'app/**/*'
  # If you want the rule not to apply to a specific folder/file
  Exclude:
    - 'db/schema.rb'

7

随着 rubocop gem 0.78.0 版本于 2019 年 12 月 18 日的最新更改,现在 LineLength cop 从 Metrics 部门移动到 Layout 部门。因此,如果有人需要禁用长行并使用高于 0.78.0 版本号,则应像这样执行。

# rubocop:disable Layout/LineLength
  "I'm a really long line"
# rubocop:enable Layout/LineLength

此外,.rubocop.yml配置已更改为以下内容。

Layout/LineLength:
  Max: 100

要查看rubocop的更改日志,请点击此处


1

您可以查看此链接以禁用单行的复制。https://dev59.com/MVEG5IYBdhLWcg3wWbL5#67450682

示例:

puts "This line is lonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnng" # rubocop:disable Layout/LineLength

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接