Skip to content

Commit

Permalink
fix: upgrade RuboCop cops
Browse files Browse the repository at this point in the history
Closes #207
  • Loading branch information
palkan committed Dec 13, 2024
1 parent f1a8635 commit 926eb6e
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 67 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Upgrade RuboCop cops. ([@palkan][])

## 1.5.4 (2024-10-08)

- Add [actioncable-next](https://github.com/anycable/actioncable-next) support. ([@palkan][])
Expand Down
2 changes: 1 addition & 1 deletion anycable-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", ">= 1.10"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec-rails", ">= 4.0.0"
spec.add_development_dependency "rubocop", ">= 0.80"
spec.add_development_dependency "rubocop", ">= 1.0"
spec.add_development_dependency "warden"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "simplecov-lcov"
Expand Down
2 changes: 1 addition & 1 deletion lib/anycable/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def extend_adapter!(adapter)

# Warn if application has been already initialized.
# AnyCable should be loaded before initialization in order to work correctly.
if defined?(::Rails) && ::Rails.application && ::Rails.application.initialized?
if defined?(::Rails) && ::Rails.application&.initialized?
puts("\n**************************************************")
puts(
"⛔️ WARNING: AnyCable loaded after application initialization. Might not work correctly.\n" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module AnyCable
# end
# end
#
class InstanceVars < RuboCop::Cop::Cop
class InstanceVars < RuboCop::Cop::Base
MSG = "Channel instance variables are not supported in AnyCable. Use `state_attr_accessor` instead"

def on_class(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module AnyCable
# periodically(:do_something, every: 2.seconds)
# end
#
class PeriodicalTimers < RuboCop::Cop::Cop
class PeriodicalTimers < RuboCop::Cop::Base
MSG = "Periodical Timers are not supported in AnyCable"

def_node_matcher :calls_periodically?, <<-PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module AnyCable
# end
# end
#
class StreamFrom < RuboCop::Cop::Cop
class StreamFrom < RuboCop::Cop::Base
def_node_matcher :stream_from_with_block?, <<-PATTERN
(block {(send _ :stream_from ...) (send _ :stream_for ...)} ...)
PATTERN
Expand Down Expand Up @@ -81,16 +81,14 @@ def find_coders(args)

def add_callback_offense(node)
add_offense(
node,
location: :expression,
node.loc.expression,
message: "Custom stream callbacks are not supported in AnyCable"
)
end

def add_custom_coder_offense(node)
add_offense(
node,
location: :expression,
node.loc.expression,
message: "Custom coders are not supported in AnyCable"
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ default: &default
<%- else -%>
# Use HTTP broadcaster
broadcast_adapter: http
http_broadcast_url: "http://localhost:8090/_anycable"
http_broadcast_url: "http://localhost:8090/_broadcast"
<%- end -%>
<%- if redis? -%>
# You can use REDIS_URL env var to configure Redis URL.
Expand Down
3 changes: 1 addition & 2 deletions spec/cops_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
subject(:cop) { described_class.new }

it "works with empty file" do
inspect_source("")
expect(cop.offenses.size).to be(0)
expect_no_offenses("")
end
end
30 changes: 10 additions & 20 deletions spec/rubocop/instance_vars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,57 @@
include_context "cop spec"

it "registers offense for instance var declaration in #subscribed" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def subscribed
@instance_var
^^^^^^^^^^^^^ AnyCable/InstanceVars: Channel instance variables are not supported in AnyCable. Use `state_attr_accessor` instead
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Channel instance variables are not supported in AnyCable")
end

it "registers offense for instance var definition inside block in #subscribed" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def subscribed
5.times { @instance_var = 1 }
^^^^^^^^^^^^^^^^^ AnyCable/InstanceVars: Channel instance variables are not supported in AnyCable. Use `state_attr_accessor` instead
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Channel instance variables are not supported in AnyCable")
end

it "registers offense for instance var definition inside condition in #subscribed" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def subscribed
@instance_var = 1 if true
^^^^^^^^^^^^^^^^^ AnyCable/InstanceVars: Channel instance variables are not supported in AnyCable. Use `state_attr_accessor` instead
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Channel instance variables are not supported in AnyCable")
end

it "registers offense for instance var definition inside multiple assignments in #subscribed" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def subscribed
a = b = @instance_var = 1
^^^^^^^^^^^^^^^^^ AnyCable/InstanceVars: Channel instance variables are not supported in AnyCable. Use `state_attr_accessor` instead
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Channel instance variables are not supported in AnyCable")
end

it "registers offense for instance var definitions inside action" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
@instance_var = 1
^^^^^^^^^^^^^^^^^ AnyCable/InstanceVars: Channel instance variables are not supported in AnyCable. Use `state_attr_accessor` instead
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Channel instance variables are not supported in AnyCable")
end
end
12 changes: 4 additions & 8 deletions spec/rubocop/periodical_timers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@
include_context "cop spec"

it "registers offense for #periodically call" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
periodically(:do_something, every: 2.seconds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AnyCable/PeriodicalTimers: Periodical Timers are not supported in AnyCable
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Periodical Timers are not supported in AnyCable")
end

it "registers offense for #periodically call explicit self" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
self.periodically(:do_something, every: 2.seconds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AnyCable/PeriodicalTimers: Periodical Timers are not supported in AnyCable
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Periodical Timers are not supported in AnyCable")
end
end
40 changes: 13 additions & 27 deletions spec/rubocop/stream_from_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,78 @@
include_context "cop spec"

it "registers offense for #stream_from with block" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
stream_from("all") {}
^^^^^^^^^^^^^^^^^^^^^ AnyCable/StreamFrom: Custom stream callbacks are not supported in AnyCable
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Custom stream callbacks are not supported in AnyCable")
end

it "registers offense for #stream_for with block" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
stream_for(user) {}
^^^^^^^^^^^^^^^^^^^ AnyCable/StreamFrom: Custom stream callbacks are not supported in AnyCable
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Custom stream callbacks are not supported in AnyCable")
end

it "registers offense for #stream_from with lambda" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
stream_from("all", -> {})
^^^^^^^^^^^^^^^^^^^^^^^^^ AnyCable/StreamFrom: Custom stream callbacks are not supported in AnyCable
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Custom stream callbacks are not supported in AnyCable")
end

it "registers offense for #stream_for with lambda" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
stream_for(user, -> {})
^^^^^^^^^^^^^^^^^^^^^^^ AnyCable/StreamFrom: Custom stream callbacks are not supported in AnyCable
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Custom stream callbacks are not supported in AnyCable")
end

it "registers offense for #stream_from with not JSON coder" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
stream_from("all", coder: SomeCoder)
^^^^^^^^^^^^^^^^ AnyCable/StreamFrom: Custom coders are not supported in AnyCable
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Custom coders are not supported in AnyCable")
end

it "registers offense for #stream_for with not JSON coder" do
inspect_source(<<~RUBY)
expect_offense(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
stream_for(user, coder: SomeCoder)
^^^^^^^^^^^^^^^^ AnyCable/StreamFrom: Custom coders are not supported in AnyCable
end
end
RUBY

expect(cop.offenses.size).to be(1)
expect(cop.messages.first).to include("Custom coders are not supported in AnyCable")
end

it "does not register offense for #stream_from with JSON coder" do
inspect_source(<<~RUBY)
expect_no_offenses(<<~RUBY)
class MyChannel < ApplicationCable::Channel
def follow
stream_from("all", coder: ActiveSupport::JSON)
end
end
RUBY

expect(cop.offenses.size).to be(0)
end
end

0 comments on commit 926eb6e

Please sign in to comment.