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, missing_dynamic_require = nil) ⇒ DependencyResolver

Returns a new instance of DependencyResolver.



443
444
445
446
447
# File 'opal/lib/opal/nodes/call.rb', line 443

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



491
492
493
494
495
496
497
498
499
500
501
# File 'opal/lib/opal/nodes/call.rb', line 491

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



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'opal/lib/opal/nodes/call.rb', line 453

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
        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
      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



449
450
451
# File 'opal/lib/opal/nodes/call.rb', line 449

def resolve
  handle_part @sexp
end