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

Inherits:
Object
  • Object
show all
Defined in:
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.



496
497
498
499
500
# File 'lib/opal/nodes/call.rb', line 496

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



544
545
546
547
548
549
550
551
552
553
554
# File 'lib/opal/nodes/call.rb', line 544

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



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/opal/nodes/call.rb', line 506

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



502
503
504
# File 'lib/opal/nodes/call.rb', line 502

def resolve
  handle_part @sexp
end