Skip to content

Fix for super optional args, downcase / upcase string methods #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ruby2js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def s(type, *args)
Parser::AST::Node.new type, args
end

# update existing node
def S(type, *args)
@ast.updated(type, args)
end
Expand Down
5 changes: 4 additions & 1 deletion lib/ruby2js/converter/super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class Converter
end

put '('
parse s(:args, *args)
cleaned_args = args.map do |arg| # FIX: #212
arg.type == :optarg ? s(:arg, arg.children[0]) : arg
end
parse s(:args, *cleaned_args)
put ')'
else
parse @class_parent
Expand Down
30 changes: 15 additions & 15 deletions lib/ruby2js/filter/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,25 @@ def on_send(node)

elsif [:start_with?, :end_with?].include? method and args.length == 1
if es2015
if method == :start_with?
if method == :start_with?
process S(:send, target, :startsWith, *args)
else
process S(:send, target, :endsWith, *args)
end
else
if args.first.type == :str
length = S(:int, args.first.children.first.length)
else
length = S(:attr, *args, :length)
end
if args.first.type == :str
length = S(:int, args.first.children.first.length)
else
length = S(:attr, *args, :length)
end

if method == :start_with?
process S(:send, S(:send, target, :substring, s(:int, 0),
length), :==, *args)
else
process S(:send, S(:send, target, :slice,
S(:send, length, :-@)), :==, *args)
end
if method == :start_with?
process S(:send, S(:send, target, :substring, s(:int, 0),
length), :==, *args)
else
process S(:send, S(:send, target, :slice,
S(:send, length, :-@)), :==, *args)
end
end

elsif method == :clear and args.length == 0 and node.is_method?
Expand Down Expand Up @@ -321,10 +321,10 @@ def on_send(node)
process S(:send, target, :forEach, *args)

elsif method == :downcase and args.length == 0
process S(:send, target, :toLowerCase)
process s(:send, target, :toLowerCase)

elsif method == :upcase and args.length == 0
process S(:send, target, :toUpperCase)
process s(:send, target, :toUpperCase)

elsif method == :strip and args.length == 0
process s(:send, target, :trim)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby2js/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Ruby2JS
module VERSION #:nodoc:
MAJOR = 5
MINOR = 1
TINY = 1
TINY = 2

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down
11 changes: 7 additions & 4 deletions spec/es2015_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,17 @@ def to_js_fn(string)
end

it "should handle super" do
to_js('class A; end; class B < A; def initialize(x); super; end; end').
class_definition = 'class A; end; class B < A;'
to_js(class_definition + ' def initialize(x); super; end; end').
must_equal 'class A {}; class B extends A {constructor(x) {super(x)}}'
to_js('class A; end; class B < A; def initialize(x); super(3); end; end').
to_js(class_definition + ' def initialize(x); super(3); end; end').
must_equal 'class A {}; class B extends A {constructor(x) {super(3)}}'
to_js('class A; end; class B < A; def foo(x); super; end; end').
to_js(class_definition + ' def foo(x); super; end; end').
must_equal 'class A {}; class B extends A {foo(x) {super.foo(x)}}'
to_js('class A; end; class B < A; def foo(x); super(3); end; end').
to_js(class_definition + ' def foo(x); super(3); end; end').
must_equal 'class A {}; class B extends A {foo(x) {super.foo(3)}}'
to_js(class_definition + ' def _render(force = false, options = {}); super; end; end').
must_equal 'class A {}; class B extends A {_render(force=false, options={}) {super._render(force, options)}}'
end

it "should handle class super" do
Expand Down
4 changes: 4 additions & 0 deletions spec/functions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ def to_js_2020(string)
to_js( 'x.upcase()' ).must_equal 'x.toUpperCase()'
end

it "should handle chained methods" do
to_js( 'x.strip.downcase ').must_equal 'x.trim().toLowerCase()'
end

it 'should handle start_with?' do
to_js( 'x.start_with?(y)' ).must_equal 'x.substring(0, y.length) == y'
to_js( 'x.start_with?("z")' ).must_equal 'x.substring(0, 1) == "z"'
Expand Down
Loading