Skip to content

Commit

Permalink
Add false value for atrules option to disable at-rule validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Dec 13, 2021
1 parent b451a41 commit e26e86a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -37,12 +37,15 @@ Setup plugin in [stylelint config](http://stylelint.io/user-guide/configuration/
- [ignore](#ignore)
- [ignoreProperties](#ignoreproperties)
- [ignoreValue](#ignorevalue)

#### atrules

Type: `Object` or `null`
Type: `Object`, `false` or `null`
Default: `null`

An option for extending or altering types syntax dictionary. An atrule definition consists of `prelude` and `descriptors`, both are optional. A `prelude` is a single expression that comes after at-rule name. A `descriptors` is a dictionary like [`properties`](#properties) option but for a specific at-rule. [CSS Value Definition Syntax](https://github.com/csstree/csstree/blob/master/docs/definition-syntax.md) is used to define value's syntax. If a definition starts with `|` it is adding to existing definition value if any. See [CSS syntax reference](https://csstree.github.io/docs/syntax/) for default definitions.
Using `false` value for the option disables at-rule validation.

Otherwise the option is using for extending or altering at-rules syntax dictionary. An atrule definition consists of `prelude` and `descriptors`, both are optional. A `prelude` is a single expression that comes after at-rule name. A `descriptors` is a dictionary like [`properties`](#properties) option but for a specific at-rule. [CSS Value Definition Syntax](https://github.com/csstree/csstree/blob/master/docs/definition-syntax.md) is used to define value's syntax. If a definition starts with `|` it is adding to existing definition value if any. See [CSS syntax reference](https://csstree.github.io/docs/syntax/) for default definitions.

The following example defines new atrule `@example` with a prelude and two descriptors (a descriptor is the same as a declaration but with no `!important` allowed):

Expand Down
9 changes: 8 additions & 1 deletion lib/index.js
Expand Up @@ -41,7 +41,8 @@ const plugin = createPlugin(ruleName, function(options) {
const ignoreProperties = Array.isArray(optionIgnoreProperties)
? new Set(optionIgnoreProperties.map(name => String(name).toLowerCase()))
: false;
const syntax = !options.properties && !options.types
const atrulesValidationDisabled = options.atrules === false;
const syntax = !options.properties && !options.types && !options.atrules
? csstree.lexer // default syntax
: csstree.fork({
properties: options.properties,
Expand All @@ -60,6 +61,12 @@ const plugin = createPlugin(ruleName, function(options) {
return;
}

// at-rule validation is disabled
if (atrulesValidationDisabled) {
ignoreAtrules.add(atrule);
return;
}

if (error = syntax.checkAtruleName(atrule.name)) {
ignoreAtrules.add(atrule);
utils.report({
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Expand Up @@ -45,6 +45,16 @@ css(null, function(tr) {
tr.notOk('.foo {\n width: 10px;\n color: red green;\n}', invalidValue('color', 3, 14));
});

// atrules
css(null, function(tr) {
tr.notOk(' @unknown {}', invalid('Unknown at-rule `@unknown`', 1, 3));
tr.notOk(' @media ??? {}', invalidPrelude('media', 1, 10));
});
css({ atrules: false }, function(tr) {
tr.ok('@unknown;');
tr.ok('@media ??? {}');
});

// ignore values with less extenstions
less(null, function(tr) {
// variables
Expand Down

0 comments on commit e26e86a

Please sign in to comment.