From 9e8c78982d09c9595f800cba9eecb686d050d99c Mon Sep 17 00:00:00 2001 From: Alexander Stathis Date: Wed, 23 Oct 2024 14:41:21 -0400 Subject: [PATCH] fix assignment case in Angellist/NoUnless, update prefer date current spec --- Gemfile.lock | 2 +- lib/rubocop-angellist.rb | 1 + lib/rubocop/cop/angellist/no_unless.rb | 4 +++- spec/rubocop/cop/angellist/no_unless_spec.rb | 12 ++++++++++++ .../cop/angellist/prefer_date_current_spec.rb | 11 ++++++----- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7a7e311..cca0545 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rubocop-angellist (1.0.1) + rubocop-angellist (1.0.2) rubocop (~> 1) rubocop-graphql rubocop-performance diff --git a/lib/rubocop-angellist.rb b/lib/rubocop-angellist.rb index 092b8a6..8a04134 100644 --- a/lib/rubocop-angellist.rb +++ b/lib/rubocop-angellist.rb @@ -2,6 +2,7 @@ # frozen_string_literal: true require 'rubocop' +require 'sorbet-runtime' require_relative 'rubocop/angellist' require_relative 'rubocop/angellist/version' diff --git a/lib/rubocop/cop/angellist/no_unless.rb b/lib/rubocop/cop/angellist/no_unless.rb index 2c7d861..883b4a2 100644 --- a/lib/rubocop/cop/angellist/no_unless.rb +++ b/lib/rubocop/cop/angellist/no_unless.rb @@ -37,7 +37,7 @@ def on_if(node) private - sig { params(corrector: RuboCop::Cop::Corrector, node: RuboCop::AST::IfNode).void } + sig { params(corrector: RuboCop::Cop::Corrector, node: RuboCop::AST::Node).void } def autocorrect(corrector, node) case node.type when :begin @@ -54,6 +54,8 @@ def autocorrect(corrector, node) corrector.replace(node.loc.expression, 'false') when :false corrector.replace(node.loc.expression, 'true') + when :lvasgn + corrector.replace(node.loc.expression, "!(#{node.source})") else corrector.replace(node.loc.expression, "!#{node.source}") end diff --git a/spec/rubocop/cop/angellist/no_unless_spec.rb b/spec/rubocop/cop/angellist/no_unless_spec.rb index 61f8441..58ebf95 100644 --- a/spec/rubocop/cop/angellist/no_unless_spec.rb +++ b/spec/rubocop/cop/angellist/no_unless_spec.rb @@ -1,3 +1,4 @@ +# typed: false # frozen_string_literal: true RSpec.describe RuboCop::Cop::Angellist::NoUnless, :config do @@ -148,5 +149,16 @@ return if x == y RUBY end + + it 'corrects assignments' do + expect_offense(<<~RUBY) + return unless x = y + ^^^^^^^^^^^^^^^^^^^ Angellist/NoUnless: Use `if !condition` instead of `unless condition`. + RUBY + + expect_correction(<<~RUBY) + return if !(x = y) + RUBY + end end end diff --git a/spec/rubocop/cop/angellist/prefer_date_current_spec.rb b/spec/rubocop/cop/angellist/prefer_date_current_spec.rb index b608f1f..45b05b2 100644 --- a/spec/rubocop/cop/angellist/prefer_date_current_spec.rb +++ b/spec/rubocop/cop/angellist/prefer_date_current_spec.rb @@ -1,3 +1,4 @@ +# typed: false # frozen_string_literal: true RSpec.describe RuboCop::Cop::Angellist::PreferDateCurrent, :config do @@ -6,16 +7,16 @@ # TODO: Write test code # # For example - it 'registers an offense when using `#bad_method`' do + it 'registers an offense when using `Time.zone.today`' do expect_offense(<<~RUBY) - bad_method - ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`. + Time.zone.today + ^^^^^^^^^^^^^^^ Angellist/PreferDateCurrent: Use `Date.current` instead of `Time.zone.today`. RUBY end - it 'does not register an offense when using `#good_method`' do + it 'does not register an offense when using `Date.current`' do expect_no_offenses(<<~RUBY) - good_method + Date.current RUBY end end