Class: Simplabs::Excellent::Checks::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simplabs/excellent/checks/base.rb

Overview

This is the base class for all code checks. All checks must specify interesting_contexts. When one of these contexts is processed by Excellent, it will invoke the evaluate_context method of all checks that specify the context as one if their interesting_contexts.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

:nodoc:



28
29
30
31
32
33
# File 'lib/simplabs/excellent/checks/base.rb', line 28

def initialize(options = {}) #:nodoc:
  @options              = options
  @interesting_contexts = []
  @warnings             = []
  @interesting_files    = [/\.rb$/]
end

Instance Attribute Details

#interesting_contextsObject (readonly)

An array of contexts that are interesting for the check. These contexts are based on symbols as returned by RubyParser (see parsetree.rubyforge.org/ruby_parser/) and add some additional data, e.g. Ifcontext or MethodContext



20
21
22
# File 'lib/simplabs/excellent/checks/base.rb', line 20

def interesting_contexts
  @interesting_contexts
end

#interesting_filesObject (readonly)

An array of regular expressions for file names that are interesting for the check. These will usually be path extensions rather than longer patterns (e.g. *.rb as well as *.erb files or *.rb files only).

Defaults to /.rb$/. If you do not specify anything else in custom checks, only *.rb files will be processed



26
27
28
# File 'lib/simplabs/excellent/checks/base.rb', line 26

def interesting_files
  @interesting_files
end

#optionsObject (readonly)

:nodoc:



15
16
17
# File 'lib/simplabs/excellent/checks/base.rb', line 15

def options
  @options
end

#warningsObject (readonly)

:nodoc:



13
14
15
# File 'lib/simplabs/excellent/checks/base.rb', line 13

def warnings
  @warnings
end

Instance Method Details

#add_warning(context, message, info = {}, offset = 0) ⇒ Object

Adds a warning

Parameters

  • context - The context the check has been executed on.

  • message - The warning message.

  • info - The information hash that contains more info on the finding.

  • offset - The line offset that is added to the context’s line property.



53
54
55
# File 'lib/simplabs/excellent/checks/base.rb', line 53

def add_warning(context, message, info = {}, offset = 0)
  @warnings << Simplabs::Excellent::Warning.new(message, context.file, context.line + offset, info)
end

#evaluate_context(context) ⇒ Object

This method is called whenever Excellent processes a context that the check specified as one of the contexts it is interested in (see interesting_contexts).

Parameters

  • context - This is the last context the code processor has constructed. It contains all information required to execute the check (see Simplabs::Excellent::Parsing::SexpContext).



41
42
43
# File 'lib/simplabs/excellent/checks/base.rb', line 41

def evaluate_context(context)
  evaluate(context)
end

#warnings_for(filename) ⇒ Object

:nodoc:



57
58
59
# File 'lib/simplabs/excellent/checks/base.rb', line 57

def warnings_for(filename) #:nodoc:
  warnings.select { |warning| warning.filename == filename }
end