diff options
| author | Mike McQuaid | 2017-09-05 18:10:02 +0100 |
|---|---|---|
| committer | GitHub | 2017-09-05 18:10:02 +0100 |
| commit | bf0ab938e7aebd13775bac82df7a7b24adefd61e (patch) | |
| tree | 19b154c8d8909485141abfee5ec7b4402adcf384 /Library/Homebrew/rubocops/extend | |
| parent | 4cc8d4737b1c87cdc2d2c9e90d80b3b372bb924c (diff) | |
| parent | 337d5c64708f3ad1d7074e17b1cd5be24e0ee488 (diff) | |
| download | brew-bf0ab938e7aebd13775bac82df7a7b24adefd61e.tar.bz2 | |
Merge pull request #3091 from GauthamGoli/audit_line_rubocop_part_3
audit: Port line_problems to rubocop and add tests part 3
Diffstat (limited to 'Library/Homebrew/rubocops/extend')
| -rw-r--r-- | Library/Homebrew/rubocops/extend/formula_cop.rb | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 59ad1aafb..47dd2d803 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -4,16 +4,17 @@ require_relative "../../extend/string" 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 - @formula_name = class_name(class_node) + @formula_name = Pathname.new(@file_path).basename(".rb").to_s audit_formula(node, class_node, parent_class_node, @body) end @@ -100,19 +101,22 @@ 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 # Matches a method with a receiver, # EX: to match `Formula.factory(name)` # call `find_instance_method_call(node, "Formula", :factory)` + # EX: to match `build.head?` + # call `find_instance_method_call(node, :build, :head?)` # yields to a block with matching method node 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 if method.receiver.nil? + next if method.receiver.const_name != instance && + method.receiver.method_name != instance @offense_source_range = method.source_range @offensive_node = method yield method @@ -400,12 +404,7 @@ module RuboCop # Returns true if the formula is versioned def versioned_formula? - formula_file_name.include?("@") || @formula_name.match(/AT\d+/) - end - - # Returns filename of the formula without the extension - def formula_file_name - File.basename(processed_source.buffer.name, ".rb") + @formula_name.include?("@") end # Returns printable component name @@ -414,6 +413,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 @@ -432,11 +437,11 @@ module RuboCop class_node && class_names.include?(string_content(class_node)) 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 |
