Class: NodeGrep::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/nodegrep/matcher.rb

Defined Under Namespace

Classes: SourceParseError

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Matcher

Returns a new instance of Matcher.



8
9
10
11
12
13
# File 'lib/nodegrep/matcher.rb', line 8

def initialize(source)
  @parsed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f)
  @parsed_source.diagnostics.each do |d|
    raise SourceParseError if d.level == :error
  end
end

Instance Method Details

#matching_nodes(pattern) ⇒ Object



15
16
17
18
19
20
# File 'lib/nodegrep/matcher.rb', line 15

def matching_nodes(pattern)
  @matches = []
  @pattern = RuboCop::NodePattern.new(pattern)
  visit_node(@parsed_source.ast)
  @matches
end