Skip to content

Commit 40ee25e

Browse files
authored
Merge pull request #16 from Shopify/Alex/namespace-unexpected-nil-error
Move `UnexpectedNilError` into gem namespace
2 parents 69a19f2 + b38c687 commit 40ee25e

5 files changed

Lines changed: 160 additions & 162 deletions

File tree

lib/rubocop/cop/type_toolkit/dont_expect_unexpected_nil.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# typed: true
22
# frozen_string_literal: true
33

4+
require "type_toolkit/ext/nil_assertions"
5+
46
module RuboCop
57
module Cop
68
module TypeToolkit
79
# This cop detects attempts to raise, rescue, or otherwise use the `UnexpectedNilError` class.
810
class DontExpectUnexpectedNil < Base
911
RESTRICT_ON_SEND = [:assert_raises, :raise].freeze
1012

13+
UNEXPECTED_NIL_ERROR_NAME = ::TypeToolkit::UnexpectedNilError.name.not_nil!
14+
.split("::").last.not_nil!
15+
.to_sym #: Symbol
16+
private_constant :UNEXPECTED_NIL_ERROR_NAME
17+
1118
#: (RuboCop::AST::SendNode) -> void
1219
def on_send(node)
1320
case node.method_name
@@ -55,7 +62,12 @@ def on_investigation_end
5562

5663
#: (RuboCop::AST::ConstNode) -> bool
5764
def unexpected_nil_error?(node)
58-
node.short_name == :UnexpectedNilError && (node.namespace.nil? || node.namespace.cbase_type?)
65+
return false unless node.short_name == UNEXPECTED_NIL_ERROR_NAME
66+
67+
ns = node.namespace
68+
return true if ns.nil? || ns.cbase_type?
69+
70+
ns.const_type? && ns.short_name == :TypeToolkit && (ns.namespace.nil? || ns.namespace.cbase_type?)
5971
end
6072

6173
# Check for `raise UnexpectedNilError`

lib/type_toolkit/ext/nil_assertions.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ class NilClass
1818
# @override
1919
#: -> bot
2020
def not_nil!
21-
raise UnexpectedNilError
21+
raise TypeToolkit::UnexpectedNilError
2222
end
2323
end
2424

25-
# An error raised when calling `#not_nil!` on a `nil` value.
26-
#
27-
# `UnexpectedNilError` should never occur in well-formed code, so it should never be rescued.
28-
# This is why it inherits from `Exception` instead of `StandardError`,
29-
# so that bare rescues clauses (like `rescue => e`) don't rescue it.
30-
class UnexpectedNilError < Exception # rubocop:disable Lint/InheritException
31-
def initialize(message = "Called `not_nil!` on nil.")
32-
super(message)
25+
module TypeToolkit
26+
# An error raised when calling `#not_nil!` on a `nil` value.
27+
#
28+
# `UnexpectedNilError` should never occur in well-formed code, so it should never be rescued.
29+
# This is why it inherits from `Exception` instead of `StandardError`,
30+
# so that bare rescues clauses (like `rescue => e`) don't rescue it.
31+
class UnexpectedNilError < Exception # rubocop:disable Lint/InheritException
32+
def initialize(message = "Called `not_nil!` on nil.")
33+
super
34+
end
3335
end
3436
end

sorbet/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
--ignore=vendor/
55
--enable-experimental-rbs-comments
66
--suppress-payload-superclass-redefinition-for=RDoc::Markup::Heading
7+
--disable-watchman

sorbet/rbi/shims/minitest.rbi

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,42 @@
22
# frozen_string_literal: true
33

44
module Minitest
5-
class Spec < Minitest::Test; end
5+
class Spec < Minitest::Test
6+
extend Minitest::Spec::DSL
7+
8+
include RuboCop::Minitest::AssertOffense
9+
10+
sig do
11+
params(
12+
desc: T.anything,
13+
block: T.proc.bind(T.self_type).void,
14+
).void
15+
end
16+
def it(desc = T.unsafe(nil), &block); end
17+
18+
module DSL
19+
has_attached_class!(:out)
20+
21+
sig do
22+
params(
23+
desc: T.anything,
24+
block: T.proc.bind(T.attached_class).void,
25+
).void
26+
end
27+
def describe(desc, &block); end
28+
29+
sig do
30+
params(
31+
desc: T.anything,
32+
block: T.proc.bind(T.attached_class).void,
33+
).void
34+
end
35+
def it(desc = T.unsafe(nil), &block); end
36+
37+
sig do
38+
params(block: T.proc.bind(T.attached_class).void).void
39+
end
40+
def before(&block); end
41+
end
42+
end
643
end

0 commit comments

Comments
 (0)