manipulating ruby ast
Post on 21-Jan-2018
141 Views
Preview:
TRANSCRIPT
Manipulating Ruby AST@jonatasdp
ABSTRACT
SYNTAX
TREE
Why we need this?● Ruby is !● Adopt the Community Style Guide● Automatic checks● Autocorrect feature
Ruby is !
3 times !
Manipulate strings !
We have a few redundant syntaxes to explore!
(good || bad)? http://gorb.ideia.me
� if the code is bad if the code is good
Before Cop Anatomy...
Let’s play with the
AST
Using Ripper
$ ruby -r ripper -e 'p Ripper.tokenize("1 + 2")'
["1", " ", "+", " ", "2"]
Using Parser
$ ruby-parse -e '1+2'
(send
(int 1) :+
(int 2))
github.com/whitequark/parser
github.com/jonatas/rubocopularUsing Parser
RuboCop::Node
#type
#parent
#children
#sibling_index
#each_ancestor(*types)
Cop Anatomy
● #add_offense● #autocorrect(node)● #on_{type}● #investigate()
RuboCop::NodePattern
Let’s have some fun
with the
Node Pattern
github.com/jonatas/rubocopular
RuboCop::NodePattern
_ is something
… whatever
$ captures
{} for union
[] for intersection
NodePatternCompiler
RuboCop #autocorrect
FastCompiler
Replacing code with fast
Thanks
@jonatasdp
http://ideia.me
top related