lib/simplabs/excellent/checks/base.rb in simplabs-excellent-1.0.1 vs lib/simplabs/excellent/checks/base.rb in simplabs-excellent-1.2.1
- old
+ new
@@ -1,39 +1,48 @@
-require 'simplabs/excellent/core/error'
+require 'simplabs/excellent/warning'
module Simplabs
module Excellent
module Checks
+ # This is the base class for all code checks. All checks must specify +interesting_nodes+. When one of these nodes is processed by Excellent, it
+ # will invoke the +evaluate_node+ method of all checks that specify the node as one if their +interesting_nodes+.
class Base
- def initialize
- @errors = []
+ attr_reader :warnings
+
+ # An array of node types that are interesting for the check. These are symbols as returned by RubyParser (see http://parsetree.rubyforge.org/ruby_parser/),
+ # e.g. <tt>:if</tt> or <tt>:defn</tt>
+ attr_reader :interesting_nodes
+
+ def initialize #:nodoc:
+ @warnings = []
end
-
- def position(offset = 0)
- "#{@line[2]}:#{@line[1] + offset}"
+
+ # This method is called whenever Excellent processes a node that the check specified as one of the nodes it is interested in (see interesting_nodes).
+ #
+ # ==== Parameters
+ #
+ # * <tt>context</tt> - This is the last context the code processor has constructed. It contains all information required to execute the check (see Simplabs::Excellent::Parsing::SexpContext).
+ def evaluate_node(context)
+ evaluate(context)
end
-
- def evaluate_node(node)
- @file = node.file
- @line = node.line
- eval_method = "evaluate_#{node.node_type}"
- self.send(eval_method, node) if self.respond_to? eval_method
- evaluate(node) if self.respond_to? :evaluate
- end
-
- def add_error(message, info = {}, offset = 0)
+
+ # Adds a warning
+ #
+ # ==== Parameters
+ #
+ # * <tt>context</tt> - The context the check has been executed on.
+ # * <tt>message</tt> - The warning message.
+ # * <tt>info</tt> - The information hash that contains more info on the finding.
+ # * <tt>offset</tt> - The line offset that is added to the context's line property.
+ def add_warning(context, message, info = {}, offset = 0)
klass = self.class
- @errors << Simplabs::Excellent::Core::Error.new(klass, message, @file, @line + offset, info)
+ @warnings << Simplabs::Excellent::Warning.new(klass, message, context.file, context.line + offset, info)
end
- def errors
- @errors
- end
-
end
end
end