forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strip_heredoc.rb
56 lines (48 loc) · 1.32 KB
/
strip_heredoc.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
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Enforces the use of squiggly heredoc over `strip_heredoc`.
#
# @example
#
# # bad
# <<EOS.strip_heredoc
# some text
# EOS
#
# # bad
# <<-EOS.strip_heredoc
# some text
# EOS
#
# # good
# <<~EOS
# some text
# EOS
#
class StripHeredoc < Base
extend AutoCorrector
extend TargetRubyVersion
MSG = 'Use squiggly heredoc (`<<~`) instead of `strip_heredoc`.'
RESTRICT_ON_SEND = %i[strip_heredoc].freeze
minimum_target_ruby_version 2.3
def on_send(node)
return unless (receiver = node.receiver)
return unless receiver.str_type? || receiver.dstr_type?
return unless receiver.respond_to?(:heredoc?) && receiver.heredoc?
register_offense(node, receiver)
end
private
def register_offense(node, heredoc)
add_offense(node) do |corrector|
squiggly_heredoc = heredoc.source.sub(/\A<<-?/, '<<~')
corrector.replace(heredoc, squiggly_heredoc)
corrector.remove(node.loc.dot)
corrector.remove(node.loc.selector)
end
end
end
end
end
end