class RubyLint::Analysis::Base

Base analysis class that provides various helper methods commonly used across analysis classes.

@!attribute [r] report

@return [RubyLint::Report]

@!attribute [r] vm

@return [RubyLint::VirtualMachine]

@!attribute [r] config

@return [RubyLint::Configuration]

Constants

SCOPES

Array containing the callback names for which a new scope should be created.

@return [Array<Symbol>]

Attributes

config[R]
report[R]
vm[R]

Public Class Methods

analyze?(ast, vm) click to toggle source

Returns a boolean that indicates if the analysis class should be used or not.

@param [RubyLint::AST::Node] ast @param [RubyLint::VirtualMachine] vm @return [TrueClass|FalseClass]

# File lib/ruby-lint/analysis/base.rb, line 47
def self.analyze?(ast, vm)
  return true
end
register(name) click to toggle source

Registers the current class in {RubyLint::Configuration.available_analysis_classes}.

@param [String] name A human friendly name of the current class.

# File lib/ruby-lint/analysis/base.rb, line 35
def self.register(name)
  Configuration.available_analysis_classes[name] = self
end

Public Instance Methods

after_initialize() click to toggle source

Called after a new instance of this class is created.

# File lib/ruby-lint/analysis/base.rb, line 54
def after_initialize
  unless vm.is_a?(VirtualMachine)
    raise(
      ArgumentError,
      'Analysis classes require a valid RubyLint::VirtualMachine ' \
        'instance to be set using `SomeAnalysisClass.new(:vm => ...)`'
    )
  end

  @scopes = []
end

Protected Instance Methods

add_message(level, message, node) click to toggle source

Adds a message of the given level.

@param [Symbol] level @param [String] message @param [String] node

# File lib/ruby-lint/analysis/base.rb, line 149
def add_message(level, message, node)
  return unless report

  report.add(
    :level    => level,
    :message  => message,
    :line     => node.line,
    :column   => node.column,
    :file     => node.file,
    :node     => node
  )
end
current_scope() click to toggle source

Returns the current scope.

@return [RubyLint::Definition::RubyObject]

# File lib/ruby-lint/analysis/base.rb, line 83
def current_scope
  return @scopes[-1]
end
error(*args) click to toggle source

Adds an error message to the report.

@see add_message

# File lib/ruby-lint/analysis/base.rb, line 120
def error(*args)
  add_message(:error, *args)
end
info(*args) click to toggle source

Adds a regular informational message to the report.

@see add_message

# File lib/ruby-lint/analysis/base.rb, line 138
def info(*args)
  add_message(:info, *args)
end
previous_scope() click to toggle source

@return [RubyLint::Definition::RubyObject]

# File lib/ruby-lint/analysis/base.rb, line 90
def previous_scope
  return @scopes[-2]
end
set_current_scope(node) click to toggle source

Sets the current scope to the definition associated with the given node.

@param [RubyLint::Node] node

# File lib/ruby-lint/analysis/base.rb, line 100
def set_current_scope(node)
  unless vm.associations.key?(node)
    raise ArgumentError, "No associations for node #{node}"
  end

  @scopes << vm.associations[node]
end
set_previous_scope() click to toggle source

Sets the current scope back to the previous one.

# File lib/ruby-lint/analysis/base.rb, line 111
def set_previous_scope
  @scopes.pop
end
warning(*args) click to toggle source

Adds a warning message to the report.

@see add_message

# File lib/ruby-lint/analysis/base.rb, line 129
def warning(*args)
  add_message(:warning, *args)
end