Sha256: 3c22f0b439648a447e8babe997e6494c89d81b98d1e978feb39bb6f415a89d37

Contents?: true

Size: 1.28 KB

Versions: 7

Compression:

Stored size: 1.28 KB

Contents

require 'sass'

module SCSSLint
  class Linter::DeclarationOrderLinter < Linter
    include LinterRegistry

    DECLARATION_ORDER = [
      Sass::Tree::ExtendNode,
      Sass::Tree::PropNode,
      Sass::Tree::RuleNode,
    ]

    class << self
      def run(engine)
        lints = []
        engine.tree.each do |node|
          if node.is_a?(Sass::Tree::RuleNode)
            lints << check_order_of_declarations(node)
          end
        end
        lints.compact
      end

      def description
        'Rule sets should start with @extend declarations, followed by ' <<
        'properties and nested rule sets, in that order'
      end

    private

      def important_node?(node)
        case node
        when *DECLARATION_ORDER
          true
        end
      end

      def check_order_of_declarations(rule_node)
        children = rule_node.children.select { |node| important_node?(node) }.
                                      map { |node| node.class }

        # Inefficient, but we're not sorting thousands of declarations
        sorted_children = children.sort do |x,y|
          DECLARATION_ORDER.index(x) <=> DECLARATION_ORDER.index(y)
        end

        if children != sorted_children
          return create_lint(rule_node.children.first)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
scss-lint-0.6 lib/scss_lint/linter/declaration_order_linter.rb
scss-lint-0.5.2 lib/scss_lint/linter/declaration_order_linter.rb
scss-lint-0.5.1 lib/scss_lint/linter/declaration_order_linter.rb
scss-lint-0.5 lib/scss_lint/linter/declaration_order_linter.rb
scss-lint-0.4 lib/scss_lint/linter/declaration_order_linter.rb
scss-lint-0.3 lib/scss_lint/linter/declaration_order_linter.rb
scss-lint-0.2 lib/scss_lint/linter/declaration_order_linter.rb