forked from rubocop/rubocop
/
heredoc_indentation.rb
256 lines (220 loc) · 7.82 KB
/
heredoc_indentation.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop checks the indentation of the here document bodies. The bodies
# are indented one step.
# In Ruby 2.3 or newer, squiggly heredocs (`<<~`) should be used. If you
# use the older rubies, you should introduce some library to your project
# (e.g. ActiveSupport, Powerpack or Unindent).
# Note: When `Layout/LineLength`'s `AllowHeredoc` is false (not default),
# this cop does not add any offenses for long here documents to
# avoid `Layout/LineLength`'s offenses.
#
# @example EnforcedStyle: squiggly (default)
# # bad
# <<-RUBY
# something
# RUBY
#
# # good
# # When EnforcedStyle is squiggly, bad code is auto-corrected to the
# # following code.
# <<~RUBY
# something
# RUBY
#
# @example EnforcedStyle: active_support
# # good
# # When EnforcedStyle is active_support, bad code is auto-corrected to
# # the following code.
# <<-RUBY.strip_heredoc
# something
# RUBY
#
# @example EnforcedStyle: powerpack
# # good
# # When EnforcedStyle is powerpack, bad code is auto-corrected to
# # the following code.
# <<-RUBY.strip_indent
# something
# RUBY
#
# @example EnforcedStyle: unindent
# # good
# # When EnforcedStyle is unindent, bad code is auto-corrected to
# # the following code.
# <<-RUBY.unindent
# something
# RUBY
#
class HeredocIndentation < Cop
include Heredoc
include ConfigurableEnforcedStyle
RUBY23_TYPE_MSG = 'Use %<indentation_width>d spaces for indentation ' \
'in a heredoc by using `<<~` instead of ' \
'`%<current_indent_type>s`.'
RUBY23_WIDTH_MSG = 'Use %<indentation_width>d spaces for '\
'indentation in a heredoc.'
LIBRARY_MSG = 'Use %<indentation_width>d spaces for indentation in a ' \
'heredoc by using %<method>s.'
STRIP_METHODS = {
unindent: 'unindent',
active_support: 'strip_heredoc',
powerpack: 'strip_indent'
}.freeze
def on_heredoc(node)
body = heredoc_body(node)
return if body.strip.empty?
body_indent_level = indent_level(body)
if heredoc_indent_type(node) == '~'
expected_indent_level = base_indent_level(node) + indentation_width
return if expected_indent_level == body_indent_level
else
return unless body_indent_level.zero?
end
return if line_too_long?(node)
add_offense(node, location: :heredoc_body)
end
def autocorrect(node)
check_style!
case style
when :squiggly
correct_by_squiggly(node)
else
correct_by_library(node)
end
end
private
def message(node)
case style
when :squiggly
current_indent_type = "<<#{heredoc_indent_type(node)}"
ruby23_message(indentation_width, current_indent_type)
when nil
method = "some library(e.g. ActiveSupport's `String#strip_heredoc`)"
library_message(indentation_width, method)
else
method = "`String##{STRIP_METHODS[style]}`"
library_message(indentation_width, method)
end
end
def library_message(indentation_width, method)
format(
LIBRARY_MSG,
indentation_width: indentation_width,
method: method
)
end
def ruby23_message(indentation_width, current_indent_type)
if current_indent_type == '<<~'
ruby23_width_message(indentation_width)
else
ruby23_type_message(indentation_width, current_indent_type)
end
end
def ruby23_type_message(indentation_width, current_indent_type)
format(
RUBY23_TYPE_MSG,
indentation_width: indentation_width,
current_indent_type: current_indent_type
)
end
def ruby23_width_message(indentation_width)
format(
RUBY23_WIDTH_MSG,
indentation_width: indentation_width
)
end
def line_too_long?(node)
return false if unlimited_heredoc_length?
body = heredoc_body(node)
expected_indent = base_indent_level(node) + indentation_width
actual_indent = indent_level(body)
increase_indent_level = expected_indent - actual_indent
longest_line(body).size + increase_indent_level >= max_line_length
end
def longest_line(lines)
lines.each_line.max_by { |line| line.chomp.size }.chomp
end
def unlimited_heredoc_length?
config.for_cop('Layout/LineLength')['AllowHeredoc']
end
def max_line_length
config.for_cop('Layout/LineLength')['Max']
end
def correct_by_squiggly(node)
lambda do |corrector|
if heredoc_indent_type(node) == '~'
adjust_squiggly(corrector, node)
else
adjust_minus(corrector, node)
end
end
end
def adjust_squiggly(corrector, node)
corrector.replace(node.loc.heredoc_body, indented_body(node))
corrector.replace(node.loc.heredoc_end, indented_end(node))
end
def adjust_minus(corrector, node)
heredoc_beginning = node.loc.expression.source
corrected = heredoc_beginning.sub(/<<-?/, '<<~')
corrector.replace(node, corrected)
end
def correct_by_library(node)
lambda do |corrector|
corrector.replace(node.loc.heredoc_body, indented_body(node))
corrected = ".#{STRIP_METHODS[style]}"
corrector.insert_after(node, corrected)
end
end
def check_style!
return if style
raise Warning, "Auto-correction does not work for #{cop_name}. " \
'Please configure EnforcedStyle.'
end
def indented_body(node)
body = heredoc_body(node)
body_indent_level = indent_level(body)
correct_indent_level = base_indent_level(node) + indentation_width
body.gsub(/^[^\S\r\n]{#{body_indent_level}}/,
' ' * correct_indent_level)
end
def indented_end(node)
end_ = heredoc_end(node)
end_indent_level = indent_level(end_)
correct_indent_level = base_indent_level(node)
if end_indent_level < correct_indent_level
end_.gsub(/^\s{#{end_indent_level}}/, ' ' * correct_indent_level)
else
end_
end
end
def base_indent_level(node)
base_line_num = node.loc.expression.line
base_line = processed_source.lines[base_line_num - 1]
indent_level(base_line)
end
def indent_level(str)
indentations = str.lines
.map { |line| line[/^\s*/] }
.reject { |line| line == "\n" }
indentations.empty? ? 0 : indentations.min_by(&:size).size
end
# Returns '~', '-' or nil
def heredoc_indent_type(node)
node.source[/^<<([~-])/, 1]
end
def indentation_width
@config.for_cop('Layout/IndentationWidth')['Width'] || 2
end
def heredoc_body(node)
node.loc.heredoc_body.source.scrub
end
def heredoc_end(node)
node.loc.heredoc_end.source.scrub
end
end
end
end
end