【问题标题】:RuboCop: Line is too long ← How to Ignore?RuboCop:行太长←如何忽略?
【发布时间】:2016-09-10 17:59:50
【问题描述】:

我刚刚将 RuboCop 添加到 Rails 项目并安装了 Sublime 包以在编辑器中查看 RuboCop 建议。我试图弄清楚如何从 80 个字符更改最大行长度,或者完全忽略该规则。

目前正在使用:

【问题讨论】:

    标签: ruby-on-rails ruby sublimetext3 rubocop


    【解决方案1】:

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

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

    或将其添加到您的 .rubocop.yml 文件以增加最大长度:

    Layout/LineLength:
      Max: 100
    

    【讨论】:

    • 我把这个放在哪里?
    • 所以我复制了这个文件github.com/bbatsov/rubocop/blob/master/config/default.yml 并进行了更改并重新启动了 sublime,但仍然看到问题..
    • 啊,我知道我哪里出错了。我忘记了.rubocop.yml 中的. 现在可以使用了,谢谢!
    • 如果您希望在 .yml 而不是本地进行更改,我更喜欢 Exclude: 选项而不是 Max: 选项。随着 Max 全局更改规则,Exclude 允许您管理少数雪花异常。当它变得更多时,那就是我觉得需要进行重构的时候。如果重构无济于事,那么我会考虑编辑 Max: 选项。
    【解决方案2】:

    在项目的根目录中创建一个.rubocop.yml 文件(注意文件名中的初始.),您将有很多选项(检查 cmets 以了解用作 @987654321 的 Rubocop 版本是什么@):

    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'
    

    【讨论】:

      【解决方案3】:

      随着 rubocop gem 版本 0.78.0 于 2019 年 18 月 12 日的最新变化,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 更改日志,click here

      【讨论】:

        猜你喜欢
        • 2021-03-10
        • 1970-01-01
        • 2014-12-08
        • 1970-01-01
        • 2020-01-08
        • 2012-03-06
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        相关资源
        最近更新 更多