Rubocop in VSCode

在 vscode 中自動套用 ruby linter - rubocop

首先在 Gemfile 安裝 rubocop

gem 'rubocop', '~> 1.18', require: false

bundle install 安裝

在專案根目錄建立 .rubocop.yml

內容可以參考這篇

vscode 可能會出現這樣的 warning:

The following cops were added to RuboCop, 
but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file. 
Please also note that can also opt-in to new cops by default by 
adding this to your config: AllCops: NewCops: enable
Gemspec/DateAssignment: # (new in 1.10) Enabled: true 
Layout/LineEndStringConcatenationIndentation: # (new in 1.18) Enabled: true 
....

官方有一篇文章說明這個原因

意思是指在新版的 rubocop 有新增加的規則,但在 rubocop.yml 中沒有明確指示是否 enable

解法是可以在 rubocop.yml 中加上這行

AllCops: NewCops: enable

代表說之後的新規則都預設打開

或者也可以根據 warning 中的每一條規則都加進 rubocop.yml 中去做設定

最終我的 rubocop.yml 是長這樣

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
AllCops:
  NewCops: enable
# Commonly used screens these days easily fit more than 80 characters.
Layout/LineLength:
  Max: 120

# Too short methods lead to extraction of single-use methods, which can make
# the code easier to read (by naming things), but can also clutter the class
Metrics/MethodLength:
  Max: 20

# The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
Metrics/ClassLength:
  Max: 1500

# No space makes the method definition shorter and differentiates
# from a regular assignment.
Layout/SpaceAroundEqualsInParameterDefault:
  EnforcedStyle: no_space

# Single quotes being faster is hardly measurable and only affects parse time.
# Enforcing double quotes reduces the times where you need to change them
# when introducing an interpolation. Use single quotes only if their semantics
# are needed.
Style/StringLiterals:
  EnforcedStyle: double_quotes

# We do not need to support Ruby 1.9, so this is good to use.
Style/SymbolArray:
  Enabled: true

# Most readable form.
Layout/HashAlignment:
  EnforcedHashRocketStyle: table
  EnforcedColonStyle: table

# Mixing the styles looks just silly.
Style/HashSyntax:
  EnforcedStyle: ruby19_no_mixed_keys

# has_key? and has_value? are far more readable than key? and value?
Style/PreferredHashMethods:
  Enabled: false

# String#% is by far the least verbose and only object oriented variant.
Style/FormatString:
  EnforcedStyle: percent

Style/CollectionMethods:
  Enabled: true
  PreferredMethods:
    # inject seems more common in the community.
    reduce: "inject"

# Either allow this style or don't. Marking it as safe with parenthesis
# is silly. Let's try to live without them for now.
Style/ParenthesesAroundCondition:
  AllowSafeAssignment: false
Lint/AssignmentInCondition:
  AllowSafeAssignment: false

# A specialized exception class will take one or more arguments and construct the message from it.
# So both variants make sense.
Style/RaiseArgs:
  Enabled: false

# Indenting the chained dots beneath each other is not supported by this cop,
# see https://github.com/bbatsov/rubocop/issues/1633
Layout/MultilineOperationIndentation:
  Enabled: false

# Fail is an alias of raise. Avoid aliases, it's more cognitive load for no gain.
# The argument that fail should be used to abort the program is wrong too,
# there's Kernel#abort for that.
Style/SignalException:
  EnforcedStyle: only_raise

# Suppressing exceptions can be perfectly fine, and be it to avoid to
# explicitly type nil into the rescue since that's what you want to return,
# or suppressing LoadError for optional dependencies
Lint/SuppressedException:
  Enabled: false

Layout/SpaceInsideBlockBraces:
  # The space here provides no real gain in readability while consuming
  # horizontal space that could be used for a better parameter name.
  # Also {| differentiates better from a hash than { | does.
  SpaceBeforeBlockParameters: false

# No trailing space differentiates better from the block:
# foo} means hash, foo } means block.
Layout/SpaceInsideHashLiteralBraces:
  EnforcedStyle: no_space

# { ... } for multi-line blocks is okay, follow Weirichs rule instead:
# https://web.archive.org/web/20140221124509/http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
Style/BlockDelimiters:
  Enabled: false

# do / end blocks should be used for side effects,
# methods that run a block for side effects and have
# a useful return value are rare, assign the return
# value to a local variable for those cases.
Style/MethodCalledOnDoEndBlock:
  Enabled: true

# Enforcing the names of variables? To single letter ones? Just no.
Style/SingleLineBlockParams:
  Enabled: false

# Shadowing outer local variables with block parameters is often useful
# to not reinvent a new name for the same thing, it highlights the relation
# between the outer variable and the parameter. The cases where it's actually
# confusing are rare, and usually bad for other reasons already, for example
# because the method is too long.
Lint/ShadowingOuterLocalVariable:
  Enabled: false

# Check with yard instead.
Style/Documentation:
  Enabled: false

# This is just silly. Calling the argument `other` in all cases makes no sense.
Naming/BinaryOperatorParameterName:
  Enabled: false

# There are valid cases, for example debugging Cucumber steps,
# also they'll fail CI anyway
Lint/Debugger:
  Enabled: false

# Style preference
Style/MethodDefParentheses:
  Enabled: false

最後在設定檔 settings.json 中加入

1
2
3
4
"editor.formatOnSave": true,
"ruby.format": "rubocop",
"ruby.rubocop.executePath": "",
"ruby.rubocop.onSave": true

就可以完成在每次存檔時自動修正

updatedupdated2023-06-122023-06-12