aboutsummaryrefslogtreecommitdiffstats
path: root/ci/custom_cops/test/support/cop_helper.rb
diff options
context:
space:
mode:
authorDaniel Colson <danieljamescolson@gmail.com>2018-04-03 20:50:00 -0400
committerDaniel Colson <danieljamescolson@gmail.com>2018-04-03 22:35:34 -0400
commitf88f5a457cb95dc22519aa33b527a73f198e92e8 (patch)
tree87b8a771d492be9ac20a72ea84466d3f251b8a5b /ci/custom_cops/test/support/cop_helper.rb
parent09b2348f7fc8d4e7191e70e06608c5909067e2aa (diff)
downloadrails-f88f5a457cb95dc22519aa33b527a73f198e92e8.tar.gz
rails-f88f5a457cb95dc22519aa33b527a73f198e92e8.tar.bz2
rails-f88f5a457cb95dc22519aa33b527a73f198e92e8.zip
Add custom RuboCop for `assert_not` over `refute`
Since at least cf4afc4 we have preferred `assert_not` methods over `refute` methods. I have seen plenty of comments in PRs about this, and we have tried to fix it a few times (5294ad8, e45f176, 8910f12, 41f50be, d4cfd54, 48a183e, and 211adb4), but the `refute` methods keep sneaking back in. This custom RuboCop will take care of enforcing this preference, so we don't have to think about it again. I suspect there are other similar stylistic preferences that could be solved with some custom RuboCops, so I will definitely keep my eyes open. `assert_not` over `assert !` might be a good candidate, for example. I wasn't totally sure if `ci/custom_cops` was the best place to put this, but nothing else seemed quite right. At one point I had it set up as a gem, but I think custom cops like this would have limited value in another context. I want to see how code climate handles the new cops before autocorrecting the existing violations. If things go as expected, I will push another commit with those corrections.
Diffstat (limited to 'ci/custom_cops/test/support/cop_helper.rb')
-rw-r--r--ci/custom_cops/test/support/cop_helper.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/ci/custom_cops/test/support/cop_helper.rb b/ci/custom_cops/test/support/cop_helper.rb
new file mode 100644
index 0000000000..d259154df5
--- /dev/null
+++ b/ci/custom_cops/test/support/cop_helper.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+require "rubocop"
+
+module CopHelper
+ def inspect_source(cop, source)
+ processed_source = parse_source(source)
+ raise "Error parsing example code" unless processed_source.valid_syntax?
+ investigate(cop, processed_source)
+ processed_source
+ end
+
+ def autocorrect_source(cop, source)
+ cop.instance_variable_get(:@options)[:auto_correct] = true
+ processed_source = inspect_source(cop, source)
+ rewrite(cop, processed_source)
+ end
+
+ private
+ TARGET_RUBY_VERSION = 2.4
+
+ def parse_source(source)
+ RuboCop::ProcessedSource.new(source, TARGET_RUBY_VERSION)
+ end
+
+ def rewrite(cop, processed_source)
+ RuboCop::Cop::Corrector.new(processed_source.buffer, cop.corrections)
+ .rewrite
+ end
+
+ def investigate(cop, processed_source)
+ RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
+ .investigate(processed_source)
+ end
+end