steadfast 0.1.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/steadfast/rewriter.rb +1 -0
- data/lib/steadfast/source_mapping.rb +52 -0
- data/lib/steadfast/sourcifier.rb +14 -3
- data/lib/steadfast/version.rb +1 -1
- data/lib/steadfast.rb +13 -35
- 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: 13c3012f9ef6e5f2a86d5b5de0b8d05a7156c712ab8cf752ff915abc803cc709
|
|
4
|
+
data.tar.gz: 520fbbb6d3f891cb375018cf11066ebdd79ab475fb3396ea114ec06660f6b228
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4f62742ad47d98ea556e7feef65441884fc5f887eb8b273b403befc2d00afdaf2e04a62229ec567a8bab3a7cb470b26522408d57f98230e7337cd8fce8f3eb31
|
|
7
|
+
data.tar.gz: 5e5108f0d52422bcdeac4171751881cf8f8009bd8861e4858e08e27d0833e0c0122476e2afd89f5735a3da27065d30bd0096431483beba7ddcadfd2173311f8d
|
data/CHANGELOG.md
CHANGED
data/lib/steadfast/rewriter.rb
CHANGED
|
@@ -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
|
data/lib/steadfast/sourcifier.rb
CHANGED
|
@@ -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
|
|
@@ -117,10 +119,11 @@ module Steadfast
|
|
|
117
119
|
#
|
|
118
120
|
# @param start_line []
|
|
119
121
|
# @return [void]
|
|
120
|
-
def initialize(source_location: nil, sourcemap: false)
|
|
122
|
+
def initialize(source_location: nil, sourcemap: false, frozen_string_literal: false)
|
|
121
123
|
@filename, @line = source_location
|
|
122
124
|
@line ||= 1
|
|
123
125
|
@sourcemap = sourcemap
|
|
126
|
+
@frozen_string_literal = frozen_string_literal
|
|
124
127
|
|
|
125
128
|
raise Steadfast::Error, "No filename given, can't generate source map" if @sourcemap && !@filename
|
|
126
129
|
end
|
|
@@ -131,6 +134,10 @@ module Steadfast
|
|
|
131
134
|
# @return [String] source code
|
|
132
135
|
def to_source(ast)
|
|
133
136
|
@buffer = +''
|
|
137
|
+
if @frozen_string_literal
|
|
138
|
+
@buffer << "#frozen_string_literal: true\n"
|
|
139
|
+
@line += 1
|
|
140
|
+
end
|
|
134
141
|
visit(ast)
|
|
135
142
|
@buffer
|
|
136
143
|
end
|
|
@@ -691,9 +698,13 @@ module Steadfast
|
|
|
691
698
|
|
|
692
699
|
if (filename = source.__filename__) && (filename || last_source.__filename__)
|
|
693
700
|
emit_newline if different_source && last_source
|
|
694
|
-
Steadfast.
|
|
701
|
+
Steadfast::SourceMapping.set(
|
|
702
|
+
@filename, @line, filename, loc.start_line
|
|
703
|
+
)
|
|
695
704
|
elsif filename.nil? && @last_loc
|
|
696
|
-
Steadfast.
|
|
705
|
+
Steadfast::SourceMapping.set(
|
|
706
|
+
@filename, @line, last_source.__filename__, @last_loc.start_line
|
|
707
|
+
)
|
|
697
708
|
end
|
|
698
709
|
end
|
|
699
710
|
|
data/lib/steadfast/version.rb
CHANGED
data/lib/steadfast.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'steadfast/source_mapping'
|
|
3
4
|
require 'steadfast/sourcifier'
|
|
4
5
|
require 'steadfast/rewriter'
|
|
5
6
|
|
|
@@ -24,39 +25,6 @@ module Steadfast
|
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
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
28
|
# Returns an AST for the given object.
|
|
61
29
|
#
|
|
62
30
|
# @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
|
|
@@ -102,12 +70,22 @@ module Steadfast
|
|
|
102
70
|
|
|
103
71
|
case source_location
|
|
104
72
|
when nil
|
|
73
|
+
# Important: ideally we'd set the source location line no to 0 (to
|
|
74
|
+
# compensate for the #frozen_string_literal comment at the top).
|
|
75
|
+
# However, while `eval` respects a starting lineno set to 0, Prism does
|
|
76
|
+
# not, which can lead to off-by-one line nos in exception backtraces for
|
|
77
|
+
# synthetic ASTs.
|
|
78
|
+
#
|
|
79
|
+
# Note that when source_location is not nil, we do decrement the start
|
|
80
|
+
# lineno. Hopefully some day this wart will be removed from Ruby.G
|
|
105
81
|
source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
|
|
106
82
|
when :caller
|
|
107
83
|
l = caller_locations[0]
|
|
108
|
-
source_location = [l.path, l.lineno]
|
|
84
|
+
source_location = [l.path, l.lineno - 1]
|
|
85
|
+
else
|
|
86
|
+
source_location = [source_location[0], source_location[1] - 1]
|
|
109
87
|
end
|
|
110
|
-
src = to_source(ast, source_location:, sourcemap: true)
|
|
88
|
+
src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
|
|
111
89
|
if eval_receiver
|
|
112
90
|
eval_receiver.instance_eval(src)
|
|
113
91
|
else
|
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.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sharon Rosner
|
|
@@ -83,6 +83,7 @@ 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
|
|
88
89
|
homepage: "http://github.com/digital-fabric/steadfast"
|