833 文字
4 分
Ruby on Railsプロジェクトのrubocop.ymlテンプレ
概要
- 最近、rubocop-rspecというrubocopのrspec用プラグインを見つけました。
- 数年前から存在しているようです。早速導入してみたところ、盛大に修正点を上げてくれて、ベストプラクティスを享受できたので感動しました。
- このプラグインはあまり知られていないのかもしれないので記事にしておきます。
設定
私が育てたプロジェクトの rubocop.yml を置いておきます。
最近の rubocop はデフォルトではルールが厳しすぎるので、コードのみやすさやパフォーマンスに影響が少ないところは無効化しています。
gem 'rubocop', require: false gem 'rubocop-performance', require: false gem 'rubocop-rails', require: false gem 'rubocop-rspec', require: falseinherit_from: .rubocop_todo.yml
## This configuration was generated by## `rubocop --auto-gen-config`## on 2017-11-25 11:44:56 +0000 using RuboCop version 0.51.0.## The point is for the user to remove these configuration records## one by one as the offenses are removed from the code base.## Note that changes in the inspected code, or installation of new## versions of RuboCop, may require this file to be generated again.
## Offense count: 15## Cop supports --auto-correct.## Configuration parameters: Include, TreatCommentsAsGroupSeparators.## Include: **/Gemfile, **/gems.rbrequire: - rubocop-rails - rubocop-performance - rubocop-rspec
AllCops: TargetRubyVersion: 3.1 Exclude: - 'vendor/**/*' - 'db/**/*' - 'lib/tasks/**/*' - "db/schema.rb" - 'bin/*' - 'test/*' - 'node_modules/**/*' - 'config/initializers/**/*' - 'public/**/*' - 'storage/**/*' - 'log/**/*' - 'tmp/**/*' - 'terraform/**/*' - 'openapi/**/*' NewCops: enable
## sider's rubocop stops during this fileRails/HttpStatus: Exclude: - 'app/controllers/application_controller.rb'
Rails/SkipsModelValidations: AllowedMethods: - 'touch' - 'upsert_all'
Layout/LineLength: Max: 200 Exclude: - 'spec/**/*' - 'config/**/*'
Layout/ParameterAlignment: Enabled: false
Layout/EmptyLineBetweenDefs: Enabled: false
Layout/EmptyLines: Enabled: false
Layout/EmptyLinesAroundAccessModifier: Enabled: false
Layout/EmptyLinesAroundBlockBody: Enabled: false
Layout/EmptyLinesAroundClassBody: Enabled: false
Layout/EmptyLinesAroundMethodBody: Enabled: false
Layout/ExtraSpacing: Enabled: false
Layout/IndentationConsistency: Enabled: false
Layout/SpaceBeforeBlockBraces: Enabled: false
Layout/SpaceInsideHashLiteralBraces: Enabled: false
Layout/EmptyLinesAroundAttributeAccessor: Enabled: true
Layout/SpaceAroundMethodCallOperator: Enabled: true
Lint/DeprecatedOpenSSLConstant: Enabled: true
Lint/DuplicateElsifCondition: Enabled: true
Lint/MixedRegexpCaptureTypes: Enabled: true
Lint/RaiseException: Enabled: true
Lint/StructNewOverride: Enabled: true
Style/AsciiComments: Enabled: false
Style/ClassAndModuleChildren: Enabled: false
Style/Documentation: Enabled: false
Style/EmptyMethod: Enabled: false
Style/FrozenStringLiteralComment: Enabled: false
Style/GuardClause: Enabled: false
## Configuration parameters: Whitelist.## Whitelist: be, be_a, be_an, be_between, be_falsey, be_kind_of, be_instance_of, be_truthy, be_within, eq, eql, end_with, include, match, raise_error, respond_to, start_withStyle/NumericPredicate: Enabled: false
## Readability is more importantStyle/RedundantReturn: Enabled: false
Style/NumericLiterals: Enabled: false
Style/AccessorGrouping: Enabled: false
Style/ArrayCoercion: Enabled: true
Style/BisectedAttrAccessor: Enabled: true
Style/CaseLikeIf: Enabled: true
Style/ExponentialNotation: Enabled: true
Style/HashAsLastArrayItem: Enabled: false
Style/HashEachMethods: Enabled: true
Style/HashLikeCase: Enabled: true
Style/HashTransformKeys: Enabled: true
Style/HashTransformValues: Enabled: true
Style/RedundantAssignment: Enabled: true
Style/RedundantFetchBlock: Enabled: true
Style/RedundantFileExtensionInRequire: Enabled: true
Style/RedundantRegexpCharacterClass: Enabled: true
Style/RedundantRegexpEscape: Enabled: true
Style/SlicingWithRange: Enabled: true
Style/TrailingCommaInHashLiteral: Enabled: false
## Configuration parameters: EnforcedStyle, SupportedStyles.## SupportedStyles: all_comparison_operators, equality_operators_onlyStyle/YodaCondition: # Readability is more important Enabled: false
Style/FormatStringToken: EnforcedStyle: template
Performance/TimesMap: Exclude: - 'spec/**/*'
Metrics/BlockLength: Exclude: - 'spec/**/*' - 'config/**/*' - 'app/views/api/v1/**/*' - 'app/models/order_product.rb' # aasm
Metrics/AbcSize: Enabled: false
Metrics/MethodLength: Enabled: false
Style/SymbolArray: Enabled: false
Style/RescueModifier: Enabled: false
Metrics/ClassLength: Enabled: false
Metrics/ParameterLists: Enabled: false
RSpec/NestedGroups: Enabled: false
Capybara/CurrentPathExpectation: Enabled: false
RSpec/MultipleMemoizedHelpers: Enabled: false
RSpec/ExampleLength: Enabled: false
RSpec/ContextWording: Enabled: false
RSpec/MultipleExpectations: Enabled: false
RSpec/LetSetup: Enabled: false
RSpec/Capybara/FeatureMethods: Enabled: false
RSpec/ChangeByZero: Enabled: false
Naming/VariableNumber: Enabled: false最新版はこちらに置いておきます。 https://gist.github.com/matsubo/ec5b6cbf0a7207075e2b081f394863e6
CIでの実行おススメ
もちろんCI上でもrubocopを実行しているのですが、rubocopをオプション無しで実行するとフォーマットの準拠を1つでもしていないとfailします。
せっかくCIを回したのに、テストがfailしてしまうのはあまりよろしくないです。開発効率を上げるためにCIを入れているのに逆に悪くなってしまいます。
なので、プログラムの動作に影響しないようなstylingの準拠は無視するようにしています。そのためのオプションがこちら。
% rubocop --fail-level WWARNIG 以上の問題だったら終了コードに 0 以外を返却して CI を fail させます。これによってわざわざコードのスタイルを準拠するために commit と push を行わなくて良くなるので気が楽になります。
Ruby on Railsプロジェクトのrubocop.ymlテンプレ
https://blog.teraren.com/posts/a88546e4efa1f3/ 関連記事
この記事が役に立ったら
GitHub Sponsorsで応援できます