Skip to content

Commit

Permalink
Implement so many ripper visitor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 5, 2024
1 parent b6927ac commit 336bd72
Showing 1 changed file with 56 additions and 11 deletions.
67 changes: 56 additions & 11 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,23 @@ def visit_block_node(node)
when "do"
on_do_block(params_val, on_bodystmt(body_val, nil, nil, nil))
else
raise raise
raise
end
end

# def foo(&bar); end
# ^^^^
def visit_block_parameter_node(node)
raise NoMethodError, __method__
if node.name_loc.nil?
bounds(node.location)
on_blockarg(nil)
else
bounds(node.name_loc)
name = visit_token(node.name.to_s)

bounds(node.location)
on_blockarg(name)
end
end

# A block's parameters.
Expand Down Expand Up @@ -1035,7 +1044,16 @@ def visit_keyword_hash_node(node)
# def foo(**); end
# ^^
def visit_keyword_rest_parameter_node(node)
raise NoMethodError, __method__
if node.name_loc.nil?
bounds(node.location)
on_kwrest_param(nil)
else
bounds(node.name_loc)
name = on_ident(node.name.to_s)

bounds(node.location)
on_kwrest_param(name)
end
end

# -> {}
Expand Down Expand Up @@ -1118,13 +1136,19 @@ def visit_match_last_line_node(node)
# foo in bar
# ^^^^^^^^^^
def visit_match_predicate_node(node)
raise NoMethodError, __method__
value = visit(node.value)
pattern = on_in(visit(node.pattern), nil, nil)

on_case(value, pattern)
end

# foo => bar
# ^^^^^^^^^^
def visit_match_required_node(node)
raise NoMethodError, __method__
value = visit(node.value)
pattern = on_in(visit(node.pattern), nil, nil)

on_case(value, pattern)
end

# /(?<foo>foo)/ =~ bar
Expand Down Expand Up @@ -1193,13 +1217,21 @@ def visit_number_nodeed_reference_read_node(node)
# def foo(bar: baz); end
# ^^^^^^^^
def visit_optional_keyword_parameter_node(node)
raise NoMethodError, __method__
bounds(node.name_loc)
name = on_label("#{node.name}:")
value = visit(node.value)

[name, value]
end

# def foo(bar = 1); end
# ^^^^^^^
def visit_optional_parameter_node(node)
raise NoMethodError, __method__
bounds(node.name_loc)
name = visit_token(node.name.to_s)
value = visit(node.value)

[name, value]
end

# a or b
Expand All @@ -1215,9 +1247,15 @@ def visit_or_node(node)
# def foo(bar, *baz); end
# ^^^^^^^^^
def visit_parameters_node(node)
requireds = visit_all(node.requireds)
requireds = visit_all(node.requireds) if node.requireds.any?
optionals = visit_all(node.optionals) if node.optionals.any?
rest = visit(node.rest)
posts = visit_all(node.posts) if node.posts.any?
keywords = visit_all(node.keywords) if node.keywords.any?
keyword_rest = visit(node.keyword_rest)
block = visit(node.block)

on_params(requireds, nil, nil, nil, nil, nil, nil)
on_params(requireds, optionals, rest, posts, keywords, keyword_rest, block)
end

# ()
Expand Down Expand Up @@ -1323,7 +1361,8 @@ def visit_regular_expression_node(node)
# def foo(bar:); end
# ^^^^
def visit_required_keyword_parameter_node(node)
raise NoMethodError, __method__
bounds(node.name_loc)
[on_label("#{node.name}:"), false]
end

# def foo(bar); end
Expand Down Expand Up @@ -1386,7 +1425,13 @@ def visit_rescue_node(node)
# def foo(*); end
# ^
def visit_rest_parameter_node(node)
raise NoMethodError, __method__
if node.name_loc.nil?
bounds(node.location)
on_rest_param(nil)
else
bounds(node.name_loc)
on_rest_param(visit_token(node.name.to_s))
end
end

# retry
Expand Down

0 comments on commit 336bd72

Please sign in to comment.