Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lists of maps with single item require trailing comma #1191

Closed
loilo opened this issue Jan 9, 2021 · 3 comments
Closed

Lists of maps with single item require trailing comma #1191

loilo opened this issue Jan 9, 2021 · 3 comments

Comments

@loilo
Copy link

loilo commented Jan 9, 2021

I ran into a Sass compiler error recently with a combination of lists and maps.
What I have is a list containing a number of maps. This works without any problems until I only have a single map in that list.

At that point Sass requires a trailing comma after the list item, otherwise it will not be recognized as a map:

@use "sass:map";
@use "sass:list";

$list-of-maps: (
  (
    "key": "value"
  )
);

$list-item: list.nth($list-of-maps, 1);
$value: map.get($list-item, "key");

This produces the following error:

Error: $map: "key" "value" is not a map.

However, the code compiles correctly if a trailing comma is added after the list item:

@use "sass:map";
@use "sass:list";

$list-of-maps: (
  (
    "key": "value"
-  )
+  ),
);

$list-item: list.nth($list-of-maps, 1);
$value: map.get($list-item, "key");

I have created a minimal reproduction repo here.

@jathak
Copy link
Member

jathak commented Jan 11, 2021

The issue here is that (<expr>) is parsed as just <expr> wrapped in parentheses, rather than a single item list, so $list-of-maps is just a map without the trailing comma, rather than a list with one item.

To write a single-item list in Sass, you either need to add a trailing comma after the item to indicate that the parentheses are making a list, or you can use square brackets for your list. See the documentation on lists here.

@jathak jathak closed this as completed Jan 11, 2021
@loilo
Copy link
Author

loilo commented Jan 12, 2021

Yes, I assumed that this had some kind of reasonable purpose, was mostly wondering if that edge case is documented somewhere. However, now it's findable via an issues search, which is something I guess. 😁

Thanks for the explanation!

@nex3
Copy link
Contributor

nex3 commented Jan 12, 2021

In the second paragraph of the lists documentation it says:

A single-element list can be written either (<expression>,) or [<expression>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants