forked from sds/scss-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace_after_comment.rb
72 lines (59 loc) · 2.06 KB
/
space_after_comment.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
65
66
67
68
69
70
71
72
module SCSSLint
# Checks for a space after comment literals
class Linter::SpaceAfterComment < Linter
include LinterRegistry
def visit_comment(node)
source = source_from_range(node.source_range).strip
check_method = "check_#{node.type}_comment"
send(check_method, node, source)
end
private
def check_silent_comment(node, source)
source.split("\n").each_with_index do |line, index|
next if config['allow_empty_comments'] && line.strip.length <= 2
whitespace = whitespace_after_comment(line.lstrip, 2)
check_for_space(node.line + index, whitespace)
end
end
def check_normal_comment(node, source)
whitespace = whitespace_after_comment(source, 2)
check_for_space(node, whitespace)
end
def check_loud_comment(node, source)
whitespace = whitespace_after_comment(source, 3)
check_for_space(node, whitespace)
end
def check_for_no_spaces(node_or_line, whitespace)
return if whitespace == 0
add_lint(node_or_line, 'Comment literal should not be followed by any spaces')
end
def check_for_one_space(node_or_line, whitespace)
return if whitespace == 1
add_lint(node_or_line, 'Comment literal should be followed by one space')
end
def check_for_at_least_one_space(node_or_line, whitespace)
return if whitespace >= 1
add_lint(node_or_line, 'Comment literal should be followed by at least one space')
end
def check_for_space(node_or_line, spaces)
case config['style']
when 'one_space'
check_for_one_space(node_or_line, spaces)
when 'no_space'
check_for_no_spaces(node_or_line, spaces)
when 'at_least_one_space'
check_for_at_least_one_space(node_or_line, spaces)
end
end
def whitespace_after_comment(source, offset)
whitespace = 0
# Allow for comments that start with `/// `.
offset += 1 if source[offset] == '/'
while [' ', "\t"].include? source[offset]
whitespace += 1
offset += 1
end
whitespace
end
end
end