Slide 1

Slide 1 text

ݱ৔ͷ RubyVM::AbstractSyntaxTree @hkdnet / ͸͘Ͳʔ

Slide 2

Slide 2 text

ͩΕʁ • hkdnet / ͸͘Ͳʔ • Ruby ͱ RoR Ͱ͓࢓ࣄ͍ͯ͠·͢

Slide 3

Slide 3 text

ઌʹ·ͱΊ

Slide 4

Slide 4 text

AST ͸ࠇຐज़͚ͩͷ΋ͷͰ͸ͳ͍ • ݱ৔Ͱ࢖͑Δ = ݱੈརӹ͕͋Δ • ੩తղੳͷҰखͱͯ͠࢖͑Δ

Slide 5

Slide 5 text

͓͜ͱΘΓ • ASTͷઆ໌͸͜ΕҎ߱Ͱ͸͠·ͤΜ • AST = Abstract Syntax Tree • ந৅ߏจ໦ͱݴΘΕΔ΍ͭͰύʔεͨ݁͠ՌͰ͋Γɺϓϩάϥ ϜͷҙຯΛࣔͨ͠໦ߏ଄ͩͱࢥ͍ͬͯͩ͘͞

Slide 6

Slide 6 text

ࣄྫ

Slide 7

Slide 7 text

deprecated ͳϝιουΛۦஞ͍ͨ͠

Slide 8

Slide 8 text

deprecated ͳϝιουΛۦஞ͍ͨ͠ • ΞϓϦέʔγϣϯ͕ҭͭͱඇਪ঑ͳϝιου͕ग़ͯ͘Δ • -> ౰વ͖Ε͍ʹ͍ͨ͠

Slide 9

Slide 9 text

deprecated ͳϝιουΛۦஞ͍ͨ͠ • ΞϓϦέʔγϣϯ͕ҭͭͱඇਪ঑ͳϝιου͕ग़ͯ͘Δ • -> ౰વ͖Ε͍ʹ͍ͨ͠ • deprecated ͳϩά͕େྔʹग़ͯͯӡ༻্अຐͩͬͨ • -> ࣮༻తʹ໰୊͋Γ

Slide 10

Slide 10 text

ର৅ • ͋ΔΦϒδΣΫτͷଐੑΛऔಘ͢ΔͨΊͷ obj.foo_attr Έ͍ͨ ͳϝιου • ֓೦తʹ foo_attr ͱ͍͏ͷ͕ద੾Ͱͳ͘ͳͬͨͷͰ obj.bar_attr ʹ৐Γ׵͍͑ͨ

Slide 11

Slide 11 text

࣮ફฤ

Slide 12

Slide 12 text

grep

Slide 13

Slide 13 text

grep • ݴΘͣͱ͠Εͨߦݕࡧπʔϧ • ͍͚ͦͦ͜͜Δ

Slide 14

Slide 14 text

grep • ݴΘͣͱ͠Εͨߦݕࡧπʔϧ • ʮͦͦ͜͜ʯ͍͚Δ

Slide 15

Slide 15 text

grepͷݶք # OK foo_attr = obj.foo_attr # NG: false positive { foo_attr: foo_attr }.to_json

Slide 16

Slide 16 text

grep ͷݶք • Ruby εΫϦϓτʹ͓͍ͯɺจࣈྻ͚ͩͰ͸ҙຯΛ൑அͰ͖ͳ ͍ x = 1 # ͋Δ͍͸͜ͷߦͳ͠ x # ϩʔΧϧม਺ݺͼग़͠ or Ϩγʔόͳ͠ϝιουݺͼग़͠

Slide 17

Slide 17 text

AST ͷग़൪ʂʂʂ

Slide 18

Slide 18 text

RubyVM::AbstractSyntaxTree • AST ͸ϓϩάϥϜͷҙຯ(syntax)Λද͍ͯ͠Δ • ౰વϩʔΧϧม਺ࢀর(NODE_DVAR)ͱҾ਺ͳ͠ϝιουݺͼ ग़͠(NODE_VCALL)͕۠ผͰ͖Δ

Slide 19

Slide 19 text

RubyVM::AbstractSyntaxTree • ࠓճ͸ɺ͋ΔAST܈͔ΒಛఆͷϝιουίʔϧΛ୳͍ͨ͠ • -> call_searcher gem • https://github.com/hkdnet/call_searcher

Slide 20

Slide 20 text

RubyVM::AbstractSyntaxTree require 'call_searcher' s = CallSearcher::Searcher.new { |e| e.mid == :search } root_dir = File.expand_path('../', __dir__) result = s.search_dir(root_dir: root_dir, path: '{app,lib,spec}', github: 'hkdnet/call_searcher') result.each { |e| puts e.github_url } # => https://github.com/hkdnet/call_searcher/blob/master/ lib/call_searcher/searcher.rb#L14

Slide 21

Slide 21 text

RubyVM::AbstractSyntaxTree • Ұ݅མண

Slide 22

Slide 22 text

RubyVM::AbstractSyntaxTree • Ұ݅མணͯ͠ͳ͍ • ʮ͋ΔAST܈͔ΒಛఆͷϝιουίʔϧΛ୳͍ͨ͠ʯ • ϓϩδΣΫτʹ͓͚Δ AST ܈Λ͢΂ͯൈ͘ඞཁ͕͋Δ • ͋Μ·Γࣗ໌Ͱ͸ͳ͍

Slide 23

Slide 23 text

RubyVM::AbstractSyntaxTree • Rails ϓϩδΣΫτʹ͓͍ͯ Ruby εΫϦϓτ͸͋·Ͷ͘ଘࡏ͠ ͍ͯΔ • .rb ϑΝΠϧ • .yaml ϑΝΠϧ(erbه๏) • .haml / .slim / .jbuilder ϑΝΠϧ

Slide 24

Slide 24 text

RubyVM::AbstractSyntaxTree • ઈࢍ࡞ۀத • https://github.com/hkdnet/asts • call_searcher ଆͰͲ͏ݕࡧ͢Δ͔ͱ͔Λؾʹ͠ͳͯ͘Α͘ͳΔ ͸ͣ……ɻ

Slide 25

Slide 25 text

ҎԼ͓·͚

Slide 26

Slide 26 text

slim ରԠ

Slide 27

Slide 27 text

slim ରԠ • ύʔεͨ͠Β SࣜΛฦ͖ͯͯ͠ΊΜͲ͘͘͞ͳͬͨ parser = Slim::Parser.new parser.call(s) [:multi, [:html, :tag, "h1", [:html, :attrs], [:slim, :output, true, "@foo", [:multi, [:newline]]]]]

Slide 28

Slide 28 text

haml ରԠ • ๻ʮHaml::Parser ͕ AST Λฦ͢ͷͰ͔ͦ͜Β Ruby ͱͯ͠ධՁ ͞ΕΔ΋ͷΛൈ͍ͯ͘Ε͹ྑ͍Μ͡ΌͶʔͷʯ

Slide 29

Slide 29 text

haml • Haml::Parser ͕ AST Λฦ͢ͷͰ͔ͦ͜Β Ruby ͱͯ͠ධՁ͞Ε Δ΋ͷΛൈ͍ͯ͘Ε͹ྑ͍ <- ΢ι • લड़ͷΑ͏ʹ a ͱ͍͏εΫϦϓτ͸ a ͕ԿͰ͋Δ͔ʹΑܾͬͯ ·Δ • ݁ہίϯςΩετґଘ͕ൈ͚ͳ͍

Slide 30

Slide 30 text

haml • ࠷ऴతʹ view Λు͘1ͭͷ Ruby εΫϦϓτʹͳΔ͸͔ͣͩΒ ͔ͦ͜Βൈ͍ͯ͘Ε͹ྑ͍ʁ • ͦΕͬͯ helper ͱ͔ਖ਼͘͠ೝࣝͰ͖ΔΜͩΖ͏͔ • ͳΜ͔ແཧͳؾ͕͠ͳ͘΋ͳ͍ͳʔ <- ΠϚίί