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 +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/steadfast/rewriter.rb +61 -0
- data/lib/steadfast/sourcifier.rb +8 -4
- data/lib/steadfast/version.rb +1 -1
- data/lib/steadfast.rb +34 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c6e3d1e91bd27e7c92b511ef755e05095e691e6489173aded42938aa6ecbdd58
|
|
4
|
+
data.tar.gz: 75a299fc9c1b3bfc89e81dbc6e1bfb0085a470b4a239aeffc02b63adefc509ad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 070eb4137ab096dcb11625c593c880952ecbd8150057849608eb8972368f047c66ff8837c0b900072620380d026f2d4043ac928aa8654f504803c683ab2a3c4c
|
|
7
|
+
data.tar.gz: 314ca97af4862e20ceab634ac1ebe3e6f72a2eac2c67c933a1df27fa1deb84a35a07a09f044311d13c53f0f4cfbde00bf60d09260a7faa72e4402709e6277153
|
data/CHANGELOG.md
CHANGED
|
@@ -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
|
data/lib/steadfast/sourcifier.rb
CHANGED
|
@@ -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
|
-
|
|
693
|
-
|
|
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.
|
data/lib/steadfast/version.rb
CHANGED
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.
|
|
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"
|