manipulating ruby ast

Post on 21-Jan-2018

138 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

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 or Bad?

http://gorb.ideia.me

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

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

RuboCop::NodePattern

_ is something

… whatever

$ captures

{} for union

[] for intersection

NodePatternCompiler

RuboCop #autocorrect

FastCompiler

Replacing code with fast

top related