diff --git a/lib/prism/translation/ruby_parser.rb b/lib/prism/translation/ruby_parser.rb index 3808cd3130..ac538a2e97 100644 --- a/lib/prism/translation/ruby_parser.rb +++ b/lib/prism/translation/ruby_parser.rb @@ -1151,8 +1151,8 @@ def visit_keyword_rest_parameter_node(node) def visit_lambda_node(node) parameters = case node.parameters - when nil, NumberedParametersNode - s(node, :args) + when nil, ItParametersNode, NumberedParametersNode + 0 else visit(node.parameters) end diff --git a/snapshots/it.txt b/snapshots/it.txt index d3d676f768..70a2916eb2 100644 --- a/snapshots/it.txt +++ b/snapshots/it.txt @@ -1,31 +1,46 @@ -@ ProgramNode (location: (1,0)-(3,3)) +@ ProgramNode (location: (1,0)-(5,9)) ├── flags: ∅ ├── locals: [] └── statements: - @ StatementsNode (location: (1,0)-(3,3)) + @ StatementsNode (location: (1,0)-(5,9)) ├── flags: ∅ - └── body: (length: 1) - └── @ CallNode (location: (1,0)-(3,3)) - ├── flags: newline, ignore_visibility - ├── receiver: ∅ - ├── call_operator_loc: ∅ - ├── name: :x - ├── message_loc: (1,0)-(1,1) = "x" - ├── opening_loc: ∅ - ├── arguments: ∅ - ├── closing_loc: ∅ - └── block: - @ BlockNode (location: (1,2)-(3,3)) + └── body: (length: 2) + ├── @ CallNode (location: (1,0)-(3,3)) + │ ├── flags: newline, ignore_visibility + │ ├── receiver: ∅ + │ ├── call_operator_loc: ∅ + │ ├── name: :x + │ ├── message_loc: (1,0)-(1,1) = "x" + │ ├── opening_loc: ∅ + │ ├── arguments: ∅ + │ ├── closing_loc: ∅ + │ └── block: + │ @ BlockNode (location: (1,2)-(3,3)) + │ ├── flags: ∅ + │ ├── locals: [] + │ ├── parameters: + │ │ @ ItParametersNode (location: (1,2)-(3,3)) + │ │ └── flags: ∅ + │ ├── body: + │ │ @ StatementsNode (location: (2,2)-(2,4)) + │ │ ├── flags: ∅ + │ │ └── body: (length: 1) + │ │ └── @ ItLocalVariableReadNode (location: (2,2)-(2,4)) + │ │ └── flags: newline + │ ├── opening_loc: (1,2)-(1,4) = "do" + │ └── closing_loc: (3,0)-(3,3) = "end" + └── @ LambdaNode (location: (5,0)-(5,9)) + ├── flags: newline + ├── locals: [] + ├── operator_loc: (5,0)-(5,2) = "->" + ├── opening_loc: (5,3)-(5,4) = "{" + ├── closing_loc: (5,8)-(5,9) = "}" + ├── parameters: + │ @ ItParametersNode (location: (5,0)-(5,9)) + │ └── flags: ∅ + └── body: + @ StatementsNode (location: (5,5)-(5,7)) ├── flags: ∅ - ├── locals: [] - ├── parameters: - │ @ ItParametersNode (location: (1,2)-(3,3)) - │ └── flags: ∅ - ├── body: - │ @ StatementsNode (location: (2,2)-(2,4)) - │ ├── flags: ∅ - │ └── body: (length: 1) - │ └── @ ItLocalVariableReadNode (location: (2,2)-(2,4)) - │ └── flags: newline - ├── opening_loc: (1,2)-(1,4) = "do" - └── closing_loc: (3,0)-(3,3) = "end" + └── body: (length: 1) + └── @ ItLocalVariableReadNode (location: (5,5)-(5,7)) + └── flags: newline diff --git a/test/prism/fixtures/it.txt b/test/prism/fixtures/it.txt index 76deb68028..5410b01e71 100644 --- a/test/prism/fixtures/it.txt +++ b/test/prism/fixtures/it.txt @@ -1,3 +1,5 @@ x do it end + +-> { it } diff --git a/test/prism/ruby/parser_test.rb b/test/prism/ruby/parser_test.rb index 82b5ea54a8..156e8f9e9f 100644 --- a/test/prism/ruby/parser_test.rb +++ b/test/prism/ruby/parser_test.rb @@ -171,9 +171,13 @@ def test_it_block_parameter_syntax actual_ast = Prism::Translation::Parser34.new.tokenize(buffer)[0] it_block_parameter_sexp = parse_sexp { + s(:begin, s(:itblock, s(:send, nil, :x), :it, - s(:lvar, :it)) + s(:lvar, :it)), + s(:itblock, + s(:lambda), :it, + s(:lvar, :it))) } assert_equal(it_block_parameter_sexp, actual_ast.to_sexp) diff --git a/test/prism/ruby/ruby_parser_test.rb b/test/prism/ruby/ruby_parser_test.rb index 03bcfafc42..960e7f63e4 100644 --- a/test/prism/ruby/ruby_parser_test.rb +++ b/test/prism/ruby/ruby_parser_test.rb @@ -13,23 +13,11 @@ return end -# We want to also compare lines and files to make sure we're setting them -# correctly. -Sexp.prepend( - Module.new do - def ==(other) - super && line == other.line && file == other.file # && line_max == other.line_max - end - end -) - module Prism class RubyParserTest < TestCase todos = [ "encoding_euc_jp.txt", - "newline_terminated.txt", "regex_char_width.txt", - "seattlerb/bug169.txt", "seattlerb/masgn_colon3.txt", "seattlerb/messy_op_asgn_lineno.txt", "seattlerb/op_asgn_primary_colon_const_command_call.txt", @@ -37,15 +25,10 @@ class RubyParserTest < TestCase "seattlerb/str_lit_concat_bad_encodings.txt", "strings.txt", "unescaping.txt", - "unparser/corpus/literal/kwbegin.txt", - "unparser/corpus/literal/send.txt", "whitequark/masgn_const.txt", "whitequark/pattern_matching_constants.txt", - "whitequark/pattern_matching_implicit_array_match.txt", "whitequark/pattern_matching_single_match.txt", "whitequark/ruby_bug_12402.txt", - "whitequark/ruby_bug_14690.txt", - "whitequark/space_args_block.txt" ] # https://github.com/seattlerb/ruby_parser/issues/344 @@ -105,10 +88,16 @@ def assert_ruby_parser(fixture, allowed_failure) source = fixture.read expected = ignore_warnings { ::RubyParser.new.parse(source, fixture.path) } actual = Prism::Translation::RubyParser.new.parse(source, fixture.path) + on_failure = -> { message(expected, actual) } if !allowed_failure - assert_equal(expected, actual, -> { message(expected, actual) }) - elsif expected == actual + assert_equal(expected, actual, on_failure) + + unless actual.nil? + assert_equal(expected.line, actual.line, on_failure) + assert_equal(expected.file, actual.file, on_failure) + end + elsif expected == actual && expected.line && actual.line && expected.file == actual.file puts "#{name} now passes" end end