+ All Categories
Home > Software > Manipulating Ruby AST

Manipulating Ruby AST

Date post: 21-Jan-2018
Category:
Upload: jonatas-paganini
View: 135 times
Download: 0 times
Share this document with a friend
23
Manipulating Ruby AST @jonatasdp
Transcript
Page 1: Manipulating Ruby AST

Manipulating Ruby AST@jonatasdp

Page 2: Manipulating Ruby AST

ABSTRACT

SYNTAX

TREE

Page 3: Manipulating Ruby AST

Why we need this?● Ruby is !● Adopt the Community Style Guide● Automatic checks● Autocorrect feature

Page 4: Manipulating Ruby AST

Ruby is !

Page 5: Manipulating Ruby AST

3 times !

Page 6: Manipulating Ruby AST

Manipulate strings !

Page 7: Manipulating Ruby AST

We have a few redundant syntaxes to explore!

Page 8: Manipulating Ruby AST

Good or Bad?

http://gorb.ideia.me

Page 10: Manipulating Ruby AST

Before Cop Anatomy...

Let’s play with the

AST

Page 11: Manipulating Ruby AST

Using Ripper

$ ruby -r ripper -e 'p Ripper.tokenize("1 + 2")'

["1", " ", "+", " ", "2"]

Page 12: Manipulating Ruby AST

Using Parser

$ ruby-parse -e '1+2'

(send

(int 1) :+

(int 2))

github.com/whitequark/parser

Page 14: Manipulating Ruby AST

RuboCop::Node

#type

#parent

#children

#sibling_index

#each_ancestor(*types)

Page 15: Manipulating Ruby AST

Cop Anatomy

● #add_offense● #autocorrect(node)● #on_{type}● #investigate()

Page 16: Manipulating Ruby AST

RuboCop::NodePattern

Let’s have some fun

with the

Node Pattern

Page 18: Manipulating Ruby AST

RuboCop::NodePattern

_ is something

… whatever

$ captures

{} for union

[] for intersection

Page 19: Manipulating Ruby AST

NodePatternCompiler

Page 20: Manipulating Ruby AST

RuboCop #autocorrect

Page 21: Manipulating Ruby AST

FastCompiler

Page 22: Manipulating Ruby AST

Replacing code with fast


Recommended