Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Recommendations for making miscellaneous Custom Cops - 雑Custom Cop作りのススメ

ydah
October 09, 2022

Recommendations for making miscellaneous Custom Cops - 雑Custom Cop作りのススメ

Kaigi on Rails _2022_ new 「雑Custom Cop作りのススメ」の発表スライド
https://kaigionrails.org/2022/ #kaigionrails

ydah

October 09, 2022
Tweet

More Decks by ydah

Other Decks in Programming

Transcript

  1. • Name: Yudai T akada • GitHub: @ydah • T

    witter: @ydah_ • An Open Source Software programmer. • Software Engineer at ANDPAD, Inc. • RuboCop Contributor. (Create and send patches) • Born and raised in Osaka. • Loves beer and coffee and metal bands 🍻 ☕ 🤘 self.inspect
  2. • Style/ObjectThen • Lint/ConstantOverwrittenInRescue • Lint/NonAtomicFileOperation • Rails/WhereMissing • Rails/FreezeTime

    • RSpec/ChangeByZero • RSpec/Capybara/SpecificFinders • RSpec/Capybara/SpecificMatcher Add new cops
  3. • A Ruby static code analyzer (a.k.a. linter) and code

    formatter. • Rubyの静的解析ツール What is RuboCop?
  4. Creating gem 'rubocop-foobar'... create rubocop-foobar/Gemfile create rubocop-foobar/lib/rubocop/foobar.rb create rubocop-foobar/lib/rubocop/foobar/version.rb create

    rubocop-foobar/rubocop-foobar.gemspec create rubocop-foobar/Rakefile create rubocop-foobar/README.md create rubocop-foobar/bin/console create rubocop-foobar/bin/setup create rubocop-foobar/.gitignore Initializing git repo in /tmp/tmp.Gu7G94wX00/rubocop-foobar Gem 'rubocop-foobar' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html create rubocop-foobar/lib/rubocop-foobar.rb create rubocop-foobar/lib/rubocop/foobar/inject.rb create rubocop-foobar/lib/rubocop/cop/foobar_cops.rb create rubocop-foobar/config/default.yml create rubocop-foobar/spec/spec_helper.rb create rubocop-foobar/.rspec update lib/rubocop/foobar.rb update lib/rubocop/foobar.rb update lib/rubocop/foobar/version.rb update rubocop-foobar.gemspec update rubocop-foobar.gemspec update Rakefile update Gemfile It's done! You can start developing a new extension of RuboCop in rubocop-foobar. For the next step, you can use the cop generator. $ bundle exec rake 'new_cop[Foobar/SuperCoolCopName]'
  5. Creating gem 'rubocop-foobar'... create rubocop-foobar/Gemfile create rubocop-foobar/lib/rubocop/foobar.rb create rubocop-foobar/lib/rubocop/foobar/version.rb create

    rubocop-foobar/rubocop-foobar.gemspec create rubocop-foobar/Rakefile create rubocop-foobar/README.md create rubocop-foobar/bin/console create rubocop-foobar/bin/setup create rubocop-foobar/.gitignore Initializing git repo in /tmp/tmp.Gu7G94wX00/rubocop-foobar Gem 'rubocop-foobar' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html create rubocop-foobar/lib/rubocop-foobar.rb create rubocop-foobar/lib/rubocop/foobar/inject.rb create rubocop-foobar/lib/rubocop/cop/foobar_cops.rb create rubocop-foobar/config/default.yml create rubocop-foobar/spec/spec_helper.rb create rubocop-foobar/.rspec update lib/rubocop/foobar.rb update lib/rubocop/foobar.rb update lib/rubocop/foobar/version.rb update rubocop-foobar.gemspec update rubocop-foobar.gemspec update Rakefile update Gemfile It's done! You can start developing a new extension of RuboCop in rubocop-foobar. For the next step, you can use the cop generator. $ bundle exec rake 'new_cop[Foobar/SuperCoolCopName]'
  6. [create] lib/rubocop/cop/foo_bar/cop_name.rb ★copの実装 [create] spec/rubocop/cop/foo_bar/cop_name_spec.rb ★テストコード [modify] lib/rubocop/cop/foobar_cops.rb - `require_relative 'foo_bar/cop_name'` was

    injected. [modify] A configuration for the cop is added into config/default.yml. Do 4 steps: 1. Modify the description of FooBar/CopName in config/default.yml 2. Implement your new cop in the generated file! 3. Commit your new cop with a message such as e.g. "Add new `FooBar/CopName` cop" 4. Run `bundle exec rake changelog:new` to generate a changelog entry for your new cop.
  7. end