Skip to content

Commit 56d01e0

Browse files
committed
Improve parentheses handling
1 parent d95bd38 commit 56d01e0

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ const trimNodes = nodes => dropWhile(dropRightWhile(nodes, isSpace), isSpace);
4646
const getPathValue = nodes =>
4747
nodes.length === 1 && nodes[0].type === "string" ? nodes[0].value : null;
4848

49-
const ensurePairsList = valuesNodes =>
50-
valuesNodes.length === 1 && valuesNodes[0].type === "function"
49+
const expandValuesParentheses = valuesNodes =>
50+
valuesNodes.length === 1 &&
51+
valuesNodes[0].type === "function" &&
52+
valuesNodes[0].value === ""
5153
? valuesNodes[0].nodes
5254
: valuesNodes;
5355

5456
const getAliasesPairs = valuesNodes =>
55-
chunkBy(ensurePairsList(valuesNodes), isComma).map(pairNodes => {
57+
chunkBy(expandValuesParentheses(valuesNodes), isComma).map(pairNodes => {
5658
const nodes = pairNodes.filter(isNotSpace);
5759
if (nodes.length === 1 && isWord(nodes[0])) {
5860
return [nodes[0].value, nodes[0].value];

test/test.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ const getWarnings = result => result.warnings().map(warning => warning.text);
1212
const run = ({ fixture, expected, warnings = [] }) =>
1313
compile(fixture).then(result => {
1414
expect(getWarnings(result)).toEqual(warnings);
15-
if (expected) {
16-
expect(result.css.trim()).toEqual(strip(expected));
17-
}
15+
expect(result.css.trim()).toEqual(strip(expected));
1816
});
1917

2018
test("export value", () => {
@@ -38,6 +36,7 @@ test("warn when there is no semicolon between lines", () => {
3836
@value red blue
3937
@value green yellow
4038
`,
39+
expected: "",
4140
warnings: [`Invalid value definition "red blue\n@value green yellow"`]
4241
});
4342
});
@@ -124,6 +123,31 @@ test("import external values with aliases", () => {
124123
});
125124
});
126125

126+
test("import multiple values grouped with parentheses on multiple lines", () => {
127+
return run({
128+
fixture: `
129+
@value (
130+
blue,
131+
red
132+
) from "path";
133+
.foo { color: red; }
134+
.bar { color: blue }
135+
`,
136+
expected: `
137+
:import('path') {
138+
__value__blue__0: blue;
139+
__value__red__1: red;
140+
}
141+
:export {
142+
blue: __value__blue__0;
143+
red: __value__red__1;
144+
}
145+
.foo { color: __value__red__1; }
146+
.bar { color: __value__blue__0 }
147+
`
148+
});
149+
});
150+
127151
test("warn on unexpected value defintion or import", () => {
128152
return run({
129153
fixture: `
@@ -136,7 +160,9 @@ test("warn on unexpected value defintion or import", () => {
136160
@value red as 'blue' from 'path';
137161
@value 'red' as blue from 'path';
138162
@value red 'as' blue from 'path';
163+
@value fn(red, blue) from 'path';
139164
`,
165+
expected: "",
140166
warnings: [
141167
`Invalid value definition "red"`,
142168
`Invalid value definition "red:"`,
@@ -146,7 +172,8 @@ test("warn on unexpected value defintion or import", () => {
146172
`Invalid value definition "red from 'path' token"`,
147173
`Invalid value definition "red as 'blue' from 'path'"`,
148174
`Invalid value definition "'red' as blue from 'path'"`,
149-
`Invalid value definition "red 'as' blue from 'path'"`
175+
`Invalid value definition "red 'as' blue from 'path'"`,
176+
`Invalid value definition "fn(red, blue) from 'path'"`
150177
]
151178
});
152179
});
@@ -237,31 +264,6 @@ test("allow all colour types", () => {
237264
});
238265
});
239266

240-
test("import multiple values grouped with parentheses on multiple lines", () => {
241-
return run({
242-
fixture: `
243-
@value (
244-
blue,
245-
red
246-
) from "path";
247-
.foo { color: red; }
248-
.bar { color: blue }
249-
`,
250-
expected: `
251-
:import('path') {
252-
__value__blue__0: blue;
253-
__value__red__1: red;
254-
}
255-
:export {
256-
blue: __value__blue__0;
257-
red: __value__red__1;
258-
}
259-
.foo { color: __value__red__1; }
260-
.bar { color: __value__blue__0 }
261-
`
262-
});
263-
});
264-
265267
test("allow definitions with commas in them", () => {
266268
return run({
267269
fixture: `

0 commit comments

Comments
 (0)