From 562d949e022cb3e7288256af8d0df3f4e17b66cb Mon Sep 17 00:00:00 2001 From: Haldun Bayhantopcu Date: Fri, 1 Dec 2023 19:59:50 +0100 Subject: [ruby/prism] Fix parsing heredoc ends https://github.com/ruby/prism/commit/aa8c702271 --- .../prism/fixtures/heredocs_leading_whitespace.txt | 19 +++++++++ test/prism/locals_test.rb | 7 ++++ test/prism/parse_test.rb | 5 +++ .../snapshots/heredocs_leading_whitespace.txt | 49 ++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 test/prism/fixtures/heredocs_leading_whitespace.txt create mode 100644 test/prism/snapshots/heredocs_leading_whitespace.txt (limited to 'test') diff --git a/test/prism/fixtures/heredocs_leading_whitespace.txt b/test/prism/fixtures/heredocs_leading_whitespace.txt new file mode 100644 index 0000000000..e786f08774 --- /dev/null +++ b/test/prism/fixtures/heredocs_leading_whitespace.txt @@ -0,0 +1,19 @@ +<<-' FOO' +a +b + FOO + +<<-' FOO' +a +b + FOO + +<<~' FOO' +a +b + FOO + +<<~' FOO' +a +b + FOO diff --git a/test/prism/locals_test.rb b/test/prism/locals_test.rb index 06324f9d94..df391ca048 100644 --- a/test/prism/locals_test.rb +++ b/test/prism/locals_test.rb @@ -68,6 +68,13 @@ module Prism # HERE todos << "seattlerb/heredoc_nested.txt" + # Ruby < 3.3.0 fails to parse: + # + # <<-' HERE' + # foo + # HERE + invalid << "heredocs_leading_whitespace.txt" if RUBY_VERSION < "3.3.0" + base = File.join(__dir__, "fixtures") skips = invalid | todos diff --git a/test/prism/parse_test.rb b/test/prism/parse_test.rb index 2feb15b48b..e2de55463c 100644 --- a/test/prism/parse_test.rb +++ b/test/prism/parse_test.rb @@ -111,6 +111,11 @@ module Prism # Additionally, Ripper cannot parse the %w[] fixture in this file, so set ripper_should_parse to false. ripper_should_parse = false if relative == "spanning_heredoc.txt" + # Ruby < 3.3.0 cannot parse heredocs where there are leading whitespace charactes in the heredoc start. + # Example: <<~' EOF' or <<-' EOF' + # https://bugs.ruby-lang.org/issues/19539 + ripper_should_parse = false if relative == "heredocs_leading_whitespace.txt" && RUBY_VERSION < "3.3.0" + define_method "test_filepath_#{relative}" do # First, read the source from the filepath. Use binmode to avoid converting CRLF on Windows, # and explicitly set the external encoding to UTF-8 to override the binmode default. diff --git a/test/prism/snapshots/heredocs_leading_whitespace.txt b/test/prism/snapshots/heredocs_leading_whitespace.txt new file mode 100644 index 0000000000..06116821ca --- /dev/null +++ b/test/prism/snapshots/heredocs_leading_whitespace.txt @@ -0,0 +1,49 @@ +@ ProgramNode (location: (1,0)-(16,10)) +├── locals: [] +└── statements: + @ StatementsNode (location: (1,0)-(16,10)) + └── body: (length: 4) + ├── @ StringNode (location: (1,0)-(1,10)) + │ ├── flags: ∅ + │ ├── opening_loc: (1,0)-(1,10) = "<<-' FOO'" + │ ├── content_loc: (2,0)-(4,0) = "a\nb\n" + │ ├── closing_loc: (4,0)-(5,0) = " FOO\n" + │ └── unescaped: "a\nb\n" + ├── @ StringNode (location: (6,0)-(6,10)) + │ ├── flags: ∅ + │ ├── opening_loc: (6,0)-(6,10) = "<<-' FOO'" + │ ├── content_loc: (7,0)-(9,0) = "a\nb\n" + │ ├── closing_loc: (9,0)-(10,0) = " FOO\n" + │ └── unescaped: "a\nb\n" + ├── @ InterpolatedStringNode (location: (11,0)-(11,10)) + │ ├── opening_loc: (11,0)-(11,10) = "<<~' FOO'" + │ ├── parts: (length: 2) + │ │ ├── @ StringNode (location: (12,0)-(13,0)) + │ │ │ ├── flags: ∅ + │ │ │ ├── opening_loc: ∅ + │ │ │ ├── content_loc: (12,0)-(13,0) = "a\n" + │ │ │ ├── closing_loc: ∅ + │ │ │ └── unescaped: "a\n" + │ │ └── @ StringNode (location: (13,0)-(14,0)) + │ │ ├── flags: ∅ + │ │ ├── opening_loc: ∅ + │ │ ├── content_loc: (13,0)-(14,0) = "b\n" + │ │ ├── closing_loc: ∅ + │ │ └── unescaped: "b\n" + │ └── closing_loc: (14,0)-(15,0) = " FOO\n" + └── @ InterpolatedStringNode (location: (16,0)-(16,10)) + ├── opening_loc: (16,0)-(16,10) = "<<~' FOO'" + ├── parts: (length: 2) + │ ├── @ StringNode (location: (17,0)-(18,0)) + │ │ ├── flags: ∅ + │ │ ├── opening_loc: ∅ + │ │ ├── content_loc: (17,0)-(18,0) = "a\n" + │ │ ├── closing_loc: ∅ + │ │ └── unescaped: "a\n" + │ └── @ StringNode (location: (18,0)-(19,0)) + │ ├── flags: ∅ + │ ├── opening_loc: ∅ + │ ├── content_loc: (18,0)-(19,0) = "b\n" + │ ├── closing_loc: ∅ + │ └── unescaped: "b\n" + └── closing_loc: (19,0)-(20,0) = " FOO\n" -- cgit v1.2.3