diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2024-03-05 21:26:16 -0500 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-03-06 16:42:50 +0000 |
| commit | 31ef2f43a77c515371fbfb921a6935bd67ac74a2 (patch) | |
| tree | 82ef1ec5d1ba0920e0a96d03dfebf57fb2663db4 | |
| parent | de610745140f51923d31f7c708ee582cc989c9e0 (diff) | |
[ruby/prism] Consolidate semicolon checking in ripper translation
https://github.com/ruby/prism/commit/798517da1a
| -rw-r--r-- | lib/prism/translation/ripper.rb | 18 | ||||
| -rw-r--r-- | test/prism/ripper_test.rb | 5 |
2 files changed, 12 insertions, 11 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index a64160c51f..12fa2a5cc8 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -392,7 +392,7 @@ module Prism on_stmts_add(on_stmts_new, on_void_stmt) else body = node.statements.body - body.unshift(nil) if source.byteslice(node.begin_keyword_loc.end_offset...node.statements.body[0].location.start_offset).include?(";") + body.unshift(nil) if semicolon?(node.begin_keyword_loc, node.statements.body[0].location) bounds(node.statements.location) visit_statements_node_body(body) @@ -406,7 +406,7 @@ module Prism [nil] else body = else_clause_node.statements.body - body.unshift(nil) if source.byteslice(else_clause_node.else_keyword_loc.end_offset...else_clause_node.statements.body[0].location.start_offset).include?(";") + body.unshift(nil) if semicolon?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location) body end @@ -466,7 +466,7 @@ module Prism braces ? stmts : on_bodystmt(stmts, nil, nil, nil) when StatementsNode stmts = node.body.body - stmts.unshift(nil) if source.byteslice((node.parameters&.location || node.opening_loc).end_offset...node.body.location.start_offset).include?(";") + stmts.unshift(nil) if semicolon?(node.parameters&.location || node.opening_loc, node.body.location) stmts = visit_statements_node_body(stmts) bounds(node.body.location) @@ -1148,7 +1148,8 @@ module Prism [nil] else body = node.statements.body - body.unshift(nil) if source.byteslice(node.else_keyword_loc.end_offset...node.statements.body[0].location.start_offset).include?(";") + body.unshift(nil) if semicolon?(node.else_keyword_loc, node.statements.body[0].location) + body end bounds(node.location) @@ -1180,7 +1181,7 @@ module Prism [nil] else body = node.statements.body - body.unshift(nil) if source.byteslice(node.ensure_keyword_loc.end_offset...body[0].location.start_offset).include?(";") + body.unshift(nil) if semicolon?(node.ensure_keyword_loc, body[0].location) body end @@ -1740,7 +1741,7 @@ module Prism braces ? stmts : on_bodystmt(stmts, nil, nil, nil) when StatementsNode stmts = node.body.body - stmts.unshift(nil) if source.byteslice((node.parameters&.location || node.opening_loc).end_offset...node.body.location.start_offset).include?(";") + stmts.unshift(nil) if semicolon?(node.parameters&.location || node.opening_loc, node.body.location) stmts = visit_statements_node_body(stmts) bounds(node.body.location) @@ -2521,6 +2522,11 @@ module Prism # Helpers ########################################################################## + # Returns true if there is a semicolon between the two locations. + def semicolon?(left, right) + source.byteslice(left.end_offset...right.start_offset).include?(";") + end + # Visit the string content of a particular node. This method is used to # split into the various token types. def visit_token(token) diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index a7589faeed..ddae1d582a 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -60,7 +60,6 @@ module Prism seattlerb/call_args_assoc_trailing_comma.txt seattlerb/call_args_command.txt seattlerb/call_array_lambda_block_call.txt - seattlerb/call_assoc_new_if_multiline.txt seattlerb/call_assoc_trailing_comma.txt seattlerb/call_block_arg_named.txt seattlerb/call_colon_parens.txt @@ -132,8 +131,6 @@ module Prism seattlerb/mlhs_mid_anonsplat.txt seattlerb/mlhs_mid_splat.txt seattlerb/module_comments.txt - seattlerb/parse_if_not_canonical.txt - seattlerb/parse_if_not_noncanonical.txt seattlerb/parse_line_dstr_escaped_newline.txt seattlerb/parse_line_dstr_soft_newline.txt seattlerb/parse_line_evstr_after_break.txt @@ -243,7 +240,6 @@ module Prism whitequark/forwarded_kwrestarg_with_additional_kwarg.txt whitequark/forwarded_restarg.txt whitequark/hash_label_end.txt - whitequark/if_else.txt whitequark/if_elsif.txt whitequark/kwbegin_compstmt.txt whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt @@ -280,7 +276,6 @@ module Prism whitequark/ternary.txt whitequark/ternary_ambiguous_symbol.txt whitequark/trailing_forward_arg.txt - whitequark/unless_else.txt whitequark/yield.txt xstring.txt yield.txt |
