steadfast 0.1.0 → 0.3.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: 92df33342d9993952237fdf8273133ad4a99998e33beeabc456c2d709582a8d3
4
- data.tar.gz: 95dc43ba5d717aa4c756ebe61e44b104757f2b47e27c53e68751bfc1d4def9c7
3
+ metadata.gz: 4c7180221c50d5f0e81f1328dbc03aa33df4a71fc4effcdc429fde0bc5c54925
4
+ data.tar.gz: 7a0af87cae2fb6f434e2d0b367cda46a6dd6f468e49ab4ccb3ed509f61279cbf
5
5
  SHA512:
6
- metadata.gz: cbf3ee2e52c30ce03b7a25a80301e20a3e853a931e070b56e40564aa08cd662537d000e268c7349f4b7332e85ae378944c39e10e3615ca07a1e2543acd6f6f91
7
- data.tar.gz: 188670f7bee400928fcbacdcfd177f2f9bc86adcb1b09be673b7d24cf8d479e52209721a00a10f81830f1f28d97dbfb474529f7654c32cc119f725cc6bf01b54
6
+ metadata.gz: 79761040dc7cd9eb03ff2ef37110dfdbf45de6a38ed6772b2d4054145610a735d849e05069d83de88caa5df1a6fa9b91ac03192829a5d7ac2a20c8ca3cc6d870
7
+ data.tar.gz: 2757ce844329d3bd3a3c73252c8cb9728450321a887da1e26fb8bf6c3a29ee0a2428ca5263d028e4d27555ec3c721ee81ed94aeef94ad3c8891e9bbeab123d1e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 0.3.0 2026-11-24
2
+
3
+ - Implement `Steadfast.find_all`
4
+ - Implement `Steadfast.find`
5
+ - Implement `Steadfast.walk`
6
+ - Add `Visitor` base class, base `Rewriter` off it
7
+
8
+ # 0.2.0 2026-11-24
9
+
10
+ - Use frozen string literals in `eval_ast`
11
+ - Refactor source mapping into separate module
12
+
1
13
  # 0.1.0 2026-11-23
2
14
 
3
15
  - Improve source mapping, make it work for derivative synthetic ASTs
@@ -1,8 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'steadfast/visitor'
4
+
3
5
  module Steadfast
6
+
4
7
  # The Rewriter class is used for rewriting ASTs.
5
- class Rewriter
8
+ class Rewriter < Visitor
6
9
  # Rewrites the given AST with a either a hash of node replacements, or a
7
10
  # block which is invoked for each node in the AST.
8
11
  #
@@ -17,23 +20,7 @@ module Steadfast
17
20
  replacements.key?(n) ? replacements[n] : n
18
21
  end
19
22
  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, *, **, &)
23
+ ast.accept(new(&rewrite_block))
37
24
  end
38
25
 
39
26
  # Visits the given node, and subsequently its subnodes, invoking the rewrite
@@ -42,7 +29,7 @@ module Steadfast
42
29
  # @param n [Ruby::Prism::Node]
43
30
  # @return [Ruby::Prism::Node]
44
31
  def visit(n)
45
- n2 = @rewrite_block.(n, &@rewrite_block)
32
+ n2 = @block.(n, &@block)
46
33
  return n2 if n2 != n
47
34
 
48
35
  changes = {}
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steadfast
4
+
5
+ # SourceMapping manages the mapping of source locations for generated code, in
6
+ # order to allow translating backtrace entries for raised exceptions such that
7
+ # they will point to correct source locations in application code.
8
+ module SourceMapping
9
+ class << self
10
+ # Adds a source mapping entry for the given path/line pairs.
11
+ #
12
+ # @param path1 [String]
13
+ # @param line1 [Integer]
14
+ # @param path2 [String]
15
+ # @param line2 [Integer]
16
+ # @return [void]
17
+ def set(path1, line1, path2, line2)
18
+ (@map ||= {})["#{path1}:#{line1}"] ||= "#{path2}:#{line2}"
19
+ end
20
+
21
+ # Translates entries in the given backtrace according to the global source
22
+ # map.
23
+ #
24
+ # @param bt [Array<String>] backtrace
25
+ # @return [Array<String>] translated source map
26
+ def translate_backtrace(bt)
27
+ return bt if !@map
28
+
29
+ bt.map {
30
+ if (m = it.match(/^(.+)(\:in.+)$/)) && (loc = find_sourcemap_entry(m[1]))
31
+ "#{loc}#{m[2]}"
32
+ else
33
+ it
34
+ end
35
+ }
36
+ end
37
+
38
+ private
39
+
40
+ # Returns a mapped location for the given location entry.
41
+ #
42
+ # @param loc [String] location string of the form file:line
43
+ # @return [String, nil] mapped location
44
+ def find_sourcemap_entry(loc)
45
+ loc = @map[loc]
46
+ return nil if !loc
47
+
48
+ (loc =~ /^\(steadfast-[0-9a-f]+\)\:\d+$/) ? find_sourcemap_entry(loc) : loc
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'steadfast/source_mapping'
4
+
3
5
  module Steadfast
4
6
 
5
7
  # A Sourcifier is used for converting an AST to source code. Sourcifier is
@@ -87,6 +89,7 @@ module Steadfast
87
89
  optional_keyword_parameter: [:name_loc, :whitespace, :value],
88
90
  or: [:left, :whitespace, :operator_loc, :whitespace, :right],
89
91
  parentheses: [:opening_loc, :body, :closing_loc],
92
+ program: :emit_verbatim,
90
93
  pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
91
94
  pinned_variable: [:operator_loc, :variable],
92
95
  rational: :emit_verbatim,
@@ -117,10 +120,11 @@ module Steadfast
117
120
  #
118
121
  # @param start_line []
119
122
  # @return [void]
120
- def initialize(source_location: nil, sourcemap: false)
123
+ def initialize(source_location: nil, sourcemap: false, frozen_string_literal: false)
121
124
  @filename, @line = source_location
122
125
  @line ||= 1
123
126
  @sourcemap = sourcemap
127
+ @frozen_string_literal = frozen_string_literal
124
128
 
125
129
  raise Steadfast::Error, "No filename given, can't generate source map" if @sourcemap && !@filename
126
130
  end
@@ -131,6 +135,10 @@ module Steadfast
131
135
  # @return [String] source code
132
136
  def to_source(ast)
133
137
  @buffer = +''
138
+ if @frozen_string_literal
139
+ @buffer << "#frozen_string_literal: true\n"
140
+ @line += 1
141
+ end
134
142
  visit(ast)
135
143
  @buffer
136
144
  end
@@ -691,9 +699,13 @@ module Steadfast
691
699
 
692
700
  if (filename = source.__filename__) && (filename || last_source.__filename__)
693
701
  emit_newline if different_source && last_source
694
- Steadfast.sourcemap["#{@filename}:#{@line}"] ||= "#{filename}:#{loc.start_line}"
702
+ Steadfast::SourceMapping.set(
703
+ @filename, @line, filename, loc.start_line
704
+ )
695
705
  elsif filename.nil? && @last_loc
696
- Steadfast.sourcemap["#{@filename}:#{@line}"] ||= "#{last_source.__filename__}:#{@last_loc.start_line}"
706
+ Steadfast::SourceMapping.set(
707
+ @filename, @line, last_source.__filename__, @last_loc.start_line
708
+ )
697
709
  end
698
710
  end
699
711
 
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.1.0'
3
+ VERSION = '0.3.0'
4
4
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steadfast
4
+
5
+ # The Visitor class is used for traversing an AST, visiting each node in it.
6
+ class Visitor
7
+ # Initializes the visitor with the given visitor block.
8
+ #
9
+ # @param block [Proc]
10
+ # @return [void]
11
+ def initialize(&block)
12
+ @block = block
13
+ end
14
+
15
+ # Intercepts visit_xxxx method calls.
16
+ #
17
+ # @param sym [Symbol]
18
+ # @return [void]
19
+ def method_missing(sym, *, **, &)
20
+ sym =~ /^visit_/ ? visit(*) : super(sym, *, **, &)
21
+ end
22
+
23
+ # Visits the given node, and subsequently its subnodes, invoking the block.
24
+ #
25
+ # @param n [Ruby::Prism::Node]
26
+ # @return [void]
27
+ def visit(n)
28
+ @block.(n, &@block)
29
+
30
+ n.deconstruct_keys(nil).each do |k, v|
31
+ if v.is_a?(Ruby::Prism::Node)
32
+ visit(v)
33
+ elsif v in [Ruby::Prism::Node, *]
34
+ v.each { visit(it) }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/steadfast.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'steadfast/source_mapping'
3
4
  require 'steadfast/sourcifier'
5
+ require 'steadfast/visitor'
4
6
  require 'steadfast/rewriter'
5
7
 
6
8
  # Tell Ruby to keep script lines in memory, so calling Proc#syntax_tree or
@@ -24,39 +26,6 @@ module Steadfast
24
26
  end
25
27
 
26
28
  class << self
27
- # Returns the global Steadfast source map hash. Currently *not* thread-safe.
28
- #
29
- # @return [Hash] source map
30
- def sourcemap
31
- @sourcemap ||= {}
32
- end
33
-
34
- # Translates entries in the given backtrace according to the global source
35
- # map.
36
- #
37
- # @param bt [Array<String>] backtrace
38
- # @return [Array<String>] translated source map
39
- def translate_backtrace(bt)
40
- bt.map {
41
- if (m = it.match(/^(.+)(\:in.+)$/)) && (loc = find_sourcemap_entry(m[1]))
42
- "#{loc}#{m[2]}"
43
- else
44
- it
45
- end
46
- }
47
- end
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
-
60
29
  # Returns an AST for the given object.
61
30
  #
62
31
  # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
@@ -102,12 +71,22 @@ module Steadfast
102
71
 
103
72
  case source_location
104
73
  when nil
74
+ # Important: ideally we'd set the source location line no to 0 (to
75
+ # compensate for the #frozen_string_literal comment at the top).
76
+ # However, while `eval` respects a starting lineno set to 0, Prism does
77
+ # not, which can lead to off-by-one line nos in exception backtraces for
78
+ # synthetic ASTs.
79
+ #
80
+ # Note that when source_location is not nil, we do decrement the start
81
+ # lineno. Hopefully some day this wart will be removed from Ruby.G
105
82
  source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
106
83
  when :caller
107
84
  l = caller_locations[0]
108
- source_location = [l.path, l.lineno]
85
+ source_location = [l.path, l.lineno - 1]
86
+ else
87
+ source_location = [source_location[0], source_location[1] - 1]
109
88
  end
110
- src = to_source(ast, source_location:, sourcemap: true)
89
+ src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
111
90
  if eval_receiver
112
91
  eval_receiver.instance_eval(src)
113
92
  else
@@ -115,6 +94,22 @@ module Steadfast
115
94
  end
116
95
  end
117
96
 
97
+ def walk(ast, &)
98
+ ast.accept(Visitor.new(&))
99
+ ast
100
+ end
101
+
102
+ def find(ast, &block)
103
+ ast.accept(Visitor.new { return it if block.(it) })
104
+ nil
105
+ end
106
+
107
+ def find_all(ast, &block)
108
+ nodes = []
109
+ ast.accept(Visitor.new { nodes << it if block.(it) })
110
+ nodes
111
+ end
112
+
118
113
  private
119
114
 
120
115
  # Sets the source filename for the given ast to the source location of the
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.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -62,17 +62,17 @@ dependencies:
62
62
  requirement: !ruby/object:Gem::Requirement
63
63
  requirements:
64
64
  -
65
- - ">="
65
+ - "="
66
66
  - !ruby/object:Gem::Version
67
- version: "0"
67
+ version: 6.3.0
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  -
73
- - ">="
73
+ - "="
74
74
  - !ruby/object:Gem::Version
75
- version: "0"
75
+ version: 6.3.0
76
76
  email: sharon@noteflakes.com
77
77
  executables: []
78
78
  extensions: []
@@ -83,8 +83,10 @@ files:
83
83
  - README.md
84
84
  - lib/steadfast.rb
85
85
  - lib/steadfast/rewriter.rb
86
+ - lib/steadfast/source_mapping.rb
86
87
  - lib/steadfast/sourcifier.rb
87
88
  - lib/steadfast/version.rb
89
+ - lib/steadfast/visitor.rb
88
90
  homepage: "http://github.com/digital-fabric/steadfast"
89
91
  licenses:
90
92
  - MIT