Class: RuboCop::Cop::Grep::Grepx

Inherits:
Base
  • Object
show all
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/grep/grep.rb

Overview

Detect code snippets which are matched with specified Regexps.

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/grep/grep.rb', line 10

def on_new_investigation
  source = processed_source.raw_source

  cop_config['Rules'].each do |rule|
    match_comment = rule['MatchInComment']

    # @type var patterns: Array[String]
    patterns = _ = Array(rule['Pattern'])

    opt = regexp_option(rule)
    patterns.each do |pat|
      re = Regexp.new(pat, opt)
      from = 0
      while m = re.match(source, from)
        if match_comment || !in_comment?(m)
          pos = position_from_matchdata(m)
          range = source_range(processed_source.buffer, pos[:line], pos[:column], pos[:length])
          add_offense(range, message: rule['Message'])
        end
        from = m.end(0) || raise
      end
    end
  end
end