Sha256: 07389aa8650f9c611360507b3eaff8e801dd089e653142aa911aff0c394d0a0b

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Rubycw
    module WarningCapturer
      if defined?(RubyVM::AbstractSyntaxTree)
        module ::Warning
          def self.warn(*message, **kwargs)
            if WarningCapturer.warnings
              WarningCapturer.warnings.concat message
            else
              if RUBY_VERSION >= '3'
                # kwargs is available since Ruby 3
                super(*message, **kwargs)
              else
                super(*message)
              end
            end
          end
        end

        def self.capture(source)
          start
          RubyVM::AbstractSyntaxTree.parse(source)
          warnings
        ensure
          stop
        end

        def self.start
          @verbose = $VERBOSE
          $VERBOSE = true
          @warnings = []
        end

        def self.stop
          $VERBOSE = @verbose if defined?(@verbose)
          @warnings = nil
        end

        def self.warnings
          @warnings
        end

        stop
      else
        require 'rbconfig'
        require 'open3'

        def self.capture(source)
          _stdout, stderr, _status = Open3.capture3(RbConfig.ruby, '-cw', stdin_data: source)
          stderr.lines.map(&:chomp)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rubycw-0.1.6 lib/rubocop/rubycw/warning_capturer.rb