class SourceMap::Map
Attributes
filename[R]
mappings[R]
Public Class Methods
decode_vlq_mappings(str, sources = [], names = [])
click to toggle source
Internal: Decode VLQ
mappings and match up sources and symbol names.
str - VLQ
string from 'mappings' attribute sources - Array of Strings from 'sources' attribute names - Array of Strings from 'names' attribute
Returns an Array of Mappings.
# File lib/source_map/map.rb, line 31 def self.decode_vlq_mappings(str, sources = [], names = []) mappings = [] source_id = 0 original_line = 1 original_column = 0 name_id = 0 VLQ.decode_mappings(str).each_with_index do |group, index| generated_column = 0 generated_line = index + 1 group.each do |segment| generated_column += segment[0] generated = Offset.new(generated_line, generated_column) if segment.size >= 4 source_id += segment[1] original_line += segment[2] original_column += segment[3] source = sources[source_id] original = Offset.new(original_line, original_column) else # TODO: Research this case next end if segment[4] name_id += segment[4] name = names[name_id] end mappings << Mapping.new(source, generated, original, name) end end mappings end
from_hash(hash)
click to toggle source
# File lib/source_map/map.rb, line 15 def self.from_hash(hash) str = hash['mappings'] sources = hash['sources'] names = hash['names'] mappings = decode_vlq_mappings(str, sources, names) new(mappings, hash['file']) end
from_json(json)
click to toggle source
# File lib/source_map/map.rb, line 11 def self.from_json(json) from_hash JSON.parse(json) end
new(mappings = [], filename = nil)
click to toggle source
# File lib/source_map/map.rb, line 71 def initialize(mappings = [], filename = nil) @mappings, @filename = mappings, filename end
Public Instance Methods
+(other)
click to toggle source
# File lib/source_map/map.rb, line 111 def +(other) mappings = @mappings.dup offset = @mappings.any? ? @mappings.last.generated.line+1 : 0 other.each do |m| mappings << Mapping.new( m.source, m.generated + offset, m.original, m.name ) end self.class.new(mappings, other.filename) end
==(other)
click to toggle source
# File lib/source_map/map.rb, line 101 def ==(other) eql?(other) end
[](i)
click to toggle source
# File lib/source_map/map.rb, line 81 def [](i) @mappings[i] end
as_json(*)
click to toggle source
# File lib/source_map/map.rb, line 161 def as_json(*) { "version" => 3, "file" => filename, "mappings" => to_s, "sources" => sources, "names" => names } end
bsearch(offset, from = 0, to = size - 1)
click to toggle source
# File lib/source_map/map.rb, line 141 def bsearch(offset, from = 0, to = size - 1) mid = (from + to) / 2 # We haven't found a match if from > to return from < 1 ? nil : self[from-1] end # We found an exact match if offset == self[mid].generated self[mid] # We need to filter more elsif offset < self[mid].generated bsearch(offset, from, mid - 1) elsif offset > self[mid].generated bsearch(offset, mid + 1, to) end end
each(&block)
click to toggle source
# File lib/source_map/map.rb, line 85 def each(&block) @mappings.each(&block) end
eql?(other)
click to toggle source
# File lib/source_map/map.rb, line 105 def eql?(other) other.is_a?(self.class) && self.mappings == other.mappings && self.filename == other.filename end
inspect()
click to toggle source
Public: Get a pretty inspect output for debugging purposes.
Returns a String.
# File lib/source_map/map.rb, line 178 def inspect str = "#<#{self.class}" str << " filename=#{filename.inspect}" if filename str << " mappings=#{mappings.map(&:to_s).inspect}" if mappings.any? str << ">" str end
names()
click to toggle source
# File lib/source_map/map.rb, line 97 def names @names ||= @mappings.map(&:name).uniq.compact end
size()
click to toggle source
# File lib/source_map/map.rb, line 77 def size @mappings.size end
sources()
click to toggle source
# File lib/source_map/map.rb, line 93 def sources @sources ||= @mappings.map(&:source).uniq.compact end
to_json(*a)
click to toggle source
# File lib/source_map/map.rb, line 171 def to_json(*a) as_json.to_json(*a) end
to_s()
click to toggle source
# File lib/source_map/map.rb, line 89 def to_s @string ||= build_vlq_string end
|(other)
click to toggle source
# File lib/source_map/map.rb, line 123 def |(other) return other.dup if self.mappings.empty? mappings = [] other.each do |m| om = bsearch(m.original) next unless om mappings << Mapping.new( om.source, m.generated, om.original, om.name ) end self.class.new(mappings, other.filename) end
Protected Instance Methods
build_vlq_string()
click to toggle source
# File lib/source_map/map.rb, line 189 def build_vlq_string source_id = 0 source_line = 1 source_column = 0 name_id = 0 by_lines = @mappings.group_by { |m| m.generated.line } sources_index = Hash[sources.each_with_index.to_a] names_index = Hash[names.each_with_index.to_a] ary = (1..(by_lines.keys.max || 1)).map do |line| generated_column = 0 (by_lines[line] || []).map do |mapping| group = [] group << mapping.generated.column - generated_column group << sources_index[mapping.source] - source_id group << mapping.original.line - source_line group << mapping.original.column - source_column group << names_index[mapping.name] - name_id if mapping.name generated_column = mapping.generated.column source_id = sources_index[mapping.source] source_line = mapping.original.line source_column = mapping.original.column name_id = names_index[mapping.name] if mapping.name group end end VLQ.encode_mappings(ary) end