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


299
300
301
302
# File 'opal/lib/opal/nodes/call.rb', line 299

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

Instance Method Details

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


338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'opal/lib/opal/nodes/call.rb', line 338

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


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'opal/lib/opal/nodes/call.rb', line 308

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


304
305
306
# File 'opal/lib/opal/nodes/call.rb', line 304

def resolve
  handle_part @sexp
end