Sha256: dca65e4fae7e7814f776cd1181b4ae26f74be156bf7ce7747aa7c312547e89d9

Contents?: true

Size: 1.67 KB

Versions: 26

Compression:

Stored size: 1.67 KB

Contents

# require 'opal'
require 'source_map'

module Opal
  class SourceMap
    attr_reader :fragments
    attr_reader :file

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

    def map
      @map ||= begin
        source_file = file+'.rb'
        generated_line, generated_column = 1, 0

        mappings = @fragments.map do |fragment|
          mapping = nil
          source_line   = fragment.line
          source_column = fragment.column
          source_code   = fragment.code

          if source_line and source_column
            source_offset    = ::SourceMap::Offset.new(source_line, source_column)
            generated_offset = ::SourceMap::Offset.new(generated_line, generated_column)

            mapping = ::SourceMap::Mapping.new(
              source_file,
              generated_offset,
              source_offset
            )
          end

          new_lines = source_code.count "\n"
          generated_line += new_lines

          if new_lines > 0
            generated_column = source_code.size - (source_code.rindex("\n") + 1)
          else
            generated_column += source_code.size
          end

          mapping
        end

        # Ensure mappings isn't empty: https://github.com/maccman/sourcemap/issues/11
        unless mappings.any?
          zero_offset = ::SourceMap::Offset.new(0,0)
          mappings = [::SourceMap::Mapping.new(source_file,zero_offset,zero_offset)]
        end

        ::SourceMap::Map.new(mappings.compact)
      end
    end

    def as_json
      map.as_json
    end

    def to_s
      map.to_s
    end

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

Version data entries

26 entries across 26 versions & 3 rubygems

Version Path
opal-0.7.0.rc1 lib/opal/source_map.rb
opal-0.7.0.beta3 lib/opal/source_map.rb
opal-0.7.0.beta2 lib/opal/source_map.rb
opal-cj-0.7.0.beta2 lib/opal/source_map.rb
opal-cj-0.7.0.beta1 lib/opal/source_map.rb
opal-0.7.0.beta1 lib/opal/source_map.rb