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



10
11
12
# File 'lib/ruby2js/filter/require.rb', line 10

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

Instance Method Details

#initialize(*args) ⇒ Object



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

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

#on_casgn(node) ⇒ Object



78
79
80
81
82
83
# File 'lib/ruby2js/filter/require.rb', line 78

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

#on_lvasgn(node) ⇒ Object



71
72
73
74
75
76
# File 'lib/ruby2js/filter/require.rb', line 71

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

#on_send(node) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby2js/filter/require.rb', line 19

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)

      segments = basename.split(/[\/\\]/)

      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

      @options[:file2] = filename
      ast, comments = Ruby2JS.parse(File.read(filename), filename)
      @comments.merge! Parser::Source::Comment.associate(ast, comments)
      @comments[node] += @comments[ast]
      process ast
    ensure
      if file2
        @options[:file2] = file2
      else
        @options.delete(:file2)
      end
    end
  else
    begin
      require_expr, @require_expr = @require_expr, true
      super
    ensure
      @require_expr = require_expr
    end
  end
end