strict_ivars 0.4.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/lib/strict_ivars/processor.rb +113 -0
- data/lib/strict_ivars/version.rb +5 -0
- data/lib/strict_ivars.rb +20 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6e9fa5d49d58c37b01f9c2e11b74ebea9809bfd880a4a3a86fc37894727bedb6
|
4
|
+
data.tar.gz: acd5fcdc8320f9f1c2f972915b0d72ed7f1e21516d1790f77f91bcd345f5e7d9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c382d756d7d25ad5ce12706a0e80a886e4d8759616e8c2d505cdd40a7dd735d21e09a1c07c325062edd452e66a3aeae4b9a560cb08296a654e027b8d07949b84
|
7
|
+
data.tar.gz: 415b00bd3f828d6b8f211774476f88e0f37ee4ed4c14d1211daf2ae68d75aa027bf60bcc79fd0ee84cd7958422780a60b715a69f8a93502ff3f63acb33b01052
|
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,39 @@
|
|
1
|
+
# StrictIvars
|
2
|
+
|
3
|
+
StrictIvars is a tiny pre-processor for Ruby that guards your instance variable reads, ensuring the instance variable is actually defined. This helps catch typos early.
|
4
|
+
|
5
|
+
## How does it work?
|
6
|
+
|
7
|
+
When StrictIvars 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.
|
8
|
+
|
9
|
+
For example, it will replace this:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
def example
|
13
|
+
foo if @bar
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
with something like this:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
def example
|
21
|
+
foo if (raise unless defined?(@bar); @bar)
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
## Setup
|
26
|
+
|
27
|
+
Install the gem by adding it to your `Gemfile` and running `bundle install`. You may want to set it to `require: false` because you need to require it at the right moment.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem "strict_ivars", require: false
|
31
|
+
```
|
32
|
+
|
33
|
+
Then require and initialize the gem as early as possible in your boot process. Ideally, this should be right after bootsnap.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require "strict_ivars"
|
37
|
+
|
38
|
+
StrictIvars.init(include: ["#{Dir.pwd}/**/*"])
|
39
|
+
```
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class StrictIvars::Processor < Prism::Visitor
|
4
|
+
def self.call(source)
|
5
|
+
visitor = new
|
6
|
+
visitor.visit(Prism.parse(source).value)
|
7
|
+
|
8
|
+
buffer = source.dup
|
9
|
+
|
10
|
+
visitor.annotations.sort_by(&:first).reverse_each do |offset, action, name|
|
11
|
+
case action
|
12
|
+
when :start
|
13
|
+
buffer.insert(offset, "((::Kernel.raise ::StrictIvars::NameError.new('Undefined instance variable #{name}') unless defined?(#{name})); ")
|
14
|
+
when :end
|
15
|
+
buffer.insert(offset, ")")
|
16
|
+
else
|
17
|
+
raise "Invalid annotation"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
buffer
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@context = Set[]
|
26
|
+
@annotations = []
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_reader :annotations
|
30
|
+
|
31
|
+
def visit_class_node(node)
|
32
|
+
new_context { super }
|
33
|
+
end
|
34
|
+
|
35
|
+
def visit_module_node(node)
|
36
|
+
new_context { super }
|
37
|
+
end
|
38
|
+
|
39
|
+
def visit_block_node(node)
|
40
|
+
new_context { super }
|
41
|
+
end
|
42
|
+
|
43
|
+
def visit_singleton_class_node(node)
|
44
|
+
new_context { super }
|
45
|
+
end
|
46
|
+
|
47
|
+
def visit_defined_node(node)
|
48
|
+
if Prism::InstanceVariableReadNode === node.value
|
49
|
+
@context << node.value.name
|
50
|
+
end
|
51
|
+
|
52
|
+
super
|
53
|
+
end
|
54
|
+
|
55
|
+
def visit_def_node(node)
|
56
|
+
new_context { super }
|
57
|
+
end
|
58
|
+
|
59
|
+
def visit_if_node(node)
|
60
|
+
visit(node.predicate)
|
61
|
+
|
62
|
+
branch { visit(node.statements) }
|
63
|
+
branch { visit(node.subsequent) }
|
64
|
+
end
|
65
|
+
|
66
|
+
def visit_case_node(node)
|
67
|
+
visit(node.predicate)
|
68
|
+
|
69
|
+
node.conditions.each do |condition|
|
70
|
+
branch { visit(condition) }
|
71
|
+
end
|
72
|
+
|
73
|
+
branch { visit(node.else_clause) }
|
74
|
+
end
|
75
|
+
|
76
|
+
def visit_instance_variable_read_node(node)
|
77
|
+
name = node.name
|
78
|
+
|
79
|
+
unless @context.include?(name)
|
80
|
+
location = node.location
|
81
|
+
|
82
|
+
@context << name
|
83
|
+
|
84
|
+
@annotations << [location.start_character_offset, :start, name]
|
85
|
+
@annotations << [location.end_character_offset, :end, name]
|
86
|
+
end
|
87
|
+
|
88
|
+
super
|
89
|
+
end
|
90
|
+
|
91
|
+
private def new_context
|
92
|
+
original_context = @context
|
93
|
+
|
94
|
+
@context = Set[]
|
95
|
+
|
96
|
+
begin
|
97
|
+
yield
|
98
|
+
ensure
|
99
|
+
@context = original_context
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
private def branch
|
104
|
+
original_context = @context
|
105
|
+
@context = original_context.dup
|
106
|
+
|
107
|
+
begin
|
108
|
+
yield
|
109
|
+
ensure
|
110
|
+
@context = original_context
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
data/lib/strict_ivars.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "prism"
|
4
|
+
require "require-hooks/setup"
|
5
|
+
|
6
|
+
module StrictIvars
|
7
|
+
NameError = Class.new(::NameError)
|
8
|
+
|
9
|
+
def self.init(include: [], exclude: [])
|
10
|
+
RequireHooks.source_transform(
|
11
|
+
patterns: include,
|
12
|
+
exclude_patterns: exclude
|
13
|
+
) do |path, source|
|
14
|
+
source ||= File.read(path)
|
15
|
+
Processor.call(source)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
require "strict_ivars/processor"
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strict_ivars
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Drapper
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: require-hooks
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: prism
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
description: Raise an exception when using undefined instance variables.
|
41
|
+
email:
|
42
|
+
- joel@drapper.me
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.md
|
49
|
+
- lib/strict_ivars.rb
|
50
|
+
- lib/strict_ivars/processor.rb
|
51
|
+
- lib/strict_ivars/version.rb
|
52
|
+
homepage: https://github.com/joeldrapper/strict_ivars
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata:
|
56
|
+
homepage_uri: https://github.com/joeldrapper/strict_ivars
|
57
|
+
source_code_uri: https://github.com/joeldrapper/strict_ivars
|
58
|
+
funding_uri: https://github.com/sponsors/joeldrapper
|
59
|
+
rubygems_mfa_required: 'true'
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.1'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubygems_version: 3.6.7
|
75
|
+
specification_version: 4
|
76
|
+
summary: Raise an exception when using undefined instance variables.
|
77
|
+
test_files: []
|