fail on duplicate key in map declaration

This commit is contained in:
ConnorSkees 2020-04-03 16:38:01 -04:00
parent 24176bb1f0
commit 04e9b99b09
3 changed files with 12 additions and 4 deletions

View File

@ -56,14 +56,16 @@ impl SassMap {
.collect()
}
pub fn insert(&mut self, key: Value, value: Value) {
for &mut (ref k, ref mut v) in &mut self.0 {
/// Returns true if the key already exists
pub fn insert(&mut self, key: Value, value: Value) -> bool {
for (ref k, ref mut v) in &mut self.0 {
if k == &key {
*v = value;
return;
return true;
}
}
self.0.push((key, value));
false
}
}

View File

@ -325,7 +325,9 @@ impl Value {
super_selector,
)?;
devour_whitespace(paren_toks);
map.insert(key, val);
if map.insert(key, val) {
return Err("Duplicate key.".into());
}
if paren_toks.peek().is_none() {
break;
}

View File

@ -129,3 +129,7 @@ test!(
"a {\n color: inspect(map-remove((\"foo\": 1, \"bar\": 2, \"baz\": 3), \"bar\", \"baz\"));\n}\n",
"a {\n color: (\"foo\": 1);\n}\n"
);
error!(
duplicate_key_in_declaration,
"a {\n $a: (foo: a, foo: b);;\n}\n", "Error: Duplicate key."
);