From d8b7ede0dcd53ec57f99cf87d9e3b4430edcf4f6 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Wed, 5 Jun 2019 00:56:06 +0300 Subject: [PATCH] fix: better handle invalid usage --- index.js | 8 ++++++++ test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/index.js b/index.js index f73437a..95c1089 100644 --- a/index.js +++ b/index.js @@ -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 ${ @@ -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; } diff --git a/test.js b/test.js index bdcb9ce..62f3461 100644 --- a/test.js +++ b/test.js @@ -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) {