steadfast 0.0.2 → 0.0.3

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: c6e3d1e91bd27e7c92b511ef755e05095e691e6489173aded42938aa6ecbdd58
4
+ data.tar.gz: 75a299fc9c1b3bfc89e81dbc6e1bfb0085a470b4a239aeffc02b63adefc509ad
5
5
  SHA512:
6
- metadata.gz: 080361ccb2c5f651657f429f82c01444c60cc208b1a4fe2bb2c390dd75d879491347a12aec7f43dcc28dc8e8eb0ee2d80d18e1aa6d6765a810d2ef3f5ce70d41
7
- data.tar.gz: a67148f3b42ce798e8278f043be6021e6cfa6fc3d9c89febb69bc1b0b68041ae86ef449af30351a55ec795c38fa6a46e245349118f9dab16564edd23e2140521
6
+ metadata.gz: 070eb4137ab096dcb11625c593c880952ecbd8150057849608eb8972368f047c66ff8837c0b900072620380d026f2d4043ac928aa8654f504803c683ab2a3c4c
7
+ data.tar.gz: 314ca97af4862e20ceab634ac1ebe3e6f72a2eac2c67c933a1df27fa1deb84a35a07a09f044311d13c53f0f4cfbde00bf60d09260a7faa72e4402709e6277153
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.0.3 2026-11-23
2
+
3
+ - Implement `Steadfast.eval_ast`
4
+ - Implement `Steadfast.rewrite`
5
+ - Improve sourcemap calculation when doing inlining
6
+
1
7
  # 0.0.2 2026-11-09
2
8
 
3
9
  - 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.
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
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.
@@ -69,9 +70,41 @@ module Steadfast
69
70
  Sourcifier.new(**opts).to_source(ast)
70
71
  end
71
72
 
73
+ # Rewrites the given AST using the given hash of node replacements or a
74
+ # rewrite block. Returns either the original AST, or a copy with changes.
75
+ #
76
+ # @param ast [Ruby::Prism::Node]
77
+ # @param replacements [Hash]
78
+ # @return [Ruby::Prism::Node]
79
+ def rewrite(ast, **replacements, &block)
80
+ Rewriter.rewrite(ast, **replacements, &block)
81
+ end
82
+
83
+ # Evaluates the given AST in the context of the given receiver or binding.
84
+ #
85
+ # @param ast [Ruby::Prism::Node]
86
+ # @param eval_receiver [any]
87
+ # @param eval_binding [Binding, nil]
88
+ # @return [any]
89
+ def eval_ast(ast, eval_receiver = nil, eval_binding = nil)
90
+ raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
91
+
92
+ src = to_source(ast)
93
+ if eval_receiver
94
+ eval_receiver.instance_eval(src)
95
+ else
96
+ eval(src, eval_binding || binding)
97
+ end
98
+ end
99
+
72
100
  private
73
101
 
74
- # Sets
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]
75
108
  def set_source_filename(ast, obj)
76
109
  source = ast.send(:source)
77
110
  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.0.3
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"