ruby Rubocop Linelength:如何忽略带有注释的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26342570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rubocop Linelength: How to ignore lines with comments?
提问by Twiek
Using a Rails 4 app I would like Rubocop to ignore lines with comments (just a comment or some code with an end of line comment) when checking if a line is to long. Is there a way to do this?
使用 Rails 4 应用程序,我希望 Rubocop 在检查一行是否过长时忽略带有注释的行(只是一个注释或一些带有行尾注释的代码)。有没有办法做到这一点?
回答by vgoff
There is a way to ignore cops on a per line basis.
有一种方法可以在每行的基础上忽略警察。
There is also a way to do it via configuration file.
还有一种方法可以通过配置文件来完成。
Run rubocop --auto-gen-configand it will generate a file that you can use to disable the offenses.
运行rubocop --auto-gen-config,它将生成一个文件,您可以使用它来禁用攻击。
The command also gives a hint on what to do to load those options.
该命令还提示了如何加载这些选项。
On a line per line basis, you can enable and disable the cops as well.
在每条线路的基础上,您还可以启用和禁用警察。
# rubocop:disable RuleByName
This is a long line
# rubocop:enable RuleByName
You can also do more than one rule at a time in your code.
您还可以在代码中一次执行多个规则。
# rubocop:disable BlockComments, AsciiComments
By using an inline directive, the directive becomes valid only for that line, and it would look like this:
通过使用内联指令,该指令仅对该行有效,如下所示:
# Thanks to @jnt30 for the comment!
method(argument) # rubocop:disable SomeRule, SomeOtherRule
You can read a ton more about RuboCop in its official manual.
您可以在其官方手册中阅读更多关于 RuboCop 的内容。
To find all the rule names its worth looking in the rubocop config files
要找到所有值得在 rubocop 配置文件中查找的规则名称
cyberwizsays - "run rubocop -Dwhen I need the rule names rather than looking in the documentation." Update: This is now the default behavior without the flag.
Cyberwiz说 - “rubocop -D当我需要规则名称而不是查看文档时运行。” 更新:这是现在没有标志的默认行为。
回答by GoBusto
It's possible to define regex patterns to automatically ignore certain lines in rubocop.yml, so you could choose to ignore all lines starting with a #character:
可以定义正则表达式模式以自动忽略 中的某些行rubocop.yml,因此您可以选择忽略以#字符开头的所有行:
Metrics/LineLength:
Max: 80
IgnoredPatterns: ['\A#']
This could be improved so that "indented" comment lines (i.e. whitespace followed by a #character) are also ignored, if that's what you want.
这可以改进,以便“缩进”注释行(即空格后跟一个#字符)也被忽略,如果这是你想要的。
Note that this doesn't account for lines of code that endwith a comment, though:
请注意,这不考虑以注释结尾的代码行,但是:
some_code(that_does_something) # This line would NOT be ignored by Rubocop.
回答by GoBusto
You can use the following comment with rubocop to ignore a specific rule:
您可以在 rubocop 中使用以下注释来忽略特定规则:
# rubocop:disable Metrics/LineLength
def this_could_be_a_very_long_line_that_extends_forever_into_infinity
end
# rubocop:enable Metrics/LineLength
You can also ignore whole files by adding them to .rubocop.yml:
您还可以通过将它们添加到以下来忽略整个文件.rubocop.yml:
AllCops:
Exclude:
- path/to/file.rb
回答by phoet
i think the basic idea here is that you want to enforce line length, no matter what is after n characters. the default to 80 characters is some cargo cult for old terminal windows that could only hold that number of chars. the only option that i saw in the codeis an option to allow urls that might exceed the character limit.
我认为这里的基本思想是,无论 n 个字符之后是什么,您都想强制执行行长度。默认为 80 个字符是旧终端窗口的一些货物崇拜,只能容纳该数量的字符。我在代码中看到的唯一选项是允许可能超过字符限制的 url 的选项。
you can ignore whole files, i guess that's not what you are looking for.
你可以忽略整个文件,我想这不是你要找的。

