lib/scss_lint/linter/indentation.rb in scss-lint-0.25.1 vs lib/scss_lint/linter/indentation.rb in scss-lint-0.26.0
- old
+ new
@@ -2,11 +2,12 @@
# Checks for consistent indentation of nested declarations and rule sets.
class Linter::Indentation < Linter
include LinterRegistry
def visit_root(_node)
- @indent_width = config['width']
+ @indent_width = config['width'].to_i
+ @indent_character = config['character'] || 'space'
@indent = 0
yield
end
def check_and_visit_children(node)
@@ -25,17 +26,38 @@
# Ignore the case where the node is on the same line as its previous
# sibling or its parent, as indentation isn't possible
return if nodes_on_same_line?(previous_node(node), node)
+ if @indent_character == 'tab'
+ other_character = ' '
+ other_character_name = 'space'
+ else
+ other_character = "\t"
+ other_character_name = 'tab'
+ end
+
+ check_indent_width(node, other_character, @indent_character, other_character_name)
+ end
+
+ def check_indent_width(node, other_character, character_name, other_character_name)
actual_indent = engine.lines[node.line - 1][/^(\s*)/, 1]
- return if actual_indent.length == @indent
+ if actual_indent.include?(other_character)
+ add_lint(node.line,
+ "Line should be indented with #{character_name}s, " \
+ "not #{other_character_name}s")
+ return true
+ end
- add_lint(node.line,
- "Line should be indented #{@indent} spaces, " \
- "but was indented #{actual_indent.length} spaces")
- true
+ unless actual_indent.length == @indent
+ add_lint(node.line,
+ "Line should be indented #{@indent} #{character_name}s, " \
+ "but was indented #{actual_indent.length} #{character_name}s")
+ return true
+ end
+
+ false
end
# Deal with `else` statements
def visit_if(node, &block)
check_and_visit_children(node, &block)