steadfast 0.5.0 → 0.7.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: 3856e55251f5e48be25a1cb9f03df36743ab36e8bfa18068b33045a977205cfc
4
- data.tar.gz: f04e002ad7a9b54e3f80f848a82cbb8efd0fd71980b7066e581473a5512b4b9a
3
+ metadata.gz: 5fcb668c3dbb5ba803c1a4d0767e565ab17fd7d67aeacc0a9ae7f8ed38db2d2e
4
+ data.tar.gz: 7ef9e7c64f361b92ecc09354fa56c9e1aedae010bb5858bace103bde9ef7f66c
5
5
  SHA512:
6
- metadata.gz: f31437cf5dd75e56eae0342d6055c50fcd82726130b22b07cbababa4dd45f7955b03497ca5ac572747a101d9bf6e5aced9b49c8c21c55bae9bf73e7c1e2b6f1c
7
- data.tar.gz: 2cc985fff855606d568445392bbaf289eb12d0876bd6879dcad7a6d01686aad46f883e0c981254b686fabeddc4d9222b4358145f733e9ae69ccd572c96547cc2
6
+ metadata.gz: 351611184a6b8d714eb620f1d9c005d9602a39ef4668bee96bfb1b3a2135c07a8301a8a22967fa6dc6c7a4fb9571c445fb4a07d483057bb35960f3733a76a1a0
7
+ data.tar.gz: c50b2765311f0fc7ecb705992b2f92dfa23cd5ac09b1054c2df37571db127a6c94de9653d2441489eb4491ab63d9f08624c3fe08a0b57be066b72258f996733d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # 0.7.0 2026-11-28
2
+
3
+ - Add support for automatic backtrace translation in `eval_ast`
4
+ - Streamline `eval_ast` options, add support for class_eval
5
+ - Move core methods to `Steadfast::AST` module
6
+ - Add support for `PreExecutionNode`, `PostExecutionNode`
7
+
8
+ # 0.6.0 2026-11-27
9
+
10
+ - Add support for emit-nothing with `unquote(:__nop__)`
11
+ - Refactor unquote code, fix rewriting of nested quote expressions
12
+ - Sourcifier: Add support for missing node types (thanks @joeldrapper !)
13
+ - Sourcifier: fix `HashPatternNode` with rest
14
+
1
15
  # 0.5.0 2026-11-26
2
16
 
3
17
  - Add examples
data/README.md CHANGED
@@ -96,7 +96,7 @@ ast = Steadfast.to_ast(->() { foo() })
96
96
  ast2 = Steadfast.rewrite(ast) { |n|
97
97
  # use next to allow the iteration to continue
98
98
  next n if !(n in Ruby::Prism::CallNode(name: :foo))
99
-
99
+
100
100
  Steadfast.quote { bar() }
101
101
  }
102
102
  ```
@@ -0,0 +1,260 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steadfast
4
+ module AST
5
+ # Returns an AST for the given object.
6
+ #
7
+ # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
8
+ # @return [Ruby::Prism::Node]
9
+ def to_ast(obj)
10
+ case obj
11
+ when Proc, Method, UnboundMethod
12
+ ast = obj.syntax_tree
13
+ raise Steadfast::Error, "AST not found for given object" if !ast
14
+
15
+ set_source_filename(ast, obj)
16
+ ast
17
+ when Ruby::Prism::Node
18
+ obj
19
+ else
20
+ raise ArgumentError, "Invalid object type"
21
+ end
22
+ end
23
+
24
+ # Returns the source code to the given object by sourcifying its AST.
25
+ #
26
+ # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
27
+ # @return [String]
28
+ def to_source(obj, **opts)
29
+ ast = to_ast(obj)
30
+ Sourcifier.new(**opts).to_source(ast)
31
+ end
32
+
33
+ # Rewrites the given AST using the given hash of node replacements or a
34
+ # rewrite block. Returns either the original AST, or a copy with changes.
35
+ #
36
+ # @param ast [Ruby::Prism::Node]
37
+ # @param replacements [Hash]
38
+ # @return [Ruby::Prism::Node]
39
+ def rewrite(ast, **replacements, &block)
40
+ Rewriter.rewrite(ast, **replacements, &block)
41
+ end
42
+
43
+ # Evaluates the given AST in the context of the given receiver, class or
44
+ # binding. If the receiver: option is given, an instance_eval is performed.
45
+ # If the class: option is given, a class_eval is performed. Otherwise, the
46
+ # given binding: is used for a normal eval. If no binding is given, no
47
+ # binding is passed to eval.
48
+ #
49
+ # @param ast [Ruby::Prism::Node]
50
+ # @param receiver: [any, nil] instance_eval receiver
51
+ # @param class: [Class, nil] class_eval receiver
52
+ # @param binding: [Binding, nil] binding
53
+ # @param translate_backtrace [true, false] auto translate exception backtraces
54
+ # @return [any]
55
+ def eval_ast(ast, source_location: nil, **opts)
56
+ raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
57
+
58
+ source_location = compute_eval_source_location(source_location)
59
+ ast = eval_ast_translate_backtrace(ast) if opts[:translate_backtrace]
60
+ src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
61
+ if (receiver = opts[:receiver])
62
+ receiver.instance_eval(src, *source_location)
63
+ elsif (receiver = opts[:class])
64
+ receiver.class_eval(src, *source_location)
65
+ else
66
+ eval(src, opts[:binding], *source_location)
67
+ end
68
+ end
69
+
70
+ # Visits the given AST, passing each node in the AST to the given block.
71
+ #
72
+ # @param ast [Ruby::Prism::Node]
73
+ # @return [void]
74
+ def walk(ast, &)
75
+ ast.accept(Visitor.new(&))
76
+ ast
77
+ end
78
+
79
+ # Visits the given AST, passing each node in the AST to the given block.
80
+ # Returns the first node for which the block returns true.
81
+ #
82
+ # @param ast [Ruby::Prism::Node]
83
+ # @return [Ruby::Prism::Node, nil]
84
+ def find(ast, &block)
85
+ ast.accept(Visitor.new { return it if block.(it) })
86
+ nil
87
+ end
88
+
89
+ # Visits the given AST, passing each node in the AST to the given block.
90
+ # Returns all nodes for which the block returns true.
91
+ #
92
+ # @param ast [Ruby::Prism::Node]
93
+ # @return [Array<Ruby::Prism::Node>]
94
+ def find_all(ast, &block)
95
+ nodes = []
96
+ ast.accept(Visitor.new { nodes << it if block.(it) })
97
+ nodes
98
+ end
99
+
100
+ private
101
+
102
+ # Sets the source filename for the given ast to the source location of the
103
+ # given proc or method.
104
+ #
105
+ # @param ast [Ruby::Prism::Node]
106
+ # @param obj [Proc, Method]
107
+ # @return [void]
108
+ def set_source_filename(ast, obj)
109
+ source = ast.send(:source)
110
+ source.__filename__ ||= obj.source_location.first
111
+ end
112
+
113
+ # Computes the source location (filename and line no) for the given
114
+ # source_location spec. If source_location is nil, computes a random
115
+ # filename. If source_location is caller, returns the location of the
116
+ # callsite to eval_ast. Otherwise returns the given source location, with
117
+ # the lineno decremented (in order to compensate for the
118
+ # frozen_string_literal comment emitted before the code.
119
+ #
120
+ # @param source_location [Array, nil, :caller]
121
+ # @return [Array] [filename, lineno]
122
+ def compute_eval_source_location(source_location)
123
+ case source_location
124
+ in nil
125
+ # Important: ideally we'd set the source location line no to 0 (to
126
+ # compensate for the #frozen_string_literal comment at the top).
127
+ # However, while `eval` respects a starting lineno set to 0, Prism does
128
+ # not, which can lead to off-by-one lineno in exception backtraces for
129
+ # synthetic ASTs.
130
+ ["(steadfast-#{SecureRandom.hex(4)})", 1]
131
+ in :caller
132
+ # Wwhen source_location is not nil, we do decrement the start lineno.
133
+ # Hopefully some day this wart will be removed from Ruby.G
134
+ l = caller_locations[1]
135
+ [l.path, l.lineno - 1]
136
+ in [String => filename, Integer => lineno]
137
+ [filename, lineno - 1]
138
+ else
139
+ raise ArgumentError, "Invalid source_location given"
140
+ end
141
+ end
142
+
143
+ # Translates the given AST by inserting a backtrace translation rescue
144
+ # block. All method and lambda definitions will be rewritten to include
145
+ # backtrace translation, as well as the top-level node.
146
+ #
147
+ # @param ast [Ruby::Prism::Node]
148
+ # @return [Ruby::Prism::Node]
149
+ def eval_ast_translate_backtrace(ast)
150
+ ast = Steadfast.rewrite(ast) do |n|
151
+ case n
152
+ when Ruby::Prism::DefNode, Ruby::Prism::LambdaNode
153
+ Steadfast.rewrite(n, n.body => eval_ast_wrapper_ast(n.body))
154
+ else
155
+ n
156
+ end
157
+ end
158
+
159
+ case ast
160
+ when Ruby::Prism::DefNode, Ruby::Prism::LambdaNode
161
+ ast
162
+ else
163
+ eval_ast_wrapper_ast(ast)
164
+ end
165
+ end
166
+
167
+ # Returns an AST wrapping the given statements with a rescue block that
168
+ # translates an unhandled exception's backtrace for source mapping purposes.
169
+ # If the given node is anything other than a StatementsNode, it is wrapped
170
+ # in a StatementsNode.
171
+ #
172
+ # @param statements [Ruby::Prism::Node]
173
+ # @return [Ruby::Prism::Node]
174
+ def eval_ast_wrapper_ast(statements)
175
+ if !statements.is_a?(Ruby::Prism::StatementsNode)
176
+ statements = Steadfast.make_statements([statements])
177
+ end
178
+ Steadfast.quote(binding) {
179
+ begin
180
+ unquote(statements)
181
+ rescue Exception => e
182
+ e.set_backtrace(Steadfast::SourceMapping.translate_backtrace(e.backtrace))
183
+ raise
184
+ end
185
+ }
186
+ end
187
+
188
+ # Finds the AST for the given proc. This method is currently not in use, but
189
+ # we may eventually use this code in order to provide backward compatibility
190
+ # for Ruby < 4.1.
191
+ #
192
+ # @param proc [Proc]
193
+ # @return [Ruby::Prism::Node]
194
+ def proc_ast(proc)
195
+ fn, lineno = proc.source_location
196
+ pr = Prism.parse(get_source(fn), filepath: fn)
197
+ program = pr.value
198
+
199
+ Finder.find(program, proc) do
200
+ on(:lambda) do |node|
201
+ found!(node) if node.location.start_line == lineno
202
+ super(node)
203
+ end
204
+ on(:block) do |node|
205
+ found!(node) if node.location.start_line == lineno
206
+ super(node)
207
+ end
208
+ on(:call) do |node|
209
+ case node.name
210
+ when :proc, :lambda
211
+ found!(node) if node.block && node.block.location.start_line == lineno
212
+ end
213
+ super(node)
214
+ end
215
+ end
216
+ rescue Errno::ENOENT
217
+ raise Sirop::Error, "Could not get source for proc"
218
+ end
219
+
220
+ # Finds the AST for the given method. This method is currently not in use,
221
+ # but we may eventually use this code in order to provide backward
222
+ # compatibility for Ruby < 4.1.
223
+ #
224
+ # @param method [Method, UnboundMethod]
225
+ # @return [Ruby::Prism::Node]
226
+ def method_ast(method)
227
+ fn, lineno = method.source_location
228
+ pr = Prism.parse(get_source(fn), filepath: fn)
229
+ program = pr.value
230
+
231
+ Finder.find(program, method) do
232
+ on(:def) do |node|
233
+ found!(node) if node.name == method.name && node.location.start_line == lineno
234
+ super(node)
235
+ end
236
+ end
237
+ end
238
+
239
+ # Returns the content of the given filename. Returns the IRB line buffer if
240
+ # '(irb)' is given. This method is not currently used.
241
+ #
242
+ # @param fn [String]
243
+ # @return [String] source code
244
+ def get_source(fn)
245
+ return get_irb_source if fn == '(irb)'
246
+
247
+ IO.read(fn)
248
+ end
249
+
250
+ # Returns the IRB line buffer (the source code for the current IRB session.)
251
+ # This method is currently not used.
252
+ #
253
+ # @return [String] IRB line buffer
254
+ def get_irb_source
255
+ io = IRB.CurrentContext.io
256
+ last_line = io.instance_variable_get(:@line_no)
257
+ (1..last_line).map { io.line(it) }.join
258
+ end
259
+ end
260
+ end
@@ -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, 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
@@ -14,7 +14,7 @@ module Steadfast
14
14
  # @return [Ruby::Prism::Node]
15
15
  def self.rewrite(ast, **replacements, &rewrite_block)
16
16
  raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
17
-
17
+
18
18
  rewrite_block ||= ->(n) do
19
19
  if (v = replacements[n]) && v.is_a?(Hash)
20
20
  n.copy(**v)
@@ -15,102 +15,128 @@ 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
- 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_target: [:receiver, :call_operator_loc, :message_loc],
30
- capture_pattern: [:value, :whitespace, :operator_loc, :whitespace, :target],
31
- class: [:class_keyword_loc, :whitespace, :constant_path, :whitespace,
32
- :inheritance_operator_loc, :whitespace, :superclass, :newline,
33
- :body, :newline, :end_keyword_loc],
34
- class_variable_read: :emit_verbatim,
35
- class_variable_target: :emit_verbatim,
36
- class_variable_write: [:name_loc, :operator_loc, :value],
37
- constant_path: [:parent, :delimiter_loc, :name_loc],
38
- constant_path_write: [:target, :operator_loc, :value],
39
- constant_read: :emit_verbatim,
40
- constant_write: [:name_loc, :operator_loc, :value],
41
- defined: [:keyword_loc, :lparen_loc, :value, :rparen_loc],
42
- else: [:else_keyword_loc, :newline, :statements],
43
- embedded_statements: [:opening_loc, :statements, :closing_loc],
44
- embedded_variable: [:operator_loc, :variable],
45
- ensure: [:ensure_keyword_loc, :newline, :statements],
46
- false: :emit_verbatim,
47
- flip_flop: [:left, :operator_loc, :right],
48
- float: :emit_verbatim,
49
- for: [:for_keyword_loc, :whitespace, :index, :whitespace, :in_keyword_loc,
50
- :whitespace, :collection, :whitespace, :do_keyword_loc, :newline,
51
- :statements, :newline, :end_keyword_loc],
52
- forwarding_arguments: :emit_verbatim,
53
- forwarding_parameter: :emit_verbatim,
54
- forwarding_super: [:keyword_loc, :block],
55
- global_variable_read: :emit_verbatim,
56
- global_variable_target: :emit_verbatim,
57
- global_variable_write: [:name_loc, :operator_loc, :value],
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: [:statements],
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
+ post_execution: [:keyword_loc, :whitespace, :opening_loc, :newline, :statements, :newline, :closing_loc],
114
+ pre_execution: [:keyword_loc, :whitespace, :opening_loc, :newline, :statements, :newline, :closing_loc],
115
+ program: [:statements],
116
+ pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
117
+ pinned_variable: [:operator_loc, :variable],
118
+ rational: :emit_verbatim,
119
+ redo: :emit_verbatim,
120
+ regular_expression: :emit_verbatim,
121
+ required_parameter: :emit_verbatim,
122
+ required_keyword_parameter: :emit_verbatim,
123
+ rescue_modifier: [:expression, :whitespace, :keyword_loc, :whitespace, :rescue_expression],
124
+ rest_parameter: [:operator_loc, :name_loc],
125
+ retry: :emit_verbatim,
126
+ return: [:keyword_loc, :whitespace, :arguments],
127
+ self: :emit_verbatim,
128
+ singleton_class: [
129
+ :class_keyword_loc, :whitespace, :operator_loc, :whitespace, :expression,
130
+ :newline, :body, :newline, :end_keyword_loc
131
+ ],
132
+ source_encoding: :emit_verbatim,
133
+ source_file: :emit_verbatim,
134
+ source_line: :emit_verbatim,
135
+ splat: [:operator_loc, :expression],
136
+ symbol: :emit_verbatim,
137
+ true: :emit_verbatim,
138
+ x_string: :emit_verbatim,
139
+ yield: [:keyword_loc, :lparen_loc, :arguments, :rparen_loc]
114
140
  }
115
141
 
116
142
  attr_reader :buffer
@@ -322,6 +348,10 @@ module Steadfast
322
348
  emit_code(node.constant)
323
349
  emit_code(node.opening_loc)
324
350
  visit_comma_separated_nodes(node.elements)
351
+ if node.elements && node.elements.size > 0 && node.rest
352
+ emit_comma
353
+ end
354
+ emit_code(node.rest)
325
355
  emit_code(node.closing_loc)
326
356
  end
327
357
 
@@ -365,23 +395,6 @@ module Steadfast
365
395
  emit('i')
366
396
  end
367
397
 
368
- def visit_interpolated_symbol_node(node)
369
- heredoc = node.opening_loc && node.opening_loc.slice =~ /\<\</
370
- emit_code(node.opening_loc)
371
- emit_newline if heredoc
372
- last_part = nil
373
- node.parts.each { |p|
374
- if heredoc && last_part && p.location.start_line != last_part.location.start_line
375
- emit_newline
376
- end
377
- emit_code(p)
378
- last_part = p
379
- }
380
- emit_newline if heredoc
381
- emit_code(node.closing_loc)
382
- end
383
- alias_method :visit_interpolated_string_node, :visit_interpolated_symbol_node
384
-
385
398
  def visit_index_operator_write_node(node)
386
399
  emit_code(node.receiver)
387
400
  emit_code(node.opening_loc)
@@ -417,12 +430,35 @@ module Steadfast
417
430
  end
418
431
  alias_method :visit_index_and_write_node, :visit_index_or_write_node
419
432
 
433
+ def visit_interpolated_match_last_line_node(node)
434
+ emit_code(node.opening_loc)
435
+ node.parts.each { |p| emit_code(p) }
436
+ emit_code(node.closing_loc)
437
+ end
438
+
420
439
  def visit_interpolated_regular_expression_node(node)
421
440
  emit_code(node.opening_loc)
422
441
  node.parts.each { |p| emit_code(p) }
423
442
  emit_code(node.closing_loc)
424
443
  end
425
444
 
445
+ def visit_interpolated_symbol_node(node)
446
+ heredoc = node.opening_loc && node.opening_loc.slice =~ /\<\</
447
+ emit_code(node.opening_loc)
448
+ emit_newline if heredoc
449
+ last_part = nil
450
+ node.parts.each { |p|
451
+ if heredoc && last_part && p.location.start_line != last_part.location.start_line
452
+ emit_newline
453
+ end
454
+ emit_code(p)
455
+ last_part = p
456
+ }
457
+ emit_newline if heredoc
458
+ emit_code(node.closing_loc)
459
+ end
460
+ alias_method :visit_interpolated_string_node, :visit_interpolated_symbol_node
461
+
426
462
  def visit_interpolated_x_string_node(node)
427
463
  emit_code(node.opening_loc)
428
464
  node.parts.each { |p| emit_code(p) }
@@ -507,6 +543,11 @@ module Steadfast
507
543
  emit_code(node.subsequent)
508
544
  end
509
545
 
546
+ def visit_shareable_constant_node(node)
547
+ emit("# shareable_constant_value: literal\n")
548
+ emit_code(node.write)
549
+ end
550
+
510
551
  def visit_statements_node(node)
511
552
  first = true
512
553
  node.body&.each do |n|
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.5.0'
3
+ VERSION = '0.7.0'
4
4
  end
data/lib/steadfast.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'securerandom'
4
4
 
5
+ require 'steadfast/ast'
5
6
  require 'steadfast/source_mapping'
6
7
  require 'steadfast/sourcifier'
7
8
  require 'steadfast/visitor'
@@ -18,7 +19,6 @@ RubyVM.keep_script_lines = true
18
19
  module Steadfast
19
20
  # Extensions to Ruby::Prism::Source
20
21
  module PrismSourceExtensions
21
-
22
22
  # The __filename__ attribute lets us track the source location for a given
23
23
  # AST.
24
24
  attr_accessor :__filename__
@@ -30,194 +30,8 @@ module Steadfast
30
30
  end
31
31
 
32
32
  class << self
33
+ include Steadfast::AST
33
34
  include Steadfast::Make
34
35
  include Steadfast::Quote
35
-
36
- # Returns an AST for the given object.
37
- #
38
- # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
39
- # @return [Ruby::Prism::Node]
40
- def to_ast(obj)
41
- case obj
42
- when Proc, Method, UnboundMethod
43
- ast = obj.syntax_tree
44
- raise Steadfast::Error, "AST not found for given object" if !ast
45
-
46
- set_source_filename(ast, obj)
47
- ast
48
- when Ruby::Prism::Node
49
- obj
50
- else
51
- raise ArgumentError, "Invalid object type"
52
- end
53
- end
54
-
55
- # Returns the source code to the given object by sourcifying its AST.
56
- #
57
- # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
58
- # @return [String]
59
- def to_source(obj, **opts)
60
- ast = to_ast(obj)
61
- Sourcifier.new(**opts).to_source(ast)
62
- end
63
-
64
- # Rewrites the given AST using the given hash of node replacements or a
65
- # rewrite block. Returns either the original AST, or a copy with changes.
66
- #
67
- # @param ast [Ruby::Prism::Node]
68
- # @param replacements [Hash]
69
- # @return [Ruby::Prism::Node]
70
- def rewrite(ast, **replacements, &block)
71
- Rewriter.rewrite(ast, **replacements, &block)
72
- end
73
-
74
- # Evaluates the given AST in the context of the given receiver or binding.
75
- #
76
- # @param ast [Ruby::Prism::Node]
77
- # @param eval_receiver [any]
78
- # @param eval_binding [Binding, nil]
79
- # @return [any]
80
- def eval_ast(ast, eval_receiver = nil, eval_binding = nil, source_location: nil)
81
- raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
82
-
83
- case source_location
84
- when nil
85
- # Important: ideally we'd set the source location line no to 0 (to
86
- # compensate for the #frozen_string_literal comment at the top).
87
- # However, while `eval` respects a starting lineno set to 0, Prism does
88
- # not, which can lead to off-by-one line nos in exception backtraces for
89
- # synthetic ASTs.
90
- #
91
- # Note that when source_location is not nil, we do decrement the start
92
- # lineno. Hopefully some day this wart will be removed from Ruby.G
93
- source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
94
- when :caller
95
- l = caller_locations[0]
96
- source_location = [l.path, l.lineno - 1]
97
- else
98
- source_location = [source_location[0], source_location[1] - 1]
99
- end
100
- src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
101
- if eval_receiver
102
- eval_receiver.instance_eval(src, source_location[0], source_location[1])
103
- else
104
- eval(src, eval_binding || binding, source_location[0], source_location[1])
105
- end
106
- end
107
-
108
- # Visits the given AST, passing each node in the AST to the given block.
109
- #
110
- # @param ast [Ruby::Prism::Node]
111
- # @return [void]
112
- def walk(ast, &)
113
- ast.accept(Visitor.new(&))
114
- ast
115
- end
116
-
117
- # Visits the given AST, passing each node in the AST to the given block.
118
- # Returns the first node for which the block returns true.
119
- #
120
- # @param ast [Ruby::Prism::Node]
121
- # @return [Ruby::Prism::Node, nil]
122
- def find(ast, &block)
123
- ast.accept(Visitor.new { return it if block.(it) })
124
- nil
125
- end
126
-
127
- # Visits the given AST, passing each node in the AST to the given block.
128
- # Returns all nodes for which the block returns true.
129
- #
130
- # @param ast [Ruby::Prism::Node]
131
- # @return [Array<Ruby::Prism::Node>]
132
- def find_all(ast, &block)
133
- nodes = []
134
- ast.accept(Visitor.new { nodes << it if block.(it) })
135
- nodes
136
- end
137
-
138
- private
139
-
140
- # Sets the source filename for the given ast to the source location of the
141
- # given proc or method.
142
- #
143
- # @param ast [Ruby::Prism::Node]
144
- # @param obj [Proc, Method]
145
- # @return [void]
146
- def set_source_filename(ast, obj)
147
- source = ast.send(:source)
148
- source.__filename__ ||= obj.source_location.first
149
- end
150
-
151
- # Finds the AST for the given proc. This method is currently not in use, but
152
- # we may eventually use this code in order to provide backward compatibility
153
- # for Ruby < 4.1.
154
- #
155
- # @param proc [Proc]
156
- # @return [Ruby::Prism::Node]
157
- def proc_ast(proc)
158
- fn, lineno = proc.source_location
159
- pr = Prism.parse(get_source(fn), filepath: fn)
160
- program = pr.value
161
-
162
- Finder.find(program, proc) do
163
- on(:lambda) do |node|
164
- found!(node) if node.location.start_line == lineno
165
- super(node)
166
- end
167
- on(:block) do |node|
168
- found!(node) if node.location.start_line == lineno
169
- super(node)
170
- end
171
- on(:call) do |node|
172
- case node.name
173
- when :proc, :lambda
174
- found!(node) if node.block && node.block.location.start_line == lineno
175
- end
176
- super(node)
177
- end
178
- end
179
- rescue Errno::ENOENT
180
- raise Sirop::Error, "Could not get source for proc"
181
- end
182
-
183
- # Finds the AST for the given method. This method is currently not in use,
184
- # but we may eventually use this code in order to provide backward
185
- # compatibility for Ruby < 4.1.
186
- #
187
- # @param method [Method, UnboundMethod]
188
- # @return [Ruby::Prism::Node]
189
- def method_ast(method)
190
- fn, lineno = method.source_location
191
- pr = Prism.parse(get_source(fn), filepath: fn)
192
- program = pr.value
193
-
194
- Finder.find(program, method) do
195
- on(:def) do |node|
196
- found!(node) if node.name == method.name && node.location.start_line == lineno
197
- super(node)
198
- end
199
- end
200
- end
201
-
202
- # Returns the content of the given filename. Returns the IRB line buffer if
203
- # '(irb)' is given. This method is not currently used.
204
- #
205
- # @param fn [String]
206
- # @return [String] source code
207
- def get_source(fn)
208
- return get_irb_source if fn == '(irb)'
209
-
210
- IO.read(fn)
211
- end
212
-
213
- # Returns the IRB line buffer (the source code for the current IRB session.)
214
- # This method is currently not used.
215
- #
216
- # @return [String] IRB line buffer
217
- def get_irb_source
218
- io = IRB.CurrentContext.io
219
- last_line = io.instance_variable_get(:@line_no)
220
- (1..last_line).map { io.line(it) }.join
221
- end
222
36
  end
223
37
  end
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.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -66,6 +66,7 @@ files:
66
66
  - CHANGELOG.md
67
67
  - README.md
68
68
  - lib/steadfast.rb
69
+ - lib/steadfast/ast.rb
69
70
  - lib/steadfast/make.rb
70
71
  - lib/steadfast/quote.rb
71
72
  - lib/steadfast/rewriter.rb