Class: Scrutinize::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/scrutinize/processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, trigger, parser) ⇒ Processor

Initialize a Processor instance.

file - the file to be processed.



13
14
15
16
17
# File 'lib/scrutinize/processor.rb', line 13

def initialize(file, trigger, parser)
  @file    = file
  @trigger = trigger
  @parser  = parser
end

Class Method Details

.process(file, trigger, parser) ⇒ Object

Process a file with a Trigger.

Returns nothing.



6
7
8
# File 'lib/scrutinize/processor.rb', line 6

def self.process(file, trigger, parser)
  new(file, trigger, parser).process
end

Instance Method Details

#astObject

The AST of the source code.

Returns a Parser::AST::Node object.



97
98
99
# File 'lib/scrutinize/processor.rb', line 97

def ast
  @ast ||= @parser.parse src
end

#get_src(node) ⇒ Object

Gets the source code of a given node.

node - a Parser::AST::Node object.

Returns a String.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/scrutinize/processor.rb', line 70

def get_src(node)
  range = node.loc.expression

  # Line numbers start at 1
  begin_line   = range.begin.line - 1
  end_line     = range.end.line - 1
  middle_lines = begin_line + 1 ... end_line - 1

  # Column numbers start at 0
  begin_column = range.begin.column
  end_column   = range.end.column

  # Beginning/ending on same line
  if begin_line == end_line
    return src_lines[begin_line][begin_column...end_column]
  end

  lines = []
  lines << src_lines[begin_line][begin_column..-1]
  lines += src_lines[middle_lines]
  lines << src_lines[end_line][0...end_line]
  lines.join "\n"
end

#node?(node) ⇒ Boolean

Is an object a Parser::AST::Node.

node - the object to test.

Returns true/false.

Returns:

  • (Boolean)


61
62
63
# File 'lib/scrutinize/processor.rb', line 61

def node?(node)
  node.is_a? Parser::AST::Node
end

#processObject

Process the AST of the file.

Returns nothing.



22
23
24
25
# File 'lib/scrutinize/processor.rb', line 22

def process
  process_node ast
rescue Parser::SyntaxError
end

#process_node(node) ⇒ Object

Recurse into an Parser::AST::Node, calling appropriate process functions for it and its children nodes.

node - a Parser::AST::Node object.

Returns nothing.



33
34
35
36
37
38
# File 'lib/scrutinize/processor.rb', line 33

def process_node(node)
  if node?(node)
    process_send(node) if node.type == :send
    node.children.each { |c| process_node c }
  end
end

#process_send(node) ⇒ Object

Process a :send Parser::AST::Node. Call any registered handler for the target/method being invoked.

node - a Parser::AST::Node object.

Returns nothing.



46
47
48
49
50
51
52
53
54
# File 'lib/scrutinize/processor.rb', line 46

def process_send(node)
  target = get_src node.children[0] if node.children[0]
  target = target.slice(2..-1) if node.children[0] && target.start_with?('::')
  method = node.children[1]

  if @trigger.match? target, method
    puts "#{@file}:#{node.loc.line} #{target}#{'.' if target}#{method}"
  end
end

#srcObject

The source code.

Returns a String.



111
112
113
# File 'lib/scrutinize/processor.rb', line 111

def src
  @src ||= File.read @file
end

#src_linesObject

The source code, broken up into lines.

Returns an Array of Strings.



104
105
106
# File 'lib/scrutinize/processor.rb', line 104

def src_lines
  @src_lines = src.lines.to_a
end