Sha256: e74d3289e1adbd913ced7bef7964d6a1478181fffa9002b4c6749007eb83f8ba

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

class SCSSBeautifier::Formatters::DeclarationOrder < SCSSBeautifier::Formatters::Base

  DEFAULT_SORT_ORDER = ["mixindef", "extend", "mixin", "prop", "rule"].freeze

  def visit_rule(node)
    order_children(node)
    visit_children(node)
  end

  def order_children(node)
    node_hash = Hash.new { |h, k| h[k] = [] }
    comment_array = []
    node.children.each do |child|
      hash_key = child.class.node_name.to_s
      if hash_key == 'comment'
        comment_array << child
      else
        if comment_array.any?
          node_hash[hash_key] = node_hash[hash_key].concat comment_array
          comment_array = []
        end
        node_hash[hash_key] << child
      end
    end

    compiled_array = sort_order.reduce([]) do |memo, key|
      memo.concat(node_hash[key])
    end

    (node_hash.keys - sort_order).reduce(compiled_array) do |memo, key|
      memo.concat(node_hash[key])
    end

    node.children = compiled_array
  end

  private

  def sort_order
    @sort_order ||= options["sort_order"] || DEFAULT_SORT_ORDER
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scss_beautifier-0.1.15 lib/scss_beautifier/formatters/declaration_order.rb
scss_beautifier-0.1.14 lib/scss_beautifier/formatters/declaration_order.rb
scss_beautifier-0.1.13 lib/scss_beautifier/formatters/declaration_order.rb
scss_beautifier-0.1.12 lib/scss_beautifier/formatters/declaration_order.rb