Skip to content

Commit 95a8b9f

Browse files
authored
Code cleaning (#1771)
* updated ameba. Fixing ameba issues which now bork on not_nil calls. * Adding a lot more return types
1 parent 6a82045 commit 95a8b9f

25 files changed

+66
-65
lines changed

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ dependencies:
6161
development_dependencies:
6262
ameba:
6363
github: crystal-ameba/ameba
64-
version: ~> 1.1.0
64+
version: ~> 1.3.0
6565

6666
scripts:
6767
postinstall: BUILD_WITHOUT_DEVELOPMENT=true script/precompile_tasks

spec/charms/cookie_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe HTTP::Cookie do
3030
it "sets expiration 20 years from now" do
3131
cookie = test_cookie.permanent
3232

33-
cookie.expires.not_nil!.should be_close(20.years.from_now, 1.minute)
33+
cookie.expires.as(Time).should be_close(20.years.from_now, 1.minute)
3434
end
3535
end
3636
end

spec/lucky/action_pipes_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ describe Lucky::Action do
218218
events << event
219219
end
220220
Pipes::HaltedBefore.new(build_context, params).call
221-
halted_pipe = events.find { |e| e.name == "redirect_me" }.not_nil!
221+
halted_pipe = events.find! { |e| e.name == "redirect_me" }
222222
halted_pipe.continued.should eq false
223223
halted_pipe.position.to_s.should eq "Before"
224224
halted_pipe.before?.should eq true
@@ -230,7 +230,7 @@ describe Lucky::Action do
230230
events << event
231231
end
232232
Pipes::HaltedAfter.new(build_context, params).call
233-
halted_pipe = events.find { |e| e.name == "redirect_me" }.not_nil!
233+
halted_pipe = events.find! { |e| e.name == "redirect_me" }
234234
halted_pipe.continued.should eq false
235235
halted_pipe.position.to_s.should eq "After"
236236
halted_pipe.after?.should eq true

spec/lucky/base_http_client_spec.cr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ describe Lucky::BaseHTTPClient do
3636
request = TestServer.last_request
3737
request.path.should eq "/hello"
3838
request.method.should eq("POST")
39-
request.body.not_nil!.gets_to_end.should eq("{}")
39+
request.body.to_s.should eq("{}")
4040
response.body.should eq "world"
4141
end
4242

4343
it "allows passing params" do
4444
response = MyClient.new.exec(HelloWorldAction, foo: "bar")
4545

4646
request = TestServer.last_request
47-
request.body.not_nil!.gets_to_end.should eq({foo: "bar"}.to_json)
47+
request.body.to_s.should eq({foo: "bar"}.to_json)
4848
end
4949
end
5050

@@ -55,15 +55,15 @@ describe Lucky::BaseHTTPClient do
5555
request = TestServer.last_request
5656
request.path.should eq "/hello"
5757
request.method.should eq("POST")
58-
request.body.not_nil!.gets_to_end.should eq("{}")
58+
request.body.to_s.should eq("{}")
5959
response.body.should eq "world"
6060
end
6161

6262
it "allows passing params" do
6363
response = MyClient.new.exec(HelloWorldAction.route, foo: "bar")
6464

6565
request = TestServer.last_request
66-
request.body.not_nil!.gets_to_end.should eq({foo: "bar"}.to_json)
66+
request.body.to_s.should eq({foo: "bar"}.to_json)
6767
end
6868
end
6969
end
@@ -79,7 +79,7 @@ describe Lucky::BaseHTTPClient do
7979
response = MyClient.new.exec_raw(HelloWorldAction, test_data)
8080

8181
request = TestServer.last_request
82-
request.body.not_nil!.gets_to_end.should eq(test_data)
82+
request.body.to_s.should eq(test_data)
8383
end
8484
end
8585

@@ -93,7 +93,7 @@ describe Lucky::BaseHTTPClient do
9393
response = MyClient.new.exec_raw(HelloWorldAction.route, test_data)
9494

9595
request = TestServer.last_request
96-
request.body.not_nil!.gets_to_end.should eq(test_data)
96+
request.body.to_s.should eq(test_data)
9797
end
9898
end
9999
end
@@ -109,7 +109,7 @@ describe Lucky::BaseHTTPClient do
109109
request = TestServer.last_request
110110
request.method.should eq({{ method.id.stringify }}.upcase)
111111
request.path.should eq "hello"
112-
request.body.not_nil!.gets_to_end.should eq({foo: "bar"}.to_json)
112+
request.body.to_s.should eq({foo: "bar"}.to_json)
113113
end
114114

115115
it "works without params" do
@@ -118,7 +118,7 @@ describe Lucky::BaseHTTPClient do
118118
request = TestServer.last_request
119119
request.method.should eq({{ method.id.stringify }}.upcase)
120120
request.path.should eq "hello"
121-
request.body.not_nil!.gets_to_end.should eq("{}")
121+
request.body.to_s.should eq("{}")
122122
end
123123
end
124124
{% end %}
@@ -140,7 +140,7 @@ describe Lucky::BaseHTTPClient do
140140
request = TestServer.last_request
141141
request.method.should eq("HEAD")
142142
request.path.should eq "hello"
143-
request.body.not_nil!.gets_to_end.should eq("{}")
143+
request.body.to_s.should eq("{}")
144144
end
145145
end
146146
end

spec/lucky/cookies/cookie_jar_spec.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe Lucky::CookieJar do
2828
jar.get_raw("symbol").value.should eq("symbol_value")
2929
jar.get_raw(:cookie).value.should eq(value)
3030
jar.get_raw("cookie").value.should eq(value)
31-
jar.get_raw?(:cookie).not_nil!.value.should eq(value)
32-
jar.get_raw?("cookie").not_nil!.value.should eq(value)
31+
jar.get_raw?(:cookie).as(HTTP::Cookie).value.should eq(value)
32+
jar.get_raw?("cookie").as(HTTP::Cookie).value.should eq(value)
3333
jar.get_raw?(:missing).should be_nil
3434
jar.get_raw?("missing").should be_nil
3535
end
@@ -136,7 +136,7 @@ describe Lucky::CookieJar do
136136
jar.set(:tabs_or_spaces, "stop it").http_only(false).expires(time)
137137

138138
jar.get_raw(:tabs_or_spaces).http_only.should be_false
139-
jar.get_raw(:tabs_or_spaces).expires.not_nil!.should eq(time)
139+
jar.get_raw(:tabs_or_spaces).expires.as(Time).should eq(time)
140140
end
141141

142142
it "raises an error if the cookie is > 4096 bytes" do

spec/lucky/paginator/paginator_spec.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
require "../../spec_helper"
22

3-
private def build_pages(page = 1, per_page = 1, item_count = 1, full_path = "/items")
4-
Lucky::Paginator.new \
3+
private def build_pages(page = 1, per_page = 1, item_count = 1, full_path = "/items") : Lucky::Paginator
4+
Lucky::Paginator.new(
55
page: page,
66
per_page: per_page,
77
item_count: item_count,
8-
full_path: full_path
8+
full_path: full_path)
99
end
1010

1111
describe Lucky::Paginator do
@@ -134,7 +134,7 @@ describe Lucky::Paginator do
134134
it "returns the path to the next page if there is a next page" do
135135
path = build_pages(page: 1, per_page: 1, item_count: 2).path_to_next
136136

137-
path.not_nil!.should end_with("?page=2")
137+
path.to_s.should end_with("?page=2")
138138
end
139139

140140
it "returns nil if there is not next page" do
@@ -148,7 +148,7 @@ describe Lucky::Paginator do
148148
it "returns the path to the previous page if there is a previous page" do
149149
path = build_pages(page: 2, per_page: 1, item_count: 2).path_to_previous
150150

151-
path.not_nil!.should end_with("?page=1")
151+
path.to_s.should end_with("?page=1")
152152
end
153153

154154
it "returns nil if there is not previous page" do

spec/lucky/param_parser_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe Lucky::ParamParser do
9696
result = Lucky::ParamParser.parse(formatted_time.value, Time)
9797

9898
result.should_not be_nil
99-
result = result.not_nil!
99+
result = result.as(Time)
100100
result.year.should eq(time.year) if formatted_time.components.includes? TimeComponent::Year
101101
result.month.should eq(time.month) if formatted_time.components.includes? TimeComponent::Month
102102
result.day.should eq(time.day) if formatted_time.components.includes? TimeComponent::Day

spec/lucky/static_compression_handler_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe Lucky::StaticCompressionHandler do
7171

7272
call_handler_with(first_context) { }
7373

74-
last_modified = HTTP.parse_time(first_context.response.headers["Last-Modified"]).not_nil!
74+
last_modified = HTTP.parse_time(first_context.response.headers["Last-Modified"]).as(Time)
7575

7676
context = build_context(path: PATH)
7777
context.request.headers["Accept-Encoding"] = "gzip"

spec/support/routes_helper.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module RoutesHelper
22
private def assert_route_added?(method, path, expected_action)
33
result = Lucky.router.find_action(method, path)
44
result.should_not be_nil
5-
result.not_nil!.payload.should eq expected_action
5+
result.as(LuckyRouter::Match).payload.should eq expected_action
66
end
77

88
private def assert_route_not_added?(method, path)

spec/support/test_server.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class TestServer < Lucky::BaseAppServer
22
class_setter last_request : HTTP::Request?
33

44
def self.last_request : HTTP::Request
5-
@@last_request.not_nil!
5+
@@last_request.as(HTTP::Request)
66
end
77

88
def middleware : Array(HTTP::Handler)
@@ -16,8 +16,8 @@ class TestServer < Lucky::BaseAppServer
1616
raise "unimplemented"
1717
end
1818

19-
def last_request
20-
self.class.last_request.not_nil!
19+
def last_request : HTTP::Request
20+
self.class.last_request.as(HTTP::Request)
2121
end
2222

2323
class LastRequestHandler

src/charms/hash_extensions.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Hash
1+
class Hash(K, V)
22
# Return the **nilable** value of a hash key
33
#
44
# This returns a value stored in a hash. The key can be specified as a String
@@ -11,7 +11,7 @@ class Hash
1111
# hash.get("name") # => "Karin" : (String | Nil)
1212
# hash.get(:asdf) # => nil : (String | Nil)
1313
# ```
14-
def get(key : String | Symbol)
14+
def get(key : String | Symbol) : V?
1515
self[key.to_s]?
1616
end
1717

@@ -28,7 +28,7 @@ class Hash
2828
# hash.get("name") # => "Karin" : String
2929
# hash.get(:asdf) # => KeyError
3030
# ```
31-
def get!(key : String | Symbol)
31+
def get!(key : String | Symbol) : V
3232
self[key.to_s]
3333
end
3434
end

src/charms/object.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ class Object
22
include ::Lucky::QuickDef
33
include ::Lucky::Memoizable
44

5-
def blank?
5+
def blank? : Bool
66
if self.responds_to?(:empty?)
77
self.empty?
88
else
99
false
1010
end
1111
end
1212

13-
def present?
13+
def present? : Bool
1414
!blank?
1515
end
1616
end
1717

1818
struct Char
19-
def blank?
19+
def blank? : Bool
2020
case ord
2121
when 9, 0xa, 0xb, 0xc, 0xd, 0x20, 0x85, 0xa0, 0x1680, 0x180e,
2222
0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006,
@@ -30,7 +30,7 @@ struct Char
3030
end
3131

3232
struct Nil
33-
def blank?
33+
def blank? : Bool
3434
true
3535
end
3636
end

src/lucky/asset_helpers.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module Lucky::AssetHelpers
9090
# NOTE: This method does *not* check assets at compile time. The asset path
9191
# is found at runtime so it is possible the asset does not exist. Be sure to
9292
# manually test that the asset is returned as expected.
93-
def dynamic_asset(path)
93+
def dynamic_asset(path) : String
9494
fingerprinted_path = Lucky::AssetHelpers::ASSET_MANIFEST[path]?
9595
if fingerprinted_path
9696
Lucky::Server.settings.asset_host + fingerprinted_path

src/lucky/base_component.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class Lucky::BaseComponent
1616
end
1717

1818
# :nodoc:
19-
def view(@view : IO)
19+
def view(@view : IO) : self
2020
# This is used by Lucky::MountComponent to set the view.
2121
self
2222
end
@@ -25,7 +25,7 @@ abstract class Lucky::BaseComponent
2525
@context || raise "No context was set in #{self.class.name}. Use 'mount' or set it with 'context(@context)' before rendering."
2626
end
2727

28-
def context(@context : HTTP::Server::Context?)
28+
def context(@context : HTTP::Server::Context?) : self
2929
# This is used by Lucky::MountComponent to set the context.
3030
self
3131
end

src/lucky/base_http_client.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ abstract class Lucky::BaseHTTPClient
6666
# .accept_plain_text
6767
# .get("/some-path")
6868
# ```
69-
def headers(**header_values)
69+
def headers(**header_values) : self
7070
@client.before_request do |request|
7171
header_values.each do |key, value|
7272
request.headers[key.to_s.gsub("-", "_")] = value.to_s

src/lucky/cookies/cookie_jar.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Lucky::CookieJar
22
MAX_COOKIE_SIZE = 4096
33
LUCKY_ENCRYPTION_PREFIX = Base64.strict_encode("lucky") + "--"
44
alias Key = String | Symbol
5-
private property cookies
6-
private property set_cookies
5+
private property cookies : HTTP::Cookies
6+
private property set_cookies : HTTP::Cookies
77

88
Habitat.create do
99
setting on_set : (HTTP::Cookie -> HTTP::Cookie)?

src/lucky/events/pipe_event.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class Lucky::Events::PipeEvent < Pulsar::Event
1313
)
1414
end
1515

16-
def before?
16+
def before? : Bool
1717
position == Position::Before
1818
end
1919

20-
def after?
20+
def after? : Bool
2121
position == Position::After
2222
end
2323
end

src/lucky/events/request_complete_event.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Lucky::Events::RequestCompleteEvent < Pulsar::Event
2-
getter :duration
2+
getter duration : Time::Span
33

44
def initialize(@duration : Time::Span)
55
end

0 commit comments

Comments
 (0)