Skip to content

Commit f561999

Browse files
Pawel SzewczykPawel Szewczyk
authored andcommitted
fix: improve support for each
1 parent 085a559 commit f561999

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

src/lib/get-value-as-object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function getValueAsObject(value) {
3838
const matchWrappingParens = /^\(([\W\w]*)\)$/g;
3939

4040
// match a property name (any-possible_name)
41-
const matchDeclaration = /^([\w-]+)\s*:\s*([\W\w]+)\s*$/;
41+
const matchDeclaration = /^["']?([\w-]+)["']?\s*:\s*([\W\w]+)\s*$/;
4242

4343
// match a trailing comma
4444
const matchTrailingComma = /\s*,\s*$/;

src/lib/transform-each-atrule.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default function transformEachAtrule(rule, opts) {
5555
// return the @each statement options (@each NAME in LIST, @each NAME ITERATOR in LIST)
5656
const getEachOpts = (node, opts) => {
5757
const params = node.params.split(matchInOperator);
58-
const args = (params[0] || '').trim().split(' ');
58+
const args = (params[0] || '').trim().split(/[\s,]+/);
5959
const varname = args[0].trim().slice(1);
6060
const incname = args.length > 1 && args[1].trim().slice(1);
6161
const rawlist = getValueAsObject(
@@ -67,6 +67,11 @@ const getEachOpts = (node, opts) => {
6767
);
6868
const list = 'string' === typeof rawlist ? [rawlist] : rawlist;
6969

70+
const reverseParams = incname && !Array.isArray(list)
71+
if (reverseParams) {
72+
return { incname: varname, varname: incname, list };
73+
}
74+
7075
return { varname, incname, list };
7176
};
7277

test/each.expect.scss

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* arrayTest: $value */
2+
.one-arrayTest {
3+
content: one;
4+
}
5+
.two-arrayTest {
6+
content: two;
7+
}
8+
9+
/* arrayTest: $value, $key */
10+
.0-arrayTest {
11+
content: one;
12+
}
13+
.1-arrayTest {
14+
content: two;
15+
}
16+
17+
/* objectTest: $value */
18+
."1"-arrayTest {
19+
content: "1";
20+
}
21+
."2"-arrayTest {
22+
content: "2";
23+
}
24+
25+
/* objectTest: $key, $value */
26+
.one-arrayTest {
27+
content: "1";
28+
}
29+
.two-arrayTest {
30+
content: "2";
31+
}
32+
33+
/* objectStringTest: $key, $value */
34+
.one-arrayTest {
35+
content: "1";
36+
}
37+
.two-arrayTest {
38+
content: "2";
39+
}
40+
41+
/* objectStringTestMultiLine: $key, $value */
42+
.one-arrayTest {
43+
content: "1";
44+
}
45+
.two-arrayTest {
46+
content: "2";
47+
}

test/each.scss

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
$arrayTest: (one, two);
2+
3+
$objectTest: (one: "1", two: "2");
4+
$objectStringTest: ("one": "1", "two": "2");
5+
6+
$objectStringTestMultiLine: (
7+
"one": "1",
8+
"two": "2"
9+
);
10+
11+
/* arrayTest: $value */
12+
@each $value in $arrayTest {
13+
.#{$value}-arrayTest {
14+
content: #{$value};
15+
}
16+
}
17+
18+
/* arrayTest: $value, $key */
19+
@each $value, $key in $arrayTest {
20+
.#{$key}-arrayTest {
21+
content: #{$value};
22+
}
23+
}
24+
25+
/* objectTest: $value */
26+
@each $value in $objectTest {
27+
.#{$value}-arrayTest {
28+
content: #{$value};
29+
}
30+
}
31+
32+
/* objectTest: $key, $value */
33+
@each $key, $value in $objectTest {
34+
.#{$key}-arrayTest {
35+
content: #{$value};
36+
}
37+
}
38+
39+
/* objectStringTest: $key, $value */
40+
@each $key, $value in $objectStringTest {
41+
.#{$key}-arrayTest {
42+
content: #{$value};
43+
}
44+
}
45+
46+
/* objectStringTestMultiLine: $key, $value */
47+
@each $key, $value in $objectStringTestMultiLine {
48+
.#{$key}-arrayTest {
49+
content: #{$value};
50+
}
51+
}

0 commit comments

Comments
 (0)