From c05d1099af58703c5ce526c9099ab898c25b91c8 Mon Sep 17 00:00:00 2001 From: Jared White Date: Thu, 9 May 2024 22:44:56 -0700 Subject: [PATCH] Fix for super optional args, downcase / upcase string methods Resolves #212 and #214 --- lib/ruby2js.rb | 1 + lib/ruby2js/converter/super.rb | 5 ++++- lib/ruby2js/filter/functions.rb | 30 +++++++++++++++--------------- lib/ruby2js/version.rb | 2 +- spec/es2015_spec.rb | 11 +++++++---- spec/functions_spec.rb | 4 ++++ 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/ruby2js.rb b/lib/ruby2js.rb index f672bd39..70f8f061 100644 --- a/lib/ruby2js.rb +++ b/lib/ruby2js.rb @@ -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 diff --git a/lib/ruby2js/converter/super.rb b/lib/ruby2js/converter/super.rb index 1472926a..b827963b 100644 --- a/lib/ruby2js/converter/super.rb +++ b/lib/ruby2js/converter/super.rb @@ -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 diff --git a/lib/ruby2js/filter/functions.rb b/lib/ruby2js/filter/functions.rb index 7ee6f043..f1be09f3 100644 --- a/lib/ruby2js/filter/functions.rb +++ b/lib/ruby2js/filter/functions.rb @@ -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? @@ -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) diff --git a/lib/ruby2js/version.rb b/lib/ruby2js/version.rb index 2e4a2a06..c9b3b235 100644 --- a/lib/ruby2js/version.rb +++ b/lib/ruby2js/version.rb @@ -2,7 +2,7 @@ module Ruby2JS module VERSION #:nodoc: MAJOR = 5 MINOR = 1 - TINY = 1 + TINY = 2 STRING = [MAJOR, MINOR, TINY].join('.') end diff --git a/spec/es2015_spec.rb b/spec/es2015_spec.rb index e1635a46..31d9f4a3 100644 --- a/spec/es2015_spec.rb +++ b/spec/es2015_spec.rb @@ -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 diff --git a/spec/functions_spec.rb b/spec/functions_spec.rb index 32483c2b..a7ae1094 100644 --- a/spec/functions_spec.rb +++ b/spec/functions_spec.rb @@ -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"'