Class: Opal::Fragment

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/fragment.rb

Overview

A fragment holds a string of generated javascript that will be written to the destination. It also keeps hold of the original sexp from which it was generated. Using this sexp, when writing fragments in order, a mapping can be created of the original location => target location, aka, source-maps!

These are generated by nodes, so will not have to create directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, scope, sexp = nil) ⇒ Fragment

Create fragment with javascript code and optional original [Opal::Sexp].

Parameters:

  • code (String)

    javascript code

  • sexp (Opal::Sexp) (defaults to: nil)

    sexp used for creating fragment



19
20
21
22
23
# File 'opal/lib/opal/fragment.rb', line 19

def initialize(code, scope, sexp = nil)
  @code = code.to_s
  @sexp = sexp
  @scope = scope
end

Instance Attribute Details

#codeString (readonly)

String of javascript this fragment holds

Returns:

  • (String)


13
14
15
# File 'opal/lib/opal/fragment.rb', line 13

def code
  @code
end

Instance Method Details

#columnInteger?

Original column this fragment was created from

Returns:

  • (Integer, nil)


44
45
46
# File 'opal/lib/opal/fragment.rb', line 44

def column
  @sexp.column if @sexp
end

#inspectObject

Inspect the contents of this fragment, f("fooo")



26
27
28
# File 'opal/lib/opal/fragment.rb', line 26

def inspect
  "f(#{@code.inspect})"
end

#lineInteger?

Original line this fragment was created from

Returns:

  • (Integer, nil)


38
39
40
# File 'opal/lib/opal/fragment.rb', line 38

def line
  @sexp.line if @sexp
end

#source_map_nameObject



30
31
32
33
34
# File 'opal/lib/opal/fragment.rb', line 30

def source_map_name
  return nil unless @scope
  def_node = @scope.def? ? @scope : @scope.find_parent_def
  def_node && def_node.mid
end