Sha256: 70b37be254aac6a5a69f1b1584c0aefa65705b271235826e6bd27fd0417279ed

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# encoding: utf-8

require 'open3'

module Rubocop
  module Cop
    class Syntax < Cop
      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

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.8.3 lib/rubocop/cop/syntax.rb
rubocop-0.8.2 lib/rubocop/cop/syntax.rb
rubocop-0.8.1 lib/rubocop/cop/syntax.rb
rubocop-0.8.0 lib/rubocop/cop/syntax.rb