forked from Shopify/erb_lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinter.rb
64 lines (53 loc) · 1.79 KB
/
linter.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
# frozen_string_literal: true
module ERBLint
# Defines common functionality available to all linters.
class Linter
class << self
attr_accessor :simple_name
attr_accessor :config_schema
# When defining a Linter class, define its simple name as well. This
# assumes that the module hierarchy of every linter starts with
# `ERBLint::Linters::`, and removes this part of the class name.
#
# `ERBLint::Linters::Foo.simple_name` #=> "Foo"
# `ERBLint::Linters::Compass::Bar.simple_name` #=> "Compass::Bar"
def inherited(linter)
super
linter.simple_name = if linter.name.start_with?("ERBLint::Linters::")
name_parts = linter.name.split("::")
name_parts[2..-1].join("::")
else
linter.name
end
linter.config_schema = LinterConfig
end
def support_autocorrect?
method_defined?(:autocorrect)
end
end
attr_reader :offenses, :config
# Must be implemented by the concrete inheriting class.
def initialize(file_loader, config)
@file_loader = file_loader
@config = config
raise ArgumentError, "expect `config` to be #{self.class.config_schema} instance, "\
"not #{config.class}" unless config.is_a?(self.class.config_schema)
@offenses = []
end
def enabled?
@config.enabled?
end
def excludes_file?(filename)
@config.excludes_file?(filename, @file_loader.base_path)
end
def run(_processed_source)
raise NotImplementedError, "must implement ##{__method__}"
end
def add_offense(source_range, message, context = nil, severity = nil)
@offenses << Offense.new(self, source_range, message, context, severity)
end
def clear_offenses
@offenses = []
end
end
end