forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
multiline_in_pattern_then.rb
60 lines (52 loc) · 1.45 KB
/
multiline_in_pattern_then.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Checks uses of the `then` keyword in multi-line `in` statement.
#
# @example
# # bad
# case expression
# in pattern then
# end
#
# # good
# case expression
# in pattern
# end
#
# # good
# case expression
# in pattern then do_something
# end
#
# # good
# case expression
# in pattern then do_something(arg1,
# arg2)
# end
#
class MultilineInPatternThen < Base
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion
minimum_target_ruby_version 2.7
MSG = 'Do not use `then` for multiline `in` statement.'
def on_in_pattern(node)
return if !node.then? || require_then?(node)
range = node.loc.begin
add_offense(range) do |corrector|
corrector.remove(range_with_surrounding_space(range, side: :left, newlines: false))
end
end
private
# Requires `then` for write `in` and its body on the same line.
def require_then?(in_pattern_node)
return true unless in_pattern_node.pattern.single_line?
return false unless in_pattern_node.body
same_line?(in_pattern_node, in_pattern_node.body)
end
end
end
end
end