Skip to content

Commit

Permalink
support $keys... argument to map-has-key
Browse files Browse the repository at this point in the history
  • Loading branch information
connorskees committed Aug 4, 2024
1 parent 1afbacb commit f16fc40
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
26 changes: 24 additions & 2 deletions crates/compiler/src/builtin/functions/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,34 @@ pub(crate) fn map_get(mut args: ArgumentResult, visitor: &mut Visitor) -> SassRe
}

pub(crate) fn map_has_key(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
args.max_args(2)?;
let key = args.get_err(1, "key")?;
let map = args
.get_err(0, "map")?
.assert_map_with_name("map", args.span())?;
Ok(Value::bool(map.get(&key).is_some()))

// since we already extracted the map and first key,
// neither will be returned in the variadic args list
let keys = args.get_variadic()?;

let mut val = match map.get(&key) {
Some(v) => v,
None => return Ok(Value::False),
};
for key in keys {
// if at any point we find a value that's not a map,
// we return null
let val_map = match val.try_map() {
Some(val_map) => val_map,
None => return Ok(Value::False),
};

val = match val_map.get(&key) {
Some(v) => v,
None => return Ok(Value::False),
};
}

Ok(Value::True)
}

pub(crate) fn map_keys(mut args: ArgumentResult, visitor: &mut Visitor) -> SassResult<Value> {
Expand Down
61 changes: 61 additions & 0 deletions crates/lib/tests/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,67 @@ test!(
"a {\n color: inspect((a: b, !important: c));\n}\n",
"a {\n color: (a: b, !important: c);\n}\n"
);
test!(
map_has_key_multiple_keys_true,
r#"
@use "sass:map";
$fonts: (
"Helvetica": (
"weights": (
"regular": 400,
"medium": 500,
"bold": 700
)
)
);
a {
color: map.has-key($fonts, "Helvetica", "weights", "regular");
}"#,
"a {\n color: true;\n}\n"
);
test!(
map_has_key_multiple_keys_false,
r#"
@use "sass:map";
$fonts: (
"Helvetica": (
"weights": (
"regular": 400,
"medium": 500,
"bold": 700
)
)
);
a {
color: map.has-key($fonts, "Helvetica", "colors");
}"#,
"a {\n color: false;\n}\n"
);
test!(
map_has_key_multiple_keys_value_is_null,
"a {\n color: map-has-key((a: (b: null)), \"a\", \"b\");\n}\n",
"a {\n color: true;\n}\n"
);
test!(
map_has_key_multiple_keys_value_is_false,
"a {\n color: map-has-key((a: (b: false)), \"a\", \"b\");\n}\n",
"a {\n color: true;\n}\n"
);
test!(
map_has_key_multiple_keys_deeply_nested,
"a {\n color: map-has-key((a: (b: (c: (d: (e: (f: (g: (h: (i: (j: (k: (l: m)))))))))))), a, b, c, d, e, f, g, h, i, j, k, l);\n}\n",
"a {\n color: true;\n}\n"
);
test!(
map_has_key_multiple_keys_empty_map,
"a {\n color: map-has-key((), a, b, c);\n}\n",
"a {\n color: false;\n}\n"
);
test!(
map_has_key_multiple_keys_value_isnt_map,
"a {\n color: map-has-key((a: (b: 5)), a, b, c);\n}\n",
"a {\n color: false;\n}\n"
);
error!(
bang_identifier_not_important_as_key,
"a {\n color: inspect((a: b, !a: c));\n}\n", r#"Error: expected ")"."#
Expand Down

0 comments on commit f16fc40

Please sign in to comment.