From 9e3029dbfb3c31d79fb195b744ed6cbcee1dec62 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 30 Mar 2020 16:41:00 -0400 Subject: [PATCH] better error messages for map-get and map-has-key --- src/builtin/map.rs | 4 ++-- tests/map.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/builtin/map.rs b/src/builtin/map.rs index 21e3b10..575dd8c 100644 --- a/src/builtin/map.rs +++ b/src/builtin/map.rs @@ -9,12 +9,12 @@ pub(crate) fn register(f: &mut HashMap) { "map-get".to_owned(), Box::new(|args, _| { max_args!(args, 2); + let key = arg!(args, 1, "key"); let map = match arg!(args, 0, "map") { Value::Map(m) => m, Value::List(v, ..) if v.is_empty() => SassMap::new(), v => return Err(format!("$map: {} is not a map.", v).into()), }; - let key = arg!(args, 1, "key"); Ok(map.get(key)?.unwrap_or(Value::Null).clone()) }), ); @@ -22,12 +22,12 @@ pub(crate) fn register(f: &mut HashMap) { "map-has-key".to_owned(), Box::new(|args, _| { max_args!(args, 2); + let key = arg!(args, 1, "key"); let map = match arg!(args, 0, "map") { Value::Map(m) => m, Value::List(v, ..) if v.is_empty() => SassMap::new(), v => return Err(format!("$map: {} is not a map.", v).into()), }; - let key = arg!(args, 1, "key"); Ok(Value::bool(map.get(key)?.is_some())) }), ); diff --git a/tests/map.rs b/tests/map.rs index f7b18bc..a5b112a 100644 --- a/tests/map.rs +++ b/tests/map.rs @@ -111,3 +111,13 @@ test!( "a {\n color: length((a: b, c: d, e: f));\n}\n", "a {\n color: 3;\n}\n" ); +error!( + map_get_one_arg, + "a {\n color: map-get(1);\n}\n", + "Error: Missing argument $key." +); +error!( + map_has_key_one_arg, + "a {\n color: map-has-key(1);\n}\n", + "Error: Missing argument $key." +);