Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix bug where wrong regexps flags were generated (fixes #426)
  • Loading branch information
adambeynon committed Dec 6, 2013
1 parent 1afbc44 commit f983a92
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -48,6 +48,9 @@

* Fix compliance of `Kernel#extend` and ensure it calls `#extended()` hook.

* Fix bug where sometimes the wrong regexp flags would be generated in the
output javascript.

## 0.5.5 2013-11-25

* Fix regression: add `%i[foo bar]` style words back to lexer
Expand Down
8 changes: 6 additions & 2 deletions lib/opal/nodes/literal.rb
Expand Up @@ -44,10 +44,14 @@ def compile
class RegexpNode < Base
handle :regexp

children :value
children :value, :flags

def compile
push((value == // ? /^/ : value).inspect)
if value == ''
push('/^/')
else
push "#{Regexp.new(value).inspect}#{flags}"
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/opal/parser.rb
Expand Up @@ -609,10 +609,10 @@ def new_str(str)
end

def new_regexp(reg, ending)
return s(:regexp, //) unless reg
return s(:regexp, '') unless reg
case reg.type
when :str
s(:regexp, Regexp.new(reg[1], value(ending)))
s(:regexp, reg[1], value(ending))
when :evstr
s(:dregx, "", reg)
when :dstr
Expand Down
1 change: 0 additions & 1 deletion lib/opal/parser/lexer.rb
Expand Up @@ -803,7 +803,6 @@ def yylex
[:tXSTRING_BEG, STR_XQUOTE]
when 'r'
[:tREGEXP_BEG, STR_REGEXP]

end

self.strterm = new_strterm2(func, term, paren)
Expand Down
2 changes: 1 addition & 1 deletion spec/cli/parser/call_spec.rb
Expand Up @@ -71,7 +71,7 @@

it "should correctly parse / and regexps" do
parsed("x / 500").should == [:call, [:call, nil, :x, [:arglist]], :/, [:arglist, [:int, 500]]]
parsed("x /foo/").should == [:call, nil, :x, [:arglist, [:regexp, /foo/]]]
parsed("x /foo/").should == [:call, nil, :x, [:arglist, [:regexp, 'foo', nil]]]
end

it "should parse LPAREN_ARG correctly" do
Expand Down
10 changes: 5 additions & 5 deletions spec/cli/parser/literal_spec.rb
Expand Up @@ -97,17 +97,17 @@
end

describe "parsing regexps" do
it "parses a regexp as a s(:lit)" do
parsed("/lol/").should == [:regexp, /lol/]
it "parses a regexp" do
parsed("/lol/").should == [:regexp, 'lol', nil]
end

it "parses regexp options" do
parsed("/lol/i").should == [:regexp, /lol/i]
parsed("/lol/i").should == [:regexp, 'lol', 'i']
end

it "can parse regexps using %r notation" do
parsed('%r(foo)').should == [:regexp, /foo/]
parsed('%r(foo)i').should == [:regexp, /foo/i]
parsed('%r(foo)').should == [:regexp, 'foo', nil]
parsed('%r(foo)i').should == [:regexp, 'foo', 'i']
end
end
end

0 comments on commit f983a92

Please sign in to comment.