Module: Ruby2JS::Filter::ESM

Includes:
SEXP
Defined in:
lib/ruby2js/filter/esm.rb

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Instance Method Details

#initialize(*args) ⇒ Object



10
11
12
13
14
# File 'lib/ruby2js/filter/esm.rb', line 10

def initialize(*args)
  super
  @esm = true # signal for other filters
  @esm_imports = nil
end

#on_const(node) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/ruby2js/filter/esm.rb', line 90

def on_const(node)
  return super unless @esm_autoimports
  if node.children.first == nil and @esm_autoimports[node.children.last]
    @esm_imports.add(node.children.last)
  end
  super
end

#on_send(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby2js/filter/esm.rb', line 36

def on_send(node)
  target, method, *args = node.children
  return super unless target.nil?

  if method == :import
    # don't do the conversion if the word import is followed by a paren
    if node.loc.respond_to? :selector
      selector = node.loc.selector
      if selector and selector.source_buffer
        return super if selector.source_buffer.source[selector.end_pos] == '('
      end
    end

    if args[0].type == :str
      # import "file.css"
      #   => import "file.css"
      s(:import, args[0].children[0])
    elsif args.length == 1 and \
       args[0].type == :send and \
      args[0].children[0].nil? and \
      args[0].children[2].type == :send and \
      args[0].children[2].children[0].nil? and \
      args[0].children[2].children[1] == :from and \
      args[0].children[2].children[2].type == :str
      # import name from "file.js"
      #  => import name from "file.js"
      s(:import,
        [args[0].children[2].children[2].children[0]],
        process(s(:attr, nil, args[0].children[1])))

    else
      # import Stuff, "file.js"
      #   => import Stuff from "file.js"
      # import Stuff, from: "file.js"
      #   => import Stuff from "file.js"
      # import Stuff, as: "*", from: "file.js"
      #   => import Stuff as * from "file.js"
      # import [ Some, Stuff ], from: "file.js"
      #   => import { Some, Stuff } from "file.js"
      imports = (args[0].type == :const || args[0].type == :send) ?
        process(args[0]) : 
        process_all(args[0].children)
      s(:import, args[1].children, imports) unless args[1].nil?
    end
  elsif method == :export          
    s(:export, *process_all(args))
  elsif @esm_imports and args.length == 0 and @esm_autoimports[method]
    @esm_imports.add(method)
    super
  else
    super
  end
end

#options=(options) ⇒ Object



16
17
18
19
20
# File 'lib/ruby2js/filter/esm.rb', line 16

def options=(options)
  super
  @esm_autoimports = options[:autoimports]
  return unless @esm_autoimports
end

#process(node) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby2js/filter/esm.rb', line 22

def process(node)
  return super if @esm_imports or not @esm_autoimports
  @esm_imports = Set.new
  result = super

  if @esm_imports.empty?
    result
  else
    s(:begin, *@esm_imports.to_a.map {|token|
      s(:import, @esm_autoimports[token], s(:const, nil, token))
    }, result)
  end
end