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: e59c0e5d60a7
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: dd7d39d25d51
Choose a head ref
  • 4 commits
  • 12 files changed
  • 2 contributors

Commits on May 13, 2015

  1. Replace ^ and $ with \A and \z in regular expressions

    Unless you specifically want to match the start and end of any line
    in the string, as opposed to the start and end of the string itself,
    you should avoid using ^ and $.
    jeremyevans committed May 13, 2015
    Copy the full SHA
    39234b4 View commit details

Commits on May 14, 2015

  1. Copy the full SHA
    d4fda8f View commit details
  2. Add Opal::REGEXP_START and REGEXP_END constants

    These will be \A and \z in ruby mode and ^ and $ in javascript
    mode, giving the regexps that use them the same semantics in
    both ruby and javascript.
    
    I think a better solution would be to have opal automatically
    translate \A and \z to ^ and $, but that invites a slippery
    slope of trying to convert ruby regexp syntax to javascript
    regexp syntax.
    jeremyevans committed May 14, 2015
    Copy the full SHA
    9d96a7f View commit details

Commits on May 15, 2015

  1. Merge pull request #852 from jeremyevans/regexp_fixes

    Replace ^ and $ with \A and \z in regular expressions
    elia committed May 15, 2015

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    dd7d39d View commit details
2 changes: 1 addition & 1 deletion lib/mspec/opal/rake_task.rb
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ def add_files specs, tag = ''

def paths_from_glob pattern
Dir.glob(File.expand_path(pattern)).map do |s|
s.sub(/^#{basedir}\//, '').sub(/\.rb$/, '')
s.sub(/\A#{basedir}\//, '').sub(/\.rb\z/, '')
end
end

4 changes: 2 additions & 2 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ def tree_requires(asset, path)
globs = extensions.map { |ext| File.join base, tree, '**', "*.#{ext}" }

Dir[*globs].map do |file|
Pathname(file).relative_path_from(Pathname(base)).to_s.gsub(/(\.js)?(\.(?:#{extensions.join '|'}))$/, '')
Pathname(file).relative_path_from(Pathname(base)).to_s.gsub(/(\.js)?(\.(?:#{extensions.join '|'}))#{REGEXP_END}/, '')
end
end
end
@@ -102,7 +102,7 @@ def read(path)
end

def process_require(filename, options)
filename.gsub!(/\.(rb|js|opal)$/, '')
filename.gsub!(/\.(rb|js|opal)#{REGEXP_END}/, '')
return if prerequired.include?(filename)
return if already_processed.include?(filename)
already_processed << filename
4 changes: 2 additions & 2 deletions lib/opal/builder_processors.rb
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ def self.handles(*extensions)
matches = extensions.join('|')
matches = "(#{matches})" if extensions.size == 1

@match_regexp = Regexp.new "\\.#{matches}$"
@match_regexp = Regexp.new "\\.#{matches}#{REGEXP_END}"
end

def self.extensions
@@ -87,7 +87,7 @@ def source_map

def compiled
@compiled ||= begin
compiler = compiler_for(@source, file: @filename.gsub(/\.(rb|js|opal)$/, ''))
compiler = compiler_for(@source, file: @filename.gsub(/\.(rb|js|opal)#{REGEXP_END}/, ''))
compiler.compile
compiler
end
2 changes: 1 addition & 1 deletion lib/opal/erb.rb
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ def find_code(result)
end

def wrap_compiled(result)
path = @file_name.sub(/\.opalerb$/, '')
path = @file_name.sub(/\.opalerb#{REGEXP_END}/, '')
result = "Template.new('#{path}') do |output_buffer|\noutput_buffer.append(\"#{result}\")\noutput_buffer.join\nend\n"
end
end
2 changes: 1 addition & 1 deletion lib/opal/nodes/call.rb
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ def recv_sexp
end

def attr_assignment?
@assignment ||= meth.to_s =~ /^[\da-z]+\=$/i
@assignment ||= meth.to_s =~ /#{REGEXP_START}[\da-z]+\=#{REGEXP_END}/i
end

# Used to generate the code to use this sexp as an ivar var reference
2 changes: 1 addition & 1 deletion lib/opal/nodes/call_special.rb
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ class AttrAssignNode < CallNode

def default_compile
# Skip, for now, if the method has square brackets: []=
return super if meth.to_s !~ /^\w+=$/
return super if meth.to_s !~ /#{REGEXP_START}\w+=#{REGEXP_END}/

with_temp do |args_tmp|
with_temp do |recv_tmp|
10 changes: 6 additions & 4 deletions lib/opal/nodes/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require 'opal/regexp_anchors'

module Opal
module Nodes
module Helpers

# Reserved javascript keywords - we cannot create variables with the
# same name (ref: http://stackoverflow.com/a/9337272/601782)
ES51_RESERVED_WORD = /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/
ES51_RESERVED_WORD = /#{REGEXP_START}(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)#{REGEXP_END}/

# ES3 reserved words that aren’t ES5.1 reserved words
ES3_RESERVED_WORD_EXCLUSIVE = /^(?:int|byte|char|goto|long|final|float|short|double|native|throws|boolean|abstract|volatile|transient|synchronized)$/
ES3_RESERVED_WORD_EXCLUSIVE = /#{REGEXP_START}(?:int|byte|char|goto|long|final|float|short|double|native|throws|boolean|abstract|volatile|transient|synchronized)#{REGEXP_END}/

# Immutable properties of the global object
IMMUTABLE_PROPS = /^(?:NaN|Infinity|undefined)$/
IMMUTABLE_PROPS = /#{REGEXP_START}(?:NaN|Infinity|undefined)#{REGEXP_END}/

# Doesn't take in account utf8
BASIC_IDENTIFIER_RULES = /^[$_a-z][$_a-z\d]*$/i
BASIC_IDENTIFIER_RULES = /#{REGEXP_START}[$_a-z][$_a-z\d]*#{REGEXP_END}/i


def property(name)
5 changes: 3 additions & 2 deletions lib/opal/parser/lexer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'opal/regexp_anchors'
require 'strscan'
require 'opal/parser/keywords'

@@ -558,7 +559,7 @@ def process_identifier(matched, cmd_start)
matched += scanner.matched
end

elsif matched =~ /^[A-Z]/
elsif matched =~ /#{REGEXP_START}[A-Z]/
result = :tCONSTANT
else
result = :tIDENTIFIER
@@ -627,7 +628,7 @@ def process_identifier(matched, cmd_start)
@lex_state = :expr_end
end

return matched =~ /^[A-Z]/ ? :tCONSTANT : :tIDENTIFIER
return matched =~ /#{REGEXP_START}[A-Z]/ ? :tCONSTANT : :tIDENTIFIER
end

# Does the heavy lifting for `next_token`.
5 changes: 5 additions & 0 deletions lib/opal/regexp_anchors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Opal
REGEXP_START = RUBY_ENGINE == 'opal' ? '^' : '\A'.freeze
REGEXP_END = RUBY_ENGINE == 'opal' ? '$' : '\z'.freeze
end

2 changes: 1 addition & 1 deletion lib/opal/sprockets/erb.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ def initialize_engine
end

def evaluate(context, locals, &block)
compiler = Opal::ERB::Compiler.new(@data, context.logical_path.sub(/^templates\//, ''))
compiler = Opal::ERB::Compiler.new(@data, context.logical_path.sub(/#{REGEXP_START}templates\//, ''))
@data = compiler.prepared_source
super
end
6 changes: 3 additions & 3 deletions lib/opal/sprockets/processor.rb
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ def process_required_trees(required_trees, context)

# This is the root dir of the logical path, we need this because
# the compiler gives us the path relative to the file's logical path.
dirname = File.dirname(file).gsub(/#{Regexp.escape File.dirname(context.logical_path)}$/, '')
dirname = File.dirname(file).gsub(/#{Regexp.escape File.dirname(context.logical_path)}#{REGEXP_END}/, '')
dirname = Pathname(dirname)

required_trees.each do |original_required_tree|
@@ -117,14 +117,14 @@ def process_required_trees(required_trees, context)
end

def self.load_asset_code(sprockets, name)
asset = sprockets[name.sub(/(\.(js|rb|opal))*$/, '.js')]
asset = sprockets[name.sub(/(\.(js|rb|opal))*#{REGEXP_END}/, '.js')]
return '' if asset.nil?

opal_extnames = sprockets.engines.map do |ext, engine|
ext if engine <= ::Opal::Processor
end.compact

module_name = -> asset { asset.logical_path.sub(/\.js$/, '') }
module_name = -> asset { asset.logical_path.sub(/\.js#{REGEXP_END}/, '') }
path_extnames = -> path { File.basename(path).scan(/\.[^.]+/) }
mark_as_loaded = -> path { "Opal.mark_as_loaded(#{path.inspect});" }
processed_by_opal = -> asset { (path_extnames[asset.pathname] & opal_extnames).any? }
8 changes: 4 additions & 4 deletions lib/opal/sprockets/source_map_server.rb
Original file line number Diff line number Diff line change
@@ -45,13 +45,13 @@ def cache_set(key, value)
end

def self.get_map_cache(sprockets, logical_path)
logical_path = logical_path.gsub(/\.js$/, '')
logical_path = logical_path.gsub(/\.js#{REGEXP_END}/, '')
cache_key = cache_key_for_path(logical_path+'.map')
cache(sprockets).cache_get(cache_key)
end

def self.set_map_cache(sprockets, logical_path, map_contents)
logical_path = logical_path.gsub(/\.js$/, '')
logical_path = logical_path.gsub(/\.js#{REGEXP_END}/, '')
cache_key = cache_key_for_path(logical_path+'.map')
cache(sprockets).cache_set(cache_key, map_contents)
end
@@ -61,7 +61,7 @@ def self.cache(sprockets)
end

def self.cache_key_for_path(logical_path)
base_name = logical_path.gsub(/\.js$/, '')
base_name = logical_path.gsub(/\.js#{REGEXP_END}/, '')
File.join('opal', 'source_maps', base_name)
end

@@ -98,7 +98,7 @@ def call(env)
return [200, {"Content-Type" => "text/json"}, [source.to_s]]
when %r{^(.*)\.rb$}
begin
asset = sprockets.resolve(path_info.sub(/\.rb$/,''))
asset = sprockets.resolve(path_info.sub(/\.rb#{REGEXP_END}/,''))
rescue Sprockets::FileNotFound
return not_found(path_info)
end