steadfast 0.0.1 → 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 +13 -1
- data/README.md +24 -2
- data/lib/steadfast/rewriter.rb +61 -0
- data/lib/steadfast/sourcifier.rb +116 -23
- data/lib/steadfast/version.rb +1 -1
- data/lib/steadfast.rb +108 -4
- 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
|
@@ -1,3 +1,15 @@
|
|
|
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
|
+
|
|
7
|
+
# 0.0.2 2026-11-09
|
|
8
|
+
|
|
9
|
+
- Emit newline upon detecting a source change while sourcifying an AST
|
|
10
|
+
- Implement source mapping
|
|
11
|
+
- Implement `Steadfast.translate_backtrace`
|
|
12
|
+
|
|
1
13
|
# 0.0.1 2026-11-06
|
|
2
14
|
|
|
3
|
-
- Implement Steadfast.to_source
|
|
15
|
+
- Implement `Steadfast.to_source`
|
data/README.md
CHANGED
|
@@ -1,2 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<br>
|
|
3
|
+
Steadfast
|
|
4
|
+
</h1>
|
|
5
|
+
|
|
6
|
+
<h4 align="center">A Metaprogramming Gem for Ruby</h4>
|
|
7
|
+
|
|
8
|
+
<p align="center">
|
|
9
|
+
<a href="http://rubygems.org/gems/steadfast">
|
|
10
|
+
<img src="https://badge.fury.io/rb/steadfast.svg" alt="Ruby gem">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/digital-fabric/steadfast/actions">
|
|
13
|
+
<img src="https://github.com/digital-fabric/steadfast/actions/workflows/test.yml/badge.svg" alt="Tests">
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://github.com/digital-fabric/steadfast/blob/master/LICENSE">
|
|
16
|
+
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT License">
|
|
17
|
+
</a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
<p align="center">
|
|
21
|
+
<img src="./steadfast.png">
|
|
22
|
+
</p>
|
|
23
|
+
|
|
24
|
+
More information coming soon...
|
|
@@ -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
|
@@ -1,5 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Steadfast
|
|
4
|
+
|
|
5
|
+
# A Sourcifier is used for converting an AST to source code. Sourcifier is
|
|
6
|
+
# implemented as an AST visitor that walks the AST, copying the relevant
|
|
7
|
+
# strings from each node into a buffer, with the addition of whitespace,
|
|
8
|
+
# commas, semicolons and newlines where applicable.
|
|
2
9
|
class Sourcifier < Ruby::Prism::BasicVisitor
|
|
10
|
+
# The following visit plans are used for generating code for node types that
|
|
11
|
+
# don't require special logic. For each node, a list of fields/elements to
|
|
12
|
+
# emit is given. For each node, a visit_xxx_node method is generated from
|
|
13
|
+
# its visit plan. Nodes requiring special logic have normal visit_xxx_node
|
|
14
|
+
# method below.
|
|
3
15
|
VISIT_PLANS = {
|
|
4
16
|
alias_global_variable: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
|
|
5
17
|
alias_method: [:keyword_loc, :whitespace, :new_name, :whitespace, :old_name],
|
|
@@ -42,8 +54,8 @@ module Steadfast
|
|
|
42
54
|
global_variable_target: :emit_verbatim,
|
|
43
55
|
global_variable_write: [:name_loc, :operator_loc, :value],
|
|
44
56
|
imaginary: :emit_verbatim,
|
|
45
|
-
implicit: :
|
|
46
|
-
implicit_rest: :
|
|
57
|
+
implicit: :nop,
|
|
58
|
+
implicit_rest: :nop,
|
|
47
59
|
in: [:in_loc, :whitespace, :pattern, :whitespace, :then_loc],
|
|
48
60
|
index_target: [:receiver, :opening_loc, :arguments, :closing_loc],
|
|
49
61
|
instance_variable_operator_write: [:name_loc, :whitespace, :binary_operator_loc, :whitespace, :value],
|
|
@@ -52,7 +64,7 @@ module Steadfast
|
|
|
52
64
|
instance_variable_target: :emit_verbatim,
|
|
53
65
|
instance_variable_write: [:name_loc, :operator_loc, :value],
|
|
54
66
|
integer: :emit_verbatim,
|
|
55
|
-
it_parameters: :
|
|
67
|
+
it_parameters: :nop,
|
|
56
68
|
it_local_variable_read: :emit_verbatim,
|
|
57
69
|
keyword_rest_parameter: [:operator_loc, :name_loc],
|
|
58
70
|
keyword_parameter: :emit_verbatim,
|
|
@@ -70,7 +82,7 @@ module Steadfast
|
|
|
70
82
|
next: [:keyword_loc, :whitespace, :arguments],
|
|
71
83
|
nil: :emit_verbatim,
|
|
72
84
|
no_keywords_parameter: :emit_verbatim,
|
|
73
|
-
numbered_parameters: :
|
|
85
|
+
numbered_parameters: :nop,
|
|
74
86
|
optional_parameter: [:name_loc, :whitespace, :operator_loc, :whitespace, :value],
|
|
75
87
|
optional_keyword_parameter: [:name_loc, :whitespace, :value],
|
|
76
88
|
or: [:left, :whitespace, :operator_loc, :whitespace, :right],
|
|
@@ -101,19 +113,30 @@ module Steadfast
|
|
|
101
113
|
|
|
102
114
|
attr_reader :buffer
|
|
103
115
|
|
|
104
|
-
|
|
105
|
-
|
|
116
|
+
# Initializes the Sourcifier instance.
|
|
117
|
+
#
|
|
118
|
+
# @param start_line []
|
|
119
|
+
# @return [void]
|
|
120
|
+
def initialize(source_location: nil, sourcemap: false)
|
|
121
|
+
@filename, @line = source_location
|
|
122
|
+
@line ||= 1
|
|
123
|
+
@sourcemap = sourcemap
|
|
124
|
+
|
|
125
|
+
raise Steadfast::Error, "No filename given, can't generate source map" if @sourcemap && !@filename
|
|
106
126
|
end
|
|
107
127
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
128
|
+
# Converts the given AST to source code.
|
|
129
|
+
#
|
|
130
|
+
# @param ast [Ruby::Prism::Node] AST
|
|
131
|
+
# @return [String] source code
|
|
132
|
+
def to_source(ast)
|
|
133
|
+
@buffer = +''
|
|
134
|
+
visit(ast)
|
|
111
135
|
@buffer
|
|
112
136
|
end
|
|
113
137
|
|
|
114
|
-
VISIT_PLANS.each do |
|
|
115
|
-
|
|
116
|
-
define_method(sym) { |n| visit_plan(plan, n) }
|
|
138
|
+
VISIT_PLANS.each do |kind, plan|
|
|
139
|
+
define_method(:"visit_#{kind}_node") { |n| visit_node_with_plan(n, plan) }
|
|
117
140
|
end
|
|
118
141
|
|
|
119
142
|
def visit_array_node(node)
|
|
@@ -577,10 +600,15 @@ module Steadfast
|
|
|
577
600
|
|
|
578
601
|
private
|
|
579
602
|
|
|
580
|
-
|
|
603
|
+
# Visits a node with the given visit plan.
|
|
604
|
+
#
|
|
605
|
+
# @param node [Ruby::Prism::Node]
|
|
606
|
+
# @param plan [Array<Symbol>, Symbol] visit plan
|
|
607
|
+
# @return [void]
|
|
608
|
+
def visit_node_with_plan(node, plan)
|
|
581
609
|
return send(plan, node) if plan.is_a?(Symbol)
|
|
582
610
|
|
|
583
|
-
plan.
|
|
611
|
+
plan.each do |sym|
|
|
584
612
|
case sym
|
|
585
613
|
when :newline
|
|
586
614
|
emit_newline
|
|
@@ -593,17 +621,28 @@ module Steadfast
|
|
|
593
621
|
end
|
|
594
622
|
end
|
|
595
623
|
|
|
624
|
+
# Visit an array of nodes, with commas emitted between them. The given comma
|
|
625
|
+
# flag determines whether to emit a comma before visiting the next node in
|
|
626
|
+
# the list, and is set to true after visiting a node.
|
|
627
|
+
#
|
|
628
|
+
# @param list [Array<Ruby::Prism::Node>, nil] node list or nil
|
|
629
|
+
# @param comma [bool] comma flag
|
|
630
|
+
# @return [bool] comma flag
|
|
596
631
|
def visit_comma_separated_nodes(list, comma = false)
|
|
597
|
-
if list
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
632
|
+
return false if !list
|
|
633
|
+
|
|
634
|
+
list.each do |child|
|
|
635
|
+
emit_comma if comma
|
|
636
|
+
emit_code(child)
|
|
637
|
+
comma = true
|
|
603
638
|
end
|
|
604
639
|
comma
|
|
605
640
|
end
|
|
606
641
|
|
|
642
|
+
# Visit an array of nodes separated by whitespace.
|
|
643
|
+
#
|
|
644
|
+
# @param list [Array<Ruby::Prism::Node>, nil] node list or nil
|
|
645
|
+
# @return [void]
|
|
607
646
|
def visit_space_separated_nodes(list)
|
|
608
647
|
return if !list
|
|
609
648
|
|
|
@@ -615,38 +654,92 @@ module Steadfast
|
|
|
615
654
|
end
|
|
616
655
|
end
|
|
617
656
|
|
|
657
|
+
# Appends a node's location verbatim into the source code buffer.
|
|
658
|
+
#
|
|
659
|
+
# @param node [Ruby::Prism::Node]
|
|
660
|
+
# @return [void]
|
|
618
661
|
def emit_verbatim(node)
|
|
619
662
|
emit_code(node.location)
|
|
620
663
|
end
|
|
621
664
|
|
|
665
|
+
# Appends a code location (or a node) into the source code buffer.
|
|
666
|
+
#
|
|
667
|
+
# @param loc [Ruby::Prism::Location, Ruby::Prism::Node, nil]
|
|
668
|
+
# @return [void]
|
|
622
669
|
def emit_code(loc)
|
|
623
670
|
return if !loc
|
|
624
671
|
return visit(loc) if loc.is_a?(Ruby::Prism::Node)
|
|
625
672
|
|
|
673
|
+
record_loc_source_mapping(loc) if @sourcemap
|
|
626
674
|
emit(loc.slice.chomp)
|
|
627
675
|
end
|
|
628
676
|
|
|
677
|
+
# Adds an entry to the sourcemap hash when detecting a change of line or
|
|
678
|
+
# filename before emitting an AST location. If a change of filename is
|
|
679
|
+
# detected, a newline is emitted in order to more accurately be able to
|
|
680
|
+
# translate backtraces if needed.
|
|
681
|
+
#
|
|
682
|
+
# @param loc [Ruby::Prism::Location] code location
|
|
683
|
+
# @return [void]
|
|
684
|
+
def record_loc_source_mapping(loc)
|
|
685
|
+
source = loc.send(:source)
|
|
686
|
+
last_source = @last_loc&.send(:source)
|
|
687
|
+
different_source = source.__filename__ && source != last_source
|
|
688
|
+
changed = different_source || (@last_loc && (loc.start_line != @last_loc.start_line))
|
|
689
|
+
@last_loc = loc if source.__filename__
|
|
690
|
+
return if !changed
|
|
691
|
+
|
|
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
|
|
698
|
+
end
|
|
699
|
+
|
|
700
|
+
# Appends the given string into the source code buffer.
|
|
701
|
+
#
|
|
702
|
+
# @param str [String]
|
|
703
|
+
# @return [void]
|
|
629
704
|
def emit(str)
|
|
630
705
|
@buffer << str
|
|
706
|
+
@line += str.count("\n")
|
|
631
707
|
end
|
|
632
708
|
|
|
709
|
+
# Appends a newline into the source code buffer.
|
|
710
|
+
#
|
|
711
|
+
# @return [void]
|
|
633
712
|
def emit_newline
|
|
634
|
-
|
|
713
|
+
return if @buffer[-1] == "\n"
|
|
714
|
+
|
|
715
|
+
@buffer << "\n"
|
|
716
|
+
@line += 1
|
|
635
717
|
end
|
|
636
718
|
|
|
719
|
+
# Appends a space character into the source code buffer.
|
|
720
|
+
#
|
|
721
|
+
# @return [void]
|
|
637
722
|
def emit_whitespace
|
|
638
723
|
@buffer << " "
|
|
639
724
|
end
|
|
640
725
|
|
|
726
|
+
# Appends a comma into the source code buffer.
|
|
727
|
+
#
|
|
728
|
+
# @return [void]
|
|
641
729
|
def emit_comma
|
|
642
730
|
@buffer << ","
|
|
643
731
|
end
|
|
644
732
|
|
|
733
|
+
# Appends a semicolon into the source code buffer.
|
|
734
|
+
#
|
|
735
|
+
# @return [void]
|
|
645
736
|
def emit_semicolon
|
|
646
737
|
@buffer << ';'
|
|
647
738
|
end
|
|
648
739
|
|
|
649
|
-
|
|
650
|
-
|
|
740
|
+
# A nop method for doing nothing.
|
|
741
|
+
#
|
|
742
|
+
# @return [void]
|
|
743
|
+
def nop(node); end
|
|
651
744
|
end
|
|
652
745
|
end
|
data/lib/steadfast/version.rb
CHANGED
data/lib/steadfast.rb
CHANGED
|
@@ -1,18 +1,59 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'steadfast/sourcifier'
|
|
4
|
+
require 'steadfast/rewriter'
|
|
4
5
|
|
|
6
|
+
# Tell Ruby to keep script lines in memory, so calling Proc#syntax_tree or
|
|
7
|
+
# Method#syntax_tree will work.
|
|
5
8
|
RubyVM.keep_script_lines = true
|
|
6
9
|
|
|
10
|
+
# Steadfast is a metaprogramming gem for Ruby. Steadfast provides a
|
|
11
|
+
# general-purpose mecehanism for generating and rewriting Ruby code at runtime.
|
|
7
12
|
module Steadfast
|
|
13
|
+
# Extensions to Ruby::Prism::Source
|
|
14
|
+
module PrismSourceExtensions
|
|
15
|
+
|
|
16
|
+
# The __filename__ attribute lets us track the source location for a given
|
|
17
|
+
# AST.
|
|
18
|
+
attr_accessor :__filename__
|
|
19
|
+
end
|
|
20
|
+
::Ruby::Prism::Source.include(PrismSourceExtensions)
|
|
21
|
+
|
|
22
|
+
# A base error class for Steadfast errors
|
|
8
23
|
class Error < StandardError
|
|
9
24
|
end
|
|
10
25
|
|
|
11
26
|
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 = sourcemap[m[1]])
|
|
42
|
+
"#{sourcemap[m[1]]}#{m[2]}"
|
|
43
|
+
else
|
|
44
|
+
it
|
|
45
|
+
end
|
|
46
|
+
}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Returns an AST for the given object.
|
|
50
|
+
#
|
|
51
|
+
# @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
|
|
52
|
+
# @return [Ruby::Prism::Node]
|
|
12
53
|
def to_ast(obj)
|
|
13
54
|
case obj
|
|
14
55
|
when Proc, Method, UnboundMethod
|
|
15
|
-
obj.syntax_tree
|
|
56
|
+
obj.syntax_tree.tap { set_source_filename(it, obj) }
|
|
16
57
|
when Ruby::Prism::Node
|
|
17
58
|
obj
|
|
18
59
|
else
|
|
@@ -20,13 +61,61 @@ module Steadfast
|
|
|
20
61
|
end
|
|
21
62
|
end
|
|
22
63
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
64
|
+
# Returns the source code to the given object by sourcifying its AST.
|
|
65
|
+
#
|
|
66
|
+
# @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
|
|
67
|
+
# @return [String]
|
|
68
|
+
def to_source(obj, **opts)
|
|
69
|
+
ast = to_ast(obj)
|
|
70
|
+
Sourcifier.new(**opts).to_source(ast)
|
|
71
|
+
end
|
|
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
|
|
26
98
|
end
|
|
27
99
|
|
|
28
100
|
private
|
|
29
101
|
|
|
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]
|
|
108
|
+
def set_source_filename(ast, obj)
|
|
109
|
+
source = ast.send(:source)
|
|
110
|
+
source.__filename__ ||= obj.source_location.first
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Finds the AST for the given proc. This method is currently not in use, but
|
|
114
|
+
# we may eventually use this code in order to provide backward compatibility
|
|
115
|
+
# for Ruby < 4.1.
|
|
116
|
+
#
|
|
117
|
+
# @param proc [Proc]
|
|
118
|
+
# @return [Ruby::Prism::Node]
|
|
30
119
|
def proc_ast(proc)
|
|
31
120
|
fn, lineno = proc.source_location
|
|
32
121
|
pr = Prism.parse(get_source(fn), filepath: fn)
|
|
@@ -53,6 +142,12 @@ module Steadfast
|
|
|
53
142
|
raise Sirop::Error, "Could not get source for proc"
|
|
54
143
|
end
|
|
55
144
|
|
|
145
|
+
# Finds the AST for the given method. This method is currently not in use,
|
|
146
|
+
# but we may eventually use this code in order to provide backward
|
|
147
|
+
# compatibility for Ruby < 4.1.
|
|
148
|
+
#
|
|
149
|
+
# @param method [Method, UnboundMethod]
|
|
150
|
+
# @return [Ruby::Prism::Node]
|
|
56
151
|
def method_ast(method)
|
|
57
152
|
fn, lineno = method.source_location
|
|
58
153
|
pr = Prism.parse(get_source(fn), filepath: fn)
|
|
@@ -66,12 +161,21 @@ module Steadfast
|
|
|
66
161
|
end
|
|
67
162
|
end
|
|
68
163
|
|
|
164
|
+
# Returns the content of the given filename. Returns the IRB line buffer if
|
|
165
|
+
# '(irb)' is given. This method is not currently used.
|
|
166
|
+
#
|
|
167
|
+
# @param fn [String]
|
|
168
|
+
# @return [String] source code
|
|
69
169
|
def get_source(fn)
|
|
70
170
|
return get_irb_source if fn == '(irb)'
|
|
71
171
|
|
|
72
172
|
IO.read(fn)
|
|
73
173
|
end
|
|
74
174
|
|
|
175
|
+
# Returns the IRB line buffer (the source code for the current IRB session.)
|
|
176
|
+
# This method is currently not used.
|
|
177
|
+
#
|
|
178
|
+
# @return [String] IRB line buffer
|
|
75
179
|
def get_irb_source
|
|
76
180
|
io = IRB.CurrentContext.io
|
|
77
181
|
last_line = io.instance_variable_get(:@line_no)
|
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"
|