forked from rubocop/rubocop
/
trailing_whitespace.rb
101 lines (88 loc) · 2.59 KB
/
trailing_whitespace.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
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# This cop looks for trailing whitespace in the source code.
#
# @example
# # The line in this example contains spaces after the 0.
# # bad
# x = 0
#
# # The line in this example ends directly after the 0.
# # good
# x = 0
#
# @example AllowInHeredoc: false (default)
# # The line in this example contains spaces after the 0.
# # bad
# code = <<~RUBY
# x = 0
# RUBY
#
# # ok
# code = <<~RUBY
# x = 0 #{}
# RUBY
#
# # good
# trailing_whitespace = ' '
# code = <<~RUBY
# x = 0#{trailing_whitespace}
# RUBY
#
# @example AllowInHeredoc: true
# # The line in this example contains spaces after the 0.
# # good
# code = <<~RUBY
# x = 0
# RUBY
#
class TrailingWhitespace < Base
include RangeHelp
extend AutoCorrector
MSG = 'Trailing whitespace detected.'
def on_new_investigation
@heredocs = extract_heredocs(processed_source.ast)
processed_source.lines.each_with_index do |line, index|
next unless line.end_with?(' ', "\t")
process_line(line, index + 1)
end
end
private
def process_line(line, lineno)
heredoc = find_heredoc(lineno)
return if skip_heredoc? && heredoc
range = offense_range(lineno, line)
add_offense(range) do |corrector|
if heredoc
corrector.insert_after(range, '#{}') unless static?(heredoc) # rubocop:disable Lint/InterpolationCheck
else
corrector.remove(range)
end
end
end
def static?(heredoc)
heredoc.loc.expression.source.end_with? "'"
end
def skip_heredoc?
cop_config.fetch('AllowInHeredoc', false)
end
def find_heredoc(line_number)
@heredocs.each { |node, r| return node if r.include?(line_number) }
nil
end
def extract_heredocs(ast)
return [] unless ast
ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node|
body = node.location.heredoc_body
[node, body.first_line...body.last_line]
end
end
def offense_range(lineno, line)
source_range(processed_source.buffer, lineno, (line.rstrip.length)...(line.length))
end
end
end
end
end