From ddf7d718fc6db559aebab59f1b7b572e220c8436 Mon Sep 17 00:00:00 2001 From: Shugo Maeda Date: Thu, 26 Mar 2026 13:23:13 +0900 Subject: [PATCH 1/2] Do not open nesting for character literals To fix the indentation of `end` in the following code: ```ruby if c == ?a puts "a" end ``` --- lib/irb/nesting_parser.rb | 3 ++- test/irb/test_nesting_parser.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/irb/nesting_parser.rb b/lib/irb/nesting_parser.rb index 05ef152c9..32357a20d 100644 --- a/lib/irb/nesting_parser.rb +++ b/lib/irb/nesting_parser.rb @@ -222,8 +222,9 @@ def heredoc_string_like(node, type) # Heredoc closing contains trailing newline. We need to exclude it close_location_start(node.closing_loc) if node.closing_loc && !node.closing.empty? elsif node.opening + return if node.closing.nil? # Character literal has no closing open_location(node.location, type, node.opening) - if node.closing && node.closing != '' + if node.closing != '' # Closing of `"#{\n` is "\n". We need to treat it as not-closed. close_location_start(node.closing_loc) if node.opening.match?(/\n\z/) || node.closing != "\n" end diff --git a/test/irb/test_nesting_parser.rb b/test/irb/test_nesting_parser.rb index cf4082609..91ea8cd90 100644 --- a/test/irb/test_nesting_parser.rb +++ b/test/irb/test_nesting_parser.rb @@ -347,5 +347,15 @@ def test_case_in assert_equal(['in'], next_opens.map(&:tok)) end end + + def test_char_literal + code = <<~EOS + ?a + EOS + prev_opens, next_opens, min_depth = parse_by_line(code).last + assert_equal([], prev_opens) + assert_equal([], next_opens) + assert_equal(0, min_depth) + end end end From 0c953c1e0def6779136a1f29ecadafd5694efddb Mon Sep 17 00:00:00 2001 From: Shugo Maeda Date: Thu, 26 Mar 2026 17:09:10 +0900 Subject: [PATCH 2/2] Check node.opening for prism-latest To fix the test failures at https://github.com/ruby/irb/actions/runs/23577491147/job/68670877251?pr=1190 --- lib/irb/nesting_parser.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/irb/nesting_parser.rb b/lib/irb/nesting_parser.rb index 32357a20d..dfa235582 100644 --- a/lib/irb/nesting_parser.rb +++ b/lib/irb/nesting_parser.rb @@ -222,9 +222,9 @@ def heredoc_string_like(node, type) # Heredoc closing contains trailing newline. We need to exclude it close_location_start(node.closing_loc) if node.closing_loc && !node.closing.empty? elsif node.opening - return if node.closing.nil? # Character literal has no closing + return if node.opening == '?' && node.closing.nil? # Character literal has no closing open_location(node.location, type, node.opening) - if node.closing != '' + if node.closing && node.closing != '' # Closing of `"#{\n` is "\n". We need to treat it as not-closed. close_location_start(node.closing_loc) if node.opening.match?(/\n\z/) || node.closing != "\n" end