Sha256: 4d2ec4a75e0035d4cc0916091a18433527d32466cc7f8c4704f820034d33f368

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

require 'open3'

module Rubocop
  module Cop
    class Syntax < Cop
      def inspect(file, source, tokens, sexp)
        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', stdin_data: source.join("\n"))
          end
        else
          _, stderr, _ =
            Open3.capture3('ruby -wc', stdin_data: source.join("\n"))
        end

        stderr.each_line do |line|
          # discard lines that are not containing relevant info
          if line =~ /.+:(\d+): (.+)/
            line_no, severity, message = process_line(line)
            add_offence(severity, line_no, message)
          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

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.7.2 lib/rubocop/cop/syntax.rb
rubocop-0.7.1 lib/rubocop/cop/syntax.rb
rubocop-0.7.0 lib/rubocop/cop/syntax.rb
rubocop-0.6.1 lib/rubocop/cop/syntax.rb
rubocop-0.6.0 lib/rubocop/cop/syntax.rb