steadfast 0.2.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: 13c3012f9ef6e5f2a86d5b5de0b8d05a7156c712ab8cf752ff915abc803cc709
4
- data.tar.gz: 520fbbb6d3f891cb375018cf11066ebdd79ab475fb3396ea114ec06660f6b228
3
+ metadata.gz: 4c7180221c50d5f0e81f1328dbc03aa33df4a71fc4effcdc429fde0bc5c54925
4
+ data.tar.gz: 7a0af87cae2fb6f434e2d0b367cda46a6dd6f468e49ab4ccb3ed509f61279cbf
5
5
  SHA512:
6
- metadata.gz: 4f62742ad47d98ea556e7feef65441884fc5f887eb8b273b403befc2d00afdaf2e04a62229ec567a8bab3a7cb470b26522408d57f98230e7337cd8fce8f3eb31
7
- data.tar.gz: 5e5108f0d52422bcdeac4171751881cf8f8009bd8861e4858e08e27d0833e0c0122476e2afd89f5735a3da27065d30bd0096431483beba7ddcadfd2173311f8d
6
+ metadata.gz: 79761040dc7cd9eb03ff2ef37110dfdbf45de6a38ed6772b2d4054145610a735d849e05069d83de88caa5df1a6fa9b91ac03192829a5d7ac2a20c8ca3cc6d870
7
+ data.tar.gz: 2757ce844329d3bd3a3c73252c8cb9728450321a887da1e26fb8bf6c3a29ee0a2428ca5263d028e4d27555ec3c721ee81ed94aeef94ad3c8891e9bbeab123d1e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
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
+
1
8
  # 0.2.0 2026-11-24
2
9
 
3
10
  - Use frozen string literals in `eval_ast`
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'steadfast/visitor'
4
+
3
5
  module Steadfast
4
6
 
5
7
  # The Rewriter class is used for rewriting ASTs.
6
- class Rewriter
8
+ class Rewriter < Visitor
7
9
  # Rewrites the given AST with a either a hash of node replacements, or a
8
10
  # block which is invoked for each node in the AST.
9
11
  #
@@ -18,23 +20,7 @@ module Steadfast
18
20
  replacements.key?(n) ? replacements[n] : n
19
21
  end
20
22
  end
21
- ast.accept(new(rewrite_block))
22
- end
23
-
24
- # Initializes the rewriter.
25
- #
26
- # @param rewrite_block [Proc]
27
- # @return [void]
28
- def initialize(rewrite_block)
29
- @rewrite_block = rewrite_block
30
- end
31
-
32
- # Intercepts visit_xxxx method calls.
33
- #
34
- # @param sym [Symbol]
35
- # @return [void]
36
- def method_missing(sym, *, **, &)
37
- sym =~ /^visit_/ ? visit(*) : super(sym, *, **, &)
23
+ ast.accept(new(&rewrite_block))
38
24
  end
39
25
 
40
26
  # Visits the given node, and subsequently its subnodes, invoking the rewrite
@@ -43,7 +29,7 @@ module Steadfast
43
29
  # @param n [Ruby::Prism::Node]
44
30
  # @return [Ruby::Prism::Node]
45
31
  def visit(n)
46
- n2 = @rewrite_block.(n, &@rewrite_block)
32
+ n2 = @block.(n, &@block)
47
33
  return n2 if n2 != n
48
34
 
49
35
  changes = {}
@@ -89,6 +89,7 @@ module Steadfast
89
89
  optional_keyword_parameter: [:name_loc, :whitespace, :value],
90
90
  or: [:left, :whitespace, :operator_loc, :whitespace, :right],
91
91
  parentheses: [:opening_loc, :body, :closing_loc],
92
+ program: :emit_verbatim,
92
93
  pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
93
94
  pinned_variable: [:operator_loc, :variable],
94
95
  rational: :emit_verbatim,
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.2.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
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'steadfast/source_mapping'
4
4
  require 'steadfast/sourcifier'
5
+ require 'steadfast/visitor'
5
6
  require 'steadfast/rewriter'
6
7
 
7
8
  # Tell Ruby to keep script lines in memory, so calling Proc#syntax_tree or
@@ -93,6 +94,22 @@ module Steadfast
93
94
  end
94
95
  end
95
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
+
96
113
  private
97
114
 
98
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.2.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: []
@@ -86,6 +86,7 @@ files:
86
86
  - lib/steadfast/source_mapping.rb
87
87
  - lib/steadfast/sourcifier.rb
88
88
  - lib/steadfast/version.rb
89
+ - lib/steadfast/visitor.rb
89
90
  homepage: "http://github.com/digital-fabric/steadfast"
90
91
  licenses:
91
92
  - MIT