lib/rubocop/cop/cop.rb in rubocop-0.15.0 vs lib/rubocop/cop/cop.rb in rubocop-0.16.0

- old
+ new

@@ -30,11 +30,11 @@ # Cops track offences and can autocorrect them of the fly. # # A commissioner object is responsible for traversing the AST and invoking # the specific callbacks on each cop. # If a cop needs to do its own processing of the AST or depends on - # something else it should define the `#investigate` method and do + # something else, it should define the `#investigate` method and do # the processing there. # # @example # # class CustomCop < Cop @@ -42,10 +42,11 @@ # # Do custom processing # end # end class Cop extend AST::Sexp + include Util # http://phrogz.net/programmingruby/language.html#table_18.4 # Backtick is added last just to help editors parse this code. OPERATOR_METHODS = %w( | ^ & <=> == === =~ > >= < <= << >> @@ -116,33 +117,37 @@ def support_autocorrect? respond_to?(:autocorrect, true) end - def add_offence(severity, node, loc, message = nil) + def add_offence(node, loc, message = nil, severity = nil) location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc return if disabled_line?(location.line) + severity = custom_severity || severity || default_severity + message = message ? message : message(node) message = debug? ? "#{name}: #{message}" : message corrected = begin autocorrect(node) if autocorrect? autocorrect? rescue CorrectionNotPossible false end @offences << Offence.new(severity, location, message, name, corrected) + yield if block_given? end - def convention(node, location, message = nil) - add_offence(:convention, node, location, message) + def config_to_allow_offences + Formatter::DisabledConfigFormatter.config_to_allow_offences[cop_name] end - def warning(node, location, message = nil) - add_offence(:warning, node, location, message) + def config_to_allow_offences=(hash) + Formatter::DisabledConfigFormatter.config_to_allow_offences[cop_name] = + hash end def cop_name self.class.cop_name end @@ -151,10 +156,38 @@ def ignore_node(node) @ignored_nodes << node end + def include_paths + cop_config && cop_config['Include'] + end + + def include_file?(file) + return true unless include_paths + + include_paths.any? do |regex| + processed_source.buffer.name =~ /#{regex}/ + end + end + + def exclude_paths + cop_config && cop_config['Exclude'] + end + + def exclude_file?(file) + return false unless exclude_paths + + exclude_paths.any? do |regex| + processed_source.buffer.name =~ /#{regex}/ + end + end + + def relevant_file?(file) + include_file?(file) && !exclude_file?(file) + end + private def disabled_line?(line_number) return false unless @processed_source disabled_lines = @processed_source.disabled_lines_for_cops[name] @@ -176,49 +209,24 @@ def ignored_node?(node) @ignored_nodes.any? { |n| n.eql?(node) } # Same object found in array? end - def on_node(syms, sexp, excludes = []) - yield sexp if Array(syms).include?(sexp.type) + def default_severity + self.class.lint? ? :warning : :convention + end - return if Array(excludes).include?(sexp.type) - - sexp.children.each do |elem| - if elem.is_a?(Parser::AST::Node) - on_node(syms, elem, excludes) { |s| yield s } + def custom_severity + severity = cop_config && cop_config['Severity'] + if severity + if Offence::SEVERITIES.include?(severity.to_sym) + severity.to_sym + else + warn "Warning: Invalid severity '#{severity}'. " + + "Valid severities are #{Offence::SEVERITIES.join(', ')}." + .color(:red) end end - end - - def command?(name, node) - return unless node.type == :send - - receiver, method_name, _args = *node - - # commands have no explicit receiver - !receiver && method_name == name - end - - def source_range(source_buffer, preceding_lines, begin_column, - column_count) - newline_length = 1 - begin_pos = preceding_lines.reduce(0) do |a, e| - a + e.length + newline_length - end + begin_column - Parser::Source::Range.new(source_buffer, begin_pos, - begin_pos + column_count) - end - - def range_with_surrounding_space(range, side = :both) - src = @processed_source.buffer.source - go_left = side == :left || side == :both - go_right = side == :right || side == :both - begin_pos = range.begin_pos - begin_pos -= 1 while go_left && src[begin_pos - 1] =~ /[ \t]/ - end_pos = range.end_pos - end_pos += 1 while go_right && src[end_pos] =~ /[ \t]/ - Parser::Source::Range.new(@processed_source.buffer, begin_pos, end_pos) end end end end