Class: SCSSLint::Linter::Indentation

Inherits:
SCSSLint::Linter
  • Object
show all
Includes:
SCSSLint::LinterRegistry
Defined in:
lib/scss_lint/linter/indentation.rb

Overview

Checks for consistent indentation of nested declarations and rule sets.

Instance Attribute Summary

Attributes inherited from SCSSLint::Linter

#config, #engine, #lints

Instance Method Summary collapse

Methods included from SCSSLint::LinterRegistry

extract_linters_from, included

Methods inherited from SCSSLint::Linter

#add_lint, #character_at, #initialize, #node_on_single_line, #run, #source_from_range, #visit, #visit_children

Methods included from Utils

#can_be_condensed?, #extract_string_selectors, #pluralize, #previous_node, #remove_quoted_strings, #shortest_hex_form

Methods included from SelectorVisitor

#visit_selector

Constructor Details

This class inherits a constructor from SCSSLint::Linter

Instance Method Details

#check_and_visit_children(node) ⇒ Object Also known as: visit_directive, visit_each, visit_for, visit_function, visit_media, visit_mixin, visit_mixindef, visit_prop, visit_rule, visit_supports, visit_while


12
13
14
15
16
17
18
19
20
21
# File 'lib/scss_lint/linter/indentation.rb', line 12

def check_and_visit_children(node)
  # Don't continue checking children as the moment a parent's indentation is
  # off it's likely the children will be as will. We don't display the child
  # indentation problems as that would likely make the lint too noisy.
  return if check_indentation(node)

  @indent += @indent_width
  yield
  @indent -= @indent_width
end

#check_indentation(node) ⇒ Object Also known as: visit_charset, visit_content, visit_cssimport, visit_extend, visit_import, visit_return, visit_variable, visit_warn


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scss_lint/linter/indentation.rb', line 23

def check_indentation(node)
  return unless node.line

  # 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 (previous = previous_node(node)) && previous.line == node.line

  actual_indent = engine.lines[node.line - 1][/^(\s*)/, 1]

  if actual_indent.length != @indent
    add_lint(node.line,
             "Line should be indented #{@indent} spaces, " <<
             "but was indented #{actual_indent.length} spaces")
    return true
  end
end

#visit_if(node, &block) ⇒ Object

Deal with `else` statements


41
42
43
44
# File 'lib/scss_lint/linter/indentation.rb', line 41

def visit_if(node, &block)
  check_and_visit_children(node, &block)
  visit(node.else) if node.else
end

#visit_root(node) ⇒ Object


6
7
8
9
10
# File 'lib/scss_lint/linter/indentation.rb', line 6

def visit_root(node)
  @indent_width = config['width']
  @indent = 0
  yield
end