Skip to content

Commit

Permalink
Rip out support for passing lists of pairs to map functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Feb 21, 2014
1 parent 2283d29 commit f16ef47
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 106 deletions.
2 changes: 2 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Expand Up @@ -58,6 +58,8 @@ can use it in a mixin to detect whether a parent selector exists:
by default. If you need the current working directory to be available,
set `SASS_PATH=.` in your shell's environment.

* Sass will now throw an error when a list of pairs is passed to a map function.

* `index()` now returns `null` rather than `false` if the value isn't found in
the list.

Expand Down
26 changes: 7 additions & 19 deletions lib/sass/script/functions.rb
Expand Up @@ -474,7 +474,7 @@ def initialize(environment)
def assert_type(value, type, name = nil)
klass = Sass::Script::Value.const_get(type)
return if value.is_a?(klass)
return if value.is_a?(Sass::Script::Value::List) && type == :Map && value.is_pseudo_map?
return if value.is_a?(Sass::Script::Value::List) && type == :Map && value.value.empty?
err = "#{value.inspect} is not a #{TYPE_NAMES[type] || type.to_s.downcase}"
err = "$#{name.to_s.gsub('_', '-')}: " + err if name
raise ArgumentError.new(err)
Expand Down Expand Up @@ -1922,7 +1922,7 @@ def list_separator(list)
# @raise [ArgumentError] if `$map` is not a map
def map_get(map, key)
assert_type map, :Map, :map
to_h(map)[key] || null
map.to_h[key] || null
end
declare :map_get, [:map, :key]

Expand All @@ -1946,7 +1946,7 @@ def map_get(map, key)
def map_merge(map1, map2)
assert_type map1, :Map, :map1
assert_type map2, :Map, :map2
map(to_h(map1).merge(to_h(map2)))
map(map1.to_h.merge(map2.to_h))
end
declare :map_merge, [:map1, :map2]

Expand All @@ -1962,7 +1962,7 @@ def map_merge(map1, map2)
# @raise [ArgumentError] if `$map` is not a map
def map_remove(map, key)
assert_type map, :Map, :map
hash = to_h(map).dup
hash = map.to_h.dup
hash.delete key
map(hash)
end
Expand All @@ -1978,7 +1978,7 @@ def map_remove(map, key)
# @raise [ArgumentError] if `$map` is not a map
def map_keys(map)
assert_type map, :Map, :map
list(to_h(map).keys, :comma)
list(map.to_h.keys, :comma)
end
declare :map_keys, [:map]

Expand All @@ -1994,7 +1994,7 @@ def map_keys(map)
# @raise [ArgumentError] if `$map` is not a map
def map_values(map)
assert_type map, :Map, :map
list(to_h(map).values, :comma)
list(map.to_h.values, :comma)
end
declare :map_values, [:map]

Expand All @@ -2010,7 +2010,7 @@ def map_values(map)
# @raise [ArgumentError] if `$map` is not a map
def map_has_key(map, key)
assert_type map, :Map, :map
bool(to_h(map).has_key?(key))
bool(map.to_h.has_key?(key))
end
declare :map_has_key, [:map, :key]

Expand Down Expand Up @@ -2262,17 +2262,5 @@ def _adjust(color, amount, attr, range, op, units = "")
color.with(attr => Sass::Util.restrict(
color.send(attr).send(op, amount.value), range))
end

def to_h(obj)
return obj.to_h unless obj.is_a?(Sass::Script::Value::List) && obj.needs_map_warning?

fn_name = Sass::Util.caller_info.last.gsub('_', '-')
Sass::Util.sass_warn <<WARNING + environment.stack.to_s.gsub(/^/, ' ')
DEPRECATION WARNING: Passing lists of pairs to #{fn_name} is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
obj.to_h
end
end
end
15 changes: 0 additions & 15 deletions lib/sass/script/value/list.rb
Expand Up @@ -65,24 +65,9 @@ def to_sass(opts = {})
# @see Value#to_h
def to_h
return Sass::Util.ordered_hash if value.empty?
return @map ||= Sass::Util.to_hash(value.map {|e| e.to_a}) if is_pseudo_map?
super
end

# Returns whether a warning still needs to be printed for this list being used as a map.
#
# @return [Boolean]
def needs_map_warning?
!@value.empty? && !@map
end

# Returns whether this is a list of pairs that can be used as a map.
#
# @return [Boolean]
def is_pseudo_map?
@is_pseudo_map ||= value.all? {|e| e.is_a?(Sass::Script::Value::List) && e.to_a.length == 2}
end

# @see Value#inspect
def inspect
"(#{value.map {|e| e.inspect}.join(sep_str(nil))})"
Expand Down
72 changes: 0 additions & 72 deletions test/sass/functions_test.rb
Expand Up @@ -1398,16 +1398,6 @@ def test_map_get
assert_equal "null", perform("map-get((), foo)").to_sass
end

def test_map_get_deprecation_warning
assert_warning(<<WARNING) do
DEPRECATION WARNING: Passing lists of pairs to map-get is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
assert_equal "1", evaluate("map-get((foo 1) (bar 2), foo)")
end
end

def test_map_get_checks_type
assert_error_message("$map: 12 is not a map for `map-get'", "map-get(12, bar)")
end
Expand All @@ -1421,26 +1411,6 @@ def test_map_merge
perform("map-merge((foo: 1, bar: 2), ())").to_sass)
end

def test_map_merge_deprecation_warning
assert_warning(<<WARNING) do
DEPRECATION WARNING: Passing lists of pairs to map-merge is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
assert_equal("(foo: 1, bar: 2, baz: 3)",
perform("map-merge((foo 1, bar 2), (baz: 3))").to_sass)
end

assert_warning(<<WARNING) do
DEPRECATION WARNING: Passing lists of pairs to map-merge is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
assert_equal("(baz: 3, foo: 1, bar: 2)",
perform("map-merge((baz: 3), (foo 1, bar 2))").to_sass)
end
end

def test_map_merge_checks_type
assert_error_message("$map1: 12 is not a map for `map-merge'", "map-merge(12, (foo: 1))")
assert_error_message("$map2: 12 is not a map for `map-merge'", "map-merge((foo: 1), 12)")
Expand All @@ -1452,17 +1422,6 @@ def test_map_remove
assert_equal("()", perform("map-remove((), foo)").to_sass)
end

def test_map_remove_deprecation_warning
assert_warning(<<WARNING) do
DEPRECATION WARNING: Passing lists of pairs to map-remove is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
assert_equal("(foo: 1, baz: 3)",
perform("map-remove((foo 1, bar 2, baz 3), bar)").to_sass)
end
end

def test_map_remove_checks_type
assert_error_message("$map: 12 is not a map for `map-remove'", "map-remove(12, foo)")
end
Expand All @@ -1473,17 +1432,6 @@ def test_map_keys
assert_equal("()", perform("map-keys(())").to_sass)
end

def test_map_keys_deprecation_warning
assert_warning(<<WARNING) do
DEPRECATION WARNING: Passing lists of pairs to map-keys is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
assert_equal("foo, bar",
perform("map-keys((foo 1, bar 2))").to_sass)
end
end

def test_map_keys_checks_type
assert_error_message("$map: 12 is not a map for `map-keys'", "map-keys(12)")
end
Expand All @@ -1495,16 +1443,6 @@ def test_map_values
assert_equal("()", perform("map-values(())").to_sass)
end

def test_map_values_deprecation_warning
assert_warning(<<WARNING) do
DEPRECATION WARNING: Passing lists of pairs to map-values is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
assert_equal("1, 2", perform("map-values((foo 1, bar 2))").to_sass)
end
end

def test_map_values_checks_type
assert_error_message("$map: 12 is not a map for `map-values'", "map-values(12)")
end
Expand All @@ -1515,16 +1453,6 @@ def test_map_has_key
assert_equal "false", evaluate("map-has-key((), foo)")
end

def test_map_has_key_deprecation_warning
assert_warning(<<WARNING) do
DEPRECATION WARNING: Passing lists of pairs to map-has-key is deprecated and will
be removed in future versions of Sass. Use Sass maps instead. For details, see
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#maps.
WARNING
assert_equal("true", evaluate("map-has-key((foo 1, bar 1), foo)"))
end
end

def test_map_has_key_checks_type
assert_error_message("$map: 12 is not a map for `map-has-key'", "map-has-key(12, foo)")
end
Expand Down

0 comments on commit f16ef47

Please sign in to comment.