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

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

Instance Method Summary collapse

Constructor Details

#initialize(compiler, sexp) ⇒ DependencyResolver

Returns a new instance of DependencyResolver



231
232
233
234
# File 'opal/lib/opal/nodes/call.rb', line 231

def initialize(compiler, sexp)
  @compiler = compiler
  @sexp = sexp
end

Instance Method Details

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



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'opal/lib/opal/nodes/call.rb', line 270

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

    p
  end.join "/"
end

#handle_part(sexp) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'opal/lib/opal/nodes/call.rb', line 240

def handle_part(sexp)
  type = sexp.type

  if type == :str
    return sexp[1]
  elsif type == :call
    _, recv, meth, args = sexp

    parts = args[1..-1].map { |s| handle_part s }

    if recv == [:const, :File]
      if meth == :expand_path
        return expand_path(*parts)
      elsif meth == :join
        return expand_path parts.join('/')
      elsif meth == :dirname
        return expand_path parts[0].split('/')[0...-1].join('/')
      end
    end
  end

  msg = "Cannot handle dynamic require"
  case @compiler.dynamic_require_severity
  when :error
    @compiler.error msg, @sexp.line
  when :warning
    @compiler.warning msg, @sexp.line
  end
end

#resolveObject



236
237
238
# File 'opal/lib/opal/nodes/call.rb', line 236

def resolve
  handle_part @sexp
end