steadfast 0.3.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +9 -0
- data/lib/steadfast/make.rb +142 -0
- data/lib/steadfast/quote.rb +173 -0
- data/lib/steadfast/source_mapping.rb +4 -4
- data/lib/steadfast/sourcifier.rb +13 -7
- data/lib/steadfast/version.rb +1 -1
- data/lib/steadfast.rb +27 -3
- metadata +3 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 65edd414b2b9a53a27c9965da47829809b1abdfc7e3cd2f9dfb26e9e155976b6
|
|
4
|
+
data.tar.gz: 6560196ff027bb0c1d7a669ce7f80c1af4e4d6419792bd872a637594f39eee74
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 01f35b02170b51dbb77f363b65d2ac812fae08c66796f8754d6a491c5357114f418ac6baccbf9646c912885f9f9ef0e23d483641f4cf63e653fc16ebcabdb4df
|
|
7
|
+
data.tar.gz: dd8a6449757d1b35bfeb5005ff2b5209b2873c869ee7425f5caf36c5b368e352d7fc364f71ef64db5962698a4a547832aeda7a21b2e2070c9279ad04601f84fd
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
# 0.4.0 2026-11-25
|
|
2
|
+
|
|
3
|
+
- Improve sourcifier output
|
|
4
|
+
- Implement `quote` and `unquote`
|
|
5
|
+
- Implement additional `make_xxx` methods
|
|
6
|
+
- Implement `make_statements`
|
|
7
|
+
- Implement `make_literal`
|
|
8
|
+
- Implement `make_node`
|
|
9
|
+
|
|
1
10
|
# 0.3.0 2026-11-24
|
|
2
11
|
|
|
3
12
|
- Implement `Steadfast.find_all`
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Steadfast
|
|
4
|
+
module Make
|
|
5
|
+
# @param src [String]
|
|
6
|
+
# @param location [Ruby::Prism::Node, Ruby::Prism::Location]
|
|
7
|
+
# @param lvars [Array<Symbol>] optional local var list
|
|
8
|
+
# @param binding [Binding] optional binding
|
|
9
|
+
# @return [Ruby::Prism::Node]
|
|
10
|
+
def make_node(src, location: nil, lvars: nil, binding: nil)
|
|
11
|
+
loc = location && __make_get_source_location__(location)
|
|
12
|
+
scopes = [(binding&.local_variables || []), lvars].compact
|
|
13
|
+
opts = {
|
|
14
|
+
raise_error: true,
|
|
15
|
+
frozen_string_literal: true,
|
|
16
|
+
filepath: loc && loc[0],
|
|
17
|
+
line: loc && loc[1],
|
|
18
|
+
scopes: scopes
|
|
19
|
+
}
|
|
20
|
+
ast = Ruby::Prism.parse(src, **opts).value.statements.body[0]
|
|
21
|
+
ast.send(:source).__filename__ = loc[0] if loc
|
|
22
|
+
ast
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
IDENTIFIER_RE = /^[^\(\)\;\:\s]+$/
|
|
26
|
+
|
|
27
|
+
# Makes a def node.
|
|
28
|
+
#
|
|
29
|
+
# @param name [String, Symbol]
|
|
30
|
+
# @param params [Ruby::Prism::Node, nil]
|
|
31
|
+
# @param body [Ruby::Prism::StatementsNode]
|
|
32
|
+
# @param location [Ruby::Prism::Location]
|
|
33
|
+
# @return [Ruby::Prism::Node]
|
|
34
|
+
def make_def(name, params: nil, body:, location: nil)
|
|
35
|
+
raise SyntaxError, 'Invalid method name' if !(IDENTIFIER_RE === name)
|
|
36
|
+
|
|
37
|
+
n = make_node("def #{name}(x); 1; end", location:)
|
|
38
|
+
n.copy(body:, parameters: params)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Makes a local variable read node.
|
|
42
|
+
#
|
|
43
|
+
# @param name [String, Symbol]
|
|
44
|
+
# @param location [Ruby::Prism::Location]
|
|
45
|
+
# @return [Ruby::Prism::Node]
|
|
46
|
+
def make_lvar_read(name, location: nil)
|
|
47
|
+
raise SyntaxError, 'Invalid identifier name' if !(IDENTIFIER_RE === name)
|
|
48
|
+
|
|
49
|
+
make_node(name.to_s, lvars: [name.to_sym], location:)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Makes a literal array node.
|
|
53
|
+
#
|
|
54
|
+
# @param array [Array]
|
|
55
|
+
# @param location [Ruby::Prism::Location]
|
|
56
|
+
# @return [Ruby::Prism::Node]
|
|
57
|
+
def make_array(array, location: nil)
|
|
58
|
+
raise ArgumentError, "Not an array" if !array.is_a?(Array)
|
|
59
|
+
|
|
60
|
+
n = make_node('[]', location:)
|
|
61
|
+
Steadfast.rewrite(n, n => { elements: array.map { make_literal(it, location:) } })
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Makes a literal hash node.
|
|
65
|
+
#
|
|
66
|
+
# @param hash [Hash]
|
|
67
|
+
# @param location [Ruby::Prism::Location]
|
|
68
|
+
# @return [Ruby::Prism::Node]
|
|
69
|
+
def make_hash(hash, location: nil)
|
|
70
|
+
raise ArgumentError, "Not a hash" if !hash.is_a?(Hash)
|
|
71
|
+
|
|
72
|
+
n = make_node('{ x => 1 }', location:)
|
|
73
|
+
assoc_node = n.elements[0]
|
|
74
|
+
elements = hash.map { |k, v|
|
|
75
|
+
Steadfast.rewrite(
|
|
76
|
+
assoc_node,
|
|
77
|
+
assoc_node => {
|
|
78
|
+
key: make_literal(k, location:),
|
|
79
|
+
value: make_literal(v, location:)
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
Steadfast.rewrite(n, n => { elements: })
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Makes a literal value node.
|
|
87
|
+
#
|
|
88
|
+
# @param value [any]
|
|
89
|
+
# @param location [Ruby::Prism::Location]
|
|
90
|
+
# @return [Ruby::Prism::Node]
|
|
91
|
+
def make_literal(value, location: nil)
|
|
92
|
+
case value
|
|
93
|
+
when Integer, Float, String, Symbol, Range, Regexp, nil, true, false, Class, Module
|
|
94
|
+
make_node(value.inspect, location:)
|
|
95
|
+
when Array
|
|
96
|
+
make_array(value, location:)
|
|
97
|
+
when Hash
|
|
98
|
+
make_hash(value, location:)
|
|
99
|
+
else
|
|
100
|
+
raise ArgumentError, "Can't make AST node for #{value.inspect}"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Makes a StatementsNode containing the given statement.
|
|
105
|
+
#
|
|
106
|
+
# @param statements [Array<Ruby::Prism::Node>]
|
|
107
|
+
# @param location [Ruby::Prism::Location]
|
|
108
|
+
# @return [Ruby::Prism::Node]
|
|
109
|
+
def make_statements(statements, location: nil)
|
|
110
|
+
raise ArgumentError, "Not an array" if !statements.is_a?(Array)
|
|
111
|
+
raise ArgumentError, "Empty array given" if statements.size == 0
|
|
112
|
+
raise ArgumentError, "Invalid array given" if statements.any? { !it.is_a?(Ruby::Prism::Node) }
|
|
113
|
+
|
|
114
|
+
n = make_node('(1)', location:)
|
|
115
|
+
Steadfast.rewrite(n.body, n.body => { body: statements })
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Makes a constant node.
|
|
119
|
+
#
|
|
120
|
+
# @param name [String, Symbol]
|
|
121
|
+
# @param location [Ruby::Prism::Location]
|
|
122
|
+
# @return [Ruby::Prism::Node]
|
|
123
|
+
def make_constant(name, location: nil)
|
|
124
|
+
raise SyntaxError, 'Invalid identifier name' if name !~ /^[A-Z][A-Za-z_\d]*$/
|
|
125
|
+
|
|
126
|
+
make_node(name.to_s, location:)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
private
|
|
130
|
+
|
|
131
|
+
# Returns the source location information (filename, line) for the given
|
|
132
|
+
# location.
|
|
133
|
+
#
|
|
134
|
+
# @param location [Ruby::Prism::Node, Ruby::Prism::Location]
|
|
135
|
+
# @return [Array] [filename, lineno]
|
|
136
|
+
def __make_get_source_location__(location)
|
|
137
|
+
filename = location.send(:source).__filename__ || raise('No filename set')
|
|
138
|
+
lineno = location.start_line
|
|
139
|
+
[filename, lineno]
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Steadfast
|
|
4
|
+
# Quote implements the quote / unquote code quoting tools. The quote/unquote
|
|
5
|
+
# pair of coding tools permits creation of code templates.
|
|
6
|
+
module Quote
|
|
7
|
+
extend self
|
|
8
|
+
|
|
9
|
+
# Quotes the code in the given block, interpolating all unquote expressions.
|
|
10
|
+
# Returns the resulting AST. The unquote call is used to perform a
|
|
11
|
+
# computation at the time of building the AST. The result of unquote is an
|
|
12
|
+
# object that can be literalized (See #unquote). A call to unquote can also
|
|
13
|
+
# be used to inject an AST into the code. The quote method takes a binding,
|
|
14
|
+
# which is then used for any unquote computations.
|
|
15
|
+
#
|
|
16
|
+
# Example:
|
|
17
|
+
#
|
|
18
|
+
# i = 4
|
|
19
|
+
# ast = Steadfast.quote(binding) { ->(x) { unquote(i) + x } }
|
|
20
|
+
# l = Steadfast.eval_ast(ast) #=> ->(x) { 4 + x }
|
|
21
|
+
# l + 3 #=> 7
|
|
22
|
+
#
|
|
23
|
+
# A call to unquote can also be in the form of a block, e.g. if the
|
|
24
|
+
# computation consists of more than one statement. Nested quote calls can be
|
|
25
|
+
# made in order to compute an AST that can be injected into the quoted code.
|
|
26
|
+
# Alternating calls to unquote and quote can be nested arbitrarily.
|
|
27
|
+
#
|
|
28
|
+
# Example:
|
|
29
|
+
#
|
|
30
|
+
# i = 4
|
|
31
|
+
# ast = Steadfast.quote(binding) {
|
|
32
|
+
# x = unquote {
|
|
33
|
+
# j = i + 3
|
|
34
|
+
# quote { (x + unquote(j)) }
|
|
35
|
+
# }
|
|
36
|
+
# }
|
|
37
|
+
#
|
|
38
|
+
# Nested calls to unquote and quote should be unqualified (i.e. quote
|
|
39
|
+
# instead of Steadfast.quote). In addition, nested calls to quote should not
|
|
40
|
+
# provide a binding, just a block. They will automatically inherit the
|
|
41
|
+
# binding of the outermost quote.
|
|
42
|
+
#
|
|
43
|
+
# @param binding [Binding, nil] binding for unquote computation
|
|
44
|
+
# @return [Ruby::Prism::Node] quoted code
|
|
45
|
+
def quote(binding, &block)
|
|
46
|
+
raise Steadfast::Error, "No block given" if !block
|
|
47
|
+
|
|
48
|
+
ast = Steadfast.to_ast(block)
|
|
49
|
+
statements = __quote_select_statements_node__(statements)
|
|
50
|
+
raise Steadfast::Error, "Block can't be quoted" if !ast
|
|
51
|
+
|
|
52
|
+
quoted = Steadfast.rewrite(ast) { |n|
|
|
53
|
+
next n if !(n in Ruby::Prism::CallNode(receiver: nil, name: :unquote))
|
|
54
|
+
|
|
55
|
+
__unquote_evaluate__(n, binding)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
__quote_normalize_return_value__(quoted)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Returns the AST for the given literal value. The value should be one of
|
|
62
|
+
# the builtin types: true, false, nil, String, Float, Integer, Symbol,
|
|
63
|
+
# Array, Hash, Module, Class, or Ruby::Prism::Node (an AST). If value is an
|
|
64
|
+
# AST, unquote will return it. All other values are converted to an AST
|
|
65
|
+
# representation as literal values.
|
|
66
|
+
#
|
|
67
|
+
# If a node location is provided, the resulting AST will use that location
|
|
68
|
+
# information (filename + lineno), for purposes of providing correct
|
|
69
|
+
# backtraces.
|
|
70
|
+
#
|
|
71
|
+
# The return value is an AST. If the unquote computation's result is an
|
|
72
|
+
# array of AST nodes, it is returned wrapped in a StatementsNode.
|
|
73
|
+
#
|
|
74
|
+
# @param value [expr] expression to evaluate and inject into surrounding code
|
|
75
|
+
# @return [Ruby::Prism::Node]
|
|
76
|
+
def unquote(value, location = nil)
|
|
77
|
+
__unquote_convert_value__(value, location)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
# Selects the StatementsNode out of the given AST node. If the
|
|
83
|
+
# statementsNode contains a single statement, returns that statement.
|
|
84
|
+
# Otherwise, returns the StatementsNode.
|
|
85
|
+
#
|
|
86
|
+
# @param node [Ruby::Prism::Node]
|
|
87
|
+
# @return [Ruby::Prism::Node]
|
|
88
|
+
def __quote_normalize_return_value__(node)
|
|
89
|
+
statements = __quote_select_statements_node__(node)
|
|
90
|
+
return node if !statements
|
|
91
|
+
|
|
92
|
+
(statements.body.size == 1) ? statements.body[0] : statements
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Selects the outermost StatementsNode for the given AST, according to the
|
|
96
|
+
# node type.
|
|
97
|
+
#
|
|
98
|
+
# @param node [Ruby::Prism::Node]
|
|
99
|
+
# @return [Ruby::Prism::StatementsNode, nil]
|
|
100
|
+
def __quote_select_statements_node__(node)
|
|
101
|
+
case node
|
|
102
|
+
when Ruby::Prism::StatementsNode
|
|
103
|
+
node
|
|
104
|
+
when Ruby::Prism::BlockNode
|
|
105
|
+
node.body
|
|
106
|
+
when Ruby::Prism::CallNode
|
|
107
|
+
node.block.body
|
|
108
|
+
else
|
|
109
|
+
nil
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Evaluates the given AST node by sourcifying it and eval'ing the resulting
|
|
114
|
+
# code with the given binding. The resulting value is rendered into an AST
|
|
115
|
+
# and returned.
|
|
116
|
+
#
|
|
117
|
+
# @param node [Ruby::Prism::Node]
|
|
118
|
+
# @param binding [Binding]
|
|
119
|
+
# @return [Ruby::Prism::Node]
|
|
120
|
+
def __unquote_evaluate__(node, binding)
|
|
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)
|
|
125
|
+
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)
|
|
129
|
+
else
|
|
130
|
+
raise ArgumentError, "Invalid unquote arguments"
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Rewrites nested calls to quote inside an unquote block or expression.
|
|
135
|
+
#
|
|
136
|
+
# @param node [Ruby::Prism::Node]
|
|
137
|
+
# @param binding [Binding]
|
|
138
|
+
# @return [Ruby::Prism::Node]
|
|
139
|
+
def __unquote_rewrite_nested_quote__(node, binding)
|
|
140
|
+
Steadfast.rewrite(node) { |n2|
|
|
141
|
+
next n2 if !(n2 in Ruby::Prism::CallNode(receiver: nil, name: :quote))
|
|
142
|
+
|
|
143
|
+
raise ArgumentError, "Invalid arguments in call to quote" if n2.arguments
|
|
144
|
+
raise ArgumentError, "Missing block in call to quote" if !n2.block
|
|
145
|
+
|
|
146
|
+
n3 = Steadfast.make_node("Steadfast::Quote.quote(binding) do\nnil \nend", location: n2)
|
|
147
|
+
Steadfast.rewrite(n3, n3.block.body => n2.block.body)
|
|
148
|
+
}
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Returns an AST for the given value. This method converts the result of the
|
|
152
|
+
# unquote computation into an AST.
|
|
153
|
+
#
|
|
154
|
+
# @param value [any]
|
|
155
|
+
# @param location [Ruby::Prism::Location]
|
|
156
|
+
# @return [Ruby::Prism::Node]
|
|
157
|
+
def __unquote_convert_value__(value, location)
|
|
158
|
+
case value
|
|
159
|
+
when Integer, Float, String, Symbol, Range, Regexp, nil, true, false, Hash, Class, Module
|
|
160
|
+
Steadfast.make_literal(value, location:)
|
|
161
|
+
when Array
|
|
162
|
+
if value.first.is_a?(Ruby::Prism::Node)
|
|
163
|
+
return Steadfast.make_statements(value)
|
|
164
|
+
end
|
|
165
|
+
Steadfast.make_literal(value, location:)
|
|
166
|
+
when Ruby::Prism::Node
|
|
167
|
+
value
|
|
168
|
+
else
|
|
169
|
+
raise Steadfast::Error, "Don't know how to unquote #{value.inspect}"
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
@@ -17,7 +17,7 @@ module Steadfast
|
|
|
17
17
|
def set(path1, line1, path2, line2)
|
|
18
18
|
(@map ||= {})["#{path1}:#{line1}"] ||= "#{path2}:#{line2}"
|
|
19
19
|
end
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
# Translates entries in the given backtrace according to the global source
|
|
22
22
|
# map.
|
|
23
23
|
#
|
|
@@ -36,7 +36,7 @@ module Steadfast
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
private
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
# Returns a mapped location for the given location entry.
|
|
41
41
|
#
|
|
42
42
|
# @param loc [String] location string of the form file:line
|
|
@@ -44,9 +44,9 @@ module Steadfast
|
|
|
44
44
|
def find_sourcemap_entry(loc)
|
|
45
45
|
loc = @map[loc]
|
|
46
46
|
return nil if !loc
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
(loc =~ /^\(steadfast-[0-9a-f]+\)\:\d+$/) ? find_sourcemap_entry(loc) : loc
|
|
49
|
-
end
|
|
49
|
+
end
|
|
50
50
|
end
|
|
51
51
|
end
|
|
52
52
|
end
|
data/lib/steadfast/sourcifier.rb
CHANGED
|
@@ -18,7 +18,6 @@ module Steadfast
|
|
|
18
18
|
alias_global_variable: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
|
|
19
19
|
alias_method: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
|
|
20
20
|
and: [:left, :whitespace, :operator_loc, :whitespace, :right],
|
|
21
|
-
assoc: [:key, :whitespace, :operator_loc, :whitespace, :value],
|
|
22
21
|
assoc_splat: [:operator_loc, :value],
|
|
23
22
|
back_reference_read: :emit_verbatim,
|
|
24
23
|
block: [:opening_loc, :parameters, :newline, :body, :newline, :closing_loc],
|
|
@@ -174,6 +173,12 @@ module Steadfast
|
|
|
174
173
|
visit_comma_separated_nodes(node.arguments[subscript])
|
|
175
174
|
end
|
|
176
175
|
|
|
176
|
+
def visit_assoc_node(node)
|
|
177
|
+
emit_code(node.key)
|
|
178
|
+
node.operator_loc ? emit(' => ') : emit_whitespace
|
|
179
|
+
emit_code(node.value)
|
|
180
|
+
end
|
|
181
|
+
|
|
177
182
|
def visit_begin_node(node)
|
|
178
183
|
emit_code(node.begin_keyword_loc)
|
|
179
184
|
emit_newline
|
|
@@ -211,8 +216,8 @@ module Steadfast
|
|
|
211
216
|
if (ml = node.message_loc)
|
|
212
217
|
ol = node.opening_loc
|
|
213
218
|
emit_message_loc = !ol || (ol.start_line != ml.start_line) || (ol.start_column != ml.start_column)
|
|
214
|
-
emit_whitespace if node.name =~
|
|
215
|
-
emit_code(node.message_loc) if emit_message_loc
|
|
219
|
+
emit_whitespace if node.name =~ /^\+|\-|\*|\/|\&|\|\*\*|\&\&|\|\||\%|={2,3}$/
|
|
220
|
+
emit_code(node.message_loc, inhibit_new_line: true) if emit_message_loc
|
|
216
221
|
end
|
|
217
222
|
emit_code(node.opening_loc)
|
|
218
223
|
emit_whitespace if !node.opening_loc && node.arguments
|
|
@@ -674,11 +679,11 @@ module Steadfast
|
|
|
674
679
|
#
|
|
675
680
|
# @param loc [Ruby::Prism::Location, Ruby::Prism::Node, nil]
|
|
676
681
|
# @return [void]
|
|
677
|
-
def emit_code(loc)
|
|
682
|
+
def emit_code(loc, inhibit_new_line: false)
|
|
678
683
|
return if !loc
|
|
679
684
|
return visit(loc) if loc.is_a?(Ruby::Prism::Node)
|
|
680
685
|
|
|
681
|
-
record_loc_source_mapping(loc) if @sourcemap
|
|
686
|
+
record_loc_source_mapping(loc, inhibit_new_line:) if @sourcemap
|
|
682
687
|
emit(loc.slice.chomp)
|
|
683
688
|
end
|
|
684
689
|
|
|
@@ -689,7 +694,8 @@ module Steadfast
|
|
|
689
694
|
#
|
|
690
695
|
# @param loc [Ruby::Prism::Location] code location
|
|
691
696
|
# @return [void]
|
|
692
|
-
def record_loc_source_mapping(loc)
|
|
697
|
+
def record_loc_source_mapping(loc, inhibit_new_line:)
|
|
698
|
+
last_loc = loc
|
|
693
699
|
source = loc.send(:source)
|
|
694
700
|
last_source = @last_loc&.send(:source)
|
|
695
701
|
different_source = source.__filename__ && source != last_source
|
|
@@ -698,7 +704,7 @@ module Steadfast
|
|
|
698
704
|
return if !changed
|
|
699
705
|
|
|
700
706
|
if (filename = source.__filename__) && (filename || last_source.__filename__)
|
|
701
|
-
emit_newline if different_source && last_source
|
|
707
|
+
emit_newline if !inhibit_new_line && different_source && last_source
|
|
702
708
|
Steadfast::SourceMapping.set(
|
|
703
709
|
@filename, @line, filename, loc.start_line
|
|
704
710
|
)
|
data/lib/steadfast/version.rb
CHANGED
data/lib/steadfast.rb
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
3
5
|
require 'steadfast/source_mapping'
|
|
4
6
|
require 'steadfast/sourcifier'
|
|
5
7
|
require 'steadfast/visitor'
|
|
6
8
|
require 'steadfast/rewriter'
|
|
9
|
+
require 'steadfast/make'
|
|
10
|
+
require 'steadfast/quote'
|
|
7
11
|
|
|
8
12
|
# Tell Ruby to keep script lines in memory, so calling Proc#syntax_tree or
|
|
9
13
|
# Method#syntax_tree will work.
|
|
@@ -26,6 +30,8 @@ module Steadfast
|
|
|
26
30
|
end
|
|
27
31
|
|
|
28
32
|
class << self
|
|
33
|
+
include Steadfast::Make
|
|
34
|
+
|
|
29
35
|
# Returns an AST for the given object.
|
|
30
36
|
#
|
|
31
37
|
# @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
|
|
@@ -33,7 +39,11 @@ module Steadfast
|
|
|
33
39
|
def to_ast(obj)
|
|
34
40
|
case obj
|
|
35
41
|
when Proc, Method, UnboundMethod
|
|
36
|
-
obj.syntax_tree
|
|
42
|
+
ast = obj.syntax_tree
|
|
43
|
+
raise Steadfast::Error, "AST not found for given object" if !ast
|
|
44
|
+
|
|
45
|
+
set_source_filename(ast, obj)
|
|
46
|
+
ast
|
|
37
47
|
when Ruby::Prism::Node
|
|
38
48
|
obj
|
|
39
49
|
else
|
|
@@ -76,7 +86,7 @@ module Steadfast
|
|
|
76
86
|
# However, while `eval` respects a starting lineno set to 0, Prism does
|
|
77
87
|
# not, which can lead to off-by-one line nos in exception backtraces for
|
|
78
88
|
# synthetic ASTs.
|
|
79
|
-
#
|
|
89
|
+
#
|
|
80
90
|
# Note that when source_location is not nil, we do decrement the start
|
|
81
91
|
# lineno. Hopefully some day this wart will be removed from Ruby.G
|
|
82
92
|
source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
|
|
@@ -88,22 +98,36 @@ module Steadfast
|
|
|
88
98
|
end
|
|
89
99
|
src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
|
|
90
100
|
if eval_receiver
|
|
91
|
-
eval_receiver.instance_eval(src)
|
|
101
|
+
eval_receiver.instance_eval(src, source_location[0], source_location[1])
|
|
92
102
|
else
|
|
93
103
|
eval(src, eval_binding || binding, source_location[0], source_location[1])
|
|
94
104
|
end
|
|
95
105
|
end
|
|
96
106
|
|
|
107
|
+
# Visits the given AST, passing each node in the AST to the given block.
|
|
108
|
+
#
|
|
109
|
+
# @param ast [Ruby::Prism::Node]
|
|
110
|
+
# @return [void]
|
|
97
111
|
def walk(ast, &)
|
|
98
112
|
ast.accept(Visitor.new(&))
|
|
99
113
|
ast
|
|
100
114
|
end
|
|
101
115
|
|
|
116
|
+
# Visits the given AST, passing each node in the AST to the given block.
|
|
117
|
+
# Returns the first node for which the block returns true.
|
|
118
|
+
#
|
|
119
|
+
# @param ast [Ruby::Prism::Node]
|
|
120
|
+
# @return [Ruby::Prism::Node, nil]
|
|
102
121
|
def find(ast, &block)
|
|
103
122
|
ast.accept(Visitor.new { return it if block.(it) })
|
|
104
123
|
nil
|
|
105
124
|
end
|
|
106
125
|
|
|
126
|
+
# Visits the given AST, passing each node in the AST to the given block.
|
|
127
|
+
# Returns all nodes for which the block returns true.
|
|
128
|
+
#
|
|
129
|
+
# @param ast [Ruby::Prism::Node]
|
|
130
|
+
# @return [Array<Ruby::Prism::Node>]
|
|
107
131
|
def find_all(ast, &block)
|
|
108
132
|
nodes = []
|
|
109
133
|
ast.accept(Visitor.new { nodes << it if block.(it) })
|
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
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sharon Rosner
|
|
@@ -9,22 +9,6 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
-
- !ruby/object:Gem::Dependency
|
|
13
|
-
name: debug_inspector
|
|
14
|
-
requirement: !ruby/object:Gem::Requirement
|
|
15
|
-
requirements:
|
|
16
|
-
-
|
|
17
|
-
- ">="
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: "0"
|
|
20
|
-
type: :runtime
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
-
|
|
25
|
-
- ">="
|
|
26
|
-
- !ruby/object:Gem::Version
|
|
27
|
-
version: "0"
|
|
28
12
|
- !ruby/object:Gem::Dependency
|
|
29
13
|
name: minitest
|
|
30
14
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -82,6 +66,8 @@ files:
|
|
|
82
66
|
- CHANGELOG.md
|
|
83
67
|
- README.md
|
|
84
68
|
- lib/steadfast.rb
|
|
69
|
+
- lib/steadfast/make.rb
|
|
70
|
+
- lib/steadfast/quote.rb
|
|
85
71
|
- lib/steadfast/rewriter.rb
|
|
86
72
|
- lib/steadfast/source_mapping.rb
|
|
87
73
|
- lib/steadfast/sourcifier.rb
|