forked from splitwise/erb-lint
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathright_trim.rb
34 lines (29 loc) · 993 Bytes
/
right_trim.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
# frozen_string_literal: true
module ERBLint
module Linters
# In ERB, right trim can be either =%> or -%>
# this linter will force one or the other.
class RightTrim < Linter
include LinterRegistry
class ConfigSchema < LinterConfig
property :enforced_style, accepts: ['-', '='], default: '-'
end
self.config_schema = ConfigSchema
def run(processed_source)
processed_source.ast.descendants(:erb).each do |erb_node|
_, _, _, trim_node = *erb_node
next if trim_node.nil? || trim_node.loc.source == @config.enforced_style
add_offense(
trim_node.loc,
"Prefer #{@config.enforced_style}%> instead of #{trim_node.loc.source}%> for trimming on the right."
)
end
end
def autocorrect(_processed_source, offense)
lambda do |corrector|
corrector.replace(offense.source_range, @config.enforced_style)
end
end
end
end
end