Class: Querly::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/querly/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, rule:) ⇒ Analyzer

Returns a new instance of Analyzer.



7
8
9
10
11
# File 'lib/querly/analyzer.rb', line 7

def initialize(config:, rule:)
  @config = config
  @scripts = []
  @rule = rule
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/querly/analyzer.rb', line 3

def config
  @config
end

#ruleObject (readonly)

Returns the value of attribute rule.



5
6
7
# File 'lib/querly/analyzer.rb', line 5

def rule
  @rule
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



4
5
6
# File 'lib/querly/analyzer.rb', line 4

def scripts
  @scripts
end

Instance Method Details

#find(pattern) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/querly/analyzer.rb', line 31

def find(pattern)
  scripts.each do |script|
    script.root_pair.each_subpair do |node_pair|
      if test_pair(node_pair, pattern)
        yield script, node_pair
      end
    end
  end
end

#runObject

yields(script, rule, node_pair)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/querly/analyzer.rb', line 16

def run
  scripts.each do |script|
    rules = config.rules_for_path(script.path)
    script.root_pair.each_subpair do |node_pair|
      rules.each do |rule|
        if rule.match?(identifier: self.rule)
          if rule.patterns.any? {|pattern| test_pair(node_pair, pattern) }
            yield script, rule, node_pair
          end
        end
      end
    end
  end
end

#test_pair(node_pair, pattern) ⇒ Object



41
42
43
# File 'lib/querly/analyzer.rb', line 41

def test_pair(node_pair, pattern)
  pattern.expr =~ node_pair && pattern.test_kind(node_pair)
end