forked from sds/scss-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
else_placement.rb
50 lines (42 loc) · 1.4 KB
/
else_placement.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
module SCSSLint
# Checks where `@else` and `@else if` directives are placed with respect to
# the previous curly brace.
class Linter::ElsePlacement < Linter
include LinterRegistry
def visit_if(node)
visit_else(node, node.else) if node.else
yield # Lint nested @if statements
visit(node.else) if node.else
end
def visit_else(if_node, else_node)
# Check each @else branch if there are multiple `@else if`s
visit_else(else_node, else_node.else) if else_node.else
# Skip @else statements on the same line as the previous @if, since we
# don't care about placement in that case
return if if_node.line == else_node.line
spaces = 0
while (char = character_at(else_node.source_range.start_pos, - (spaces + 1)))
if char == '}'
curly_on_same_line = true
break
end
spaces += 1
end
check_placement(else_node, curly_on_same_line)
end
private
def check_placement(else_node, curly_on_same_line)
if same_line_preferred?
unless curly_on_same_line
add_lint(else_node,
'`@else` should be placed on same line as previous curly brace')
end
elsif curly_on_same_line
add_lint(else_node, '`@else` should be placed on its own line')
end
end
def same_line_preferred?
config['style'] == 'same_line'
end
end
end