Class: JRuby::Lint::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby/lint/collectors.rb

Defined Under Namespace

Classes: CheckersVisitor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project = nil, file = nil) ⇒ Collector

Returns a new instance of Collector.



5
6
7
8
9
10
11
12
13
# File 'lib/jruby/lint/collectors.rb', line 5

def initialize(project = nil, file = nil)
  @checkers = Checker.loaded_checkers.map(&:new)
  @checkers.each {|c| c.collector = self }
  @findings = []
  @project, @file = project, file || '<inline-script>'

  # top to bottom list of elements as they are visited
  @stack = [] # FIXME: ast visiting is not something checkers can see so stored here for now
end

Instance Attribute Details

#checkersObject

Returns the value of attribute checkers.



3
4
5
# File 'lib/jruby/lint/collectors.rb', line 3

def checkers
  @checkers
end

#contentsObject

Returns the value of attribute contents.



3
4
5
# File 'lib/jruby/lint/collectors.rb', line 3

def contents
  @contents
end

#fileObject

Returns the value of attribute file.



3
4
5
# File 'lib/jruby/lint/collectors.rb', line 3

def file
  @file
end

#findingsObject

Returns the value of attribute findings.



3
4
5
# File 'lib/jruby/lint/collectors.rb', line 3

def findings
  @findings
end

#projectObject

Returns the value of attribute project.



3
4
5
# File 'lib/jruby/lint/collectors.rb', line 3

def project
  @project
end

#stackObject

Returns the value of attribute stack.



3
4
5
# File 'lib/jruby/lint/collectors.rb', line 3

def stack
  @stack
end

Class Method Details

.allObject



77
78
79
# File 'lib/jruby/lint/collectors.rb', line 77

def self.all
  @collectors ||= []
end

.inherited(base) ⇒ Object



73
74
75
# File 'lib/jruby/lint/collectors.rb', line 73

def self.inherited(base)
  self.all << base
end

Instance Method Details

#add_finding(message, tags, line = nil) ⇒ Object



15
16
17
18
# File 'lib/jruby/lint/collectors.rb', line 15

def add_finding(message, tags, line=nil)
  src_line = line ? contents.split(/\n/)[line-1] : nil
  @findings << Finding.new(message, tags, file, line, src_line)
end

#astObject



65
66
67
# File 'lib/jruby/lint/collectors.rb', line 65

def ast
  @ast ||= JRuby.parse(contents, file, true)
end

#runObject



56
57
58
59
60
61
62
63
# File 'lib/jruby/lint/collectors.rb', line 56

def run
  begin
    CheckersVisitor.new(ast, self, checkers).traverse
  rescue SyntaxError => e
    file, line, message = e.message.split(/:\s*/, 3)
    add_finding(message, [:syntax, :error], line.to_i)
  end
end