Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undef and alias indent #838

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/irb/nesting_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def self.scan_opens(tokens)
skip = false
last_tok, state, args = opens.last
case state
when :in_alias_undef
skip = t.event == :on_kw
when :in_unquoted_symbol
unless IGNORE_TOKENS.include?(t.event)
opens.pop
Expand Down Expand Up @@ -130,6 +132,10 @@ def self.scan_opens(tokens)
opens.pop
opens << [t, nil]
end
when 'alias'
opens << [t, :in_alias_undef, 2]
when 'undef'
opens << [t, :in_alias_undef, 1]
when 'elsif', 'else', 'when'
opens.pop
opens << [t, nil]
Expand Down Expand Up @@ -174,6 +180,10 @@ def self.scan_opens(tokens)
pending_heredocs.reverse_each { |t| opens << [t, nil] }
pending_heredocs = []
end
if opens.last && opens.last[1] == :in_alias_undef && !IGNORE_TOKENS.include?(t.event) && t.event != :on_heredoc_end
tok, state, arg = opens.pop
opens << [tok, state, arg - 1] if arg >= 1
end
yield t, opens if block_given?
end
opens.map(&:first) + pending_heredocs.reverse
Expand Down
2 changes: 1 addition & 1 deletion lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def calc_indent_level(opens)
when :on_embdoc_beg
indent_level = 0
else
indent_level += 1
indent_level += 1 unless t.tok == 'alias' || t.tok == 'undef'
end
end
indent_level
Expand Down
38 changes: 38 additions & 0 deletions test/irb/test_nesting_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,44 @@ def test_while_until
end
end

def test_undef_alias
codes = [
'undef foo',
'alias foo bar',
'undef !',
'alias + -',
'alias $a $b',
'undef do',
'alias do do',
'undef :do',
'alias :do :do',
'undef :"#{alias do do}"',
'alias :"#{undef do}" do',
'alias do :"#{undef do}"'
]
code_with_comment = <<~EOS
undef #
#
do #
alias #
#
do #
#
do #
EOS
code_with_heredoc = <<~EOS
<<~A; alias
A
:"#{<<~A}"
A
do
EOS
[*codes, code_with_comment, code_with_heredoc].each do |code|
opens = IRB::NestingParser.open_tokens(IRB::RubyLex.ripper_lex_without_warning('(' + code + "\nif"))
assert_equal(%w[( if], opens.map(&:tok))
end
end

def test_case_in
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7.0')
pend 'This test requires ruby version that supports case-in syntax'
Expand Down