Sha256: 2999e96cba0ec0fae29809fe7319f388adac43935ac6f1be24f681c262e74d37

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'ruby2js'

module Ruby2JS
  module Filter
    module ActiveFunctions
      include SEXP

      def on_send(node)
        target, method, *args = node.children

        if es2015 and method == :blank?
          create_or_update_import("blank$")
          process node.updated :send, [nil, "blank$", target]
        elsif es2015 and method == :present?
          create_or_update_import("present$")
          process node.updated :send, [nil, "present$", target]
        elsif es2015 and method == :presence
          create_or_update_import("presence$")
          process node.updated :send, [nil, "presence$", target]
        else
          super
        end
      end

      private

      def create_or_update_import(token)
        af_import = @options[:import_from_skypack] ? "https://cdn.skypack.dev/@ruby2js/active-functions" : "@ruby2js/active-functions"

        if found_node = prepend_list.find {|ast| ast.type == :import && ast.children.first == af_import}
          unless found_node.children.find {|child| child == token}
            prepend_list.delete found_node
            prepend_list << s(:import, found_node.children.first, found_node.children.last.push(s(:const, nil, token)))
          end
        else
          prepend_list << s(:import, af_import, [s(:const, nil, token)])
        end
      end
    end

    DEFAULTS.push ActiveFunctions
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby2js-3.6.0 lib/ruby2js/filter/active_functions.rb