File tree Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ def self.scan_opens(tokens)
12
12
skip = false
13
13
last_tok , state , args = opens . last
14
14
case state
15
+ when :in_alias_undef
16
+ skip = t . event == :on_kw
15
17
when :in_unquoted_symbol
16
18
unless IGNORE_TOKENS . include? ( t . event )
17
19
opens . pop
@@ -130,6 +132,10 @@ def self.scan_opens(tokens)
130
132
opens . pop
131
133
opens << [ t , nil ]
132
134
end
135
+ when 'alias'
136
+ opens << [ t , :in_alias_undef , 2 ]
137
+ when 'undef'
138
+ opens << [ t , :in_alias_undef , 1 ]
133
139
when 'elsif' , 'else' , 'when'
134
140
opens . pop
135
141
opens << [ t , nil ]
@@ -174,6 +180,10 @@ def self.scan_opens(tokens)
174
180
pending_heredocs . reverse_each { |t | opens << [ t , nil ] }
175
181
pending_heredocs = [ ]
176
182
end
183
+ if opens . last && opens . last [ 1 ] == :in_alias_undef && !IGNORE_TOKENS . include? ( t . event ) && t . event != :on_heredoc_end
184
+ tok , state , arg = opens . pop
185
+ opens << [ tok , state , arg - 1 ] if arg >= 1
186
+ end
177
187
yield t , opens if block_given?
178
188
end
179
189
opens . map ( &:first ) + pending_heredocs . reverse
Original file line number Diff line number Diff line change @@ -290,7 +290,7 @@ def calc_indent_level(opens)
290
290
when :on_embdoc_beg
291
291
indent_level = 0
292
292
else
293
- indent_level += 1
293
+ indent_level += 1 unless t . tok == 'alias' || t . tok == 'undef'
294
294
end
295
295
end
296
296
indent_level
Original file line number Diff line number Diff line change @@ -280,6 +280,44 @@ def test_while_until
280
280
end
281
281
end
282
282
283
+ def test_undef_alias
284
+ codes = [
285
+ 'undef foo' ,
286
+ 'alias foo bar' ,
287
+ 'undef !' ,
288
+ 'alias + -' ,
289
+ 'alias $a $b' ,
290
+ 'undef do' ,
291
+ 'alias do do' ,
292
+ 'undef :do' ,
293
+ 'alias :do :do' ,
294
+ 'undef :"#{alias do do}"' ,
295
+ 'alias :"#{undef do}" do' ,
296
+ 'alias do :"#{undef do}"'
297
+ ]
298
+ code_with_comment = <<~EOS
299
+ undef #
300
+ #
301
+ do #
302
+ alias #
303
+ #
304
+ do #
305
+ #
306
+ do #
307
+ EOS
308
+ code_with_heredoc = <<~EOS
309
+ <<~A; alias
310
+ A
311
+ :"#{ <<~A } "
312
+ A
313
+ do
314
+ EOS
315
+ [*codes, code_with_comment, code_with_heredoc].each do |code|
316
+ opens = IRB::NestingParser.open_tokens(IRB::RubyLex.ripper_lex_without_warning('(' + code + "\n if"))
317
+ assert_equal(%w[( if], opens.map(&:tok))
318
+ end
319
+ end
320
+
283
321
def test_case_in
284
322
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7.0')
285
323
pend 'This test requires ruby version that supports case-in syntax'
You can’t perform that action at this time.
0 commit comments