Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8fecb7fc41dc^
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5ab0842a4388
Choose a head ref
  • 4 commits
  • 15 files changed
  • 1 contributor

Commits on Sep 6, 2014

  1. Add 0d123 base 10 syntax

    elia committed Sep 6, 2014

    Unverified

    The committer email address is not verified.
    Copy the full SHA
    8fecb7f View commit details
  2. Unverified

    The committer email address is not verified.
    Copy the full SHA
    284d3fd View commit details

Commits on Sep 7, 2014

  1. Copy the full SHA
    a036bd8 View commit details
  2. Copy the full SHA
    5ab0842 View commit details
2 changes: 1 addition & 1 deletion lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ def to_s
end

def source_map
processed.map(&:source_map).reduce(:+).to_s
processed.map(&:source_map).reduce(:+).as_json.to_json
end

attr_reader :processed
19 changes: 18 additions & 1 deletion lib/opal/builder_processors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'opal/compiler'
require 'opal/erb'
require 'source_map'

module Opal
module BuilderProcessors
@@ -29,7 +30,19 @@ def self.match_regexp
end

def source_map
'a map for: '+filename
@source_map ||= begin
mappings = []
source_file = filename+'.js'

source.each_line.with_index do |line_contents, line|
line += 1 # lines start with 1
line_contents.size.times do |column|
offset = ::SourceMap::Offset.new(line, column)
mappings << ::SourceMap::Mapping.new(source_file, offset, offset)
end
end
::SourceMap::Map.new(mappings)
end
end

def mark_as_required(filename)
@@ -52,6 +65,10 @@ def source
compiled.result
end

def source_map
compiled.source_map.map
end

def compiled
@compiled ||= begin
compiler = compiler_for(@source, file: @filename)
21 changes: 12 additions & 9 deletions lib/opal/parser/lexer.rb
Original file line number Diff line number Diff line change
@@ -176,21 +176,24 @@ def new_op_asgn(value)
def process_numeric
@lex_state = :expr_end

if scan(/0[bB](0|1|_)+/)
self.yylval = scanner.matched.to_i(2)
return :tINTEGER
elsif scan(/0[oO]?([0-7]|_)+/)
self.yylval = scanner.matched.to_i(8)
return :tINTEGER
elsif scan(/[\d_]+\.[\d_]+\b|[\d_]+(\.[\d_]+)?[eE][-+]?[\d_]+\b/)
if scan(/[\d_]+\.[\d_]+\b|[\d_]+(\.[\d_]+)?[eE][-+]?[\d_]+\b/) # FLOATS
self.yylval = scanner.matched.gsub(/_/, '').to_f
return :tFLOAT
elsif scan(/[\d_]+\b/)
elsif scan(/([^0][\d_]*|0)\b/) # BASE 10
self.yylval = scanner.matched.gsub(/_/, '').to_i
return :tINTEGER
elsif scan(/0[xX](\d|[a-f]|[A-F]|_)+/)
elsif scan(/0[bB](0|1|_)+/) # BASE 2
self.yylval = scanner.matched.to_i(2)
return :tINTEGER
elsif scan(/0[xX](\d|[a-f]|[A-F]|_)+/) # BASE 16
self.yylval = scanner.matched.to_i(16)
return :tINTEGER
elsif scan(/0[oO]?([0-7]|_)+/) # BASE 8
self.yylval = scanner.matched.to_i(8)
return :tINTEGER
elsif scan(/0[dD]([0-9]|_)+/) # BASE 10
self.yylval = scanner.matched.gsub(/_/, '').to_i
return :tINTEGER
else
raise "Lexing error on numeric type: `#{scanner.peek 5}`"
end
38 changes: 22 additions & 16 deletions lib/opal/source_map.rb
Original file line number Diff line number Diff line change
@@ -12,29 +12,35 @@ def initialize(fragments, file)
end

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
)
@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, source_offset, generated_offset)
end

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

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

mapping
end

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

2 changes: 1 addition & 1 deletion lib/opal/sprockets/processor.rb
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ def evaluate(context, locals, &block)
result = builder.build_str(data, path, :prerequired => prerequired)

if self.class.source_map_enabled
register_source_map(context.pathname, result.source_map.to_s)
register_source_map(context.pathname, result.source_map)
"#{result.to_s}\n//# sourceMappingURL=#{context.logical_path}.map\n"
else
result.to_s
2 changes: 1 addition & 1 deletion opal.gemspec
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.require_paths = ['lib']

s.add_dependency 'source_map'
s.add_dependency 'sourcemap', '~> 0.0.2'
s.add_dependency 'sprockets', '~> 2.12.1'
s.add_dependency 'hike', '~> 1.2'
s.add_dependency 'tilt', '~> 1.4'
68 changes: 5 additions & 63 deletions stdlib/source_map.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,5 @@
require 'json'

require 'source_map/vlq.rb'
require 'source_map/generator.rb'
require 'source_map/parser.rb'

class SourceMap
include SourceMap::Generator
include SourceMap::Parser

# Create a new blank SourceMap
#
# Options may include:
#
# :file => String # See {#file}
# :source_root => String # See {#source_root}
# :generated_output => IO # See {#generated_output}
#
# :sources => Array[String] # See {#sources}
# :names => Array[String] # See {#names}
#
# :version => 3 # Which version of SourceMap to use (only 3 is allowed)
#
def initialize(opts={})
unless (remain = opts.keys - [:generated_output, :file, :source_root, :sources, :names, :version]).empty?
raise ArgumentError, "Unsupported options to SourceMap.new: #{remain.inspect}"
end
self.generated_output = opts[:generated_output]
self.file = opts[:file] || ''
self.source_root = opts[:source_root] || ''
self.version = opts[:version] || 3
self.sources = opts[:sources] || []
self.names = opts[:names] || []
self.mappings = []
raise "version #{opts[:version]} not supported" if version != 3
end

# The name of the file containing the code that this SourceMap describes.
# (default "")
attr_accessor :file

# The URL/directory that contains the original source files.
#
# This is prefixed to the entries in ['sources']
# (default "")
attr_accessor :source_root

# The version of the SourceMap spec we're using.
# (default 3)
attr_accessor :version

# The list of sources (used during parsing/generating)
# These are relative to the source_root.
# (default [])
attr_accessor :sources

# A list of names (used during parsing/generating)
# (default [])
attr_accessor :names

# A list of mapping objects.
attr_accessor :mappings
end
require 'source_map/map'
require 'source_map/mapping'
require 'source_map/offset'
require 'source_map/version'
require 'source_map/vlq'
Loading