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

#on_class(node) ⇒ Object



50
51
52
53
54
# File 'lib/ruby2js/filter/esm.rb', line 50

def on_class(node)
  @esm_explicit_tokens << node.children.first.children.last

  super
end

#on_const(node) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/ruby2js/filter/esm.rb', line 129

def on_const(node)
  if node.children.first == nil and found_import = find_autoimport(node.children.last)
    prepend_list << s(:import, found_import[0], found_import[1])
  end

  super
end

#on_def(node) ⇒ Object



56
57
58
59
60
# File 'lib/ruby2js/filter/esm.rb', line 56

def on_def(node)
  @esm_explicit_tokens << node.children.first

  super
end

#on_export(node) ⇒ Object



137
138
139
# File 'lib/ruby2js/filter/esm.rb', line 137

def on_export(node)
  s(:export, *process_all(node.children))
end

#on_lvasgn(node) ⇒ Object



62
63
64
65
66
# File 'lib/ruby2js/filter/esm.rb', line 62

def on_lvasgn(node)
  @esm_explicit_tokens << node.children.first

  super
end

#on_send(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby2js/filter/esm.rb', line 68

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"
      @esm_explicit_tokens << args[0].children[1]

      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 = if args[0].type == :const || args[0].type == :send
        @esm_explicit_tokens << args[0].children.last
        process(args[0])
      else
        args[0].children.each {|i| @esm_explicit_tokens << i.children.last}
        process_all(args[0].children)
      end

      s(:import, args[1].children, imports) unless args[1].nil?
    end
  elsif method == :export          
    s(:export, *process_all(args))
  elsif target.nil? and found_import = find_autoimport(method)
    prepend_list << s(:import, found_import[0], found_import[1])
    super
  else
    super
  end
end

#options=(options) ⇒ Object



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

def options=(options)
  super
  @esm_autoexports = options[:autoexports] && !@disable_autoexports
  @esm_autoimports = options[:autoimports]
  @esm_explicit_tokens = Set.new
end

#process(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby2js/filter/esm.rb', line 17

def process(node)
  return super unless @esm_autoexports
  @esm_autoexports = false

  list = [node]
  while list.length == 1 and list.first.type == :begin
    list = list.first.children.dup
  end

  list.map! do |child|
    replacement = child

    if [:module, :class].include? child.type and
      child.children.first.type == :const and
      child.children.first.children.first == nil \
    then
      replacement = s(:export, child)
    elsif child.type == :casgn and child.children.first == nil
      replacement = s(:export, child)
    elsif child.type == :def
      replacement = s(:export, child)
    end

    if replacement != child and @comments[child]
      @comments[replacement] = @comments[child]
    end

    replacement
  end

  process s(:begin, *list)
end