strict_ivars 1.0.0.rc3 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 937599360a03f578b3d440efc2d58eede817bc6ec71727f4ba74c1c0fb11dc0d
4
- data.tar.gz: cb212c70dab8bcf3a9d163f80f864ddf81c8f0a0e3e2ae6781f2bcff37590b88
3
+ metadata.gz: 5a6e9c3b4af8b1558193b058b36ab883ecc8dbce111b13bafb0cedd54852faa9
4
+ data.tar.gz: 22858692a9849c8f1f3b33450fe78054a622216221d81edd7992868a530dfc3c
5
5
  SHA512:
6
- metadata.gz: 53fdba71a8e909d0afc5b797b1b4b7658a5ab956fab7946e1ead3e59be9298c5928a1b788c2be3781c46df3e2bb28505b5ca911357026aa0992c9f57a45e81e5
7
- data.tar.gz: 86d26b307d4697843d10216e7c783a5db2207945ed3bb7a9e70959dd120e9652249dfef523b2803905728c1e4f4ed1de180a50c472f983068fbb38147367996a
6
+ metadata.gz: b52c5c582936f6905a00b381846109d673b6ff8b9a6c7f568d4c8bab82b60c8158afbf3924628d93ee133e5d98583169462ac42887a8f50e0c96068c23a450ea
7
+ data.tar.gz: 0b0d00db3c3daf0dd7c72ad2c3702e57240dcd93a5965d7b0e18b97ba56f5ec53dffc42cf4a7d845adc03ff0cd09c8f9c777d25d2c711be1c26de357583de977
data/README.md CHANGED
@@ -1,13 +1,19 @@
1
1
  # Strict Ivars
2
2
 
3
- Strict Ivars is a tiny pre-processor for Ruby that guards your instance variable reads, ensuring the instance variable is actually defined. This helps catch typos nice and early. It‘s especially good when used with [Literal](https://literal.fun) and [Phlex](https://www.phlex.fun), though it also works with ERB.
3
+ If you reference an undefined method, constant or local varaible, Ruby will helpfully raise a `NameError`. 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
4
 
5
- > [!NOTE]
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]
6
12
  > JRuby and TruffleRuby are not currently supported.
7
13
 
8
14
  ## Setup
9
15
 
10
- Strict Ivars should be used in apps not libraries. Though you could use it in your library’s test suite.
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.
11
17
 
12
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.
13
19
 
@@ -29,6 +35,19 @@ StrictIvars.init(include: ["#{Dir.pwd}/**/*"], exclude: ["#{Dir.pwd}/vendor/**/*
29
35
 
30
36
  This example include everything in the current directory apart from the `./vendor` folder (which is where GitHub Actions installs gems).
31
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
+
32
51
  If you’re using Bootsnap, you should clear your bootsnap cache by deleting the folder `tmp/cache/bootsnap`.
33
52
 
34
53
  ## How does it work?
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "did_you_mean/spell_checker"
4
+
5
+ class StrictIvars::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
@@ -65,7 +65,7 @@ class StrictIvars::Processor < StrictIvars::BaseProcessor
65
65
 
66
66
  @annotations.push(
67
67
  [location.start_character_offset, "(defined?(#{name}) ? "],
68
- [location.end_character_offset, " : (::Kernel.raise(::StrictIvars::NameError.new('Undefined instance variable #{name}'))))"]
68
+ [location.end_character_offset, " : (::Kernel.raise(::StrictIvars::NameError.new(self, :#{name}))))"]
69
69
  )
70
70
  end
71
71
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StrictIvars
4
- VERSION = "1.0.0.rc3"
4
+ VERSION = "1.0.1"
5
5
  end
data/lib/strict_ivars.rb CHANGED
@@ -4,16 +4,15 @@ require "set"
4
4
  require "prism"
5
5
  require "securerandom"
6
6
 
7
- require "require-hooks/setup"
8
-
9
7
  require "strict_ivars/version"
8
+ require "strict_ivars/name_error"
10
9
  require "strict_ivars/base_processor"
11
10
  require "strict_ivars/processor"
12
11
  require "strict_ivars/configuration"
13
12
 
14
- module StrictIvars
15
- NameError = Class.new(::NameError)
13
+ require "require-hooks/setup"
16
14
 
15
+ module StrictIvars
17
16
  EMPTY_ARRAY = [].freeze
18
17
  EVERYTHING = ["**/*"].freeze
19
18
  METHOD_METHOD = Module.instance_method(:method)
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.rc3
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Drapper
@@ -37,7 +37,9 @@ dependencies:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
- description: Raise an exception when using undefined instance variables.
40
+ description: Strict Ivars is a tiny pre-processor that guards you against undefined
41
+ instance variable reads. If you do read an undefined instance variable, it will
42
+ raise a NameError.
41
43
  email:
42
44
  - joel@drapper.me
43
45
  executables: []
@@ -49,14 +51,15 @@ files:
49
51
  - lib/strict_ivars.rb
50
52
  - lib/strict_ivars/base_processor.rb
51
53
  - lib/strict_ivars/configuration.rb
54
+ - lib/strict_ivars/name_error.rb
52
55
  - lib/strict_ivars/processor.rb
53
56
  - lib/strict_ivars/version.rb
54
- homepage: https://github.com/joeldrapper/strict_ivars
57
+ homepage: https://github.com/yippee-fun/strict_ivars
55
58
  licenses:
56
59
  - MIT
57
60
  metadata:
58
- homepage_uri: https://github.com/joeldrapper/strict_ivars
59
- source_code_uri: https://github.com/joeldrapper/strict_ivars
61
+ homepage_uri: https://github.com/yippee-fun/strict_ivars
62
+ source_code_uri: https://github.com/yippee-fun/strict_ivars
60
63
  funding_uri: https://github.com/sponsors/joeldrapper
61
64
  rubygems_mfa_required: 'true'
62
65
  rdoc_options: []
@@ -75,5 +78,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
78
  requirements: []
76
79
  rubygems_version: 3.6.7
77
80
  specification_version: 4
78
- summary: Raise an exception when using undefined instance variables.
81
+ summary: Make Ruby raise a NameError if you read an undefined instance variable.
79
82
  test_files: []