Skip to content

Commit 4b765b1

Browse files
fix: better handle invalid usage (#17)
1 parent 8c306e6 commit 4b765b1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ function localizeNode(rule, mode, localAliasMap) {
119119
// :local(.foo)
120120
if (isNested) {
121121
if (isScoped) {
122+
if (node.nodes.length === 0) {
123+
throw new Error(`${node.value}() can't be empty`);
124+
}
125+
122126
if (context.inside) {
123127
throw new Error(
124128
`A ${node.value} is not allowed inside of a ${
@@ -206,6 +210,10 @@ function localizeNode(rule, mode, localAliasMap) {
206210
}
207211
case 'id':
208212
case 'class': {
213+
if (!node.value) {
214+
throw new Error('Invalid class or id selector syntax');
215+
}
216+
209217
if (context.global) {
210218
break;
211219
}

test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,44 @@ const tests = [
616616
:local(.foo) { animation-name: a_value; }
617617
`,
618618
},
619+
{
620+
should: 'throw on invalid syntax id usage',
621+
input: '. {}',
622+
error: /Invalid class or id selector syntax/,
623+
},
624+
{
625+
should: 'throw on invalid syntax class usage',
626+
input: '# {}',
627+
error: /Invalid class or id selector syntax/,
628+
},
629+
{
630+
should: 'throw on invalid syntax local class usage',
631+
input: ':local(.) {}',
632+
error: /Invalid class or id selector syntax/,
633+
},
634+
{
635+
should: 'throw on invalid syntax local id usage',
636+
input: ':local(#) {}',
637+
error: /Invalid class or id selector syntax/,
638+
},
639+
{
640+
should: 'throw on invalid global class usage',
641+
input: ':global(.) {}',
642+
error: /Invalid class or id selector syntax/,
643+
},
644+
{
645+
should: 'throw on invalid global class usage',
646+
input: ':global(#) {}',
647+
error: /Invalid class or id selector syntax/,
648+
},
649+
/*
650+
Bug in postcss-selector-parser
651+
{
652+
should: 'throw on invalid global class usage',
653+
input: ':global() {}',
654+
error: /:global\(\) can't be empty/
655+
},
656+
*/
619657
];
620658

621659
function process(css, options) {

0 commit comments

Comments
 (0)