forked from rubocop/rubocop
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiline_hash_brace_layout.rb
118 lines (111 loc) · 2.99 KB
/
multiline_hash_brace_layout.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop checks that the closing brace in a hash literal is either
# on the same line as the last hash element, or a new line.
#
# When using the `symmetrical` (default) style:
#
# If a hash's opening brace is on the same line as the first element
# of the hash, then the closing brace should be on the same line as
# the last element of the hash.
#
# If a hash's opening brace is on the line above the first element
# of the hash, then the closing brace should be on the line below
# the last element of the hash.
#
# When using the `new_line` style:
#
# The closing brace of a multi-line hash literal must be on the line
# after the last element of the hash.
#
# When using the `same_line` style:
#
# The closing brace of a multi-line hash literal must be on the same
# line as the last element of the hash.
#
# @example EnforcedStyle: symmetrical (default)
#
# # bad
# { a: 1,
# b: 2
# }
# # bad
# {
# a: 1,
# b: 2 }
#
# # good
# { a: 1,
# b: 2 }
#
# # good
# {
# a: 1,
# b: 2
# }
#
# @example EnforcedStyle: new_line
# # bad
# {
# a: 1,
# b: 2 }
#
# # bad
# { a: 1,
# b: 2 }
#
# # good
# { a: 1,
# b: 2
# }
#
# # good
# {
# a: 1,
# b: 2
# }
#
# @example EnforcedStyle: same_line
# # bad
# { a: 1,
# b: 2
# }
#
# # bad
# {
# a: 1,
# b: 2
# }
#
# # good
# {
# a: 1,
# b: 2 }
#
# # good
# { a: 1,
# b: 2 }
class MultilineHashBraceLayout < Cop
include MultilineLiteralBraceLayout
SAME_LINE_MESSAGE = 'Closing hash brace must be on the same line as ' \
'the last hash element when opening brace is on the same line as ' \
'the first hash element.'
NEW_LINE_MESSAGE = 'Closing hash brace must be on the line after ' \
'the last hash element when opening brace is on a separate line ' \
'from the first hash element.'
ALWAYS_NEW_LINE_MESSAGE = 'Closing hash brace must be on the line ' \
'after the last hash element.'
ALWAYS_SAME_LINE_MESSAGE = 'Closing hash brace must be on the same ' \
'line as the last hash element.'
def on_hash(node)
check_brace_layout(node)
end
def autocorrect(node)
MultilineLiteralBraceCorrector.new(node, processed_source)
end
end
end
end
end