lib/scss_lint/linter/indentation.rb in scss_lint-0.38.0 vs lib/scss_lint/linter/indentation.rb in scss_lint-0.39.0
- old
+ new
@@ -54,14 +54,20 @@
else
check_regular_indent(node, actual_indent.length, character_name)
end
end
- # Deal with `else` statements
- def visit_if(node, &block)
- check_and_visit_children(node, &block)
- visit(node.else) if node.else
+ # Deal with `else` statements, which require special care since they are
+ # considered children of `if` statements.
+ def visit_if(node)
+ check_indentation(node)
+
+ if config['allow_non_nested_indentation']
+ yield # Continue linting else statement
+ else
+ visit(node.else) if node.else
+ end
end
# Need to define this explicitly since @at-root directives can contain
# inline selectors which produces the same parse tree as if the selector was
# nested within it. For example:
@@ -87,10 +93,16 @@
else
check_and_visit_children(node, &block)
end
end
+ def visit_import(node)
+ prev = previous_node(node)
+ return if prev.is_a?(Sass::Tree::ImportNode) && source_from_range(prev.source_range) =~ /,$/
+ check_indentation(node)
+ end
+
# Define node types that increase indentation level
alias_method :visit_directive, :check_and_visit_children
alias_method :visit_each, :check_and_visit_children
alias_method :visit_for, :check_and_visit_children
alias_method :visit_function, :check_and_visit_children
@@ -105,11 +117,10 @@
# Define node types to check indentation of (notice comments are left out)
alias_method :visit_charset, :check_indentation
alias_method :visit_content, :check_indentation
alias_method :visit_cssimport, :check_indentation
alias_method :visit_extend, :check_indentation
- alias_method :visit_import, :check_indentation
alias_method :visit_return, :check_indentation
alias_method :visit_variable, :check_indentation
alias_method :visit_warn, :check_indentation
private
@@ -155,11 +166,11 @@
"Line should be indented 0 #{character_name}s, " \
"but was indented #{actual_indent} #{character_name}s")
return true
end
elsif !one_shift_greater_than_parent?(node, actual_indent)
- parent_indent = node_indent(node.node_parent).length
+ parent_indent = node_indent(node_indent_parent(node)).length
expected_indent = parent_indent + @indent_width
add_lint(node.line,
"Line should be indented #{expected_indent} #{character_name}s, " \
"but was indented #{actual_indent} #{character_name}s")
@@ -179,19 +190,30 @@
# parent.
#
# @param node [Sass::Tree::Node]
# @return [true,false]
def one_shift_greater_than_parent?(node, actual_indent)
- parent_indent = node_indent(node.node_parent).length
+ parent_indent = node_indent(node_indent_parent(node)).length
expected_indent = parent_indent + @indent_width
expected_indent == actual_indent
end
# Return indentation of a node.
#
# @param node [Sass::Tree::Node]
# @return [Integer]
def node_indent(node)
engine.lines[node.line - 1][/^(\s*)/, 1]
+ end
+
+ def node_indent_parent(node)
+ if else_node?(node)
+ while node.node_parent.is_a?(Sass::Tree::IfNode) &&
+ node.node_parent.else == node
+ node = node.node_parent
+ end
+ end
+
+ node.node_parent
end
end
end