diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c6e3d..e4dad3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/anycable-rails.gemspec b/anycable-rails.gemspec index 60c9d96..53e8ac0 100644 --- a/anycable-rails.gemspec +++ b/anycable-rails.gemspec @@ -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" diff --git a/lib/anycable/rails.rb b/lib/anycable/rails.rb index 6182281..58fb203 100644 --- a/lib/anycable/rails.rb +++ b/lib/anycable/rails.rb @@ -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" \ diff --git a/lib/anycable/rails/compatibility/rubocop/cops/anycable/instance_vars.rb b/lib/anycable/rails/compatibility/rubocop/cops/anycable/instance_vars.rb index f1e79af..24ce2a9 100644 --- a/lib/anycable/rails/compatibility/rubocop/cops/anycable/instance_vars.rb +++ b/lib/anycable/rails/compatibility/rubocop/cops/anycable/instance_vars.rb @@ -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) diff --git a/lib/anycable/rails/compatibility/rubocop/cops/anycable/periodical_timers.rb b/lib/anycable/rails/compatibility/rubocop/cops/anycable/periodical_timers.rb index a3f182c..ff75ae2 100644 --- a/lib/anycable/rails/compatibility/rubocop/cops/anycable/periodical_timers.rb +++ b/lib/anycable/rails/compatibility/rubocop/cops/anycable/periodical_timers.rb @@ -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 diff --git a/lib/anycable/rails/compatibility/rubocop/cops/anycable/stream_from.rb b/lib/anycable/rails/compatibility/rubocop/cops/anycable/stream_from.rb index 420c4d4..fb7fd8f 100644 --- a/lib/anycable/rails/compatibility/rubocop/cops/anycable/stream_from.rb +++ b/lib/anycable/rails/compatibility/rubocop/cops/anycable/stream_from.rb @@ -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 @@ -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 diff --git a/lib/generators/anycable/setup/templates/config/anycable.yml.tt b/lib/generators/anycable/setup/templates/config/anycable.yml.tt index 9cc5093..e5d964c 100644 --- a/lib/generators/anycable/setup/templates/config/anycable.yml.tt +++ b/lib/generators/anycable/setup/templates/config/anycable.yml.tt @@ -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. diff --git a/spec/cops_spec_helper.rb b/spec/cops_spec_helper.rb index 8f3be02..08e3c06 100644 --- a/spec/cops_spec_helper.rb +++ b/spec/cops_spec_helper.rb @@ -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 diff --git a/spec/rubocop/instance_vars_spec.rb b/spec/rubocop/instance_vars_spec.rb index 5907052..3816770 100644 --- a/spec/rubocop/instance_vars_spec.rb +++ b/spec/rubocop/instance_vars_spec.rb @@ -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 diff --git a/spec/rubocop/periodical_timers_spec.rb b/spec/rubocop/periodical_timers_spec.rb index e52e54d..7cb9fbf 100644 --- a/spec/rubocop/periodical_timers_spec.rb +++ b/spec/rubocop/periodical_timers_spec.rb @@ -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 diff --git a/spec/rubocop/stream_from_spec.rb b/spec/rubocop/stream_from_spec.rb index 84a0335..a01fa31 100644 --- a/spec/rubocop/stream_from_spec.rb +++ b/spec/rubocop/stream_from_spec.rb @@ -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