Class: Opal::Nodes::CallNode::DependencyResolver

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/nodes/call/require.rb

Instance Method Summary collapse

Constructor Details

#initialize(compiler, sexp, missing_dynamic_require = nil) ⇒ DependencyResolver

Returns a new instance of DependencyResolver.



57
58
59
60
61
# File 'opal/lib/opal/nodes/call/require.rb', line 57

def initialize(compiler, sexp, missing_dynamic_require = nil)
  @compiler = compiler
  @sexp = sexp
  @missing_dynamic_require = missing_dynamic_require || @compiler.dynamic_require_severity
end

Instance Method Details

#expand_path(path, base = '') ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'opal/lib/opal/nodes/call/require.rb', line 106

def expand_path(path, base = '')
  "#{base}/#{path}".split('/').each_with_object([]) do |part, p|
    if part == ''
      # we had '//', so ignore
    elsif part == '..'
      p.pop
    else
      p << part
    end
  end.join '/'
end

#handle_part(sexp, missing_dynamic_require = @missing_dynamic_require) ⇒ Object



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
# File 'opal/lib/opal/nodes/call/require.rb', line 67

def handle_part(sexp, missing_dynamic_require = @missing_dynamic_require)
  if sexp
    case sexp.type
    when :str
      return sexp.children[0]
    when :dstr
      return sexp.children.map { |i| handle_part i }.join
    when :begin
      return handle_part sexp.children[0] if sexp.children.length == 1
    when :send
      recv, meth, *args = sexp.children

      parts = args.map { |s| handle_part(s, :ignore) }

      return nil if parts.include? nil

      if recv.is_a?(::Opal::AST::Node) && recv.type == :const && recv.children.last == :File
        case meth
        when :expand_path
          return expand_path(*parts)
        when :join
          return expand_path parts.join('/')
        when :dirname
          return expand_path parts[0].split('/')[0...-1].join('/')
        end
      elsif meth == :__dir__
        return File.dirname(Opal::Compiler.module_name(@compiler.file))
      end
    end
  end

  case missing_dynamic_require
  when :error
    @compiler.error 'Cannot handle dynamic require', @sexp.line
  when :warning
    @compiler.warning 'Cannot handle dynamic require', @sexp.line
  end
end

#resolveObject



63
64
65
# File 'opal/lib/opal/nodes/call/require.rb', line 63

def resolve
  handle_part @sexp
end