Class: Querly::CLI::Find

Inherits:
Object
  • Object
show all
Includes:
Querly::Concerns::BacktraceFormatter
Defined in:
lib/querly/cli/find.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Querly::Concerns::BacktraceFormatter

#format_backtrace

Constructor Details

#initialize(pattern:, paths:) ⇒ Find

Returns a new instance of Find.



11
12
13
14
# File 'lib/querly/cli/find.rb', line 11

def initialize(pattern:, paths:)
  @pattern_str = pattern
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



9
10
11
# File 'lib/querly/cli/find.rb', line 9

def paths
  @paths
end

#pattern_strObject (readonly)

Returns the value of attribute pattern_str.



8
9
10
# File 'lib/querly/cli/find.rb', line 8

def pattern_str
  @pattern_str
end

Instance Method Details

#analyzerObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/querly/cli/find.rb', line 48

def analyzer
  return @analyzer if @analyzer

  @analyzer = Analyzer.new(config: nil, rule: nil)

  ScriptEnumerator.new(paths: paths, config: nil).each do |path, script|
    case script
    when Script
      @analyzer.scripts << script
    when StandardError
      p path: path, script: script.inspect
      puts script.backtrace
    end
  end

  @analyzer
end

#patternObject



44
45
46
# File 'lib/querly/cli/find.rb', line 44

def pattern
  Pattern::Parser.parse(pattern_str, where: {})
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/querly/cli/find.rb', line 16

def start
  count = 0

  analyzer.find(pattern) do |script, pair|
    path = script.path.to_s
    line_no = pair.node.loc.first_line
    range = pair.node.loc.expression
    start_col = range.column
    end_col = range.last_column

    src = range.source_buffer.source_lines[line_no-1]
    src = Rainbow(src[0...start_col]).blue +
      Rainbow(src[start_col...end_col]).bright.blue.bold +
      Rainbow(src[end_col..-1]).blue

    puts "  #{path}:#{line_no}:#{start_col}\t#{src}"

    count += 1
  end

  puts "#{count} results"
rescue => exn
  STDOUT.puts Rainbow("Error: #{exn}").red
  STDTOU.puts "pattern: #{pattern_str}"
  STDOUT.puts "Backtrace:"
  STDOUT.puts format_backtrace(exn.backtrace)
end