aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorGautham Goli2017-01-18 15:54:47 +0530
committerGautham Goli2017-01-18 15:54:47 +0530
commit483c40fb03b83d109795ece1351dcc1ca318a143 (patch)
treef4c2c70fabe770a6609f1b090cfcb570a87d05ae /Library
parent10b8d27d118c5db7ce4293861cded28e8516e849 (diff)
downloadbrew-483c40fb03b83d109795ece1351dcc1ca318a143.tar.bz2
Add custom cop to refactor revision to rebuild in bottle block
Diffstat (limited to 'Library')
-rw-r--r--Library/.rubocop.yml6
-rw-r--r--Library/Homebrew/rubocops/bottle_block_cop.rb54
2 files changed, 60 insertions, 0 deletions
diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml
index 097eff995..012fcd609 100644
--- a/Library/.rubocop.yml
+++ b/Library/.rubocop.yml
@@ -6,6 +6,12 @@ AllCops:
- '**/Casks/**/*'
- '**/vendor/**/*'
+require:
+ - ./Homebrew/rubocops/bottle_block_cop.rb
+
+CustomCops/CorrectBottleBlock:
+ Enabled: true
+
Metrics/AbcSize:
Enabled: false
diff --git a/Library/Homebrew/rubocops/bottle_block_cop.rb b/Library/Homebrew/rubocops/bottle_block_cop.rb
new file mode 100644
index 000000000..7cb97ea89
--- /dev/null
+++ b/Library/Homebrew/rubocops/bottle_block_cop.rb
@@ -0,0 +1,54 @@
+module RuboCop
+ module Cop
+ module CustomCops
+ class CorrectBottleBlock < Cop
+ MSG = 'Use rebuild instead of revision in bottle block'.freeze
+
+ def on_block(node)
+ return if block_length(node).zero?
+ method, _args, _body = *node
+
+ keyword, method_name = *method
+
+ if method_name.equal?(:bottle) and has_revision?(_body)
+ add_offense(node, :expression)
+ end
+ end
+
+ private
+
+ def autocorrect(node)
+ ->(corrector) do
+ # Check for revision
+ method, _args, _body = *node
+ if has_revision?(_body)
+ replace_revision(corrector, node)
+ end
+ end
+ end
+
+ def has_revision?(body)
+ body.children.each do |method_call_node|
+ _receiver, _method_name, *args = *method_call_node
+ if _method_name == :revision
+ return true
+ end
+ end
+ false
+ end
+
+ def replace_revision(corrector, node)
+ new_source = String.new
+ node.source.each_line do |line|
+ if line =~ /\A\s*revision/
+ line = line.sub('revision','rebuild')
+ end
+ new_source << line
+ end
+ corrector.insert_before(node.source_range, new_source)
+ corrector.remove(node.source_range)
+ end
+ end
+ end
+ end
+end