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 2.8 (when |
None |
|
|
|
Forwarding argument list, for Ruby 2.7 (when |
None |
|
|
|
Forwarding 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 assigment. |
First set of children are all |
|
|
|
Multiple left-hand side. Used inside a |
Children must all be assignment nodes. Represents the left side of a multiple assignment ( |
|
|
|
Module definition |
Two children. First child is a |
|
|
|
next keyword |
Zero or one child with an expression node for the results to be passed through the next |
|
|
|
Nil literal |
None |
|
|
|
Regular expression capture group ($1, $2 etc.) |
One child: The capture name, e.g. |
|
|
|
Block that has numbered arguments ( |
Three children. First child is a |
|
|
|
Operator-assignment - perform an operation and assign the value. |
Three children. First child must be an assignment node, second child is the operator (e.g. |
|
|
|
Optional positional argument. Must come inside an |
One child - a symbol, representing the argument name. |
|
|
|
Or operator |
Two children are both expression nodes representing the operands. |
|
|
|
Or-assignment (OR the receiver with the argument and assign it back to receiver). |
Two children. First child must be an assignment node, second child is the expression node. |
|
|
|
One entry in a hash. |
Two children, the key and value nodes. |
|
|
|
Rational literal |
One child, the Rational value |
|
|
|
Redo command |
None |
|
|
|
Regular expression literal. |
Children are split into |
|
|
|
Regular expression option, appearing after a regexp literal (the "im" in the example). |
A list of symbols representing the options (e.g. |
|
|
|
Exception rescue. Always occurs inside a |
Three children. First child is either |
|
|
|
A rescue statement.May be "top-level" or may be nested inside an |
First node is a body statement. Last child is the "else" body statement, or |
|
|
|
Positional splat argument. Must come inside an |
One child - a symbol, representing the argument name (if given). If no name given, there are no children. |
|
|
|
Return statement |
Zero or one child, an expression node for the value to return. |
|
|
|
Singleton class declaration. |
Two children. The first child is the expression for the class being opened (e.g. |
|
|
|
Access to self |
None |
|
|
|
Non-safe method invocation (i.e. top-level or using a dot) |
First child is the receiver node (e.g. |
|
|
|
Shadow argument, aka block-local variable. Must come inside an |
One child - a symbol, representing the argument name. |
|
|
|
Array or function argument * operator |
One child, an expression. |
|
|
|
Non-interpolated string literal. The heredoc version works very differently from the regular version and the location info is totally separate. |
One child, the String content. |
|
|
|
Super method call with arguments and/or brackets. |
Children are expression nodes representing arguments. |
|
|
|
Non-interpolated symbol |
One child, the Symbol content. |
|
|
|
True literal |
None |
|
|
|
Method undefinition |
A list of |
|
|
|
Negative loop with condition coming first. |
Two children. First child is an expression node for condition, second child is a body statement. |
|
|
|
Negative loop with condition coming last. |
Two children. First child is an expression node for condition, second child is a body statement. |
|
|
|
Case matching. Usually nested under |
Two children. First child is a regexp, expression node, or |
|
|
|
Loop with condition coming first. |
Two children. First child is an expression node for condition, second child is a body statement. |
|
|
|
Loop with condition coming last. |
Two children. First child is an expression node for condition, second child is a body statement. |
|
|
|
Execute string (backticks). The heredoc version is treated totally differently from the regular version. |
Children are split into |
|
|
|
Yield to a block. |
Children are expression nodes representing arguments. |
|
|
|
Super method call with no arguments or brackets. |
None |
|