Module: Ruby2JS::Filter::Require

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

Constant Summary collapse

@@valid_path =
/\A[-\w.]+\Z/

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Class Method Details

.valid_path=(valid_path) ⇒ Object



13
14
15
# File 'lib/ruby2js/filter/require.rb', line 13

def self.valid_path=(valid_path)
  @@valid_path = valid_path
end

Instance Method Details

#initialize(*args) ⇒ Object



17
18
19
20
21
22
# File 'lib/ruby2js/filter/require.rb', line 17

def initialize(*args)
  @require_expr = nil
  @require_seen = {}
  @require_relative = '.'
  super
end

#on_casgn(node) ⇒ Object



187
188
189
190
191
192
# File 'lib/ruby2js/filter/require.rb', line 187

def on_casgn(node)
  require_expr, @require_expr = @require_expr, true
  super
ensure
  @require_expr = require_expr
end

#on_lvasgn(node) ⇒ Object



180
181
182
183
184
185
# File 'lib/ruby2js/filter/require.rb', line 180

def on_lvasgn(node)
  require_expr, @require_expr = @require_expr, true
  super
ensure
  @require_expr = require_expr
end

#on_send(node) ⇒ Object



30
31
32
33
34
35
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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ruby2js/filter/require.rb', line 30

def on_send(node)
  if \
    not @require_expr and # only statements
    node.children.length == 3 and
    node.children[0] == nil and
    [:require, :require_relative].include? node.children[1] and
    node.children[2].type == :str and
    @options[:file]
  then

    begin
      file2 = @options[:file2]  

      basename = node.children[2].children.first
      dirname = File.dirname(File.expand_path(@options[:file]))

      if file2 and node.children[1] == :require_relative
        dirname = File.dirname(File.expand_path(file2))
      end

      filename = File.join(dirname, basename)

      if not File.file? filename and File.file? filename+".rb"
        filename += '.rb'
      elsif not File.file? filename and File.file? filename+".js.rb"
        filename += '.js.rb'
      end

      realpath = File.realpath(filename)
      if @require_seen[realpath]
        ast = s(:hide)
      else
        @require_seen[realpath] = []

        @options[:file2] = filename
        ast, comments = Ruby2JS.parse(File.read(filename), filename)
        @comments.merge! Parser::Source::Comment.associate(ast, comments)
        @comments[node] += @comments[ast]
      end

      children = ast.type == :begin ? ast.children : [ast]

      named_exports = []
      auto_exports = []
      default_exports = []
      children.each do |child|
        if child&.type == :send and child.children[0..1] == [nil, :export]
          child = child.children[2]
          if child&.type == :send and child.children[0..1] == [nil, :default]
            child = child.children[2]
            target = default_exports
          else
            target = named_exports
          end
        elsif @require_autoexports
          target = auto_exports
        else
          next
        end

        if i[class module].include? child.type and child.children[0].children[0] == nil
          target << child.children[0].children[1]
        elsif child.type == :casgn and child.children[0] == nil
          target << child.children[1]
        elsif child.type == :def
          target << child.children[0]
        elsif child.type == :send && child.children[1] == :async
          target << child.children[2].children[0]
        elsif child.type == :const
          target << child.children[1]
        elsif child.type == :array
          child.children.each do |export_statement|
            if export_statement.type == :const
              target << export_statement.children[1]
            elsif export_statement.type == :hash
              default_exports << export_statement.children[0].children[1].children[1]
            end
          end
        end
      end

      if @require_autoexports == :default and auto_exports.length == 1
        default_exports += auto_exports
      else
        named_exports += auto_exports
      end
      default_exports.map! { _1.to_s.sub(/[?!]/, '').then do |name|
        respond_to?(:camelCase) ? camelCase(name) : name.to_sym
      end }
      named_exports.map! { _1.to_s.sub(/[?!]/, '').then do |name|
        respond_to?(:camelCase) ? camelCase(name) : name.to_sym
      end }

      imports = @require_seen[realpath]
      imports << s(:const, nil, default_exports.first) unless default_exports.empty?
      imports << named_exports.map {|id| s(:const, nil, id)} unless named_exports.empty?

      if imports.empty?
        process ast
      else
        @require_seen[realpath] = imports

        importname = Pathname.new(filename).relative_path_from(Pathname.new(dirname)).to_s
        importname = Pathname.new(@require_relative).join(importname).to_s
        importname = "./#{importname}" unless importname.start_with? '.'

        prepend_list << s(:import, importname, *imports)

        save_prepend_list = prepend_list.dup

        begin
          require_relative = @require_relative
          @require_relative = Pathname.new(@require_relative).join(basename).parent.to_s
          node = process s(:hide, ast)
        ensure
          @require_relative = require_relative
        end

        if @require_recursive
          block = node.children
          while block.length == 1 and block.first.type == :begin
             block = block.first.children
          end

          block.each do |child|
            if child&.type == :import
              puts ['rr', basename, child.inspect]
              prepend_list << child 
            end
          end
        else
          prepend_list.keep_if do |import|
            save_prepend_list.include? import 
          end
        end

        node
      end
    ensure
      if file2
        @options[:file2] = file2
      else
        @options.delete(:file2)
      end
    end
  else
    super
  end
end

#options=(options) ⇒ Object



24
25
26
27
28
# File 'lib/ruby2js/filter/require.rb', line 24

def options=(options)
  super
  @require_autoexports = !@disable_autoexports && options[:autoexports]
  @require_recursive = options[:require_recursive]
end