【问题标题】:How to set rubocop case indentation correctly如何正确设置 rubocop 大小写缩进
【发布时间】:2023-03-25 21:14:01
【问题描述】:

我如何让 rubo-cop 接受以下正确的案例选择:

variable = case some_other_variable
  when 'some value' then 'some result'
  when 'some other value' then 'some other result'
  when 'one more value'  then 'one more result'
end

我目前在我的.rubocop.yml

CaseIndentation:
  EnforcedStyle: end
  IndentOneStep: true

但它总是像这样出错:

C: Layout/CaseIndentation: Indent when one step more than end.
    when 'some value' then 'some result'

我做错了什么,我该如何解决?

【问题讨论】:

    标签: ruby rubocop


    【解决方案1】:

    它说

    • 缩进与大小写一样深
    • 将条件表达式的结果分配给变量时, 保持其分支的通常对齐方式

    所以它可能以这种方式对 Rubocop 有效:

    variable = case some_other_variable
               when 'some value' then 'some result'
               when 'some other value' then 'some other result'
               when 'one more value' then 'one more result'
               end
    

    或者这样

    variable =
      case some_other_variable
      when 'some value' then 'some result'
      when 'some other value' then 'some other result'
      when 'one more value' then 'one more result'
      end
    

    【讨论】:

    • 也许是这样,但我们希望它就像它已显示在 OP 中一样,我知道应该可以按照我想要的方式设置它:github.com/bbatsov/rubocop/issues/653#issuecomment-29840347,我只是想知道如何使用当前版本的 rubocop 来做到这一点。
    • 那你为什么不在问题中问这个? :'-)
    • 因为该问题是从 2013 年开始的,并且 IndentWhenRelativeTo 已重命名为 EnforcedStyle 很可能该选项不再有效。正如@jonas054 指出的那样,我无法让它发挥作用。
    猜你喜欢
    • 1970-01-01
    • 2018-12-28
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多