diff options
| author | Gautham Goli | 2017-08-04 01:18:00 +0530 |
|---|---|---|
| committer | Gautham Goli | 2017-08-06 18:36:09 +0530 |
| commit | b8f811cca669e9e20fb6e8a8ac8d44f0b8761f5f (patch) | |
| tree | 26532370b24f015f419c5db8a6f01384e0715bbe /Library/Homebrew/rubocops | |
| parent | 3edae73cd90cc9e6233718bfb48c5cc075aa0f36 (diff) | |
| download | brew-b8f811cca669e9e20fb6e8a8ac8d44f0b8761f5f.tar.bz2 | |
audit: Port rules from line_problems to rubocop part 3
Diffstat (limited to 'Library/Homebrew/rubocops')
| -rw-r--r-- | Library/Homebrew/rubocops/extend/formula_cop.rb | 22 | ||||
| -rw-r--r-- | Library/Homebrew/rubocops/lines_cop.rb | 57 |
2 files changed, 71 insertions, 8 deletions
diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index f8864538a..991551585 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -3,12 +3,13 @@ require "parser/current" module RuboCop module Cop class FormulaCop < Cop + attr_accessor :file_path @registry = Cop.registry # This method is called by RuboCop and is the main entry point def on_class(node) - file_path = processed_source.buffer.name - return unless file_path_allowed?(file_path) + @file_path = processed_source.buffer.name + return unless file_path_allowed? return unless formula_class?(node) return unless respond_to?(:audit_formula) class_node, parent_class_node, @body = *node @@ -99,8 +100,7 @@ module RuboCop def find_method_with_args(node, method_name, *args) methods = find_every_method_call_by_name(node, method_name) methods.each do |method| - next unless parameters_passed?(method, *args) - yield method + yield method if parameters_passed?(method, *args) end end @@ -111,7 +111,7 @@ module RuboCop def find_instance_method_call(node, instance, method_name) methods = find_every_method_call_by_name(node, method_name) methods.each do |method| - next unless method.receiver && method.receiver.const_name == instance + next unless method.receiver && (method.receiver.const_name == instance || method.receiver.method_name == instance) @offense_source_range = method.source_range @offensive_node = method yield method @@ -400,6 +400,12 @@ module RuboCop method_name(component_node) if component_node.def_type? end + # Returns the formula tap + def formula_tap + return unless match_obj = @file_path.match(%r{/(homebrew-\w+)/}) + match_obj[1] + end + def problem(msg) add_offense(@offensive_node, @offense_source_range, msg) end @@ -411,11 +417,11 @@ module RuboCop class_node && string_content(class_node) == "Formula" end - def file_path_allowed?(file_path) + def file_path_allowed? paths_to_exclude = [%r{/Library/Homebrew/compat/}, %r{/Library/Homebrew/test/}] - return true if file_path.nil? # file_path is nil when source is directly passed to the cop eg., in specs - file_path !~ Regexp.union(paths_to_exclude) + return true if @file_path.nil? # file_path is nil when source is directly passed to the cop eg., in specs + @file_path !~ Regexp.union(paths_to_exclude) end end end diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 536f5e2ec..7bf9e6056 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -73,7 +73,64 @@ module RuboCop next unless block_arg.source.size>1 problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{block_arg.source}|\"." end + + [:rebuild, :version_scheme].each do |m| + find_method_with_args(body_node, m, 0) do + problem "'#{m} 0' should be removed" + end + end + + [:mac?, :linux?].each do |m| + next unless formula_tap == "homebrew-core" + find_instance_method_call(body_node, "OS", m) do |check| + problem "Don't use #{check.source}; Homebrew/core only supports macOS" + end + end + + find_method_with_args(body_node, :fails_with, :llvm) do + problem "'fails_with :llvm' is now a no-op so should be removed" + end + + find_method_with_args(body_node, :system, /^(otool|install_name_tool|lipo)$/) do + problem "Use ruby-macho instead of calling #{@offensive_node.source}" + end + # + find_method_with_args(body_node, :system, /npm/, /install/) do |m| + next if @formula_name =~ /^kibana(\@\d+(\.\d+)?)?$/ + problem "Use Language::Node for npm install args" unless languageNode?(m) + end + if find_method_def(body_node, :test) + problem "Use new-style test definitions (test do)" + end + + if find_method_def(body_node, :options) + problem "Use new-style option definitions" + end + + find_method_with_args(body_node, :skip_clean, :all) do + problem "`skip_clean :all` is deprecated; brew no longer strips symbols\n" \ + "\tPass explicit paths to prevent Homebrew from removing empty folders." + end + + find_instance_method_call(body_node, :build, :universal?) do + problem "macOS has been 64-bit only so build.universal? is deprecated." + end + + find_instance_method_call(body_node, "ENV", :universal_binary) do + problem "macOS has been 64-bit only since 10.6 so ENV.universal_binary is deprecated." + end + + find_instance_method_call(body_node, "ENV", :x11) do + problem 'Use "depends_on :x11" instead of "ENV.x11"' + end end + + # This is Pattern Matching method for AST + # Takes the AST node as argument and yields matching node if block given + # Else returns boolean for the match + def_node_search :languageNode?, <<-PATTERN + (const (const nil :Language) :Node) + PATTERN end end end |
