Skip to content

fix: better handle invalid usage #17

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

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ function localizeNode(rule, mode, localAliasMap) {
// :local(.foo)
if (isNested) {
if (isScoped) {
if (node.nodes.length === 0) {
throw new Error(`${node.value}() can't be empty`);
}

if (context.inside) {
throw new Error(
`A ${node.value} is not allowed inside of a ${
Expand Down Expand Up @@ -206,6 +210,10 @@ function localizeNode(rule, mode, localAliasMap) {
}
case 'id':
case 'class': {
if (!node.value) {
throw new Error('Invalid class or id selector syntax');
}

if (context.global) {
break;
}
Expand Down
38 changes: 38 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,44 @@ const tests = [
:local(.foo) { animation-name: a_value; }
`,
},
{
should: 'throw on invalid syntax id usage',
input: '. {}',
error: /Invalid class or id selector syntax/,
},
{
should: 'throw on invalid syntax class usage',
input: '# {}',
error: /Invalid class or id selector syntax/,
},
{
should: 'throw on invalid syntax local class usage',
input: ':local(.) {}',
error: /Invalid class or id selector syntax/,
},
{
should: 'throw on invalid syntax local id usage',
input: ':local(#) {}',
error: /Invalid class or id selector syntax/,
},
{
should: 'throw on invalid global class usage',
input: ':global(.) {}',
error: /Invalid class or id selector syntax/,
},
{
should: 'throw on invalid global class usage',
input: ':global(#) {}',
error: /Invalid class or id selector syntax/,
},
/*
Bug in postcss-selector-parser
{
should: 'throw on invalid global class usage',
input: ':global() {}',
error: /:global\(\) can't be empty/
},
*/
];

function process(css, options) {
Expand Down