Skip to content

Commit da6fac2

Browse files
author
Quang Phan
committed
test: test case for invalid parameter
1 parent e0c5423 commit da6fac2

3 files changed

Lines changed: 60 additions & 51 deletions

File tree

.changeset/happy-suits-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'postcss-color-scheme': patch
3+
---
4+
5+
test case for invalid parameters

lib/color-scheme.js

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,60 +19,56 @@ function findRootOrMediaNode(node) {
1919
* @param {ColorSchemeTransformConfig} config
2020
*/
2121
function transform(helpers, atRule, theme, config = {}) {
22-
try {
23-
const param = atRule.params.trim();
24-
if (param && param !== 'global') {
25-
atRule.warn('Unsupported parameter, expected "global" or nothing.');
26-
}
27-
const global = config.global ?? param === 'global';
28-
const parent = atRule.parent;
29-
const container = findRootOrMediaNode(parent);
30-
31-
let htmlSelectorChunk = `html:not([data-color-scheme="${theme === 'dark' ? 'light' : 'dark'}"])`;
32-
if (global) {
33-
htmlSelectorChunk = `:global(${htmlSelectorChunk})`;
34-
}
35-
const implicitRule = new helpers.Rule({
36-
selector: helpers.list
37-
.comma(parent.selector)
38-
.map((s) => `${htmlSelectorChunk} ${s}`)
39-
.join(', '),
40-
nodes: atRule.nodes,
41-
})
42-
const implicitAtRule = new helpers.AtRule({
43-
source: atRule.source,
44-
name: 'media',
45-
params: `(prefers-color-scheme: ${theme})`,
46-
nodes: [implicitRule],
47-
});
48-
const existingAtRule = container.nodes.find((node) => {
49-
return node.type === implicitAtRule.type && node.name === implicitAtRule.name && node.params === implicitAtRule.params;
50-
});
51-
if (existingAtRule) {
52-
existingAtRule.append(implicitRule);
53-
} else {
54-
container.append(implicitAtRule);
55-
}
22+
const param = atRule.params.trim();
23+
if (param && param !== 'global') {
24+
throw atRule.error('Invalid parameter, expected "global" or nothing.');
25+
}
26+
const global = config.global ?? param === 'global';
27+
const parent = atRule.parent;
28+
const container = findRootOrMediaNode(parent);
5629

57-
htmlSelectorChunk = `html[data-color-scheme="${theme}"]`;
58-
if (global) {
59-
htmlSelectorChunk = `:global(${htmlSelectorChunk})`;
60-
}
61-
const explicitRule = new helpers.Rule({
62-
selector: helpers.list
63-
.comma(parent.selector)
64-
.map((s) => `${htmlSelectorChunk} ${s}`)
65-
.join(', '),
66-
source: atRule.source,
67-
nodes: atRule.nodes,
68-
});
69-
container.append(explicitRule);
30+
let htmlSelectorChunk = `html:not([data-color-scheme="${theme === 'dark' ? 'light' : 'dark'}"])`;
31+
if (global) {
32+
htmlSelectorChunk = `:global(${htmlSelectorChunk})`;
33+
}
34+
const implicitRule = new helpers.Rule({
35+
selector: helpers.list
36+
.comma(parent.selector)
37+
.map((s) => `${htmlSelectorChunk} ${s}`)
38+
.join(', '),
39+
nodes: atRule.nodes,
40+
})
41+
const implicitAtRule = new helpers.AtRule({
42+
source: atRule.source,
43+
name: 'media',
44+
params: `(prefers-color-scheme: ${theme})`,
45+
nodes: [implicitRule],
46+
});
47+
const existingAtRule = container.nodes.find((node) => {
48+
return node.type === implicitAtRule.type && node.name === implicitAtRule.name && node.params === implicitAtRule.params;
49+
});
50+
if (existingAtRule) {
51+
existingAtRule.append(implicitRule);
52+
} else {
53+
container.append(implicitAtRule);
54+
}
7055

71-
atRule.remove();
72-
if (parent.nodes.length === 0) parent.remove();
73-
} catch (error) {
74-
throw atRule.error('Something went wrong:', error.message);
56+
htmlSelectorChunk = `html[data-color-scheme="${theme}"]`;
57+
if (global) {
58+
htmlSelectorChunk = `:global(${htmlSelectorChunk})`;
7559
}
60+
const explicitRule = new helpers.Rule({
61+
selector: helpers.list
62+
.comma(parent.selector)
63+
.map((s) => `${htmlSelectorChunk} ${s}`)
64+
.join(', '),
65+
source: atRule.source,
66+
nodes: atRule.nodes,
67+
});
68+
container.append(explicitRule);
69+
70+
atRule.remove();
71+
if (parent.nodes.length === 0) parent.remove();
7672
}
7773

7874
/**

lib/color-scheme.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,11 @@ describe('usage with postcss-nested', async () => {
8888
compareCSS(output, result.css);
8989
});
9090
});
91+
92+
test('unsupported parameter', async () => {
93+
const input = 'input { @dark meow { color: red } }';
94+
const processor = postcss([plugin()]);
95+
await expect(
96+
processor.process(input, { from: undefined })
97+
).rejects.toThrowError(/Invalid parameter/);
98+
});

0 commit comments

Comments
 (0)