empirical 0.0.1
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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +175 -0
- data/lib/empirical/base_processor.rb +61 -0
- data/lib/empirical/configuration.rb +32 -0
- data/lib/empirical/name_error.rb +22 -0
- data/lib/empirical/processor.rb +257 -0
- data/lib/empirical/version.rb +5 -0
- data/lib/empirical.rb +96 -0
- metadata +83 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3c9b64cdbca71cda53eac79ca46ed80f2e2d0e19901dfa5b7b675c0465c3f4eb
|
|
4
|
+
data.tar.gz: c8abbdbbdb0cc7dd461e810ee398a8046f259fdc78a1c7b6aa117749c6263f38
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 114a23808cdb5746bfcc791207a80693900c960ef37870f0370c35b3c0bc6d215fff22f59ba9b57516002a31f56d20a76b8982473606da9421dffe9cff585876
|
|
7
|
+
data.tar.gz: da7ef76d2913b4be14a5dc08ce01a8a4f01fba6d103500eb197c7604cb38dcb12d9f0e23dd24a6d8e9381bd547db6e24bf9876418b42b9e75431d55e1723fbbd
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Joel Drapper
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Strict Ivars
|
|
2
|
+
|
|
3
|
+
If you reference an undefined method, constant or local varaible, Ruby will helpfully raise an error. But reference an undefined _instance_ variable and Ruby just returns `nil`. This can lead to all kinds of bugs — many of which can lay dormant for years before surprising you with an unexpected outage, data breach or data loss event.
|
|
4
|
+
|
|
5
|
+
Strict Ivars solves this by making Ruby raise a `NameError` any time you read an undefined instance varaible. It’s enabled with two lines of code in your boot process, then it just works in the background and you’ll never have to think about it again. Strict Ivars has no known false-positives or false-negatives.
|
|
6
|
+
|
|
7
|
+
It’s especially good when used with [Literal](https://literal.fun) and [Phlex](https://www.phlex.fun), though it also works with regular Ruby objects and even ERB templates, which are actually pretty common spots for undefined instance variable reads to hide since that’s the main way of passing data to ERB.
|
|
8
|
+
|
|
9
|
+
When combined with Literal, you can essentially remove all unexpected `nil`s. Literal validates your inputs and Strict Ivars ensures you’re reading the right instance variables.
|
|
10
|
+
|
|
11
|
+
> [!NOTE]
|
|
12
|
+
> JRuby and TruffleRuby are not currently supported.
|
|
13
|
+
|
|
14
|
+
## Setup
|
|
15
|
+
|
|
16
|
+
Strict Ivars should really be used in apps not libraries. Though you could definitely use it in your library’s test suite to help catch issues in the library code.
|
|
17
|
+
|
|
18
|
+
Install the gem by adding it to your `Gemfile` and running `bundle install`. You’ll probably want to set it to `require: false` here because you should require it manually at precisely the right moment.
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem "empirical", require: false
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Now the gem is installed, you should require and initialize the gem as early as possible in your boot process. Ideally, this should be right after Bootsnap is set up. In Rails, this will be in your `boot.rb` file.
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
require "empirical"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
You can pass an array of globs to `Empirical.init` as `include:` and `exclude:`
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
Empirical.init(include: ["#{Dir.pwd}/**/*"], exclude: ["#{Dir.pwd}/vendor/**/*"])
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
This example include everything in the current directory apart from the `./vendor` folder (which is where GitHub Actions installs gems).
|
|
37
|
+
|
|
38
|
+
If you’re setting this up in Rails, your `boot.rb` file should look something like this.
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
|
|
42
|
+
|
|
43
|
+
require "bundler/setup" # Set up gems listed in the Gemfile.
|
|
44
|
+
require "bootsnap/setup" # Speed up boot time by caching expensive operations.
|
|
45
|
+
|
|
46
|
+
require "empirical"
|
|
47
|
+
|
|
48
|
+
Empirical.init(include: ["#{Dir.pwd}/**/*"], exclude: ["#{Dir.pwd}/vendor/**/*"])
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
If you’re using Bootsnap, you should clear your bootsnap cache by deleting the folder `tmp/cache/bootsnap`.
|
|
52
|
+
|
|
53
|
+
## How does it work?
|
|
54
|
+
|
|
55
|
+
When Strict Ivars detects that you are loading code from paths its configured to handle, it quickly looks for instance variable reads and guards them with a `defined?` check.
|
|
56
|
+
|
|
57
|
+
For example, it will replace this:
|
|
58
|
+
|
|
59
|
+
```ruby
|
|
60
|
+
def example
|
|
61
|
+
foo if @bar
|
|
62
|
+
end
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
...with something like this:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
def example
|
|
69
|
+
foo if (defined?(@bar) ? @bar : raise)
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The replacement happens on load, so you never see this in your source code. It’s also always wrapped in parentheses and takes up a single line, so it won’t mess up the line numbers in exceptions.
|
|
74
|
+
|
|
75
|
+
**Writes:**
|
|
76
|
+
|
|
77
|
+
Strict Ivars doesn’t apply to writes, since these are considered the authoritative source of the instance variable definitions.
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
@foo = 1
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**Or-writes:**
|
|
84
|
+
|
|
85
|
+
Or-writes are considered an authoritative definition, not a read.
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
@foo ||= 1
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**And-writes:**
|
|
92
|
+
|
|
93
|
+
And-writes are considered an authoritative definition, not a read.
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
@foo &&= 1
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Common mistakes
|
|
100
|
+
|
|
101
|
+
#### Implicitly depending on undefined instance variables
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
def description
|
|
105
|
+
return @description if @description.present?
|
|
106
|
+
@description = get_description
|
|
107
|
+
end
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
This example is relying on Ruby’s behaviour of returning `nil` for undefiend instance variables, which is completely unnecessary. Instead of using `present?`, we could use `defined?` here.
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
def description
|
|
114
|
+
return @description if defined?(@description)
|
|
115
|
+
@description = get_description
|
|
116
|
+
end
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Alternatively, as long as `get_description` doesn’t return `nil` and expect us to memoize it, we could use an “or-write” `||=`
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
def description
|
|
123
|
+
@description ||= get_description
|
|
124
|
+
end
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### Rendering instance variables that are only set somtimes
|
|
128
|
+
|
|
129
|
+
It’s common to render an instance variable in an ERB view that you only set on some controllers.
|
|
130
|
+
|
|
131
|
+
```erb
|
|
132
|
+
<div data-favourites="<%= @user_favourites %>"></div>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The best solution to this to always set it on all controllers, but set it to `nil` in the cases where you don’t have anything to render. This will prevent you from making a typo in your views.
|
|
136
|
+
|
|
137
|
+
Alternatively, you could update the view to be explicit about the fact this ivar may not be set.
|
|
138
|
+
|
|
139
|
+
```erb
|
|
140
|
+
<div data-favourites="<%= (@user_favourites ||= nil) %>"></div>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Better yet, add a `defined?` check:
|
|
144
|
+
|
|
145
|
+
```erb
|
|
146
|
+
<% if defined?(@user_favourites) %>
|
|
147
|
+
<div data-favourites="<%= @user_favourites %>"></div>
|
|
148
|
+
<% end %>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Performance
|
|
152
|
+
|
|
153
|
+
#### Boot performance
|
|
154
|
+
|
|
155
|
+
Using Strict Ivars will impact startup performance since it needs to process each Ruby file you require. However, if you are using Bootsnap, the processed RubyVM::InstructionSequences will be cached and you probably won’t notice the incremental cache misses day-to-day.
|
|
156
|
+
|
|
157
|
+
#### Runtime performance
|
|
158
|
+
|
|
159
|
+
In my benchmarks on Ruby 3.4 with YJIT, it’s difficult to tell if there is any performance difference with or without the `defined?` guards at runtime. Sometimes it’s about 1% faster with the guards than without. Sometimes the other way around.
|
|
160
|
+
|
|
161
|
+
On my laptop, a method that returns an instance varible takes about 15ns and a method that checks if an instance varible is defined and then returns it takes about 15ns. All this is to say, I don’t think there will be any measurable runtime performance impact, at least not in Ruby 3.4.
|
|
162
|
+
|
|
163
|
+
#### Dynamic evals
|
|
164
|
+
|
|
165
|
+
There is a small additional cost to dynamically evaluating code via `eval`, `class_eval`, `module_eval`, `instance_eval` and `binding.eval`. Dynamic evaluation usually only happens at boot time but it can happen at runtime depending on how you use it.
|
|
166
|
+
|
|
167
|
+
## Stability
|
|
168
|
+
|
|
169
|
+
Strict Ivars has 100% line and branch coverage and there are no known false-positives, false-negatives or bugs.
|
|
170
|
+
|
|
171
|
+
## Uninstall
|
|
172
|
+
|
|
173
|
+
Becuase Strict Ivars only ever makes your code safer, you can always back out without anything breaking.
|
|
174
|
+
|
|
175
|
+
To uninstall Strict Ivars, first remove the require and initialization code from wherever you added it and then remove the gem from your `Gemfile`. If you are using Bootsnap, there’s a good chance it cached some pre-processed code with the instance variable read guards in it. To clear this, you’ll need to delete your bootsnap cache, which should be in `tmp/cache/bootsnap`.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Empirical::BaseProcessor < Prism::Visitor
|
|
4
|
+
EVAL_METHODS = Set[:class_eval, :module_eval, :instance_eval, :eval].freeze
|
|
5
|
+
|
|
6
|
+
#: (String) -> String
|
|
7
|
+
def self.call(source)
|
|
8
|
+
visitor = new
|
|
9
|
+
visitor.visit(Prism.parse(source).value)
|
|
10
|
+
buffer = source.dup
|
|
11
|
+
annotations = visitor.annotations
|
|
12
|
+
annotations.sort_by!(&:first)
|
|
13
|
+
|
|
14
|
+
annotations.reverse_each do |offset, length, string|
|
|
15
|
+
buffer[offset, length] = string
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
buffer
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize
|
|
22
|
+
@context = Set[]
|
|
23
|
+
@annotations = []
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
#: Array[[Integer, String]]
|
|
27
|
+
attr_reader :annotations
|
|
28
|
+
|
|
29
|
+
def visit_call_node(node)
|
|
30
|
+
name = node.name
|
|
31
|
+
|
|
32
|
+
if EVAL_METHODS.include?(name) && (arguments = node.arguments)
|
|
33
|
+
location = arguments.location
|
|
34
|
+
|
|
35
|
+
closing = if arguments.contains_forwarding?
|
|
36
|
+
")), &(::Empirical.__eval_block_from_forwarding__(...))"
|
|
37
|
+
else
|
|
38
|
+
"))"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
if node.receiver
|
|
42
|
+
receiver_local = "__eval_receiver_#{SecureRandom.hex(8)}__"
|
|
43
|
+
receiver_location = node.receiver.location
|
|
44
|
+
|
|
45
|
+
@annotations.push(
|
|
46
|
+
[receiver_location.start_character_offset, 0, "(#{receiver_local} = "],
|
|
47
|
+
[receiver_location.end_character_offset, 0, ")"],
|
|
48
|
+
[location.start_character_offset, 0, "*(::Empirical.__process_eval_args__(#{receiver_local}, :#{name}, "],
|
|
49
|
+
[location.end_character_offset, 0, closing]
|
|
50
|
+
)
|
|
51
|
+
else
|
|
52
|
+
@annotations.push(
|
|
53
|
+
[location.start_character_offset, 0, "*(::Empirical.__process_eval_args__(self, :#{name}, "],
|
|
54
|
+
[location.end_character_offset, 0, closing]
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
super
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Empirical::Configuration
|
|
4
|
+
def initialize
|
|
5
|
+
@mutex = Mutex.new
|
|
6
|
+
@include = []
|
|
7
|
+
@exclude = []
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
#: (*String) -> void
|
|
11
|
+
def include(*patterns)
|
|
12
|
+
@mutex.synchronize do
|
|
13
|
+
@include.concat(patterns)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
#: (*String) -> void
|
|
18
|
+
def exclude(*patterns)
|
|
19
|
+
@mutex.synchronize do
|
|
20
|
+
@exclude.concat(patterns)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
#: (String) -> bool
|
|
25
|
+
def match?(path)
|
|
26
|
+
return false unless String === path
|
|
27
|
+
path = File.absolute_path(path)
|
|
28
|
+
return false if @exclude.any? { |pattern| File.fnmatch?(pattern, path) }
|
|
29
|
+
return true if @include.any? { |pattern| File.fnmatch?(pattern, path) }
|
|
30
|
+
false
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "did_you_mean/spell_checker"
|
|
4
|
+
|
|
5
|
+
class Empirical::NameError < ::NameError
|
|
6
|
+
INSTANCE_VARIABLE_METHOD = Kernel.instance_method(:instance_variables)
|
|
7
|
+
|
|
8
|
+
def initialize(object, name)
|
|
9
|
+
checker = DidYouMean::SpellChecker.new(
|
|
10
|
+
dictionary: INSTANCE_VARIABLE_METHOD.bind_call(object)
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
suggestion = checker.correct(name).first
|
|
14
|
+
|
|
15
|
+
message = [
|
|
16
|
+
"Undefined instance variable `#{name}`.",
|
|
17
|
+
("Did you mean `#{suggestion}`?" if suggestion),
|
|
18
|
+
].join(" ")
|
|
19
|
+
|
|
20
|
+
super(message)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Empirical::Processor < Empirical::BaseProcessor
|
|
4
|
+
#: (Prism::ClassNode) -> void
|
|
5
|
+
def visit_class_node(node)
|
|
6
|
+
new_context { super }
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
#: (Prism::ModuleNode) -> void
|
|
10
|
+
def visit_module_node(node)
|
|
11
|
+
new_context { super }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
#: (Prism::BlockNode) -> void
|
|
15
|
+
def visit_block_node(node)
|
|
16
|
+
new_context { super }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
#: (Prism::SingletonClassNode) -> void
|
|
20
|
+
def visit_singleton_class_node(node)
|
|
21
|
+
new_context { super }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
#: (Prism::IfNode) -> void
|
|
25
|
+
def visit_if_node(node)
|
|
26
|
+
visit(node.predicate)
|
|
27
|
+
|
|
28
|
+
branch { visit(node.statements) }
|
|
29
|
+
branch { visit(node.subsequent) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
#: (Prism::CaseNode) -> void
|
|
33
|
+
def visit_case_node(node)
|
|
34
|
+
visit(node.predicate)
|
|
35
|
+
|
|
36
|
+
node.conditions.each do |condition|
|
|
37
|
+
branch { visit(condition) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
branch { visit(node.else_clause) }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
#: (Prism::DefinedNode) -> void
|
|
44
|
+
def visit_defined_node(node)
|
|
45
|
+
value = node.value
|
|
46
|
+
|
|
47
|
+
return if Prism::InstanceVariableReadNode === value
|
|
48
|
+
|
|
49
|
+
super
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
#: (Prism::InstanceVariableReadNode) -> void
|
|
53
|
+
def visit_instance_variable_read_node(node)
|
|
54
|
+
name = node.name
|
|
55
|
+
|
|
56
|
+
unless @context.include?(name)
|
|
57
|
+
location = node.location
|
|
58
|
+
|
|
59
|
+
@context << name
|
|
60
|
+
|
|
61
|
+
@annotations.push(
|
|
62
|
+
[location.start_character_offset, 0, "(defined?(#{name}) ? "],
|
|
63
|
+
[location.end_character_offset, 0, " : (::Kernel.raise(::Empirical::NameError.new(self, :#{name}))))"]
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
super
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
#: () { () -> void } -> void
|
|
71
|
+
private def new_context
|
|
72
|
+
original_context = @context
|
|
73
|
+
|
|
74
|
+
@context = Set[]
|
|
75
|
+
|
|
76
|
+
begin
|
|
77
|
+
yield
|
|
78
|
+
ensure
|
|
79
|
+
@context = original_context
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
#: () { () -> void } -> void
|
|
84
|
+
private def branch
|
|
85
|
+
original_context = @context
|
|
86
|
+
@context = original_context.dup
|
|
87
|
+
|
|
88
|
+
begin
|
|
89
|
+
yield
|
|
90
|
+
ensure
|
|
91
|
+
@context = original_context
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def visit_def_node(node)
|
|
96
|
+
new_context do
|
|
97
|
+
return super unless node.equal_loc
|
|
98
|
+
return super unless node in {
|
|
99
|
+
body: {
|
|
100
|
+
body: [
|
|
101
|
+
Prism::CallNode[
|
|
102
|
+
block: Prism::BlockNode[
|
|
103
|
+
body: Prism::StatementsNode
|
|
104
|
+
] => block
|
|
105
|
+
] => call
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
signature = build_typed_parameters_assertion(node)
|
|
111
|
+
|
|
112
|
+
if node.rparen_loc
|
|
113
|
+
@annotations << [
|
|
114
|
+
start = node.rparen_loc.start_offset + 1,
|
|
115
|
+
block.opening_loc.end_offset - start,
|
|
116
|
+
";binding.assert(#{signature});__literally_returns__ = (;",
|
|
117
|
+
]
|
|
118
|
+
else
|
|
119
|
+
@annotations << [
|
|
120
|
+
start = node.equal_loc.start_offset - 1,
|
|
121
|
+
block.opening_loc.end_offset - start,
|
|
122
|
+
";__literally_returns__ = (;",
|
|
123
|
+
]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
return_type = if call.closing_loc
|
|
127
|
+
node.slice[(call.start_offset)...(call.closing_loc.end_offset)]
|
|
128
|
+
else
|
|
129
|
+
call.name
|
|
130
|
+
end
|
|
131
|
+
@annotations << [
|
|
132
|
+
block.closing_loc.start_offset,
|
|
133
|
+
0,
|
|
134
|
+
";);binding.assert(__literally_returns__: #{return_type});__literally_returns__;",
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
@annotations << [
|
|
138
|
+
start = block.closing_loc.start_offset,
|
|
139
|
+
block.closing_loc.end_offset - start,
|
|
140
|
+
"end",
|
|
141
|
+
]
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
private def build_typed_parameters_assertion(node)
|
|
146
|
+
return unless node.parameters
|
|
147
|
+
|
|
148
|
+
if (requireds = node.parameters.requireds)&.any?
|
|
149
|
+
raise Empirical::TypedSignatureError.new("Typed method signatures don't allow required keyword parameters: #{requireds.inspect}")
|
|
150
|
+
elsif (rest = node.parameters.rest)&.any?
|
|
151
|
+
raise Empirical::TypedSignatureError.new("Typed method signatures don't allow a splat array parameter: #{rest.inspect}")
|
|
152
|
+
elsif (posts = node.parameters.posts)&.any?
|
|
153
|
+
raise Empirical::TypedSignatureError.new("Typed method signatures don't allow a splat hash parameter: #{posts.inspect}")
|
|
154
|
+
elsif (keyword_rest = node.parameters.keyword_rest)&.any?
|
|
155
|
+
raise Empirical::TypedSignatureError.new("Typed method signatures don't allow a splat hash parameter: #{keyword_rest.inspect}")
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
parameters_assertions = []
|
|
159
|
+
|
|
160
|
+
if (optionals = node.parameters.optionals)&.any?
|
|
161
|
+
parameters_assertions << optionals.map do |optional|
|
|
162
|
+
case optional
|
|
163
|
+
# typed splats, e.g.
|
|
164
|
+
# `(names = [String])` => `(*names); assert(names: _Array(String))` and
|
|
165
|
+
# `(position = [*Position])` => `(*position); assert(position: Position)`
|
|
166
|
+
in { value: Prism::ArrayNode[elements: [type_node]] => value }
|
|
167
|
+
if type_node in Prism::SplatNode
|
|
168
|
+
type = type_node.expression.slice
|
|
169
|
+
else
|
|
170
|
+
type = "::Literal::_Array(#{type_node.slice})"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Make the parameter a splat
|
|
174
|
+
@annotations << [optional.name_loc.start_offset, 0, "*"]
|
|
175
|
+
|
|
176
|
+
# Remove the type signature (the default value)
|
|
177
|
+
@annotations << [optional.operator_loc.start_offset, value.closing_loc.end_offset - optional.operator_loc.start_offset, ""]
|
|
178
|
+
next "#{optional.name}: #{type}"
|
|
179
|
+
# With default
|
|
180
|
+
in {
|
|
181
|
+
value: Prism::CallNode[
|
|
182
|
+
block: Prism::BlockNode[
|
|
183
|
+
body: Prism::StatementsNode => default_node
|
|
184
|
+
]
|
|
185
|
+
] => call
|
|
186
|
+
}
|
|
187
|
+
default = "(#{default_node.slice})"
|
|
188
|
+
|
|
189
|
+
type = if call.closing_loc
|
|
190
|
+
node.slice[(call.start_offset)...(call.closing_loc.end_offset)]
|
|
191
|
+
else
|
|
192
|
+
call.name
|
|
193
|
+
end
|
|
194
|
+
# No default
|
|
195
|
+
else
|
|
196
|
+
default = "nil"
|
|
197
|
+
type = optional.value.slice
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
value_location = optional.value.location
|
|
201
|
+
@annotations << [value_location.start_offset, value_location.end_offset - value_location.start_offset, default]
|
|
202
|
+
"#{optional.name}: #{type}"
|
|
203
|
+
end.join(", ")
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
if (keywords = node.parameters.keywords)&.any?
|
|
207
|
+
parameters_assertions << keywords.map do |keyword|
|
|
208
|
+
case keyword
|
|
209
|
+
# Splat
|
|
210
|
+
in { value: Prism::HashNode[elements: [Prism::AssocNode[key: key_type_node, value: val_type_node]]] => value }
|
|
211
|
+
type = "::Literal::_Hash(#{key_type_node.slice}, #{val_type_node.slice})"
|
|
212
|
+
|
|
213
|
+
# Make the parameter a splat
|
|
214
|
+
@annotations << [keyword.name_loc.start_offset, 0, "**"]
|
|
215
|
+
|
|
216
|
+
# Remove the type signature (the default value) and the colon at the end of the keyword
|
|
217
|
+
@annotations << [keyword.name_loc.end_offset - 1, value.closing_loc.end_offset - keyword.name_loc.end_offset + 1, ""]
|
|
218
|
+
next "#{keyword.name}: #{type}"
|
|
219
|
+
in { value: Prism::HashNode[elements: [Prism::AssocSplatNode[value: val_type_node]]] => value }
|
|
220
|
+
type = val_type_node.slice
|
|
221
|
+
|
|
222
|
+
# Make the parameter a splat
|
|
223
|
+
@annotations << [keyword.name_loc.start_offset, 0, "**"]
|
|
224
|
+
|
|
225
|
+
# Remove the type signature (the default value) and the colon at the end of the keyword
|
|
226
|
+
@annotations << [keyword.name_loc.end_offset - 1, value.closing_loc.end_offset - keyword.name_loc.end_offset + 1, ""]
|
|
227
|
+
next "#{keyword.name}: #{type}"
|
|
228
|
+
# With default
|
|
229
|
+
in {
|
|
230
|
+
value: Prism::CallNode[
|
|
231
|
+
block: Prism::BlockNode[
|
|
232
|
+
body: Prism::StatementsNode => default_node
|
|
233
|
+
]
|
|
234
|
+
] => call
|
|
235
|
+
}
|
|
236
|
+
default = "(#{default_node.slice})"
|
|
237
|
+
|
|
238
|
+
type = if call.closing_loc
|
|
239
|
+
node.slice[(call.start_offset)...(call.closing_loc.end_offset)]
|
|
240
|
+
else
|
|
241
|
+
call.name
|
|
242
|
+
end
|
|
243
|
+
# No default
|
|
244
|
+
else
|
|
245
|
+
default = "nil"
|
|
246
|
+
type = keyword.value.slice
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
value_location = keyword.value.location
|
|
250
|
+
@annotations << [value_location.start_offset, value_location.end_offset - value_location.start_offset, default]
|
|
251
|
+
"#{keyword.name}: #{type}"
|
|
252
|
+
end.join(", ")
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
parameters_assertions.join(", ")
|
|
256
|
+
end
|
|
257
|
+
end
|
data/lib/empirical.rb
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "set"
|
|
4
|
+
require "prism"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
|
|
7
|
+
require "empirical/version"
|
|
8
|
+
require "empirical/name_error"
|
|
9
|
+
require "empirical/base_processor"
|
|
10
|
+
require "empirical/processor"
|
|
11
|
+
require "empirical/configuration"
|
|
12
|
+
|
|
13
|
+
require "require-hooks/setup"
|
|
14
|
+
|
|
15
|
+
module Empirical
|
|
16
|
+
EMPTY_ARRAY = [].freeze
|
|
17
|
+
EVERYTHING = ["**/*"].freeze
|
|
18
|
+
METHOD_METHOD = Module.instance_method(:method)
|
|
19
|
+
|
|
20
|
+
CONFIG = Configuration.new
|
|
21
|
+
TypedSignatureError = Class.new(StandardError)
|
|
22
|
+
|
|
23
|
+
# Initializes Empirical so that code loaded after this point will be
|
|
24
|
+
# guarded against undefined instance variable reads. You can pass an array
|
|
25
|
+
# of globs to `include:` and `exclude:`.
|
|
26
|
+
#
|
|
27
|
+
# ```ruby
|
|
28
|
+
# Empirical.init(
|
|
29
|
+
# include: ["#{Dir.pwd}/**/*"],
|
|
30
|
+
# exclude: ["#{Dir.pwd}/vendor/**/*"]
|
|
31
|
+
# )
|
|
32
|
+
# ```
|
|
33
|
+
#: (include: Array[String], exclude: Array[String]) -> void
|
|
34
|
+
def self.init(include: EMPTY_ARRAY, exclude: EMPTY_ARRAY)
|
|
35
|
+
CONFIG.include(*include)
|
|
36
|
+
CONFIG.exclude(*exclude)
|
|
37
|
+
|
|
38
|
+
RequireHooks.source_transform(
|
|
39
|
+
patterns: EVERYTHING,
|
|
40
|
+
exclude_patterns: EMPTY_ARRAY
|
|
41
|
+
) do |path, source|
|
|
42
|
+
source ||= File.read(path)
|
|
43
|
+
|
|
44
|
+
if CONFIG.match?(path)
|
|
45
|
+
Processor.call(source)
|
|
46
|
+
else
|
|
47
|
+
BaseProcessor.call(source)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# For internal use only. This method pre-processes arguments to an eval method.
|
|
53
|
+
#: (Object, Symbol, *untyped)
|
|
54
|
+
def self.__process_eval_args__(receiver, method_name, *args)
|
|
55
|
+
method = METHOD_METHOD.bind_call(receiver, method_name)
|
|
56
|
+
owner = method.owner
|
|
57
|
+
|
|
58
|
+
source, file = nil
|
|
59
|
+
|
|
60
|
+
case method_name
|
|
61
|
+
when :class_eval, :module_eval
|
|
62
|
+
if Module == owner
|
|
63
|
+
source, file = args
|
|
64
|
+
end
|
|
65
|
+
when :instance_eval
|
|
66
|
+
if BasicObject == owner
|
|
67
|
+
source, file = args
|
|
68
|
+
end
|
|
69
|
+
when :eval
|
|
70
|
+
if Kernel == owner
|
|
71
|
+
source, binding, file = args
|
|
72
|
+
elsif Binding == owner
|
|
73
|
+
source, file = args
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
if String === source
|
|
78
|
+
file ||= caller_locations(1, 1).first.path
|
|
79
|
+
|
|
80
|
+
if CONFIG.match?(file)
|
|
81
|
+
args[0] = Processor.call(source)
|
|
82
|
+
else
|
|
83
|
+
args[0] = BaseProcessor.call(source)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
args
|
|
88
|
+
rescue ::NameError
|
|
89
|
+
args
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
#: () { () -> void } -> Proc
|
|
93
|
+
def self.__eval_block_from_forwarding__(*, &block)
|
|
94
|
+
block
|
|
95
|
+
end
|
|
96
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: empirical
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Joel Drapper
|
|
8
|
+
- Stephen Margheim
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: require-hooks
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0.2'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0.2'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: prism
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
description: Based on, concerned with, or verifiable by observation or experience
|
|
42
|
+
rather than theory or pure logic.
|
|
43
|
+
email:
|
|
44
|
+
- joel@drapper.me
|
|
45
|
+
executables: []
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- LICENSE.txt
|
|
50
|
+
- README.md
|
|
51
|
+
- lib/empirical.rb
|
|
52
|
+
- lib/empirical/base_processor.rb
|
|
53
|
+
- lib/empirical/configuration.rb
|
|
54
|
+
- lib/empirical/name_error.rb
|
|
55
|
+
- lib/empirical/processor.rb
|
|
56
|
+
- lib/empirical/version.rb
|
|
57
|
+
homepage: https://github.com/yippee-fun/empirical
|
|
58
|
+
licenses:
|
|
59
|
+
- MIT
|
|
60
|
+
metadata:
|
|
61
|
+
homepage_uri: https://github.com/yippee-fun/empirical
|
|
62
|
+
source_code_uri: https://github.com/yippee-fun/empirical
|
|
63
|
+
funding_uri: https://github.com/sponsors/joeldrapper
|
|
64
|
+
rubygems_mfa_required: 'true'
|
|
65
|
+
rdoc_options: []
|
|
66
|
+
require_paths:
|
|
67
|
+
- lib
|
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '3.1'
|
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
|
+
requirements:
|
|
75
|
+
- - ">="
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: '0'
|
|
78
|
+
requirements: []
|
|
79
|
+
rubygems_version: 3.6.9
|
|
80
|
+
specification_version: 4
|
|
81
|
+
summary: Based on, concerned with, or verifiable by observation or experience rather
|
|
82
|
+
than theory or pure logic.
|
|
83
|
+
test_files: []
|