Class: Rubocop::Cop::Syntax

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/syntax.rb

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#inspect_file(file) ⇒ Object



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

def inspect_file(file)
  # Starting JRuby processes would be extremely slow
  # We need to check if rbx returns nice warning messages
  return unless RUBY_ENGINE == 'ruby'

  stderr = nil

  # it's extremely important to run the syntax check in a
  # clean environment - otherwise it will be extremely slow
  if defined? Bundler
    Bundler.with_clean_env do
      _, stderr, _ = Open3.capture3("ruby -wc #{file}")
    end
  else
    _, stderr, _ = Open3.capture3("ruby -wc #{file}")
  end

  stderr.each_line do |line|
    # discard lines that are not containing relevant info
    if line =~ /.+:(\d+): (.+)/
      # Assignment to unused variables beginning with underscore
      # is reported by Ruby 1.9, but not 2.0. Make 1.9 behave
      # like 2.0.
      unless line =~ /assigned but unused variable - _\w+/
        line_no, severity, message = process_line(line)
        add_offence(severity, line_no, message)
      end
    end
  end
end

#process_line(line) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/syntax.rb', line 39

def process_line(line)
  line_no, message = line.match(/.+:(\d+): (.+)/).captures
  if message.start_with?('warning: ')
    [line_no.to_i, :warning, message.sub(/warning: /, '').capitalize]
  else
    [line_no.to_i, :error, message.capitalize]
  end
end