Sha256: f26ed414ce7c569959b0d2cbb45c25d66e26541550777b07806bdd0f5787dcbd

Contents?: true

Size: 1.61 KB

Versions: 110

Compression:

Stored size: 1.61 KB

Contents

module Sass::Script::Tree
  # A class representing a map literal. When resolved, this returns a
  # {Sass::Script::Node::Map}.
  class MapLiteral < Node
    # The key/value pairs that make up this map node. This isn't a Hash so that
    # we can detect key collisions once all the keys have been performed.
    #
    # @return [Array<(Node, Node)>]
    attr_reader :pairs

    # Creates a new map literal.
    #
    # @param pairs [Array<(Node, Node)>] See \{#pairs}
    def initialize(pairs)
      @pairs = pairs
    end

    # @see Node#children
    def children
      @pairs.flatten
    end

    # @see Node#to_sass
    def to_sass(opts = {})
      return "()" if pairs.empty?

      to_sass = lambda do |value|
        if value.is_a?(ListLiteral) && value.separator == :comma
          "(#{value.to_sass(opts)})"
        else
          value.to_sass(opts)
        end
      end

      "(" + pairs.map {|(k, v)| "#{to_sass[k]}: #{to_sass[v]}"}.join(', ') + ")"
    end
    alias_method :inspect, :to_sass

    # @see Node#deep_copy
    def deep_copy
      node = dup
      node.instance_variable_set('@pairs',
        pairs.map {|(k, v)| [k.deep_copy, v.deep_copy]})
      node
    end

    protected

    # @see Node#_perform
    def _perform(environment)
      keys = Set.new
      map = Sass::Script::Value::Map.new(Sass::Util.to_hash(pairs.map do |(k, v)|
        k, v = k.perform(environment), v.perform(environment)
        if keys.include?(k)
          raise Sass::SyntaxError.new("Duplicate key #{k.inspect} in map #{to_sass}.")
        end
        keys << k
        [k, v]
      end))
      map.options = options
      map
    end
  end
end

Version data entries

110 entries across 107 versions & 7 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.4.0 bundle/ruby/2.5.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.3.1 bundle/ruby/2.5.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.3.0 bundle/ruby/2.5.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.2.1 bundle/ruby/2.5.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.2.0 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.1.1 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.1.0 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.0.1 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.0.1.pre1 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-4.0.0 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-3.7.2 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
brakeman-3.7.1 bundle/ruby/2.3.0/gems/sass-3.4.25/lib/sass/script/tree/map_literal.rb
sass-3.4.25 lib/sass/script/tree/map_literal.rb
brakeman-3.7.0 bundle/ruby/2.3.0/gems/sass-3.4.24/lib/sass/script/tree/map_literal.rb
brakeman-3.6.2 bundle/ruby/2.3.0/gems/sass-3.4.24/lib/sass/script/tree/map_literal.rb
sass-3.4.24 lib/sass/script/tree/map_literal.rb
brakeman-3.6.1 bundle/ruby/2.3.0/gems/sass-3.4.23/lib/sass/script/tree/map_literal.rb
brakeman-3.6.0 bundle/ruby/2.3.0/gems/sass-3.4.23/lib/sass/script/tree/map_literal.rb
brakeman-3.5.0 bundle/ruby/2.3.0/gems/sass-3.4.23/lib/sass/script/tree/map_literal.rb