Skip to content

Commit

Permalink
Implement hashes for ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 5, 2024
1 parent 9a883a4 commit 57f991a
Showing 1 changed file with 53 additions and 13 deletions.
66 changes: 53 additions & 13 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ def visit_alias_global_variable_node(node)
# foo => bar | baz
# ^^^^^^^^^
def visit_alternation_pattern_node(node)
raise NoMethodError, __method__
left = visit(node.left)
right = visit(node.right)

bounds(node.location)
on_binary(left, :|, right)
end

# a and b
Expand Down Expand Up @@ -265,7 +269,18 @@ def visit_arguments_node(node)
# { a: 1 }
# ^^^^
def visit_assoc_node(node)
raise NoMethodError, __method__
key =
if node.key.is_a?(SymbolNode) && node.operator_loc.nil?
bounds(node.key.location)
on_label(node.key.slice)
else
visit(node.key)
end

value = visit(node.value)

bounds(node.location)
on_assoc_new(key, value)
end

# def foo(**); bar(**); end
Expand All @@ -274,7 +289,10 @@ def visit_assoc_node(node)
# { **foo }
# ^^^^^
def visit_assoc_splat_node(node)
raise NoMethodError, __method__
value = visit(node.value)

bounds(node.location)
on_assoc_splat(value)
end

# $+
Expand Down Expand Up @@ -883,7 +901,18 @@ def visit_global_variable_target_node(node)
# {}
# ^^
def visit_hash_node(node)
raise NoMethodError, __method__
elements = visit_hash_node_elements(node.elements) unless node.elements.empty?

bounds(node.location)
on_hash(elements)
end

# Visit the elements of a hash node.
private def visit_hash_node_elements(elements)
args = visit_all(elements)

bounds(elements.first.location)
on_assoclist_from_args(args)
end

# foo => {}
Expand Down Expand Up @@ -913,7 +942,6 @@ def visit_imaginary_node(node)
# { foo: }
# ^^^^
def visit_implicit_node(node)
raise NoMethodError, __method__
end

# foo { |bar,| }
Expand Down Expand Up @@ -1078,6 +1106,11 @@ def visit_interpolated_x_string_node(node)
end
end

# -> { it }
# ^^^^^^^^^
def visit_it_parameters_node(node)
end

# foo(bar: baz)
# ^^^^^^^^
def visit_keyword_hash_node(node)
Expand Down Expand Up @@ -1111,14 +1144,19 @@ def visit_lambda_node(node)
# ^^^
def visit_local_variable_read_node(node)
bounds(node.location)
on_var_ref(on_ident(node.name.to_s))

if node.name == :"0it"
on_vcall(on_ident(node.slice))
else
on_var_ref(on_ident(node.slice))
end
end

# foo = 1
# ^^^^^^^
def visit_local_variable_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_ident(node.name.to_s))
target = on_var_field(on_ident(node.name_loc.slice))
value = visit(node.value)

bounds(node.location)
Expand All @@ -1129,7 +1167,7 @@ def visit_local_variable_write_node(node)
# ^^^^^^^^^^
def visit_local_variable_operator_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_ident(node.name.to_s))
target = on_var_field(on_ident(node.name_loc.slice))

bounds(node.operator_loc)
operator = on_op("#{node.operator}=")
Expand All @@ -1143,7 +1181,7 @@ def visit_local_variable_operator_write_node(node)
# ^^^^^^^^^^^
def visit_local_variable_and_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_ident(node.name.to_s))
target = on_var_field(on_ident(node.name_loc.slice))

bounds(node.operator_loc)
operator = on_op("&&=")
Expand All @@ -1157,7 +1195,7 @@ def visit_local_variable_and_write_node(node)
# ^^^^^^^^^^^
def visit_local_variable_or_write_node(node)
bounds(node.name_loc)
target = on_var_field(on_ident(node.name.to_s))
target = on_var_field(on_ident(node.name_loc.slice))

bounds(node.operator_loc)
operator = on_op("||=")
Expand Down Expand Up @@ -1252,7 +1290,6 @@ def visit_no_keywords_parameter_node(node)
# -> { _1 + _2 }
# ^^^^^^^^^^^^^^
def visit_numbered_parameters_node(node)
raise NoMethodError, __method__
end

# $1
Expand Down Expand Up @@ -1327,13 +1364,16 @@ def visit_parentheses_node(node)
# foo => ^(bar)
# ^^^^^^
def visit_pinned_expression_node(node)
raise NoMethodError, __method__
expression = visit(node.expression)

bounds(node.location)
on_begin(expression)
end

# foo = 1 and bar => ^foo
# ^^^^
def visit_pinned_variable_node(node)
raise NoMethodError, __method__
visit(node.variable)
end

# END {}
Expand Down

0 comments on commit 57f991a

Please sign in to comment.