strict_ivars 1.0.0 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dfeb3ee6871a0bbc8ba42d3b713697b66afc3158894189cc8f1aa52d78acdee2
4
- data.tar.gz: db9a7c4610f9d2514d29ce3340326a9135b93a5106ad5389733e758c7adac828
3
+ metadata.gz: 8b40852e65a52e4b3dd5336b8cfc174ff60de3e7fd7d6549622b1738c828753d
4
+ data.tar.gz: 63e200edf34b4eb3848b40cd0e4f921572de924d1309ac93a59398a484ab8678
5
5
  SHA512:
6
- metadata.gz: 731e53215db39bf6bcd9850eeb521fede63e2acad7b347f14702d921478a53adcc6635350ace7e646bd6f4dbd7416b0e38937dca70cea53624143150536924a3
7
- data.tar.gz: b40c2da16f5179d9f15af2d39c3c506907dfc04116c66de6f1ffac9454f2fc8b52ec2a834c78fbd2a95ad1e8a36b8911e05e4b2a2270a4453bef0bcffa8b45b4
6
+ metadata.gz: 1248ce12ade07900a700fed083f5a62063f8e986932937d0ae9be17c09bc3678d5f9d3b27e58933e5b74a8f0585bd1bf05477848ea8ee185376c9c791207928f
7
+ data.tar.gz: 82c46c5c5ca35a9de828063d026ce13c0b66053a4132c6d20c7c4a641d00c1206fe43d14a247295594c6f2443e0edb4bd4129a31fb8c5d122cc0d182b752902b
data/README.md CHANGED
@@ -4,11 +4,11 @@ If you reference an undefined method, constant or local varaible, Ruby will help
4
4
 
5
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
6
 
7
- Its 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.
7
+ Its 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
8
 
9
- When combined with Literal, you can completely remove unexpected `nil`. Literal validates your inputs and Strict Ivars ensures you’re reading the right instance variables.
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
10
 
11
- > [!NOTE]
11
+ > [!NOTE]
12
12
  > JRuby and TruffleRuby are not currently supported.
13
13
 
14
14
  ## Setup
@@ -35,6 +35,19 @@ StrictIvars.init(include: ["#{Dir.pwd}/**/*"], exclude: ["#{Dir.pwd}/vendor/**/*
35
35
 
36
36
  This example include everything in the current directory apart from the `./vendor` folder (which is where GitHub Actions installs gems).
37
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 "strict_ivars"
47
+
48
+ StrictIvars.init(include: ["#{Dir.pwd}/**/*"], exclude: ["#{Dir.pwd}/vendor/**/*"])
49
+ ```
50
+
38
51
  If you’re using Bootsnap, you should clear your bootsnap cache by deleting the folder `tmp/cache/bootsnap`.
39
52
 
40
53
  ## How does it work?
@@ -23,7 +23,7 @@ class StrictIvars::BaseProcessor < Prism::Visitor
23
23
  @annotations = []
24
24
  end
25
25
 
26
- #: Array[[Integer, :start_ivar_read | :end_ivar_read, Symbol]]
26
+ #: Array[[Integer, String]]
27
27
  attr_reader :annotations
28
28
 
29
29
  def visit_call_node(node)
@@ -1,12 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "did_you_mean/spell_checker"
4
+
3
5
  class StrictIvars::NameError < ::NameError
4
6
  INSTANCE_VARIABLE_METHOD = Kernel.instance_method(:instance_variables)
5
7
 
6
8
  def initialize(object, name)
7
- suggestion = INSTANCE_VARIABLE_METHOD.bind_call(object).max_by do |candidate|
8
- n_common_trigrams(candidate, name) / Math.sqrt(candidate.length)
9
- end
9
+ checker = DidYouMean::SpellChecker.new(
10
+ dictionary: INSTANCE_VARIABLE_METHOD.bind_call(object)
11
+ )
12
+
13
+ suggestion = checker.correct(name).first
10
14
 
11
15
  message = [
12
16
  "Undefined instance variable `#{name}`.",
@@ -15,43 +19,4 @@ class StrictIvars::NameError < ::NameError
15
19
 
16
20
  super(message)
17
21
  end
18
-
19
- private def n_common_trigrams(left, right)
20
- left = "\x03\x02#{left}"
21
- right = "\x03\x02#{right}"
22
-
23
- left_len = left.length
24
- right_len = right.length
25
-
26
- return 0 if left_len < 3 || right_len < 3
27
-
28
- # Process shorter string first
29
- if left_len > right_len
30
- left, right = right, left
31
- left_len, right_len = right_len, left_len
32
- end
33
-
34
- # Use a Set for lookup
35
- trigrams = Set.new
36
- count = 0
37
-
38
- # Generate trigrams from shorter string
39
- i = 0
40
- left_max = left_len - 2
41
- while i < left_max
42
- trigram = left[i, 3]
43
- trigrams.add(trigram)
44
- i += 1
45
- end
46
-
47
- # Check trigrams from longer string
48
- i = 0
49
- right_max = right_len - 2
50
- while i < right_max
51
- count += 1 if trigrams.include?(right[i, 3])
52
- i += 1
53
- end
54
-
55
- count
56
- end
57
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StrictIvars
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.2"
5
5
  end
data/lib/strict_ivars.rb CHANGED
@@ -31,8 +31,8 @@ module StrictIvars
31
31
  # ```
32
32
  #: (include: Array[String], exclude: Array[String]) -> void
33
33
  def self.init(include: EMPTY_ARRAY, exclude: EMPTY_ARRAY)
34
- CONFIG.include(*include) unless include.length == 0
35
- CONFIG.exclude(*exclude) unless exclude.length == 0
34
+ CONFIG.include(*include)
35
+ CONFIG.exclude(*exclude)
36
36
 
37
37
  RequireHooks.source_transform(
38
38
  patterns: EVERYTHING,
@@ -74,7 +74,7 @@ module StrictIvars
74
74
  end
75
75
 
76
76
  if String === source
77
- file ||= caller_locations(1, 1).first&.path
77
+ file ||= caller_locations(1, 1).first.path
78
78
 
79
79
  if CONFIG.match?(file)
80
80
  args[0] = Processor.call(source)
@@ -83,6 +83,8 @@ module StrictIvars
83
83
  end
84
84
  end
85
85
 
86
+ args
87
+ rescue ::NameError
86
88
  args
87
89
  end
88
90
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strict_ivars
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Drapper
@@ -38,7 +38,8 @@ dependencies:
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  description: Strict Ivars is a tiny pre-processor that guards you against undefined
41
- instance variable reads.
41
+ instance variable reads. If you do read an undefined instance variable, it will
42
+ raise a NameError.
42
43
  email:
43
44
  - joel@drapper.me
44
45
  executables: []