class SCSSLint::Linter::DeclarationOrder

Checks the order of nested items within a rule set.

Constants

DECLARATION_ORDER
MESSAGE
MIXIN_WITH_CONTENT

Public Instance Methods

check_order(node) { || ... } click to toggle source
# File lib/scss_lint/linter/declaration_order.rb, line 8
def check_order(node)
  check_node(node)
  yield # Continue linting children
end
Also aliased as: visit_rule, visit_mixin, visit_media
visit_media(node)
Alias for: check_order
visit_mixin(node)
Alias for: check_order
visit_rule(node)
Alias for: check_order

Private Instance Methods

check_children_order(sorted_children, children) click to toggle source

Find the child that is out of place

# File lib/scss_lint/linter/declaration_order.rb, line 52
def check_children_order(sorted_children, children)
  sorted_children.each_with_index do |sorted_item, index|
    next if sorted_item == children[index]

    add_lint(sorted_item.first.line,
             "Expected item on line #{sorted_item.first.line} to appear "                   "before line #{children[index].first.line}. #{MESSAGE}")
    break
  end
end
check_node(node) click to toggle source
# File lib/scss_lint/linter/declaration_order.rb, line 39
def check_node(node)
  children = node.children.each_with_index
                 .select { |n, _| important_node?(n) }
                 .map { |n, i| [n, node_declaration_type(n), i] }

  sorted_children = children.sort do |(_, a_type, i), (_, b_type, j)|
    [DECLARATION_ORDER.index(a_type), i] <=> [DECLARATION_ORDER.index(b_type), j]
  end

  check_children_order(sorted_children, children)
end
important_node?(node) click to toggle source
# File lib/scss_lint/linter/declaration_order.rb, line 35
def important_node?(node)
  DECLARATION_ORDER.include?(node.class)
end
node_declaration_type(node) click to toggle source
# File lib/scss_lint/linter/declaration_order.rb, line 63
def node_declaration_type(node)
  # If the node has no children, return the class.
  return node.class unless node.has_children

  # If the node is a mixin with children, indicate that;
  # otherwise, just return the class.
  return node.class unless node.is_a?(Sass::Tree::MixinNode)
  MIXIN_WITH_CONTENT
end