Node Types
This is a partial list of the node types parsed by the AST and corresponding methods and information associated with them.
RuboCop::AST::Node
defines some additional methods on certain node types by giving them a separate class. You can see the method definitions in the API documentation.
For full information, please see the parser documentation. This page will act as a quick index to that page, which has more examples and fuller explanation of the different location pieces. You can also see a full AST for yourself by running ruby-parse --legacy -L -e "ruby(code: 'here')"
.
There are a few "meta-types" of nodes that will be mentioned in descriptions:
-
Expression nodes: any expression that returns a value - variables (
lvar
,ivar
,cvar
etc.),send
orcsend
,const
,self
, any literal value (int
,str
,nil
, etc.), or control statements likeif
,case
,begin
, etc. -
Assignment nodes: Any node that assigns a value.
lvasgn
,ivasgn
,cvasgn
,gvasgn
,send
(e.g.self.foo = 5
), orcsend
. -
Body statement: This can be essentially any node except those that must be nested (like
args
ormlhs
). Typically it comes as the children of a node that can contain arbitrary code, likedef
orclass
. This will always be a single node which is either an expression, abegin
node, ornil
.
Location Information
There are different parts of the source map you can get from the node, by calling .loc
on it. Every node has an expression
value which
usually represents the entire node, but others have additional fields.
The following fields are given when relevant to nodes in the source code:
|
Description |
|
The fat-arrow: |
|
|
|
The |
|
The |
|
The |
|
The |
|
|
|
The body of a string in heredoc format. |
|
The end of the heredoc statement. |
|
The |
|
Any text-based keyword, like |
|
Used when defining something (like |
|
Any symbol representing an operator, like |
|
The |
|
Used for method invocation ( |
Node Types
Type | Description | Children | Example | Node Class |
---|---|---|---|---|
|
Method alias |
Two children - both are |
|
|
|
And operator |
Two children are both expression nodes representing the operands. |
|
|
|
And-assignment (AND the receiver with the argument and assign it back to receiver). |
First child must be an assignment node, second child is the expression node. |
|
|
|
Required positional argument. Must come inside an |
One child - a symbol, representing the argument name. |
|
|
|
Argument list. Must come inside a |
Children must be |
|
|
|
Array literal. |
The values in the array, including a possible |
|
|
|
Regular expression back-reference, e.g. $&. |
One child (symbol) representing the reference name, e.g. |
|
|
|
Block execution. |
Three children. First child is the receiver or a |
|
|
|
Used when passing a block as an argument. |
One child, an expression node representing the block to pass. |
|
|
|
Reference to block argument from a function definition. Must come inside an |
One child - a symbol, representing the argument name. |
|
|
|
break keyword |
One child with an expression node for the results to be passed through the break. |
|
|
|
Case statement. |
First child is an expression node for the condition to check. Last child is an expression node for the "else" condition. All middle nodes are |
|
|
|
Constant assignment |
Three children: the parent object (either an expression, |
|
|
|
Represents the top-module constant (i.e. the '::' before a constant name). Only occurs inside a |
None |
|
|
|
Complex literal |
One child, the Complex value |
|
|
|
Constant reference. |
Two children, the parent object (either an expression, |
|
|
|
Class definition |
Three children. First child is a |
|
|
|
Null-safe method invocation, i.e. using |
First child is the receiver node (e.g. |
|
|
|
Class variable access |
One child, the variable name |
|
|
|
Class variable assignment |
Two children: the variable name |
|
|
|
Instance method definition (full format) |
Three children. First child is the name of the method (symbol); second child is |
|
|
|
|
One child, an expression. |
|
|
|
Singleton method definition (full format) - i.e. defining a method on a single object. |
Four children. First child is the receiver; second child is the name of the method (symbol); third child is |
|
|
|
Interpolated string literal. |
Children are split into |
|
|
|
Interpolated symbol literal. |
Children are split into |
|
|
|
Block that contains an |
The last child is the body statement of the |
|
|
|
Exclusive range literal |
Two children, the start and end nodes (including |
|
|
|
False literal |
None |
|
|
|
Floating point literal |
One child, the Float value |
|
|
|
for..in looping condition |
Three children. First child is a |
|
|
|
Forwarding argument, for Ruby 3.0 (when |
None |
|
|
|
Forwarding argument list, for Ruby 2.7 (when |
None |
|
|
|
Forwarding arguments into a method call |
None |
|
|
|
Forwarding positional arguments into a method call |
None |
|
|
|
Forwarding keyword arguments into a method call |
None |
|
|
|
Global variable access |
One child, the variable name as a symbol |
|
|
|
Global variable assignment |
Two children, the variable name |
|
|
|
Hash literal. |
|
|
|
|
If, else, elif, unless and ternary conditions |
Three children. First child is the expression node representing the condition; second child is an expression node representing the true condition; third child is an expression, node representing the false condition. |
|
|
|
Integer literal |
1, the integer value |
|
|
|
Instance variable access |
One child, the variable name |
|
|
|
Instance variable assignment |
Two children, the variable name |
|
|
|
Inclusive range literal. |
Two children, the start and end nodes (including |
|
|
|
Required keyword argument. Must come inside an |
One child - a symbol, representing the argument name. |
|
|
|
Explicit |
Child nodes are body statements. |
|
|
|
Double splat with nil in function definition, used to specify that the function does not accept keyword args. Must come inside an |
None |
|
|
|
Optional keyword argument. Must come inside an |
Two children - a symbol, representing the argument name, and an expression node for the value. |
|
|
|
Double splat used for keyword arguments inside a function call (as opposed to a function definition). |
One child, an expression. |
|
|
|
Double splat used for keyword arguments inside a function definition (as opposed to a function call). Must come inside an |
One child - a symbol, representing the argument name, if a name is given. If no name given, it has no children.. |
|
|
|
Local variable access |
One child, the variable name |
|
|
|
Local variable assignment |
Two children: The variable name (symbol) and the expression. |
|
|
|
Multiple assignment. |
First set of children are all |
|
|
mlhs |
Multiple left-hand side. Used inside a |
|
|
|
module |
Module definition |
|
|
|
next |
next keyword |
|
|
|
|
nil |
Nil literal |
|
|
|
nth_ref |
Regular expression capture group ($1, $2 etc.) |
|
|
|
numblock |
Block that has numbered arguments ( |
|
|
op_asgn |
Operator-assignment - perform an operation and assign the value. |
|
|
|
optarg |
Optional positional argument. Must come inside an |
|
|
|
or |
Or operator |
|
|
|
or_asgn |
Or-assignment (OR the receiver with the argument and assign it back to receiver). |
|
|
|
pair |
One entry in a hash. |
|
|
|
rational |
Rational literal |
|
|
|
|
redo |
Redo command |
|
|
|
regexp |
Regular expression literal. |
|
|
regopt |
Regular expression option, appearing after a regexp literal (the "im" in the example). |
|
|
|
|
resbody |
Exception rescue. Always occurs inside a |
|
|
rescue |
A rescue statement.May be "top-level" or may be nested inside an |
|
|
|
restarg |
Positional splat argument. Must come inside an |
|
|
|
return |
Return statement |
|
|
|
sclass |
Singleton class declaration. |
|
|
|
self |
Access to self |
|
|
|
|
send |
Non-safe method invocation (i.e. top-level or using a dot) |
|
|
shadowarg |
Shadow argument, aka block-local variable. Must come inside an |
|
|
|
splat |
Array or function argument * operator |
|
|
|
|
str |
Non-interpolated string literal. The heredoc version works very differently from the regular version and the location info is totally separate. |
|
|
super |
Super method call with arguments and/or brackets. |
|
|
|
sym |
Non-interpolated symbol |
|
|
|
true |
True literal |
|
|
|
|
undef |
Method undefinition |
|
|
|
until |
Negative loop with condition coming first. |
|
|
until_post |
Negative loop with condition coming last. |
|
|
|
when |
Case matching. Usually nested under |
|
|
|
while |
Loop with condition coming first. |
|
|
|
while_post |
Loop with condition coming last. |
|
|
|
xstr |
Execute string (backticks). The heredoc version is treated totally differently from the regular version. |
|
|
|
yield |
Yield to a block. |
|
|
|
zsuper |
Super method call with no arguments or brackets. |
|
|