ESLintとCyclomatic complexityの話

ESLintとCyclomatic complexityの話

ESLintを使って、JavaScriptのコード内でCyclomatic complexity(循環的複雑度)が高い箇所を特定するお話です。

90f8b0a76f7dc881ae2e10aaf91c62d0?s=128

Kenya Hoshi

July 13, 2019
Tweet

Transcript

  1. &4-JOUͱ $ZDMPNBUJDDPNQMFYJUZͷ࿩ !ZBIPPTIJLFO 4VTUBJOBCMF'SPOUFOE

  2. ࣗݾ঺հ w ੕ໂᜌ݈໵)PTIJNVSF,FOZB w !ZBIPPTIJLFO w প௡ߴઐˠେࡕେֶجૅ޻ֶ෦೥ٳֶத w גࣜձࣾ10-ΤϯδχΞ w

    Likes:8FC'SPOUFOE 3FBDU 5ZQF4DSJQU  &MN )3 4BB4 .BOBHFNFOU
  3. ʮྑ͍ʯίʔυͱ͸ʁ ಥવͰ͕͢ʜ

  4. 8IBU`Tl$MFBO$PEFz ࢲ͸ɺࣗ෼ͷίʔυ͕ΤϨΨϯτͰޮ཰ྑ͍͜ͱΛ޷Έ·͢ɻ ϩδοΫ͸όά͕ೖΓࠐΈʹ͍͘Α͏ʹ୯७Ͱ͋Δ΂͖Ͱɺ ґଘੑ͸อक͕༻ҙͱͳΔΑ͏ʹ࠷খݶʹ͢΂͖Ͱ͋Γɺ Τϥʔॲཧ͸Ұ࿈ͷઓུʹଇͬͯ׬શͰͳ͚Ε͹ͳΒͣɺ ޙʑɺଞͷਓ͕ཧ࿦ʹཪଧͪ͞Ε͍ͯͳ͍࠷దԽΛߦ͏ ༠࿭ʹ͔ΒΕͯɺίʔυ͕͙ͪΌ͙ͪΌʹͳͬͯ͠·Θͳ͍Α͏ɺ ࣮ߦޮ཰͸࠷ߴʹ͍ۙ΋ͷͰͳ͚Ε͹ͳΓ·ͤΜɻ ΫϦʔϯίʔυͱ͸ͭͷ͜ͱΛ্ख͘΍Δ͜ͱͰ͢ɻ #KBSOF4USPVTUSVQ

  5. ։ൃऀʹͱͬͯʮྑ͍ʯίʔυ Մಡੑ͕ߴ͍ʢSFBEBCMFʣ อकੑ͕ߴ͍ʢNBJOUBJOBCMFʣ ϘΫ͕ࢥ͏

  6. ͦΜͳ؆୯ʹʮྑ͍ʯίʔυ͸
 ॻ͚ͳ͍ ͱ͸͍͑ɺ

  7. ͳ͔ͥʁ

  8. None
  9. ͜͜Ͱ؀ڥ൜ࡑֶͷ࿩Λ ࣏͕҆ѱԽ͢Δ·Ͱʹ͸࣍ͷܦաΛͨͲΔɻ ݐ෺ͷ૭͕յΕ͍ͯΔͷΛ์ஔ͢ΔͱɺͦΕ͕ʮ୭΋౰֘஍۠ʹ ରؔ͠৺Λ෷͍ͬͯͳ͍ʯͱ͍͏αΠϯͱͳΓɺ൜ࡑΛى͜͠΍͢ ͍؀ڥΛ࡞Γग़͢ɻ ΰϛͷϙΠࣺͯͳͲͷܰ൜ࡑ͕ى͖Δɻ ॅຽͷϞϥϧ͕௿Լͯ͠ɺ஍Ҭͷৼڵɺ҆શ֬อʹڠྗ͠ͳ͘ͳ ΔɻͦΕ͕͞Βʹ؀ڥΛѱԽͤ͞Δɻ ڟѱ൜ࡑΛؚΊͨ൜ࡑ͕ଟൃ͢ΔΑ͏ʹͳΔɻ

  10. ιϑτ΢ΣΞ։ൃͷݱ৔Ͱ΋ ࣅͨΑ͏ͳޫܠ͋Γ·ͤΜ͔ʁ

  11. #SPLFO8JOEPX5IFPSZ ࣏҆Λճ෮ͤ͞Δʹ͸ ɾҰݟແ֐Ͱ͋ͬͨΓɺܰඍͳடংҧ൓ߦҝͰ΋औΓక·Δ
 ʢ͝Έ͸͖ͪΜͱ෼ผࣺͯͯ͠ΔͳͲʣ ɾܯ࡯৬һʹΑΔύτϩʔϧ΍ަ௨ҧ൓ͷऔΓక·ΓΛڧԽ͢Δ ɾ஍Ҭࣾձ͸ܯ࡯৬һʹڠྗ͠ɺடংͷҡ࣋ʹ౒ྗ͢Δ

  12. ׂΕͨ૭Λ์ஔ͓͔ͯ͠ͳ͍ #PZ4DPVU3VMF ɾѱ͍ઃܭɺؒҧͬͨҙࢥܾఆɺ࣭ͷѱ͍ίʔ υ͸ɺൃݟͱಉ࣌ʹશͯम෮͢Δ ɾ΋͠ਖ਼͘͠म෮͢Δ͚ͩͷ͕࣌ؒͳ͚Ε ͹ɺͦͷࢫΛΘ͔Γ΍͘͢ίϝϯτͳͲͰ໌ ࣔ͢Δ

  13. -FBWFUIJTXPSMEBMJUUMF CFUUFSUIBOZPVGPVOEJU ࣗ෼͕࠷ॳʹݟͨ࣌ΑΓ΋ɺੈքΛ͍͍৔ॴʹ͢΂͘౒ྗ͠Α͏ 3PCFSU#BEFO1PXFMM

  14. Ͳ͏΍ͬͯιʔείʔυͷ ໰୊Λૣظൃݟ͢Δ͔ʁ ͡Ό͋ɺ

  15. ͦ͜Ͱ

  16. &4-JOUΛ࢖͏ϝϦοτ ຊ࣭తͰ͸ͳ͍ϨϏϡʔ޻਺͕ݮΔ ੩తղੳʹΑΔෆ۩߹ͷൃݟ

  17. &4-JOUΛ࢖͏ϝϦοτ ຊ࣭తͰ͸ͳ͍ϨϏϡʔ޻਺͕ݮΔ ੩తղੳʹΑΔෆ۩߹ͷൃݟ

  18. ຊ࣭తͰ͸ͳ͍ϨϏϡʔ const person = { name: “Steve Jobs”, age: 45

    }; ɾ0CKFDUͷ࠷ޙͷϓϩύςΟʹίϯϚ͸͚ͭͯཉ ͍͠ͳɻHJUͷࠩ෼͕ΑΓΘ͔Γ΍͍͔͢ΒͶɻ ɾϘΫ͸μϒϧΫΥʔτΑΓ΋γϯάϧΫΥʔτͷ ํ͕޷͖ͳΜͩɻ4IJGUΛԡͣ͞ʹೖྗͰ͖Δ͔Β Ͷɻ
  19. ୀ۶ͳ͜ͱ͸&4-JOUʹ΍ΒͤΑ͏ ɾਓ͕ؒϓϩάϥϜͷॻ͖ํͷҧ ͍Λࢦఠ͢Δ͜ͱ͸ɺ݂Ͱ݂Λચ ͏ऴΘΓͷͳ͍૪͍ΛੜΈग़͢ ɾਓؒؔ܎ͷѱԽΛڪΕͯɺࢦఠ Λ͠ͳ͍ͷ͸ιϑτ΢ΣΞͷʮׂ Εͨ૭ʯΛ์ஔ͢Δ͜ͱ ɾຊདྷɺਓ͕ؒ΍Δ΂͖αʔϏε ϩδοΫ΍࢓༷ͷϨϏϡʔʹूத ͠Α͏

  20. &4-JOUΛ࢖͏ϝϦοτ ຊ࣭తͰ͸ͳ͍ϨϏϡʔ޻਺͕ݮΔ ੩తղੳʹΑΔෆ۩߹ͷൃݟ

  21. ੩తղੳͱ͸ʁ ίʔυΛ࣮ߦͤͣʹߦ͏ݕূ ɾίʔυͷՄಡੑʹؔ͢Δ໰୊ ɹɹΠϯσϯτͳͲͷॻࣜҧ൓ ɹɹ໋໊ن໿ҧ൓ ɾίʔυͷෳࡶ͞ͳͲʹؔ͢Δݕূ ɹɹΫϥεɺϝιουͷॲཧͷෳࡶ͞ ɹɹΫϥεؒͷґଘؔ܎

  22. ࠓ೔ͷຊ୊ ΍ͬͱ

  23. $ZDMPNBUJDDPNQMFYJUZ ॥؀తෳࡶ౓

  24. ʮ॥؀తෳࡶ౓ʯͱ͸ʁʁʁ ɾ5IPNBT.D$BCF͕։ൃͨ͠
 ιϑτ΢ΣΞଌఆ๏ͷҰछ ɾϓϩάϥϜͷ੍ޚϑϩʔΛάϥϑͱͯ͠
 දݱ͠ɺܭࢉ͞ΕΔɻ ɾJG FMTF GPS TXJUDI͕ଟ͍෼ذύλʔϯ ͕ଟ͍΄Ͳෳࡶ౓͕ߴ͍

    ɾ॥؀తෳࡶ౓͕ߴ͍΄Ͳɺόάͷࠞೖ཰͕
 ߴ͍ͱݴΘΕ͍ͯΔɻ
  25. &4-JOUͰ $ZDMPNBUJDDPNQMFYJUZ { "complexity": ["error", 10] } Λɺ͋ͳͨͷFTMJOUSDʹ௥Ճ͢Δ͚ͩʂ؆୯ʂ

  26. ෳࡶ౓ΛଌΔπʔϧͰ ͜Μͳͷ΋͋Γ·ͨ͠ ͦͷଞʹ΋

  27. FTBOBMZTJTQMBUP +BWB4DSJQUͷίʔυΛղੳͯ͠ɺίʔυߦ਺΍
 ෳࡶ౓΍εςοϓ਺ͳͲΛՄࢹԽͯ͘͠ΕΔ
 OQNύοέʔδ IUUQTXXXOQNKTDPNQBDLBHFQMBUP

  28. ࣮ࡍͷγεςϜͰݕূͯ͠Έͨ

  29. ࠓճͷݕূର৅γεςϜ ɾ೥݄pSTUDPNNJUͷ3FBDUϓϩδΣΫτ ɾॳظ͸ϑϩϯτΤϯυઐ೚ΤϯδχΞਓ ɾۀ຿ҕୗͱΠϯλʔϯੜͷ׼ͱྦͷ݁থ ɾνʔϜϝϯόʔʮෛ࠴΍͹ͦ͏ʜʯ 

  30. QMBUPͰͷݕূ݁Ռ ສߦʂ ͼͬ͘Γʂ ͓ʁ ҙ֎ͱେৎ෉ʁ

  31. &4-JOUͰͷݕূ݁Ռ ສߦͷதͰFSSPSʂࢥͬͯΑΓগͳ͍ʜ☺

  32. ॥؀తෳࡶ౓ͱόάࠞೖ཰ͷؔ܎ ࢀߟɿIUUQTXXXJOGPRDPNOFXTDZDMPNBUJDDPNQMFYJUZ ॥؀తෳࡶ౓ όάࠞೖ֬཰

  33. ·ͱΊ ɾϦϑΝΫλϦϯάΛͲ͔͜ΒखΛ෇͚Δ͔ͷ ํ਑ΛܾΊΔ൑அࡐྉ͕ू·ͬͨʂ ɹϦϑΝΫλલޙͰ਺஋͕Ͳ͏ͳΔָ͔͠Έ ɾνʔϜϝϯόʔͷίʔυʹରͯ͠ͷةػҙࣝ ͕ߴ·ͬͨ ɹΞʔΩςΫνϟ࡮৽ϓϩδΣΫτ࢝ಈʂ 

  34. 5IBOLT