forked from sds/scss-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor_prefix.rb
64 lines (55 loc) · 2.11 KB
/
vendor_prefix.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
module SCSSLint
# Checks for vendor prefixes.
class Linter::VendorPrefix < Linter
include LinterRegistry
def visit_root(_node)
@identifiers = Set.new(extract_identifiers_from_config)
@identifiers.merge(Set.new(config['additional_identifiers']))
@exclusions = Set.new(config['excluded_identifiers'])
yield
end
def check_node(node)
name = node.name.is_a?(Array) ? node.name.join : node.name
# Ignore '@' from @keyframes node name
check_identifier(node, name.sub(/^@/, ''))
# Check for values
return unless node.respond_to?(:value) && node.value.respond_to?(:source_range)
check_identifier(node, source_from_range(node.value.source_range))
end
alias visit_prop check_node
alias visit_pseudo check_node
alias visit_directive check_node
private
def check_identifier(node, identifier)
return unless identifier =~ /^[_-]/
# Strip vendor prefix to check against identifiers.
# (Also strip closing parentheticals from values like linear-gradient.)
stripped_identifier = identifier.gsub(/(^[_-][a-zA-Z0-9_]+-|\(.*\)|;)/, '').strip
return if @exclusions.include?(stripped_identifier)
return unless @identifiers.include?(stripped_identifier)
add_lint(node, 'Avoid vendor prefixes.')
end
def extract_identifiers_from_config
case config['identifier_list']
when nil
nil
when Array
config['identifier_list']
when String
begin
file = File.open(File.join(SCSS_LINT_DATA,
'prefixed-identifiers',
"#{config['identifier_list']}.txt"))
file.read.split("\n").reject { |line| line =~ /^(#|\s*$)/ }
rescue Errno::ENOENT
raise SCSSLint::Exceptions::LinterError,
"Identifier list '#{config['identifier_list']}' does not exist"
end
else
raise SCSSLint::Exceptions::LinterError,
'Invalid identifier list specified -- must be the name of a '\
'preset or an array of strings'
end
end
end
end