Class: Parser::Builders::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/builders/default.rb

Overview

Default AST builder. Uses AST::Nodes.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefault

Initializes attributes:

* `emit_file_line_as_literals`: `true`


164
165
166
# File 'lib/parser/builders/default.rb', line 164

def initialize
  @emit_file_line_as_literals = true
end

Class Attribute Details

.emit_arg_inside_procarg0Boolean

AST compatibility attribute; causes a single non-mlhs block argument to be wrapped in s(:procarg0).

If set to false (the default), block arguments ‘|a|` are emitted as `s(:args, s(:procarg0, :a))`

If set to true, block arguments ‘|a|` are emitted as `s(:args, s(:procarg0, s(:arg, :a))`

Returns:

  • (Boolean)


97
98
99
# File 'lib/parser/builders/default.rb', line 97

def emit_arg_inside_procarg0
  @emit_arg_inside_procarg0
end

.emit_encodingBoolean

AST compatibility attribute; locations of ‘__ENCODING__` are not the same as locations of `Encoding::UTF_8` causing problems during rewriting, all new code should set this attribute to true.

If set to false (the default), ‘__ENCODING__` is emitted as ` s(:const, s(:const, nil, :Encoding), :UTF_8)`.

If set to true, ‘__ENCODING__` is emitted as `s(:__ENCODING__)`.

Returns:

  • (Boolean)


58
59
60
# File 'lib/parser/builders/default.rb', line 58

def emit_encoding
  @emit_encoding
end

.emit_forward_argBoolean

AST compatibility attribute; arguments forwarding initially didn’t have support for leading arguments (i.e. ‘def m(a, …); end` was a syntax error). However, Ruby 3.0 added support for any number of arguments in front of the `…`.

If set to false (the default):

1. `def m(...) end` is emitted as
   s(:def, :m, s(:forward_args), nil)
2. `def m(a, b, ...) end` is emitted as
   s(:def, :m,
     s(:args, s(:arg, :a), s(:arg, :b), s(:forward_arg)))

If set to true it uses a single format:

1. `def m(...) end` is emitted as
   s(:def, :m, s(:args, s(:forward_arg)))
2. `def m(a, b, ...) end` is emitted as
   s(:def, :m, s(:args, s(:arg, :a), s(:arg, :b), s(:forward_arg)))

It does’t matter that much on 2.7 (because there can’t be any leading arguments), but on 3.0 it should be better enabled to use a single AST format.

Returns:

  • (Boolean)


126
127
128
# File 'lib/parser/builders/default.rb', line 126

def emit_forward_arg
  @emit_forward_arg
end

.emit_indexBoolean

AST compatibility attribute; indexed assignment, ‘x[] = 1`, is not semantically equivalent to calling the method directly, `x.[]=(1)`. Specifically, in the former case, the expression’s value is always 1, and in the latter case, the expression’s value is the return value of the ‘[]=` method.

If set to false (the default), ‘self` is emitted as `s(:send, s(:self), :[], s(:int, 1))`, and `self = 2` is emitted as `s(:send, s(:self), :[]=, s(:int, 1), s(:int, 2))`.

If set to true, ‘self` is emitted as `s(:index, s(:self), s(:int, 1))`, and `self = 2` is emitted as `s(:indexasgn, s(:self), s(:int, 1), s(:int, 2))`.

Returns:

  • (Boolean)


80
81
82
# File 'lib/parser/builders/default.rb', line 80

def emit_index
  @emit_index
end

.emit_lambdaBoolean

AST compatibility attribute; since ‘-> {}` is not semantically equivalent to `lambda {}`, all new code should set this attribute to true.

If set to false (the default), ‘-> {}` is emitted as `s(:block, s(:send, nil, :lambda), s(:args), nil)`.

If set to true, ‘-> {}` is emitted as `s(:block, s(:lambda), s(:args), nil)`.

Returns:

  • (Boolean)


22
23
24
# File 'lib/parser/builders/default.rb', line 22

def emit_lambda
  @emit_lambda
end

.emit_procarg0Boolean

AST compatibility attribute; block arguments of ‘m { |a| }` are not semantically equivalent to block arguments of `m { |a,| }` or `m { |a, b| }`, all new code should set this attribute to true.

If set to false (the default), arguments of ‘m { |a| }` are emitted as `s(:args, s(:arg, :a))`.

If set to true, arguments of ‘m { |a| }` are emitted as `s(:args, s(:procarg0, :a)).

Returns:

  • (Boolean)


40
41
42
# File 'lib/parser/builders/default.rb', line 40

def emit_procarg0
  @emit_procarg0
end

Instance Attribute Details

#emit_file_line_as_literalsBoolean

If set to true (the default), ‘__FILE__` and `__LINE__` are transformed to literal nodes. For example, `s(:str, “lib/foo.rb”)` and `s(:int, 10)`.

If set to false, ‘__FILE__` and `__LINE__` are emitted as-is, i.e. as `s(:__FILE__)` and `s(:__LINE__)` nodes.

Source maps are identical in both cases.

Returns:

  • (Boolean)


158
159
160
# File 'lib/parser/builders/default.rb', line 158

def emit_file_line_as_literals
  @emit_file_line_as_literals
end

#parserObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



146
147
148
# File 'lib/parser/builders/default.rb', line 146

def parser
  @parser
end

Class Method Details

.modernizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



134
135
136
137
138
139
140
141
# File 'lib/parser/builders/default.rb', line 134

def modernize
  @emit_lambda = true
  @emit_procarg0 = true
  @emit_encoding = true
  @emit_index = true
  @emit_arg_inside_procarg0 = true
  @emit_forward_arg = true
end