steadfast 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65edd414b2b9a53a27c9965da47829809b1abdfc7e3cd2f9dfb26e9e155976b6
4
- data.tar.gz: 6560196ff027bb0c1d7a669ce7f80c1af4e4d6419792bd872a637594f39eee74
3
+ metadata.gz: 8cb638379609c138ddd9d582bc651c4c89e1ea5109687ff1455fe47a5c22995c
4
+ data.tar.gz: 869bf5c02b66b1ecfcc5cfabb2f506cd9d8e3ee1ccb6f2e96304e4d9725cddbb
5
5
  SHA512:
6
- metadata.gz: 01f35b02170b51dbb77f363b65d2ac812fae08c66796f8754d6a491c5357114f418ac6baccbf9646c912885f9f9ef0e23d483641f4cf63e653fc16ebcabdb4df
7
- data.tar.gz: dd8a6449757d1b35bfeb5005ff2b5209b2873c869ee7425f5caf36c5b368e352d7fc364f71ef64db5962698a4a547832aeda7a21b2e2070c9279ad04601f84fd
6
+ metadata.gz: 71bfa853f0cc05c979e2abb8918167c99bde44e0b181d5347a0719ceec7551597393ae02eaed37b73a1fb2c342ac10a9778d35c664c2476c24ab580a7a13ba54
7
+ data.tar.gz: 5e5cefe1d89228fa9518456a1df81903157e472522b73d3cebad05d309dfbebccb56304f46c122921d7341bd9f3ac046177a791bf5a65f01a416b7bb213206d8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ # 0.6.0 2026-11-27
2
+
3
+ - Add support for emit-nothing with `unquote(:__nop__)`
4
+ - Refactor unquote code, fix rewriting of nested quote expressions
5
+ - Sourcifier: Add support for missing node types (thanks @joeldrapper !)
6
+ - Sourcifier: fix `HashPatternNode` with rest
7
+
8
+ # 0.5.0 2026-11-26
9
+
10
+ - Add examples
11
+ - Sourcifier: fix source formatting for `ForwardingSuperNode`, `ImaginaryNode`, `ProgramNode`
12
+ - Add support for AlternationPatternNode
13
+ - Add `Steadfast.quote` - Include `Steadfast::Quote` methods in `Steadfast`
14
+ - `Steadfast.rewrite` - raise on invalid AST
15
+
1
16
  # 0.4.0 2026-11-25
2
17
 
3
18
  - Improve sourcifier output
data/README.md CHANGED
@@ -21,4 +21,206 @@
21
21
  <img src="./steadfast.png">
22
22
  </p>
23
23
 
24
- More information coming soon...
24
+ Steadfast is a gem for working with Ruby [Prism](https://github.com/ruby/prism/)
25
+ [ASTs](https://en.wikipedia.org/wiki/Abstract_syntax_tree). Steadfast provides a
26
+ convenient, rich API for generating and transforming ASTs.
27
+
28
+ ## Features
29
+
30
+ - Convert any proc or method to AST, convert any AST to source code.
31
+ - Rewrite, transform, compose and take apart ASTs.
32
+ - Comprehensive set of tools for creating synthetic AST nodes.
33
+ - Code quoting and templating using quote / unquote.
34
+ - Partial evaluation / specialization.
35
+ - Additional tools for iterating over ASTs and finding nodes.
36
+
37
+ ## Requirements
38
+
39
+ Steadfast requires Ruby 4.1 or newer.
40
+
41
+ ## Getting Started
42
+
43
+ Steadfast provides a variety of tools for working with ASTs. Here are the main
44
+ ones:
45
+
46
+ - `Steadfast.to_ast` - used to get the AST of a proc or a method.
47
+ - `Steadfast.rewrite` - used to create modified copies of ASTs.
48
+ - `Steadfast.quote` and `Steadfast.unquote` - used to generate ASTs and create
49
+ code templates.
50
+ - `Steadfast.eval_ast` - used to evaluate ASTs.
51
+
52
+ In the following example, we'll take a lambda that returns an integer, extract
53
+ its AST, and modify it to return an interpolated string instead.
54
+
55
+ ```ruby
56
+ require 'steadfast'
57
+
58
+ l = -> { 42 }
59
+ ast = Steadfast.to_ast(l)
60
+
61
+ msg = 'Hello'
62
+ ast2 = Steadfast.rewrite(ast) do |n|
63
+ next n if !(n in Ruby::Prism::IntegerNode(value: 42))
64
+
65
+ # Inject the value of a local variable into the string
66
+ Steadfast.quote(binding) { "#{unquote(msg)} from Steadfast!" }
67
+ end
68
+
69
+ l2 = Steadfast.eval_ast(ast2)
70
+ l2.() #=> "Hello from Steadfast!"
71
+ ```
72
+
73
+ ## Rewriting ASTs
74
+
75
+ The `Steadfast.rewrite` method is a general-purpose tool for modifying ASTs.
76
+ Since Prism ASTs are immutable, `Steadfast.rewrite` lets you modify an AST by
77
+ creating a copy of it with the requisite node replacements. You can replace
78
+ nodes by providing a hash with the replacements to be made. For example:
79
+
80
+ ```ruby
81
+ ast = Steadfast.to_ast(->() { foo() })
82
+ call_node = Steadfast.find(ast) { n in Ruby::Prism::CallNode(name: :foo)) }
83
+
84
+ # replace foo call with bar call
85
+ ast2 = Steadfast.rewrite(ast, call_node => quote { bar() })
86
+ ```
87
+
88
+ Another way to use Steadfast.rewrite is by providing a block that will be
89
+ invoked for each node in the AST. The block should return eith the node passed
90
+ to it, or a replacement node if it should be replaced. For example:
91
+
92
+ ```ruby
93
+ ast = Steadfast.to_ast(->() { foo() })
94
+
95
+ # replace foo call with bar call
96
+ ast2 = Steadfast.rewrite(ast) { |n|
97
+ # use next to allow the iteration to continue
98
+ next n if !(n in Ruby::Prism::CallNode(name: :foo))
99
+
100
+ Steadfast.quote { bar() }
101
+ }
102
+ ```
103
+
104
+ ### Recursive rewriting
105
+
106
+ When using the block form of `Steadfast.rewrite`, if a block returns a
107
+ replacement node, it will not be iterated on. In order to allow recursive
108
+ processing of replacement nodes, `Steadfast.rewrite` provides the block with a
109
+ reference to itself, as a block argument, which you can use to make recursive
110
+ calls to `Steadfast.rewrite`:
111
+
112
+ ```ruby
113
+ Steadfast.rewrite(ast) { |node, &block|
114
+ if (replacement = should_replace_node?(node))
115
+ Steadfast.rewrite(replacement, &block)
116
+ else
117
+ node
118
+ end
119
+ }
120
+ ```
121
+
122
+ ## Code Quoting and Templating
123
+
124
+ Steadfast provides a quote / unquote mechanism similar to that found in
125
+ languages such as Elixir or Lisp, to facilitate code generation and code
126
+ templating.
127
+
128
+ `Steadfast.quote` is used to quote code, returning an AST for the code block
129
+ provided to it:
130
+
131
+ ```ruby
132
+ Steadfast.quote(binding) { 42 } #=> IntegerNode
133
+ Steadfast.quote(binding) { foo(42) } #=> CallNode
134
+ Steadfast.quote(binding) { -> { 42 } } #=> LambdaNode
135
+ Steadfast.quote(binding) { foo; bar } #=> StatementsNode
136
+ ```
137
+
138
+ `Steadfast.unquote` is used to convert a literal value into an AST. Normally, it
139
+ is used inside a `quote` block in order to inject the result of an expression or
140
+ block. The `unquote` call lets you inject arbitrary values into the quoted code:
141
+
142
+ ```ruby
143
+ foo = 42
144
+ Steadfast.quote(binding) { x + unquote(foo) } #=> x + 42
145
+ ```
146
+
147
+ ## Creating Nodes
148
+
149
+ The easiest way to create new AST nodes is by using `Steadfast.quote`, as
150
+ explained above. Literal values can also be created using `Steadfast.unquote`:
151
+
152
+ ```ruby
153
+ Steadfast.unquote({ foo: 1, bar: 2 })
154
+ ```
155
+
156
+ Arbitrary types of nodescan be created by using `Steadfast.make_node` and
157
+ providing a string containing Ruby syntax:
158
+
159
+ ```ruby
160
+ Steadfast.make_node('foo.bar(:baz)') #=> CallNode
161
+ ```
162
+
163
+ The `Steadfast.make_node` method takes the following optional arguments:
164
+
165
+ - `location`: a Prism node or Prism location which will be used as the location
166
+ for the generated AST.
167
+ - `lvars`: an array containing local variable names used in the given code
168
+ snippet.
169
+ - `binding`: a binding for determining local variable names used in the given
170
+ code snippet.
171
+
172
+ In addition to `Steadfast.make_node`, additional methods allow the creation of
173
+ specific node types:
174
+
175
+ - `Steadfast.make_call(name:, receiver:, args:)` - create a call node.
176
+ - `Steadfast.make_def(name:, params:, :body)` - create a method definition.
177
+
178
+ ## Traversing ASTs and Finding Nodes
179
+
180
+ Use `Steadfast.walk` to visit each node in an AST:
181
+
182
+ ```ruby
183
+ Steadfast.walk(ast) { p it } #=> print out each node in the AST
184
+ ```
185
+
186
+ To find a node, use `Steadfast.find`. This method will return the first node for
187
+ which the given block returns true, or nil if none found:
188
+
189
+ ```ruby
190
+ # look for a call to foo
191
+ Steadfast.find(ast) { n in Ruby::CallNode(receiver: nil, name: :foo) }
192
+ ```
193
+
194
+ To find all nodes that respond to specific criteria, use `Steadfast.find_all`:
195
+
196
+ ```ruby
197
+ # find all method calls without a receiver
198
+ Steadfast.find_all(ast) { n in Ruby::CallNode(receiver: nil) }
199
+ ```
200
+
201
+ ## AST Conversion and Evaluation
202
+
203
+ Additional Steadfast allow you to convert a method or proc to AST, to convert an
204
+ AST to source code, and to evaluate ASTs. To get the AST for a proc or method,
205
+ use `Steadfast.to_ast`:
206
+
207
+ ```ruby
208
+ def foo
209
+ 42
210
+ end
211
+
212
+ foo_ast = Steadfast.to_ast(method(:foo))
213
+ ```
214
+
215
+ To convert an AST to source code, use `Steadfast.to_source`:
216
+
217
+ ```ruby
218
+ Steadfast.to_source(foo_ast) #=> "def foo..."
219
+ ```
220
+
221
+ To evaluate an AST use `Steadfast.eval_ast`:
222
+
223
+ ```ruby
224
+ ast = quote { puts 'Hi!' }
225
+ Steadfast.eval_ast(ast) #=> 'Hi!!!'
226
+ ```
@@ -119,18 +119,23 @@ module Steadfast
119
119
  # @return [Ruby::Prism::Node]
120
120
  def __unquote_evaluate__(node, binding)
121
121
  if node.arguments && (node.arguments.arguments.size == 1) && !node.block
122
- arg = __unquote_rewrite_nested_quote__(node.arguments.arguments[0], binding)
123
- v = Steadfast.eval_ast(arg, nil, binding)
124
- __unquote_convert_value__(v, node)
122
+ # unquote(expr)
123
+ __unquote_evaluate_target_node__(node.arguments.arguments[0], binding, node)
125
124
  elsif node.block && !node.arguments
126
- ast = __unquote_rewrite_nested_quote__(node.block.body, binding)
127
- v = Steadfast.eval_ast(node.block.body, nil, binding)
128
- __unquote_convert_value__(v, node)
125
+ # unquote { expr }
126
+ __unquote_evaluate_target_node__(node.block.body, binding, node)
129
127
  else
128
+ # unquote(), unquote(expr) { expr }
130
129
  raise ArgumentError, "Invalid unquote arguments"
131
130
  end
132
131
  end
133
132
 
133
+ def __unquote_evaluate_target_node__(node, binding, location)
134
+ node2 = __unquote_rewrite_nested_quote__(node, binding)
135
+ v = Steadfast.eval_ast(node2, nil, binding)
136
+ __unquote_convert_value__(v, location)
137
+ end
138
+
134
139
  # Rewrites nested calls to quote inside an unquote block or expression.
135
140
  #
136
141
  # @param node [Ruby::Prism::Node]
@@ -156,6 +161,8 @@ module Steadfast
156
161
  # @return [Ruby::Prism::Node]
157
162
  def __unquote_convert_value__(value, location)
158
163
  case value
164
+ when :__nop__
165
+ nil
159
166
  when Integer, Float, String, Symbol, Range, Regexp, nil, true, false, Hash, Class, Module
160
167
  Steadfast.make_literal(value, location:)
161
168
  when Array
@@ -13,6 +13,8 @@ module Steadfast
13
13
  # @replacements [Hash] replacements
14
14
  # @return [Ruby::Prism::Node]
15
15
  def self.rewrite(ast, **replacements, &rewrite_block)
16
+ raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
17
+
16
18
  rewrite_block ||= ->(n) do
17
19
  if (v = replacements[n]) && v.is_a?(Hash)
18
20
  n.copy(**v)
@@ -15,102 +15,126 @@ module Steadfast
15
15
  # its visit plan. Nodes requiring special logic have normal visit_xxx_node
16
16
  # method below.
17
17
  VISIT_PLANS = {
18
- alias_global_variable: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
19
- alias_method: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
20
- and: [:left, :whitespace, :operator_loc, :whitespace, :right],
21
- assoc_splat: [:operator_loc, :value],
22
- back_reference_read: :emit_verbatim,
23
- block: [:opening_loc, :parameters, :newline, :body, :newline, :closing_loc],
24
- block_argument: [:operator_loc, :expression],
25
- block_local_variable: :emit_verbatim,
26
- block_parameter: [:operator_loc, :name_loc],
27
- break: [:keyword_loc, :whitespace, :arguments],
28
- call_target: [:receiver, :call_operator_loc, :message_loc],
29
- capture_pattern: [:value, :whitespace, :operator_loc, :whitespace, :target],
30
- class: [:class_keyword_loc, :whitespace, :constant_path, :whitespace,
31
- :inheritance_operator_loc, :whitespace, :superclass, :newline,
32
- :body, :newline, :end_keyword_loc],
33
- class_variable_read: :emit_verbatim,
34
- class_variable_target: :emit_verbatim,
35
- class_variable_write: [:name_loc, :operator_loc, :value],
36
- constant_path: [:parent, :delimiter_loc, :name_loc],
37
- constant_path_write: [:target, :operator_loc, :value],
38
- constant_read: :emit_verbatim,
39
- constant_write: [:name_loc, :operator_loc, :value],
40
- defined: [:keyword_loc, :lparen_loc, :value, :rparen_loc],
41
- else: [:else_keyword_loc, :newline, :statements],
42
- embedded_statements: [:opening_loc, :statements, :closing_loc],
43
- embedded_variable: [:operator_loc, :variable],
44
- ensure: [:ensure_keyword_loc, :newline, :statements],
45
- false: :emit_verbatim,
46
- flip_flop: [:left, :operator_loc, :right],
47
- float: :emit_verbatim,
48
- for: [:for_keyword_loc, :whitespace, :index, :whitespace, :in_keyword_loc,
49
- :whitespace, :collection, :whitespace, :do_keyword_loc, :newline,
50
- :statements, :newline, :end_keyword_loc],
51
- forwarding_arguments: :emit_verbatim,
52
- forwarding_parameter: :emit_verbatim,
53
- forwarding_super: :emit_verbatim,
54
- global_variable_read: :emit_verbatim,
55
- global_variable_target: :emit_verbatim,
56
- global_variable_write: [:name_loc, :operator_loc, :value],
57
- imaginary: :emit_verbatim,
58
- implicit: :nop,
59
- implicit_rest: :nop,
60
- in: [:in_loc, :whitespace, :pattern, :whitespace, :then_loc],
61
- index_target: [:receiver, :opening_loc, :arguments, :closing_loc],
18
+ alias_global_variable: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
19
+ alias_method: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
20
+ alternation_pattern: [:left, :whitespace, :operator_loc, :whitespace, :right],
21
+ and: [:left, :whitespace, :operator_loc, :whitespace, :right],
22
+ assoc_splat: [:operator_loc, :value],
23
+ back_reference_read: :emit_verbatim,
24
+ block: [:opening_loc, :parameters, :newline, :body, :newline, :closing_loc],
25
+ block_argument: [:operator_loc, :expression],
26
+ block_local_variable: :emit_verbatim,
27
+ block_parameter: [:operator_loc, :name_loc],
28
+ break: [:keyword_loc, :whitespace, :arguments],
29
+ call_and_write: [:receiver, :call_operator_loc, :message_loc, :whitespace, :operator_loc, :whitespace, :value],
30
+ call_or_write: [:receiver, :call_operator_loc, :message_loc, :whitespace, :operator_loc, :whitespace, :value],
31
+ call_target: [:receiver, :call_operator_loc, :message_loc],
32
+ capture_pattern: [:value, :whitespace, :operator_loc, :whitespace, :target],
33
+ class: [
34
+ :class_keyword_loc, :whitespace, :constant_path, :whitespace,
35
+ :inheritance_operator_loc, :whitespace, :superclass, :newline,
36
+ :body, :newline, :end_keyword_loc
37
+ ],
38
+ class_variable_and_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
39
+ class_variable_operator_write: [:name_loc, :whitespace, :binary_operator_loc, :value],
40
+ class_variable_or_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
41
+ class_variable_read: :emit_verbatim,
42
+ class_variable_target: :emit_verbatim,
43
+ class_variable_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
44
+ constant_and_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
45
+ constant_operator_write: [:name_loc, :whitespace, :binary_operator_loc, :whitespace, :value],
46
+ constant_or_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
47
+ constant_path: [:parent, :delimiter_loc, :name_loc],
48
+ constant_path_and_write: [:target, :whitespace, :operator_loc, :whitespace, :value],
49
+ constant_path_operator_write: [:target, :whitespace, :binary_operator_loc, :whitespace, :value],
50
+ constant_path_or_write: [:target, :whitespace, :operator_loc, :whitespace, :value],
51
+ constant_path_target: [:parent, :delimiter_loc, :name_loc],
52
+ constant_path_write: [:target, :whitespace, :operator_loc, :whitespace, :value],
53
+ constant_read: :emit_verbatim,
54
+ constant_target: :emit_verbatim,
55
+ constant_write: [:name_loc, :operator_loc, :value],
56
+ defined: [:keyword_loc, :lparen_loc, :value, :rparen_loc],
57
+ else: [:else_keyword_loc, :newline, :statements],
58
+ embedded_statements: [:opening_loc, :statements, :closing_loc],
59
+ embedded_variable: [:operator_loc, :variable],
60
+ ensure: [:ensure_keyword_loc, :newline, :statements],
61
+ false: :emit_verbatim,
62
+ flip_flop: [:left, :operator_loc, :right],
63
+ float: :emit_verbatim,
64
+ for: [
65
+ :for_keyword_loc, :whitespace, :index, :whitespace, :in_keyword_loc,
66
+ :whitespace, :collection, :whitespace, :do_keyword_loc, :newline,
67
+ :statements, :newline, :end_keyword_loc
68
+ ],
69
+ forwarding_arguments: :emit_verbatim,
70
+ forwarding_parameter: :emit_verbatim,
71
+ forwarding_super: [:keyword_loc, :block],
72
+ global_variable_and_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
73
+ global_variable_operator_write: [:name_loc, :whitespace, :binary_operator_loc, :whitespace, :value],
74
+ global_variable_or_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
75
+ global_variable_read: :emit_verbatim,
76
+ global_variable_target: :emit_verbatim,
77
+ global_variable_write: [:name_loc, :operator_loc, :value],
78
+ implicit: :nop,
79
+ implicit_rest: :nop,
80
+ in: [:in_loc, :whitespace, :pattern, :whitespace, :then_loc],
81
+ index_target: [:receiver, :opening_loc, :arguments, :closing_loc],
82
+ instance_variable_and_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
62
83
  instance_variable_operator_write: [:name_loc, :whitespace, :binary_operator_loc, :whitespace, :value],
63
- instance_variable_or_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
64
- instance_variable_read: :emit_verbatim,
65
- instance_variable_target: :emit_verbatim,
66
- instance_variable_write: [:name_loc, :operator_loc, :value],
67
- integer: :emit_verbatim,
68
- it_parameters: :nop,
69
- it_local_variable_read: :emit_verbatim,
70
- keyword_rest_parameter: [:operator_loc, :name_loc],
71
- keyword_parameter: :emit_verbatim,
72
- lambda: [:operator_loc, :parameters, :whitespace, :opening_loc, :newline, :body, :newline, :closing_loc],
73
- local_variable_and_write: [:name_loc, :operator_loc, :value],
74
- local_variable_operator_write: [:name_loc, :binary_operator_loc, :value],
75
- local_variable_or_write: [:name_loc, :operator_loc, :value],
76
- local_variable_read: :emit_verbatim,
77
- local_variable_target: :emit_verbatim,
78
- local_variable_write: [:name_loc, :operator_loc, :value],
79
- match_predicate: [:value, :whitespace, :operator_loc, :whitespace, :pattern],
80
- match_required: [:value, :whitespace, :operator_loc, :whitespace, :pattern],
81
- match_write: [:call],
82
- module: [:module_keyword_loc, :whitespace, :constant_path, :newline, :body, :newline, :end_keyword_loc],
83
- next: [:keyword_loc, :whitespace, :arguments],
84
- nil: :emit_verbatim,
85
- no_keywords_parameter: :emit_verbatim,
86
- numbered_parameters: :nop,
87
- optional_parameter: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
88
- optional_keyword_parameter: [:name_loc, :whitespace, :value],
89
- or: [:left, :whitespace, :operator_loc, :whitespace, :right],
90
- parentheses: [:opening_loc, :body, :closing_loc],
91
- program: :emit_verbatim,
92
- pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
93
- pinned_variable: [:operator_loc, :variable],
94
- rational: :emit_verbatim,
95
- redo: :emit_verbatim,
96
- regular_expression: :emit_verbatim,
97
- required_parameter: :emit_verbatim,
98
- required_keyword_parameter: :emit_verbatim,
99
- rescue_modifier: [:expression, :whitespace, :keyword_loc, :whitespace, :rescue_expression],
100
- rest_parameter: [:operator_loc, :name_loc],
101
- retry: :emit_verbatim,
102
- return: [:keyword_loc, :whitespace, :arguments],
103
- self: :emit_verbatim,
104
- singleton_class: [:class_keyword_loc, :whitespace, :operator_loc, :whitespace, :expression,
105
- :newline, :body, :newline, :end_keyword_loc],
106
- source_encoding: :emit_verbatim,
107
- source_file: :emit_verbatim,
108
- source_line: :emit_verbatim,
109
- splat: [:operator_loc, :expression],
110
- symbol: :emit_verbatim,
111
- true: :emit_verbatim,
112
- x_string: :emit_verbatim,
113
- yield: [:keyword_loc, :lparen_loc, :arguments, :rparen_loc]
84
+ instance_variable_or_write: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
85
+ instance_variable_read: :emit_verbatim,
86
+ instance_variable_target: :emit_verbatim,
87
+ instance_variable_write: [:name_loc, :operator_loc, :value],
88
+ integer: :emit_verbatim,
89
+ it_parameters: :nop,
90
+ it_local_variable_read: :emit_verbatim,
91
+ keyword_rest_parameter: [:operator_loc, :name_loc],
92
+ keyword_parameter: :emit_verbatim,
93
+ lambda: [:operator_loc, :parameters, :whitespace, :opening_loc, :newline, :body, :newline, :closing_loc],
94
+ local_variable_and_write: [:name_loc, :operator_loc, :value],
95
+ local_variable_operator_write: [:name_loc, :binary_operator_loc, :value],
96
+ local_variable_or_write: [:name_loc, :operator_loc, :value],
97
+ local_variable_read: :emit_verbatim,
98
+ local_variable_target: :emit_verbatim,
99
+ local_variable_write: [:name_loc, :operator_loc, :value],
100
+ match_predicate: [:value, :whitespace, :operator_loc, :whitespace, :pattern],
101
+ match_required: [:value, :whitespace, :operator_loc, :whitespace, :pattern],
102
+ match_write: [:call],
103
+ module: [:module_keyword_loc, :whitespace, :constant_path, :newline, :body, :newline, :end_keyword_loc],
104
+ next: [:keyword_loc, :whitespace, :arguments],
105
+ nil: :emit_verbatim,
106
+ no_keywords_parameter: :emit_verbatim,
107
+ numbered_parameters: :nop,
108
+ numbered_reference_read: :emit_verbatim,
109
+ optional_parameter: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
110
+ optional_keyword_parameter: [:name_loc, :whitespace, :value],
111
+ or: [:left, :whitespace, :operator_loc, :whitespace, :right],
112
+ parentheses: [:opening_loc, :body, :closing_loc],
113
+ program: [:statements],
114
+ pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
115
+ pinned_variable: [:operator_loc, :variable],
116
+ rational: :emit_verbatim,
117
+ redo: :emit_verbatim,
118
+ regular_expression: :emit_verbatim,
119
+ required_parameter: :emit_verbatim,
120
+ required_keyword_parameter: :emit_verbatim,
121
+ rescue_modifier: [:expression, :whitespace, :keyword_loc, :whitespace, :rescue_expression],
122
+ rest_parameter: [:operator_loc, :name_loc],
123
+ retry: :emit_verbatim,
124
+ return: [:keyword_loc, :whitespace, :arguments],
125
+ self: :emit_verbatim,
126
+ singleton_class: [
127
+ :class_keyword_loc, :whitespace, :operator_loc, :whitespace, :expression,
128
+ :newline, :body, :newline, :end_keyword_loc
129
+ ],
130
+ source_encoding: :emit_verbatim,
131
+ source_file: :emit_verbatim,
132
+ source_line: :emit_verbatim,
133
+ splat: [:operator_loc, :expression],
134
+ symbol: :emit_verbatim,
135
+ true: :emit_verbatim,
136
+ x_string: :emit_verbatim,
137
+ yield: [:keyword_loc, :lparen_loc, :arguments, :rparen_loc]
114
138
  }
115
139
 
116
140
  attr_reader :buffer
@@ -322,6 +346,10 @@ module Steadfast
322
346
  emit_code(node.constant)
323
347
  emit_code(node.opening_loc)
324
348
  visit_comma_separated_nodes(node.elements)
349
+ if node.elements && node.elements.size > 0 && node.rest
350
+ emit_comma
351
+ end
352
+ emit_code(node.rest)
325
353
  emit_code(node.closing_loc)
326
354
  end
327
355
 
@@ -360,22 +388,10 @@ module Steadfast
360
388
  emit_code(node.subsequent)
361
389
  end
362
390
 
363
- def visit_interpolated_symbol_node(node)
364
- heredoc = node.opening_loc && node.opening_loc.slice =~ /\<\</
365
- emit_code(node.opening_loc)
366
- emit_newline if heredoc
367
- last_part = nil
368
- node.parts.each { |p|
369
- if heredoc && last_part && p.location.start_line != last_part.location.start_line
370
- emit_newline
371
- end
372
- emit_code(p)
373
- last_part = p
374
- }
375
- emit_newline if heredoc
376
- emit_code(node.closing_loc)
391
+ def visit_imaginary_node(node)
392
+ emit_code(node.numeric)
393
+ emit('i')
377
394
  end
378
- alias_method :visit_interpolated_string_node, :visit_interpolated_symbol_node
379
395
 
380
396
  def visit_index_operator_write_node(node)
381
397
  emit_code(node.receiver)
@@ -412,12 +428,35 @@ module Steadfast
412
428
  end
413
429
  alias_method :visit_index_and_write_node, :visit_index_or_write_node
414
430
 
431
+ def visit_interpolated_match_last_line_node(node)
432
+ emit_code(node.opening_loc)
433
+ node.parts.each { |p| emit_code(p) }
434
+ emit_code(node.closing_loc)
435
+ end
436
+
415
437
  def visit_interpolated_regular_expression_node(node)
416
438
  emit_code(node.opening_loc)
417
439
  node.parts.each { |p| emit_code(p) }
418
440
  emit_code(node.closing_loc)
419
441
  end
420
442
 
443
+ def visit_interpolated_symbol_node(node)
444
+ heredoc = node.opening_loc && node.opening_loc.slice =~ /\<\</
445
+ emit_code(node.opening_loc)
446
+ emit_newline if heredoc
447
+ last_part = nil
448
+ node.parts.each { |p|
449
+ if heredoc && last_part && p.location.start_line != last_part.location.start_line
450
+ emit_newline
451
+ end
452
+ emit_code(p)
453
+ last_part = p
454
+ }
455
+ emit_newline if heredoc
456
+ emit_code(node.closing_loc)
457
+ end
458
+ alias_method :visit_interpolated_string_node, :visit_interpolated_symbol_node
459
+
421
460
  def visit_interpolated_x_string_node(node)
422
461
  emit_code(node.opening_loc)
423
462
  node.parts.each { |p| emit_code(p) }
@@ -502,6 +541,11 @@ module Steadfast
502
541
  emit_code(node.subsequent)
503
542
  end
504
543
 
544
+ def visit_shareable_constant_node(node)
545
+ emit("# shareable_constant_value: literal\n")
546
+ emit_code(node.write)
547
+ end
548
+
505
549
  def visit_statements_node(node)
506
550
  first = true
507
551
  node.body&.each do |n|
@@ -672,6 +716,16 @@ module Steadfast
672
716
  # @param node [Ruby::Prism::Node]
673
717
  # @return [void]
674
718
  def emit_verbatim(node)
719
+ if node.child_nodes.size > 0
720
+ puts
721
+ puts '*' * 40
722
+ pp node
723
+ puts
724
+ pp node.child_nodes
725
+ puts
726
+ puts caller.join("\n")
727
+ exit!
728
+ end
675
729
  emit_code(node.location)
676
730
  end
677
731
 
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.4.0'
3
+ VERSION = '0.6.0'
4
4
  end
data/lib/steadfast.rb CHANGED
@@ -31,6 +31,7 @@ module Steadfast
31
31
 
32
32
  class << self
33
33
  include Steadfast::Make
34
+ include Steadfast::Quote
34
35
 
35
36
  # Returns an AST for the given object.
36
37
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steadfast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner