lib/opal/nodes/args.rb in opal-1.8.0 vs lib/opal/nodes/args.rb in opal-1.8.1
- old
+ new
@@ -23,42 +23,14 @@
module Opal
module Nodes
class ArgsNode < Base
handle :args
- # ruby allows for args with the same name, if the arg starts with a '_', like:
- # def funny_method_name(_, _)
- # puts _
- # end
- # but javascript in strict mode does not allow for args with the same name
- # ruby assigns the value of the first arg given
- # funny_method_name(1, 2) => 1
- # 1. check for args starting with '_' and check if they appear multiple times
- # 2. leave the first appearance as it is and rename the other ones
- # compiler result:
- # function $$funny_method_name(_, __$2)
-
def compile
- same_arg_counter = {}
children.each_with_index do |arg, idx|
- if multiple_underscore?(arg)
- same_arg_counter[arg] ||= 0
- same_arg_counter[arg] += 1
- if same_arg_counter[arg] > 1
- arg = s(arg.type, :"#{arg.children[0]}_$#{same_arg_counter[arg]}")
- end
- end
-
push ', ' if idx != 0
push process(arg)
end
- end
-
- def multiple_underscore?(arg)
- arg.type == :arg &&
- arg.children.count == 1 &&
- arg.children.first.to_s.start_with?('_') &&
- children.count(arg) > 1
end
end
end
end