Class: Opal::Nodes::ArgsNode
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#compile ⇒ Object
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.
- #multiple_underscore?(arg) ⇒ Boolean
Methods inherited from Base
#add_gvar, #add_ivar, #add_local, #add_temp, #children, children, #class_variable_owner, #class_variable_owner_nesting_level, #comments, #compile_to_fragments, #error, #expr, #expr?, #expr_or_nil, #fragment, handle, handlers, #has_rescue_else?, #helper, #in_ensure, #in_ensure?, #in_resbody, #in_resbody?, #in_rescue, #in_while?, #initialize, #process, #push, #recv, #recv?, #s, #scope, #source_location, #stmt, #stmt?, #top_scope, truthy_optimize?, #unshift, #while_loop, #with_temp, #wrap
Methods included from Helpers
#current_indent, #empty_line, #indent, #js_truthy, #js_truthy_optimize, #line, #mid_to_jsid, #property, #valid_name?
Constructor Details
This class inherits a constructor from Opal::Nodes::Base
Instance Method Details
#compile ⇒ Object
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
- check for args starting with '_' and check if they appear multiple times
- leave the first appearance as it is and rename the other ones compiler result: function $$funny_method_name(, _$2)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'opal/lib/opal/nodes/args.rb', line 40 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 |
#multiple_underscore?(arg) ⇒ Boolean
56 57 58 59 60 61 |
# File 'opal/lib/opal/nodes/args.rb', line 56 def multiple_underscore?(arg) arg.type == :arg && arg.children.count == 1 && arg.children.first.to_s.start_with?('_') && children.count(arg) > 1 end |