scss-lint 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sass/script.rb +20 -4
- data/lib/scss_lint/linter/color_keyword.rb +30 -5
- data/lib/scss_lint/linter/hex_format.rb +11 -24
- data/lib/scss_lint/utils.rb +11 -0
- data/lib/scss_lint/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69f5645967ac2ed4c054af95508d25ffb88328f7
|
4
|
+
data.tar.gz: b8d83cdd575da8e42307dedfdeb32d2bb264a210
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cd037b4f7b02d0b011e2fc0db246cdfafa8f69a78dc2ad49e66d81df3e470f07731b4d23c115fc700426fc2c5f4dd41578be8b937a8449e4a977ef308944a40
|
7
|
+
data.tar.gz: dffa479078fac884d6ead1759882a9d23b1f1212618418b89a78b9fce1b2e0cf061817a26f9b523ae246bb7bb16a6b2ec7da52107b1487cc560822f38e2ac778
|
data/lib/sass/script.rb
CHANGED
@@ -17,10 +17,12 @@ module Sass::Script
|
|
17
17
|
class Color
|
18
18
|
attr_accessor :original
|
19
19
|
|
20
|
-
def self.from_string(string)
|
21
|
-
rgb
|
22
|
-
|
23
|
-
|
20
|
+
def self.from_string(string, rgb = nil)
|
21
|
+
unless rgb
|
22
|
+
rgb = string.scan(/^#(..?)(..?)(..?)$/).
|
23
|
+
first.
|
24
|
+
map { |hex| hex.ljust(2, hex).to_i(16) }
|
25
|
+
end
|
24
26
|
|
25
27
|
color = Color.new(rgb, false)
|
26
28
|
color.original = string
|
@@ -45,4 +47,18 @@ module Sass::Script
|
|
45
47
|
[:color, Color.from_string(color_string)]
|
46
48
|
end
|
47
49
|
end
|
50
|
+
|
51
|
+
class Parser
|
52
|
+
# We redefine the ident parser to specially handle color keywords.
|
53
|
+
def ident
|
54
|
+
return funcall unless @lexer.peek && @lexer.peek.type == :ident
|
55
|
+
return if @stop_at && @stop_at.include?(@lexer.peek.value)
|
56
|
+
|
57
|
+
name = @lexer.next
|
58
|
+
if color = Color::COLOR_NAMES[name.value.downcase]
|
59
|
+
return node(Color.from_string(name.value, color))
|
60
|
+
end
|
61
|
+
node(Sass::Script::String.new(name.value, :identifier))
|
62
|
+
end
|
63
|
+
end
|
48
64
|
end
|
@@ -2,18 +2,43 @@ module SCSSLint
|
|
2
2
|
class Linter::ColorKeyword < Linter
|
3
3
|
include LinterRegistry
|
4
4
|
|
5
|
-
def
|
6
|
-
|
5
|
+
def visit_script_color(node)
|
6
|
+
add_color_lint(node, node.original) if color_keyword?(node.original)
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def visit_script_string(node)
|
10
|
+
return unless node.type == :identifier
|
11
|
+
|
12
|
+
remove_quoted_strings(node.value).scan(/[a-z]+/i) do |word|
|
13
|
+
add_color_lint(node, word) if color_keyword?(word)
|
14
|
+
end
|
11
15
|
end
|
12
16
|
|
13
17
|
private
|
14
18
|
|
19
|
+
def add_color_lint(node, original)
|
20
|
+
hex_form = Sass::Script::Color.new(color_rgb(original)).inspect
|
21
|
+
add_lint(node,
|
22
|
+
"Color `#{original}` should be written in hexadecimal form " <<
|
23
|
+
"as `#{shortest_hex_form(hex_form)}`")
|
24
|
+
end
|
25
|
+
|
15
26
|
def color_keyword?(string)
|
16
|
-
!!
|
27
|
+
!!color_rgb(string)
|
28
|
+
end
|
29
|
+
|
30
|
+
def color_rgb(string)
|
31
|
+
Sass::Script::Color::COLOR_NAMES[string]
|
32
|
+
end
|
33
|
+
|
34
|
+
# Takes a string like `hello "world" 'how are' you` and turns it into:
|
35
|
+
# `hello you`.
|
36
|
+
# This is useful for scanning for keywords in shorthand properties or lists
|
37
|
+
# which can contain quoted strings but for which you don't want to inspect
|
38
|
+
# quoted strings (e.g. you care about the actual color keyword `red`, not
|
39
|
+
# the string "red").
|
40
|
+
def remove_quoted_strings(string)
|
41
|
+
string.gsub(/"[^"]*"|'[^']*'/, '')
|
17
42
|
end
|
18
43
|
end
|
19
44
|
end
|
@@ -2,21 +2,19 @@ module SCSSLint
|
|
2
2
|
class Linter::HexFormat < Linter
|
3
3
|
include LinterRegistry
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
node.value.type == :identifier
|
5
|
+
def visit_script_color(node)
|
6
|
+
return unless node.original && node.original.match(HEX_REGEX)
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
8
|
+
unless valid_hex_format?(node.original[HEX_REGEX, 1])
|
9
|
+
add_hex_lint(node, node.original)
|
12
10
|
end
|
13
|
-
|
14
|
-
yield # Continue visiting children
|
15
11
|
end
|
16
12
|
|
17
|
-
def
|
18
|
-
unless
|
19
|
-
|
13
|
+
def visit_script_string(node)
|
14
|
+
return unless node.type == :identifier
|
15
|
+
|
16
|
+
node.value.scan(HEX_REGEX) do |match|
|
17
|
+
add_hex_lint(node, match.first) unless valid_hex_format?(match.first)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
@@ -25,22 +23,11 @@ module SCSSLint
|
|
25
23
|
HEX_REGEX = /(#\h{3,6})/
|
26
24
|
|
27
25
|
def add_hex_lint(node, hex)
|
28
|
-
add_lint(node, "Color `#{hex}` should be written as `#{
|
26
|
+
add_lint(node, "Color `#{hex}` should be written as `#{shortest_hex_form(hex)}`")
|
29
27
|
end
|
30
28
|
|
31
29
|
def valid_hex_format?(hex)
|
32
|
-
hex ==
|
33
|
-
end
|
34
|
-
|
35
|
-
def shortest_form(hex)
|
36
|
-
(can_be_condensed?(hex) ? (hex[0..1] + hex[3] + hex[5]) : hex).downcase
|
37
|
-
end
|
38
|
-
|
39
|
-
def can_be_condensed?(hex)
|
40
|
-
hex.length == 7 &&
|
41
|
-
hex[1] == hex[2] &&
|
42
|
-
hex[3] == hex[4] &&
|
43
|
-
hex[5] == hex[6]
|
30
|
+
hex == shortest_hex_form(hex)
|
44
31
|
end
|
45
32
|
end
|
46
33
|
end
|
data/lib/scss_lint/utils.rb
CHANGED
@@ -23,6 +23,17 @@ module SCSSLint
|
|
23
23
|
node.name =~ /#{INVALID_NAME_CHARS}/
|
24
24
|
end
|
25
25
|
|
26
|
+
def shortest_hex_form(hex)
|
27
|
+
(can_be_condensed?(hex) ? (hex[0..1] + hex[3] + hex[5]) : hex).downcase
|
28
|
+
end
|
29
|
+
|
30
|
+
def can_be_condensed?(hex)
|
31
|
+
hex.length == 7 &&
|
32
|
+
hex[1] == hex[2] &&
|
33
|
+
hex[3] == hex[4] &&
|
34
|
+
hex[5] == hex[6]
|
35
|
+
end
|
36
|
+
|
26
37
|
private
|
27
38
|
|
28
39
|
INVALID_NAME_CHARS = '[_A-Z]'
|
data/lib/scss_lint/version.rb
CHANGED