Class: Opal::SourceMap

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fragments, file) ⇒ SourceMap

Returns a new instance of SourceMap



9
10
11
12
# File 'opal/lib/opal/source_map.rb', line 9

def initialize(fragments, file)
  @fragments = fragments
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file



7
8
9
# File 'opal/lib/opal/source_map.rb', line 7

def file
  @file
end

#fragmentsObject (readonly)

Returns the value of attribute fragments



6
7
8
# File 'opal/lib/opal/source_map.rb', line 6

def fragments
  @fragments
end

Instance Method Details

#as_jsonObject



41
42
43
# File 'opal/lib/opal/source_map.rb', line 41

def as_json
  map.as_json
end

#magic_comment(map_path) ⇒ Object



49
50
51
# File 'opal/lib/opal/source_map.rb', line 49

def magic_comment map_path
  "\n//# sourceMappingURL=file://#{map_path}"
end

#mapObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'opal/lib/opal/source_map.rb', line 14

def map
  @map ||= ::SourceMap.new.tap do |map|
    line, column = 1, 0

    @fragments.each do |fragment|
      if source_line = fragment.line
        map.add_mapping(
          :generated_line => line,
          :generated_col  => column,
          :source_line    => source_line,
          :source_col     => fragment.column,
          :source         => file
        )
      end

      new_lines = fragment.code.count "\n"
      line += new_lines

      if new_lines > 0
        column = fragment.code.size - (fragment.code.rindex("\n") + 1)
      else
        column += fragment.code.size
      end
    end
  end
end

#to_sObject



45
46
47
# File 'opal/lib/opal/source_map.rb', line 45

def to_s
  map.to_s
end