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

サクッと作る静的解析ツール -importの依存制御ツールを作る-

サクッと作る静的解析ツール -importの依存制御ツールを作る-

More Decks by 株式会社ビットキー / Bitkey Inc.

Other Decks in Technology

Transcript

  1. Outline 2 2 1. ࣗݾ঺հ 2. ϞϊϨϙ։ൃͰؾʹͳΔ͜ͱ 3. ਽ܗੜ੒πʔϧ: skeleton

    4. ੩తղੳ༻ύοέʔδ: analysis 5. ύοέʔδґଘ੍ޚπʔϧ࣮૷ 6. ·ͱΊ
  2. 1. ࣗݾ঺հ 3 • ∁ڮయੜ(Noriki Takahashi) ◦ Twitter: @norinoriki0 ◦

    GitHub: nrnrk • גࣜձࣾBitkey ◦ bitkey platform ͱ͍͏"ΧΪ"ͷϓϥοτϑΥʔϜͷ։ൃ • Go Ͱ GKE ্Ͱಈ͘APIαʔόʔͷ։ൃͳͲΛ͍ͯ͠·͢ • झຯ: ਺ֶɾ෺ཧֶ ◦ zk-SNARKs ͷ਺ֶతͳଆ໘ΛטΈࡅ͘هࣄΛॻ͍ͨΓͯ͠·͢
  3. 1. ࣗݾ঺հ 4 • ∁ڮయੜ(Noriki Takahashi) ◦ Twitter: @norinoriki0 ◦

    GitHub: nrnrk • גࣜձࣾBitkey ◦ bitkey platform ͱ͍͏"ΧΪ"ͷϓϥοτϑΥʔϜͷ։ൃ • Go Ͱ GKE ্Ͱಈ͘APIαʔόʔͷ։ൃͳͲΛ͍ͯ͠·͢ • झຯ: ਺ֶɾ෺ཧֶ ◦ zk-SNARKs ͷ਺ֶతͳଆ໘ΛטΈࡅ͘هࣄΛॻ͍ͨΓͯ͠·͢
  4. 13 13 2. ϞϊϨϙ։ൃͰؾʹͳΔ͜ͱ • Go ͷϞϊϨϙͷҰྫ ղܾํ๏ᶃ internal ύοέʔδΛར༻

    serviceA,B,C ͦΕͧΕͷύοέʔδ಺Ͱެ։͠ ͨ͘ͳ͍ύοέʔδ͸ internal ͷதʹೖΕΔ
  5. 14 14 2. ϞϊϨϙ։ൃͰؾʹͳΔ͜ͱ • Go ͷϞϊϨϙͷҰྫ ղܾํ๏ᶃ internal ύοέʔδΛར༻

    serviceA,B,C ͦΕͧΕͷύοέʔδ಺Ͱެ։͠ ͨ͘ͳ͍ύοέʔδ͸ internal ͷதʹೖΕΔ → ڞ௨ར༻͢Δ΋ͷ͸ϥΠϒϥϦͱͯ͠੾Γ ग़͢ͷͰɺશ෦ internal ͷதʹೖΕΔ͜ͱʹ...
  6. 15 15 2. ϞϊϨϙ։ൃͰؾʹͳΔ͜ͱ • Go ͷϞϊϨϙͷҰྫ ղܾํ๏ᶄ Go Module

    ͷ෼ׂ serviceA,B,C ҎԼʹ go.mod Λ͓͍ͯผͷϞ δϡʔϧͱΈͳ͢
  7. 16 16 2. ϞϊϨϙ։ൃͰؾʹͳΔ͜ͱ • Go ͷϞϊϨϙͷҰྫ ղܾํ๏ᶄ Go Module

    ͷ෼ׂ serviceA,B,C ҎԼʹ go.mod Λ͓͍ͯผͷϞ δϡʔϧͱΈͳ͢ → ؅ཧ͕൥ࡶʹͳΔͷͰආ͚͍ͨ
  8. 18 18 2. ϞϊϨϙ։ൃͰؾʹͳΔ͜ͱ • Go ͷϞϊϨϙͷҰྫ ղܾํ๏ᶅ linter Ͱ੍ޚ

    ύοέʔδؒͷґଘΛνΣοΫͯ͠CIͳͲͰ੍ ޚͰ͖ΔΑ͏ʹ͢Δ
  9. 19 19 2. ϞϊϨϙ։ൃͰؾʹͳΔ͜ͱ • Go ͷϞϊϨϙͷҰྫ ղܾํ๏ᶅ linter Ͱ੍ޚ

    ύοέʔδؒͷґଘΛνΣοΫͯ͠CIͳͲͰ੍ ޚͰ͖ΔΑ͏ʹ͢Δ → ͪΐͬͱअಓͬΆ͍͕ɺࠓճ͸͜ΕΛࢼ͢
  10. • analysis ύοέʔδ ◦ ੩తղੳ༻ͷ"४ެࣜ"ύοέʔδ (golang.org/x/tools/go/analysis) ▪ → ੩తղੳʹ͓͚ΔϝΠϯͷϩδοΫ͚ͩʹूதͰ͖Δ ◦

    ಛ௃ ▪ Analyzer ߏ଄ମͰ੩తղੳͷ݁Ռͷ࠶ར༻͕؆୯ʹ 4. ੩తղੳ༻ύοέʔδ: analysis 38
  11. • analysis ύοέʔδ ◦ ੩తղੳ༻ͷ"४ެࣜ"ύοέʔδ (golang.org/x/tools/go/analysis) ▪ → ੩తղੳʹ͓͚ΔϝΠϯͷϩδοΫ͚ͩʹूதͰ͖Δ ◦

    ಛ௃ ▪ Analyzer ߏ଄ମͰ੩తղੳͷ݁Ռͷ࠶ར༻͕؆୯ʹ ◦ ઌఔͷ skeleton Ͱੜ੒͞ΕͨίʔυͰ࢖ΘΕ͍ͯΔ 4. ੩తղੳ༻ύοέʔδ: analysis 39
  12. • Analyzer ߏ଄ମ ͷ࠶ར༻ • Analyzer ߏ଄ମΛఆٛ͢Δࡍʹɺଞͷ Analyzer Λར༻͢Δ͜ͱ͕Ͱ͖Δ •

    ྫ: golang.org/x/tools/go/analysis/passes/inspect.Analyzer Λ༻͍ͯϊʔ υͷϑΟϧλϦϯά 4. ੩తղੳ༻ύοέʔδ: analysis 45
  13. • Analyzer ߏ଄ମ ͷ࠶ར༻ • Analyzer ߏ଄ମΛఆٛ͢Δࡍʹɺଞͷ Analyzer Λར༻͢Δ͜ͱ͕Ͱ͖Δ •

    ྫ: golang.org/x/tools/go/analysis/passes/inspect.Analyzer Λ༻͍ͯϊʔυ ͷϑΟϧλϦϯά 4. ੩తղੳ༻ύοέʔδ: analysis 46 Analyzer ͷ࠶ར༻ͷ؆୯ͳྫͱͯ͠ɺ ࣝผࢠ͕ "gopher" ͱͳ͍ͬͯΔ΋ͷΛݟ͚ͭΔ࣮૷ྫΛݟͯΈΔ → ࣍ͷεϥΠυ ※ skeleton ͔Βੜ੒͞ΕΔαϯϓϧ
  14. 4. ੩తղੳ༻ύοέʔδ: analysis 49 ࠶ར༻͢Δ Analyzer Λఆٛ ͜͜Ͱ͸ɺAST ݕࠪ༻ ͷ

    Analyzer Λར༻ golang.org/x/tools/go/ analysis/passes/ inspect mylinter.go
  15. 4. ੩తղੳ༻ύοέʔδ: analysis 52 inspect Λར༻ͯ͠ ast.Ident ͷΈʹߜͬͯ ॲཧ͢Δ ଞͷ

    AST ͷϊʔυ͕ དྷͨͱ͖ͷॲཧΛॻ͔ ͳͯ͘΋ྑ͍ʂ mylinter.go
  16. • ᶄ ςετ࡞੒ 5. ύοέʔδґଘ੍ޚπʔϧ࣮૷ 66 svcb → svcc ʹґଘΛ΋͓ͨͤͯ͘

    → ͜Ε͸Τϥʔͱͯ͠ݕग़͢Δ testdata/src/a/srcb/ main.go testdata/src/a/srcc/main.go
  17. • ᶄ ςετ࡞੒ 5. ύοέʔδґଘ੍ޚπʔϧ࣮૷ 67 ίϝϯτͰϝοηʔδͷظ଴஋Λهࡌ // want <͜͜ʹهࡌ>

    ← analysistest ύοέʔδ͕ղऍ testdata/src/a/srcb/ main.go testdata/src/a/srcc/main.go
  18. • ·ͱΊ ◦ skeleton Λ࢖ͬͯαΫοͱ੩తղੳπʔϧ͕࡞ΕΔʂ ◦ ϞϊϨϙͳͲͰύοέʔδؒͷґଘΛ੍ޚ͍ͨ͠৔߹ ▪ ͞Βʹɺinternal ύοέʔδͷར༻΍Ϟδϡʔϧͷ෼ׂ͕ϑΟοτ͠ͳ͍৔߹

    ▪ → banimport Λ࢖ͬͯύοέʔδؒͷґଘ੍ޚΛࢼ͢Ձ஋͋Γʂ ▪ CI ͳͲʹ૊ΈࠐΉͱޡͬͨ import ͕๷͛Δ ◦ ͦͷଞ ▪ skeleton ͷ೔ຊޠ README ▪ Go ͷ AST ͷೖ໳తͳ಺༰ʹ͍ͭͯͷաڈͷൃද (એ఻) 105 6. ·ͱΊ