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:, config: nil, threads:) ⇒ Find

Returns a new instance of Find.



13
14
15
16
17
18
# File 'lib/querly/cli/find.rb', line 13

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#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

#threadsObject (readonly)

Returns the value of attribute threads.



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

def threads
  @threads
end

Instance Method Details

#analyzerObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/querly/cli/find.rb', line 52

def analyzer
  return @analyzer if @analyzer

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

  ScriptEnumerator.new(paths: paths, config: config, threads: threads).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



48
49
50
# File 'lib/querly/cli/find.rb', line 48

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

#startObject



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

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
  STDOUT.puts "pattern: #{pattern_str}"
  STDOUT.puts "Backtrace:"
  STDOUT.puts format_backtrace(exn.backtrace)
end