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

Inherits:
Object
  • Object
show all
Defined in:
/Users/elia/Code/opal-docs/opal/lib/opal/nodes/call.rb

Instance Method Summary collapse

Constructor Details

#initialize(compiler, sexp) ⇒ DependencyResolver

Returns a new instance of DependencyResolver


236
237
238
239
# File '/Users/elia/Code/opal-docs/opal/lib/opal/nodes/call.rb', line 236

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

Instance Method Details

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


275
276
277
278
279
280
281
282
283
284
285
286
287
# File '/Users/elia/Code/opal-docs/opal/lib/opal/nodes/call.rb', line 275

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


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File '/Users/elia/Code/opal-docs/opal/lib/opal/nodes/call.rb', line 245

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


241
242
243
# File '/Users/elia/Code/opal-docs/opal/lib/opal/nodes/call.rb', line 241

def resolve
  handle_part @sexp
end