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



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

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

Instance Method Details

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



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

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



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

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



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

def resolve
  handle_part @sexp
end