steadfast 0.0.2 → 0.1.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: 5d446eb7d3f0db23310b1ea5dfc4343ff7b82bfaae804de963ea42616977b2ac
4
- data.tar.gz: c02e0997705985da9800da6db4a88ea9e5a652e4e6562a2a7aba276eadf8d536
3
+ metadata.gz: 92df33342d9993952237fdf8273133ad4a99998e33beeabc456c2d709582a8d3
4
+ data.tar.gz: 95dc43ba5d717aa4c756ebe61e44b104757f2b47e27c53e68751bfc1d4def9c7
5
5
  SHA512:
6
- metadata.gz: 080361ccb2c5f651657f429f82c01444c60cc208b1a4fe2bb2c390dd75d879491347a12aec7f43dcc28dc8e8eb0ee2d80d18e1aa6d6765a810d2ef3f5ce70d41
7
- data.tar.gz: a67148f3b42ce798e8278f043be6021e6cfa6fc3d9c89febb69bc1b0b68041ae86ef449af30351a55ec795c38fa6a46e245349118f9dab16564edd23e2140521
6
+ metadata.gz: cbf3ee2e52c30ce03b7a25a80301e20a3e853a931e070b56e40564aa08cd662537d000e268c7349f4b7332e85ae378944c39e10e3615ca07a1e2543acd6f6f91
7
+ data.tar.gz: 188670f7bee400928fcbacdcfd177f2f9bc86adcb1b09be673b7d24cf8d479e52209721a00a10f81830f1f28d97dbfb474529f7654c32cc119f725cc6bf01b54
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ # 0.1.0 2026-11-23
2
+
3
+ - Improve source mapping, make it work for derivative synthetic ASTs
4
+ - Add automatic source location info in `Steadfast.eval_ast`
5
+
6
+ # 0.0.3 2026-11-23
7
+
8
+ - Implement `Steadfast.eval_ast`
9
+ - Implement `Steadfast.rewrite`
10
+ - Improve sourcemap calculation when doing inlining
11
+
1
12
  # 0.0.2 2026-11-09
2
13
 
3
14
  - Emit newline upon detecting a source change while sourcifying an AST
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steadfast
4
+ # The Rewriter class is used for rewriting ASTs.
5
+ class Rewriter
6
+ # Rewrites the given AST with a either a hash of node replacements, or a
7
+ # block which is invoked for each node in the AST.
8
+ #
9
+ # @param ast [Ruby::Prism::Node] AST
10
+ # @replacements [Hash] replacements
11
+ # @return [Ruby::Prism::Node]
12
+ def self.rewrite(ast, **replacements, &rewrite_block)
13
+ rewrite_block ||= ->(n) do
14
+ if (v = replacements[n]) && v.is_a?(Hash)
15
+ n.copy(**v)
16
+ else
17
+ replacements.key?(n) ? replacements[n] : n
18
+ end
19
+ end
20
+ ast.accept(new(rewrite_block))
21
+ end
22
+
23
+ # Initializes the rewriter.
24
+ #
25
+ # @param rewrite_block [Proc]
26
+ # @return [void]
27
+ def initialize(rewrite_block)
28
+ @rewrite_block = rewrite_block
29
+ end
30
+
31
+ # Intercepts visit_xxxx method calls.
32
+ #
33
+ # @param sym [Symbol]
34
+ # @return [void]
35
+ def method_missing(sym, *, **, &)
36
+ sym =~ /^visit_/ ? visit(*) : super(sym, *, **, &)
37
+ end
38
+
39
+ # Visits the given node, and subsequently its subnodes, invoking the rewrite
40
+ # block, and returning either the original node or its replacement.
41
+ #
42
+ # @param n [Ruby::Prism::Node]
43
+ # @return [Ruby::Prism::Node]
44
+ def visit(n)
45
+ n2 = @rewrite_block.(n, &@rewrite_block)
46
+ return n2 if n2 != n
47
+
48
+ changes = {}
49
+ n.deconstruct_keys(nil).each do |k, v|
50
+ if v.is_a?(Ruby::Prism::Node)
51
+ v2 = visit(v)
52
+ changes[k] = v2 if v2 != v
53
+ elsif v in [Ruby::Prism::Node, *]
54
+ v2 = v.map { visit(it) }
55
+ changes[k] = v2 if v2 != v
56
+ end
57
+ end
58
+ changes.empty? ? n : n.copy(**changes)
59
+ end
60
+ end
61
+ end
@@ -672,7 +672,6 @@ module Steadfast
672
672
 
673
673
  record_loc_source_mapping(loc) if @sourcemap
674
674
  emit(loc.slice.chomp)
675
- @last_loc = loc
676
675
  end
677
676
 
678
677
  # Adds an entry to the sourcemap hash when detecting a change of line or
@@ -685,12 +684,17 @@ module Steadfast
685
684
  def record_loc_source_mapping(loc)
686
685
  source = loc.send(:source)
687
686
  last_source = @last_loc&.send(:source)
688
- different_source = source != last_source
687
+ different_source = source.__filename__ && source != last_source
689
688
  changed = different_source || (@last_loc && (loc.start_line != @last_loc.start_line))
689
+ @last_loc = loc if source.__filename__
690
690
  return if !changed
691
691
 
692
- emit_newline if different_source && last_source
693
- Steadfast.sourcemap["#{@filename}:#{@line}"] ||= "#{loc.send(:source).__filename__}:#{loc.start_line}"
692
+ if (filename = source.__filename__) && (filename || last_source.__filename__)
693
+ emit_newline if different_source && last_source
694
+ Steadfast.sourcemap["#{@filename}:#{@line}"] ||= "#{filename}:#{loc.start_line}"
695
+ elsif filename.nil? && @last_loc
696
+ Steadfast.sourcemap["#{@filename}:#{@line}"] ||= "#{last_source.__filename__}:#{@last_loc.start_line}"
697
+ end
694
698
  end
695
699
 
696
700
  # Appends the given string into the source code buffer.
@@ -707,7 +711,7 @@ module Steadfast
707
711
  # @return [void]
708
712
  def emit_newline
709
713
  return if @buffer[-1] == "\n"
710
-
714
+
711
715
  @buffer << "\n"
712
716
  @line += 1
713
717
  end
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.0.2'
3
+ VERSION = '0.1.0'
4
4
  end
data/lib/steadfast.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'steadfast/sourcifier'
4
+ require 'steadfast/rewriter'
4
5
 
5
6
  # Tell Ruby to keep script lines in memory, so calling Proc#syntax_tree or
6
7
  # Method#syntax_tree will work.
@@ -24,7 +25,7 @@ module Steadfast
24
25
 
25
26
  class << self
26
27
  # Returns the global Steadfast source map hash. Currently *not* thread-safe.
27
- #
28
+ #
28
29
  # @return [Hash] source map
29
30
  def sourcemap
30
31
  @sourcemap ||= {}
@@ -37,14 +38,25 @@ module Steadfast
37
38
  # @return [Array<String>] translated source map
38
39
  def translate_backtrace(bt)
39
40
  bt.map {
40
- if (m = it.match(/^(.+)(\:in.+)$/)) && (loc = sourcemap[m[1]])
41
- "#{sourcemap[m[1]]}#{m[2]}"
41
+ if (m = it.match(/^(.+)(\:in.+)$/)) && (loc = find_sourcemap_entry(m[1]))
42
+ "#{loc}#{m[2]}"
42
43
  else
43
44
  it
44
45
  end
45
46
  }
46
47
  end
47
48
 
49
+ # Returns a mapped location for the given location entry.
50
+ #
51
+ # @param loc [String] location string of the form file:line
52
+ # @return [String, nil] mapped location
53
+ def find_sourcemap_entry(loc)
54
+ loc = sourcemap[loc]
55
+ return nil if !loc
56
+
57
+ (loc =~ /^\(steadfast-[0-9a-f]+\)\:\d+$/) ? find_sourcemap_entry(loc) : loc
58
+ end
59
+
48
60
  # Returns an AST for the given object.
49
61
  #
50
62
  # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
@@ -69,9 +81,48 @@ module Steadfast
69
81
  Sourcifier.new(**opts).to_source(ast)
70
82
  end
71
83
 
84
+ # Rewrites the given AST using the given hash of node replacements or a
85
+ # rewrite block. Returns either the original AST, or a copy with changes.
86
+ #
87
+ # @param ast [Ruby::Prism::Node]
88
+ # @param replacements [Hash]
89
+ # @return [Ruby::Prism::Node]
90
+ def rewrite(ast, **replacements, &block)
91
+ Rewriter.rewrite(ast, **replacements, &block)
92
+ end
93
+
94
+ # Evaluates the given AST in the context of the given receiver or binding.
95
+ #
96
+ # @param ast [Ruby::Prism::Node]
97
+ # @param eval_receiver [any]
98
+ # @param eval_binding [Binding, nil]
99
+ # @return [any]
100
+ def eval_ast(ast, eval_receiver = nil, eval_binding = nil, source_location: nil)
101
+ raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
102
+
103
+ case source_location
104
+ when nil
105
+ source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
106
+ when :caller
107
+ l = caller_locations[0]
108
+ source_location = [l.path, l.lineno]
109
+ end
110
+ src = to_source(ast, source_location:, sourcemap: true)
111
+ if eval_receiver
112
+ eval_receiver.instance_eval(src)
113
+ else
114
+ eval(src, eval_binding || binding, source_location[0], source_location[1])
115
+ end
116
+ end
117
+
72
118
  private
73
119
 
74
- # Sets
120
+ # Sets the source filename for the given ast to the source location of the
121
+ # given proc or method.
122
+ #
123
+ # @param ast [Ruby::Prism::Node]
124
+ # @param obj [Proc, Method]
125
+ # @return [void]
75
126
  def set_source_filename(ast, obj)
76
127
  source = ast.send(:source)
77
128
  source.__filename__ ||= obj.source_location.first
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.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -82,6 +82,7 @@ files:
82
82
  - CHANGELOG.md
83
83
  - README.md
84
84
  - lib/steadfast.rb
85
+ - lib/steadfast/rewriter.rb
85
86
  - lib/steadfast/sourcifier.rb
86
87
  - lib/steadfast/version.rb
87
88
  homepage: "http://github.com/digital-fabric/steadfast"