forked from Shopify/erb_lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace_indentation.rb
41 lines (34 loc) · 1.13 KB
/
space_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
# frozen_string_literal: true
module ERBLint
module Linters
# Detects indentation with tabs and autocorrect them to spaces
class SpaceIndentation < Linter
include LinterRegistry
class ConfigSchema < LinterConfig
property :tab_width, converts: :to_i, accepts: Integer, default: 2
end
self.config_schema = ConfigSchema
START_SPACES = /\A([[:blank:]]*)/
def run(processed_source)
lines = processed_source.file_content.split("\n", -1)
document_pos = 0
lines.each do |line|
spaces = line.match(START_SPACES)&.captures&.first
if spaces.include?("\t")
add_offense(
processed_source.to_source_range(document_pos...(document_pos + spaces.length)),
"Indent with spaces instead of tabs.",
spaces.gsub("\t", " " * @config.tab_width),
)
end
document_pos += line.length + 1
end
end
def autocorrect(_processed_source, offense)
lambda do |corrector|
corrector.replace(offense.source_range, offense.context)
end
end
end
end
end