Mastering config/default.yml for RuboCop

Mastering config/default.yml for RuboCop

銀座Rails#9

023b04c98f39cc041293d780352432ff?s=128

Koichi ITO

May 30, 2019
Tweet

Transcript

  1.  5IV RuboCopೖ໳ ۜ࠲3BJMT ˏϦϯΫΞϯυϞνϕʔγϣϯ Mastering config/default.yml ,PJDIJ*50&4. *OD

  2. !LPJD w 3VCP$PQ$PNNJUUFS w "30SBDMFFOIBODFE BEBQUFS$PNNJUUFS w $PNNVOJUZ.BOBHFS BU&4. *OD

  3. &4. *OD

  4. ւ֎3VCZΧϯϑΝϨϯε΁ͷ౿Έग़͠ํ LPJD

  5. 3VCZॲཧܥʹΠγϡʔϨϙʔτͯ͠ΈΑ͏ LPJD

  6. 044ίϛϡχςΟͱͷڞੜΛࢧԉ Support coexistence with OSS community

  7. ୡਓϓϩάϥϚʔ΁ͷجૅٕज़ ςεςΟϯά %FWFMPQFS5FTUJOH ࣗಈԽ 1SPKFDU"VUPNBUJPO όʔδϣϯ؅ཧ 7FSTJPO$POUSPM

  8. ୡਓϓϩάϥϚʔ΁ͷجૅٕज़ ςεςΟϯά %FWFMPQFS5FTUJOH ࣗಈԽ 1SPKFDU"VUPNBUJPO όʔδϣϯ؅ཧ 7FSTJPO$POUSPM

  9. None
  10. #VOEMFS 1SZ 34QFD 3VCP$PQ CZFCVH 3VCZ,BJHJ %3&$0.͞Μϒʔεʹͯ

  11. None
  12. ͸͡Ίʹ w ൃද࣌఺ͷ3VCP$PQΛ ϕʔεʹ͍ͯ͠·͢ w 3VCP$PQͷ࢖͍ํͱΧελϚΠ ζͷجૅ͕਎ʹͭ͘͜ͱΛ໨ඪʹ͠ ͍ͯ·͢ w ίΞϢʔβʔ޲͚ʹۙະདྷͷ

    3VCP$PQͷల๬Λ͍ࠞͥͯ·͢
  13. .BTUFSJOHDPOpHEFGBVMUZNM w 3VCP$PQͷجૅઃఆ͸3VCP$PQ ϦϙδτϦͰ؅ཧ͞Ε͍ͯΔ DPOpHEFGBVMUZNMͰ͢ w DPOpHEFGBVMUZNMͱαϯϓϧ ίʔυ͕ಡΊΕ͹ΧελϚΠζͷί π͕෼͔ͬͯ͘Δͱࢥ͍·͢ͷͰਵ ॴʹҾ༻ͯ͠·͢

  14. EPDTSVCPDPQPSH

  15. 0GpDJBMNBOVBM

  16. )FSF

  17. None
  18. None
  19. % rubocop Mastering config/default.yml

  20. "OFYBNQMFDPEF def badName if something test end end

  21. ࣮ߦํ๏ DEQBUIUPSFQP SVCPDPQ w SVCPDPQίϚϯυͰ࣮ߦͰ͖Δ

  22. None
  23. %FQBSU/BNF$PQ/BNF%FTDSJQUJPO

  24. -BZPVU%FQBSUNFOU "MJHO"SSBZ ܯ࡯ॺͷϝλϑΝʔ "MJHO)BTI &MTF"MJHONFOU &NQUZ-JOFT &OE"MJHONFOU &YUSB4QBDJOH *OEFOU"SSBZ -JOU%FQBSUNFOU

    %VQMJDBUF.FUIPET %VQMJDBUFE,FZ &MTF"MJHONFOU 4DSJQU1FSNJTTJPO 6OJpFE*OUFHFS 6OSFBDIBCMF$PEF 6OMFTT"TTJHONFOU .FUSJDT%FQBSUNFOU "CD4J[F #MPDL-FOHUI #MPDL/FTUJOH $MBTT-FOHUI -JOF-FOHUI .FUIPE-FOHUI 1BSBNFUFS-JTUT ʜ
  25. ର৅ϑΝΠϧ w σϑΥϧτͰ֓Ͷͷ3VCZϑΝΠϧΛΧόʔ IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWDPOpHEFGBVMUZNM-- ڞ௨ઃఆ

  26. w 4UZMF4USJOH-JUFSBMTจࣈྻ͕γϯάϧΫΥʔ τ͔μϒϧΫΥʔτ͔ͱ͍͏ɺϓϩδΣΫτ΍ݸ ਓʹΑΔ΋ͷ w 4UZMF'SP[FO4USJOH-JUFSBM$PNNFOU GTUSJOHMJUFSBM͕ه͞Ε͍ͯΔ͔ͱ͍͏কདྷͷ 3VCZ΁ͷඋ͑ʹΑΔ΋ͷ w -JOU6SJ&TDBQF6OFTDBQF3VCZຊମ͕ඇ

    ਪ঑ͷܯࠂΛ͢ΔΑ͏ͳ΋ͷ ෦ॺͱ$PQͷྫ
  27. w 4UZMF4USJOH-JUFSBMTจࣈྻ͕γϯάϧΫΥʔ τ͔μϒϧΫΥʔτ͔ͱ͍͏ɺϓϩδΣΫτ΍ݸ ਓʹΑΔ΋ͷ w 4UZMF'SP[FO4USJOH-JUFSBM$PNNFOU GTUSJOHMJUFSBM͕ه͞Ε͍ͯΔ͔ͱ͍͏কདྷͷ 3VCZ΁ͷඋ͑ʹΑΔ΋ͷ w -JOU6SJ&TDBQF6OFTDBQF3VCZຊମ͕ඇ

    ਪ঑ͷܯࠂΛ͢ΔΑ͏ͳ΋ͷ ෦ॺͱ$PQͷྫ จԽґଘ ݴޠґଘ
  28. จԽ΍޷Έʹ͋ΘͤΔ

  29. wॻ͖ํͷબ୒ࢶ͕ෆ۞ଷఱ ʹͳΓ͏ΔελΠϧ w3VCP$PQͷσϑΥϧτͱ
 จԽ͕͍͋ͬͯΕ͹ྑ͠ɺͦ͏ Ͱͳ͚Ε͹ΧελϚΠζͰ͖Δ wυΩϡϝϯτϑΝʔετͰΧε λϚΠζʹ͍ͭͯݟͯΈΑ͏ γϯάϧΫΥʔτ೿ʁμϒϧΫΥʔτ೿ʁ

  30. )FSF

  31. w σϑΥϧτͷৼΔ෣͍͸3VCP$PQϦϙδτϦ ͷDPOpHEFGBVMUZNMʹ४ڌ͢Δ &OGPSDFE4UZMF IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWDPOpHEFGBVMUZNM- )FSF

  32. % rubocop --init Mastering config/default.yml

  33. QSPKFDUSPPU ɹSVCPDPQZNM )FSF

  34. w SVCPDPQZNMͰσϑΥϧτͷߏ੒Λมߋ͢Δ ͜ͱ͕Ͱ͖Δ w SVCPDPQ͔Β͸SVCPDPQJOJU ͰSVCPDPQZNMΛੜ੒Ͱ͖Δ ϧʔϧͷΧελϚΠζ SVCPDPQJOJU

  35. w ΋͠จࣈྻΛμϒϧΫΥʔτͰ౷Ұ͍ͨ͠৔߹ ͸SVCPDPQZNMͷ্ॻ͖Ͱ࣮ݱͰ͖Δ ΧελϚΠζ͢Δ SVCPDPQZNM 4UZMF4USJOH-JUFSBMT &OGPSDFE4UZMFEPVCMF@RVPUFT w $PQ໊ΛΩʔʹΧελϚΠζ͍ͨ͠ &OGPSDFE4UZMFΛࢦఆ͢Δ

  36. 3VCZ4UZMF(VJEF Mastering config/default.yml

  37. w 3VCP$PQ͸5IF3VCZ4UZMFHVJEFͷ࣮૷ ͱͳΔͨΊɺ Ұ෦ େݩͷσϑΥϧτϧʔϧ͸ͦ ͪΒ 3VCZ4UZMF(VJEF IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWDPOpHEFGBVMUZNM-- )FSF

  38. IUUQTHJUIVCDPNSVCPDPQIRSVCZTUZMFHVJEFCMPCNBTUFS3&"%.&NE

  39. IUUQTHJUIVCDPNSVCPDPQIRSVCZTUZMFHVJEFCMPCNBTUFS3&"%.&NE

  40. &OBCMFE %JTBCMFE

  41. w $PQ͝ͱʹ༗ޮԽແޮԽ͕Ͱ͖Δ w ໌ࣔతʹ&OBCMFEUSVFͱ͢Δ͜ͱͰσϑΥ ϧτແޮͷ΋ͷΛ༗ޮʹɺ&OBCMFEGBMTF͢ Δ͜ͱͰσϑΥϧτ༗ޮͷ΋ͷΛແޮʹͰ͖Δ w σϑΥϧτ͸$PQຖʹUSVFGBMTFࢦఆ͞ΕͯΔ &OBCMFE $PQ୯Ґ

    IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWDPOpHEFGBVMUZNM- )FSF
  42. w ෦ॺ୯ҐͰ༗ޮԽແޮԽ͢Δ͜ͱ΋Մೳ w 3BJMT෦ॺ͸σϑΥϧτͰແޮʹͳ͍ͬͯΔ w 3VCP$PQ࣌఺͸A&OBCMFEUSVFA΍ ASVCPDPQSBJMTAίϚϯυͳͲࢦఆ͕ඞཁ w 3VCP$PQͰࢦఆํ๏มߋͷ༧ఆ ޙड़

    &OBCMFE ෦ॺ୯Ґ IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWDPOpHEFGBVMUZNM- )FSF
  43. w ҰׅͰ͢΂ͯͷ$PQΛແޮʹͰ͖Δ w ରরతʹ&OBCMFE#Z%FGBVMU΋͋Δ %JTBCMFE#Z%FGBVMU IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWDPOpHEFGBVMUZNM- )FSF

  44. w ͍ͪͲશͯΛແޮʹͨ͠͏͑Ͱ࢖͍͍ͨ$PQ͚ͩ Λ&OBMCFEUSVFʹ͢Δઓུ͕ͱΒΕ͍ͯΔ ࣄྫͱͯ͠SBJMTSBJMTϦϙδτϦ͕͋Δ %JTBCMFE#Z%FGBVMU IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDSVCPDPQZNM )FSF

  45. 5BSHFU3VCZ7FSTJPO 3VCP$PQTVQQPSUT3VCZPSIJHIFS

  46. w ࢦఆͷͳ͍σϑΥϧτ͸3VCP$PQ͸ w Ͳͷ3VCZόʔδϣϯͰ੩తղੳΛߦ͏͔ࢦఆՄ 5BSHFU3VCZ7FSTJPO IUUQTHJUIVCDPNSBJMTSBJMTCMPCWSDSVCPDPQZNM )FSF

  47. SVCPDPQZNMͷ5BSHFU3VCZ7FSTJPO ͷ஋ SVCZWFSTJPOͷ஋ (FNpMFMPDLʹه͞Εͨ3VCZόʔδϣϯ ͦΕΒʹઃఆ͕ͳ͍৔߹͸3VCP$PQͰσ ϑΥϧτͷ3VCZόʔδϣϯͱͳ͍ͬͯΔ 5BSHFU3VCZ7FSTJPO IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWDPOpHEFGBVMUZNM- 5BSHFU3VCZ7FSTJPO͸ҎԼͷॱʹࢀরͯ͠ ݟ͔ͭͬͨ΋ͷͰܾఆ͞ΕΔ

  48. % rubocop --auto-correct Mastering config/default.yml

  49. w SVCPDPQBVUPDPSSFDUͰϧʔϧʹԊͬͨ ࣗಈमਖ਼͕Ͱ͖Δ ίʔυͷࣗಈमਖ਼ SVCPDPQBVUPDPSSFDU

  50. "OFYBNQMFDPEF def badName if something test end end

  51. SVCPDPQBVUPDPSSFDU % rubocop -a # -a is a alias for

    --auto-correct Inspecting 1 file C Offenses: example.rb:1:1: C: [Corrected] Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true. def badName ^ example.rb:2:3: C: [Corrected] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||. if something ^^ example.rb:3:5: C: Naming/MethodName: Use snake_case for method names. def badName ^^^^^^^ 1 file inspected, 3 offenses detected, 2 offenses corrected
  52. HJUEJ⒎ % git diff diff --git a/example.rb b/example.rb index 82446de..f57e73e

    100644 --- a/example.rb +++ b/example.rb @@ -1,5 +1,5 @@ +# frozen_string_literal: true + def badName - if something - test - end + test if something end
  53. w BVUPDPSSFDUJPOΛαϙʔτ͍ͯ͠ΔDPQ ͱαϙʔτ͍ͯ͠ͳ͍DPQ͕͋Δ ίʔυͷࣗಈमਖ਼ )FSF

  54. w 3VCZͰϥϯλΠϜ৘ใΛಘΔ͜ͱ ͳ͘੩తղੳ͚ͩͰl҆શʹzݩͷ ίʔυͱ౳Ձͷ࣮ߦ͕Ͱ͖Δ͔ʁ w ݩͷৼΔ෣͍Λҡ࣋ͯࣗ͠ಈमਖ਼͕ Ͱ͖͍ͯΔ͔෼͔Βͳ͍ w 8IBUXPVMEZPVEP ίʔυͷࣗಈमਖ਼

  55. w <ॏཁ>όʔδϣϯ؅ཧͰࣗಈमਖ਼લ ͷίʔυʹ໭ͤΔ͜ͱ w <ॏཁ>·ͣ͸ςετίʔυͰৼΔ෣ ͍ΛकΒΕ͍ͯΔ͜ͱ ࣗಈमਖ਼ͷమଇ

  56. ܧଓతσϦόϦʔͷͨΊͷجૅٕज़ ςεςΟϯά %FWFMPQFS5FTUJOH ࣗಈԽ 1SPKFDU"VUPNBUJPO όʔδϣϯ؅ཧ 7FSTJPO$POUSPM

  57. ܧଓతσϦόϦʔͷͨΊͷجૅٕज़ ςεςΟϯά %FWFMPQFS5FTUJOH ࣗಈԽ 1SPKFDU"VUPNBUJPO όʔδϣϯ؅ཧ 7FSTJPO$POUSPM

  58. 'PSMBSHFQSPKFDU

  59. 6/*9ͱ͍͏ߟ͑ํ IUUQTXXXBNB[PODPKQEQ

  60. % rubocop —safe % rubocop --safe-auto-correct Mastering config/default.yml

  61. w 3VCP$PQ  ͔ΒTBGF ΦϓγϣϯͱTBGFBVUP DPSSFDUΦϓγϣϯ͕ಋೖ͞Εͨ w ϧʔϧʹԊͬͨίʔυΛϧʔϧʹԊͬ ͍ͯͳ͍ͱใࠂ͞ΕΔِཅੑ΍ɺط ଘͷৼΔ෣͍Λഁյ͢Δࣗಈमਖ਼Λ

    ݮΒ͢͜ͱΛओମͱ͍ͯ͠Δ ҆શͳݕ஌ͱࣗಈमਖ਼
  62. w BVUPDPSSFDUJPOʹΑΔࣗಈमਖ਼͕҆શͰ ͳ͍΋ͷ͸TBGFBVUPDPSSFDUJPOͰআ֎ ͢Δ 6OTBGFͳमਖ਼Λআ֎͢Δ )FSF

  63. % rubocop --auto-gen-config Mastering config/default.yml

  64. w SVCPDPQίϚϯυͰݟ͔ͭͬͨPGGFOTF Λআ֎͢ΔSVCPDPQ@UPEPZNMͷੜ੒ ͱߋ৽Λߦ͏ w SVCPDPQ@UPEPZNM ͸SVCPDPQZNMͷܧঝݩʹͳΔ ൜ࡑͷݕ஌ PGGFOTF ͷอཹ

    SVCPDPQZNM JOIFSJU@GSPNSVCPDPQ@UPEPZNM
  65. 3VCP$PQࣗମͷSVCPDPQ@UPEPZNM

  66. 3VCP$PQࣗମͷSVCPDPQ@UPEPZNM &OBCMFEGBMTF &OBCMFEGBMTF

  67. 3VCP$PQࣗମͷSVCPDPQ@UPEPZNM 3FMBYUISFTIPME 3FMBYUISFTIPME 3FMBYUISFTIPME 3FMBYUISFTIPME

  68. % rubocop --parallel Mastering config/default.yml

  69. w QPDLF͞Μ͕࣮૷ͯ͘͠Εͨ3VCP$PQ ͷฒྻ࣮ߦΦϓγϣϯ w ಺෦తʹ͸QBSBMMFMHFNΛ࢖͍ͬͯ Δ w BVUPDPSSFDUͳͲෳ਺ϓϩηε͔Βಉ ҰϑΝΠϧΛߋ৽͢ΔΑ͏ͳΦϓγϣϯ ͱ͸ڞଘͰ͖ͳ͍

    w <*.0>কདྷతʹσϑΥϧτʹͰ͖Δ΋ͷ ͸͓͚ͯ͠Δͱྑͦ͞͏ͱ͸ࢥ͍ͬͯΔ 3VCP$PQͷฒྻ࣮ߦ
  70. 5PXBSET 3VCP$PQ Mastering config/default.yml

  71. w "EEJOHTVQQPSUGPSNVMUJQMF FOGPSDFETUZMFT w &YUSBDU3BJMT$PQTJOB TFQBSBUF w *OUSPEVDFOFXDPQTXJUIB TQFDJBMTUBUVT JOTUFBEPG

    FOBCMFEEJTBCMFE 3VCP$PQҎ߱
  72. SVCPDPQIRSVCPDPQ -BZPVU"MJHO)BTIBMMPXUXPPQUJPOT

  73. None
  74. &OGPSDFE4UZMFLFZ

  75. &OGPSDFE4UZMFLFZ &OGPSDFE4UZMFUBCMF

  76. w αϙʔτ͞Ε͍ͯΔෳ਺ͷελΠϧ͕ڐՄ͞ΕΔ ΦϓγϣϯͰ3VCP$PQ͔Β༗ޮ "MMPX.VMUJQMF4UZMFT IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQQVMMpMFTEJ⒎FC )FSF

  77. SVCPDPQIRSVCPDPQ &YUSBDU3BJMT$PQTJOBTFQBSBUF

  78. None
  79. w 3BJMT෦ॺ͸3VCP$PQͰ࡟আ༧ఆ w AHFNSVCPDPQSBJMTAʹ੾Γग़͍ͯ͠Δ w ݱࡏ3BJMTDPQTΛ࢖͏࣌͸ASVCPDPQ3 SBJMTAΦϓγϣϯ͕ඞཁ͕ͩɺͦΕΒΛഇࢭ ͯ͠SFRVJSFSVCPDPQSBJMTΛ͍ͯ͠Δ͔ Ͳ͏͔ʹมߋ͢Δ༧ఆ w

    3VCP$PQࣗମ͸ϑϨʔϜϫʔΫϥΠϒϥϦ ϥϯλΠϜʹґଘͤͣ3VCZʹϑΥʔΧε͍ͨ͠ 3BJMTDPQTΛநग़͢Δ
  80. w SFRVJSFSVCPDPQQFSGPSNBODFͱಉ༷ ʹɺ3BJMTDPQTΛ࢖͏৔߹͸Πϯετʔϧޙʹ SFRVJSFSVCPDPQSBJMTͱهड़͢Δɺ࢖Θͳ ͚Ε͹هड़͠ͳ͍ SFRVJSF IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCWSVCPDPQZNM SEQBSUZDPQTΛ௥ՃͰ͖Δ

  81. SVCPDPQIRSVCPDPQ *OUSPEVDFOFXDPQTXJUIBTQFDJBMTUBUVT JOTUFBEPGFOBCMFEEJTBCMFE

  82. None
  83. w طଘͷFOBCMFEEJTBCMFE΁ ͷ֦ுͱղऍͰ͖Δ w ৽ͨʹ/FX ͋Δ͍͸OJM Λ༻ҙ ͯ͠ɺͦͷ஋ͷ৔߹͸৽ͨͳ$PQ Λ࢖͏͔Ϣʔβʔʹ༗ޮແޮΛΏ ͩͶΔܯࠂΛදࣔ͢Δ

    5-%3
  84. w ΞοϓάϨʔυ࣌ʹ໰୊ʹͳΔͷ͸ރ Ε͍ͯͳ͍৽͍͠$PQͷέʔε͕ଟ ͍ ϦάϨογϣϯΑΓͪ͜Β͕ଟ͍  w ϝλσʔλͷ7FSTJPO"EEFEɺ͋ Δ͍͸&OBCMFEͷ/FXΛݩʹͯ͠ ࠷৽ͷόʔδϣϯͰ௥Ճ͞Εͨ$PQ

    Λআ֎͢Ε͹ޡݕ஌ΛݮΒͤͦ͏ *.0ΞοϓάϨʔυ΁ͷ҆৺ͷ޲্
  85. w ݱঢ়ͷ3VCP$PQͰແޮʹ͍ͨ͠ $PQ͸Enabled: falseͱͯ͠ ରॲͰ͖Δ͕ɺԿ͔ͷҙਤ͕͋ͬͯ EJTBCMFEʹͨ͠ͷ͔ɺόʔδϣ ϯΞοϓ࣌ͷރΕ͍ͯͳ͍$PQͩ ͔Βແޮʹ͍ͯ͠Δͷ͔ͷঢ়ଶ۠ผ ͕͔ͭͳ͍ ৽ͨͳϥϕϦϯά

  86. Finally…

  87. ࠔͬͨ࣌ͷಓ͠Δ΂ w SVCPDPQPSHͷϚχϡΞϧΛಡΉ w ίϯϑΟάϨʔγϣϯͷৄࡉʹ͍ͭͯ͸ɺ ϦϙδτϦͷDPOpHEFGBVMUZNM͕ ۙಓͳͷ͕ݱঢ় w IUUQTHJUIVCDPNSVCPDPQ IRSVCPDPQCMPCNBTUFS

    DPOpHEFGBVMUZNM w ஍Ҭ3VCZίϛΟχςΟͳͲʹإΛग़͠ ͯΈΔ
  88. "EWBODFE5BML IUUQTSVCZLBJHJPSHQSFTFOUBUJPOTLPJDIUNM

  89. Happy your culture!