現場の RubyVM::AbstractSyntaxTree
by
hkdnet
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
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 ͱ͔ਖ਼͘͠ೝࣝͰ͖ΔΜͩΖ͏͔ • ͳΜ͔ແཧͳؾ͕͠ͳ͘ͳ͍ͳʔ <- ΠϚίί