-
Notifications
You must be signed in to change notification settings - Fork 26
/
buggy_obsolete_strict_memoization_spec.rb
165 lines (148 loc) · 4.59 KB
/
buggy_obsolete_strict_memoization_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true
require "spec_helper"
RSpec.describe(RuboCop::Cop::Sorbet::BuggyObsoleteStrictMemoization, :config) do
let(:specs_without_sorbet) do
[
Gem::Specification.new("foo", "0.0.1"),
Gem::Specification.new("bar", "0.0.2"),
]
end
before(:each) do
allow(Bundler).to(receive(:locked_gems)).and_return(
Struct.new(:specs).new([
*specs_without_sorbet,
Gem::Specification.new("sorbet-static", "0.5.10210"),
]),
)
allow(cop).to(receive(:configured_indentation_width).and_return(2))
end
describe "non-offending cases" do
it "the new memoization pattern doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def foo
@foo ||= T.let(Foo.new, T.nilable(Foo))
end
RUBY
end
describe "the correct obsolete memoization pattern" do
it " doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def foo
@foo = T.let(@foo, T.nilable(Foo))
@foo ||= Foo.new
end
RUBY
end
describe "with fully qualified ::T" do
it " doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def foo
@foo = ::T.let(@foo, ::T.nilable(Foo))
@foo ||= Foo.new
end
RUBY
end
end
describe "with a complex type" do
it "doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def client_info_hash
@client_info_hash = T.let(@client_info_hash, T.nilable(T::Hash[Symbol, T.untyped]))
@client_info_hash ||= client_info.to_hash
end
RUBY
end
end
describe "with multiline initialization expression" do
it "doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def foo
@foo = T.let(@foo, T.nilable(Foo))
@foo ||= multiline_method_call(
foo,
bar,
baz,
)
end
RUBY
end
describe "with a gap between the two lines" do
it "doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def foo
@foo = T.let(@foo, T.nilable(Foo))
@foo ||= multiline_method_call(
foo,
bar,
baz,
)
end
RUBY
end
end
end
describe "with non-empty lines between the two lines" do
it "doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def foo
@foo = T.let(@foo, T.nilable(Foo))
some_other_computation
@foo ||= multiline_method_call(
foo,
bar,
baz,
)
end
RUBY
end
end
context "when its not the first line in a method" do
it "doesn't register any offense" do
expect_no_offenses(<<~RUBY)
def foo
some
other
code
@foo = T.let(@foo, T.nilable(Foo))
@foo ||= Foo.new
end
RUBY
end
end
end
end
describe "a mistaken variant of the obsolete memoization pattern" do
context "not using Sorbet" do
# If the project is not using Sorbet, the obsolete memoization pattern might be intentional.
it "does not register an offence" do
allow(Bundler).to(receive(:locked_gems)).and_return(
Struct.new(:specs).new(specs_without_sorbet),
)
expect_no_offenses(<<~RUBY)
sig { returns(Foo) }
def foo
@foo = T.let(@foo, T.nilable(Foo))
@foo ||= Foo.new
end
RUBY
end
end
it "registers an offence and autocorrects" do
# This variant would have been a mistake, which would have caused the memoized value to be discarded
# and recomputed on every call. We can fix it up into the working version.
expect_offense(<<~RUBY)
def foo
@foo = T.let(nil, T.nilable(Foo))
^^^ This might be a mistaken variant of the two-stage workaround that used to be needed for memoization in `#typed: strict` files. See https://sorbet.org/docs/type-assertions#put-type-assertions-behind-memoization.
@foo ||= Foo.new
end
RUBY
expect_correction(<<~RUBY)
def foo
@foo = T.let(@foo, T.nilable(Foo))
@foo ||= Foo.new
end
RUBY
end
end
end