Sha256: 70a4a2a8c30c043f752a481eba520bf4c4b2c37516e3a6e3b0b3e27d17886ee9

Contents?: true

Size: 1.16 KB

Versions: 7

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true
require 'opal/rewriters/base'

module Opal
  module Rewriters
    class OpalEngineCheck < Base
      def on_if(node)
        test, true_body, false_body = *node.children

        if skip_check_present?(test)
          false_body = s(:nil)
        end

        if skip_check_present_not?(test)
          true_body = s(:nil)
        end

        node.updated(nil, process_all([test, true_body, false_body]))
      end

      def skip_check_present?(test)
        test == RUBY_ENGINE_CHECK or test == RUBY_PLATFORM_CHECK
      end

      def skip_check_present_not?(test)
        test == RUBY_ENGINE_CHECK_NOT or test == RUBY_PLATFORM_CHECK_NOT
      end

      RUBY_ENGINE_CHECK = s(:send, s(:const, nil, :RUBY_ENGINE),
                              :==, s(:str, "opal"))

      RUBY_ENGINE_CHECK_NOT = s(:send, s(:const, nil, :RUBY_ENGINE),
                              :!=, s(:str, "opal"))

      RUBY_PLATFORM_CHECK = s(:send, s(:const, nil, :RUBY_PLATFORM),
                              :==, s(:str, "opal"))

      RUBY_PLATFORM_CHECK_NOT = s(:send, s(:const, nil, :RUBY_PLATFORM),
                              :!=, s(:str, "opal"))
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opal-0.11.4 lib/opal/rewriters/opal_engine_check.rb
opal-0.11.3 lib/opal/rewriters/opal_engine_check.rb
opal-0.11.2 lib/opal/rewriters/opal_engine_check.rb
opal-0.11.1 lib/opal/rewriters/opal_engine_check.rb
opal-0.11.1.pre lib/opal/rewriters/opal_engine_check.rb
opal-0.11.0 lib/opal/rewriters/opal_engine_check.rb
opal-0.11.0.rc1 lib/opal/rewriters/opal_engine_check.rb