Module: Ruby2JS::Filter::ESMMigration

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

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Instance Method Details

#esm_walk(node) ⇒ Object

gather constants



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby2js/filter/esm_migration.rb', line 46

def esm_walk(node)
  # extract ivars and cvars
  if node.type == :const and node.children.first == nil
    @esm_include << node.children.last.to_s
  elsif node.type == :xnode
    name = node.children.first
    @esm_include << name unless name.empty? or name =~ /^[a-z]/
  elsif node.type == :casgn and node.children.first == nil
    @esm_exclude << node.children[1].to_s
  elsif node.type == :class and node.children.first.type == :const
    if node.children.first.children.first == nil
      name = node.children.first.children.last.to_s
      @esm_exclude << name
      @esm_export ||= name
    end
  end

  # recurse
  node.children.each do |child|
    esm_walk(child) if Parser::AST::Node === child
  end
end

#initialize(*args) ⇒ Object



8
9
10
11
# File 'lib/ruby2js/filter/esm_migration.rb', line 8

def initialize(*args)
  @esm_include = nil
  super
end

#process(node) ⇒ Object



13
14
15
16
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
# File 'lib/ruby2js/filter/esm_migration.rb', line 13

def process(node)
  return super if @esm_include
  @esm_include = Set.new
  @esm_exclude = Set.new
  @esm_export = nil
  result = super

  esm_walk(result)

  inventory = (@esm_include - @esm_exclude).to_a.sort

  if inventory.empty? and not @esm_export
    result
  else
    list = inventory.map do |name|
       if name == "React" and defined? Ruby2JS::Filter::React
         s(:import, "#{name.downcase}", s(:const, nil, name))
       elsif not %w(JSON Object).include? name
         s(:import, "./#{name.downcase}.js", s(:const, nil, name))
       end
    end

    list.push result

    if @esm_export
      list.push s(:export, :default, s(:const, nil, @esm_export))
    end

    s(:begin, *list.compact)
  end
end