Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

stylelint-csstree-validator

csstree254.8kMIT3.0.0

Stylelint plugin to validate CSS syntax

stylelint-plugin, stylelint, csstree, css, validator, syntax

readme

NPM version Build Status Coverage Status

stylelint-csstree-validator

A stylelint plugin based on csstree to examinate CSS syntax. It examinates at-rules and declaration values to match W3C specs and browsers extensions. It might be extended in future to validate other parts of CSS.

⚠️ Warning ⚠️: The plugin is designed to validate CSS syntax only. However stylelint may be configured to use for other syntaxes like Less or Sass. In this case, the plugin avoids examination of expressions containing non-standard syntax, but you need specify which preprocessor is used with the syntaxExtensions option.

Install

$ npm install --save-dev stylelint-csstree-validator

Usage

Setup plugin in stylelint config:

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": true
  }
}

Options

syntaxExtensions

Type: Array<'sass' | 'less'> or false
Default: false

Since the plugin focuses on CSS syntax validation it warns on a syntax which is introducing by preprocessors like Less or Sass. The syntaxExtensions option allows to specify that some preprocessor's syntaxes are used for styles so the plugin may avoid warnings when met such a syntax.

By default the plugin exams styles as pure CSS. To specify that a preprocessor's syntax is used, you must specify an array with the names of these extensions. Currently supported:

  • sass – declaration values with Sass syntax will be ignored as well as custom at-rules introduced by Saas (e.g. @if, @else, @mixin etc). For now Sass at-rules are allowed with any prelude, but it might be replaced for real syntax definitions in future releases
  • less – declaration values with Sass syntax will be ignored as well as @plugin at-rule introduced by Less

Using both syntax extensions is also possible:

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "syntaxExtensions": ["sass", "less"]
    }
  }
}

atrules

Type: Object, false or null
Default: null

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 option but for a specific at-rule. CSS Value Definition Syntax 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 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):

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "atrules": {
        "example": {
          "prelude": "<custom-ident>",
          "descriptors": {
            "foo": "<number>",
            "bar": "<color>"
          }
        }
      }
    }
  }
}

properties

Type: Object or null
Default: null

An option for extending or altering properties syntax dictionary. CSS Value Definition Syntax 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 for default definitions.

The following example extends width and defines size properties:

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "properties": {
        "width": "| new-keyword | custom-function(<length>, <percentage>)",
        "size": "<length-percentage>"
      }
    }
  }
}

Using <any-value> for a property definition is an alternative for ignoreProperties option.

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "properties": {
        "my-custom-property": "<any-value>"
      }
    }
  }
}

types

Type: Object or null
Default: null

An option for extending or altering types syntax dictionary. Types are something like a preset which allow reuse a definition across other definitions. CSS Value Definition Syntax 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 for default definitions.

The following example defines a new functional type my-fn() and extends color type:

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "properties": {
        "some-property": "<my-fn()>"
      },
      "types": {
        "color": "| darken(<color>, [ <percentage> | <number [0, 1]> ])",
        "my-fn()": "my-fn( <length-percentage> )"
      }
    }
  }
}

ignore

Works the same as ignoreProperties but deprecated, use ignoreProperties instead.

ignoreAtrules

Type: Array<string|RegExp> or false
Default: false

Defines a list of at-rules names that should be ignored by the plugin. Ignorance for an at-rule means no validation for its name, prelude or descriptors. The names provided are used for full case-insensitive matching, i.e. a vendor prefix is mandatory and prefixed names should be provided as well if you need to ignore them. You can use RegExp patterns in the list as well.

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "ignoreAtrules": ["custom-at-rule", "-webkit-keyframes"]
    }
  }
}

ignoreProperties

Type: Array<string|RegExp> or false
Default: false

Defines a list of property names that should be ignored by the plugin. The names provided are used for full case-insensitive matching, i.e. a vendor prefix is mandatory and prefixed names should be provided as well if you need to ignore them.

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "ignoreProperties": ["composes", "mask", "-webkit-mask"]
    }
  }
}

In this example, plugin will not test declarations with a property name composes, mask or -webkit-mask, i.e. no warnings for these declarations would be raised. You can use RegExp patterns in the list as well.

ignoreValue

Type: RegExp or false
Default: false

Defines a pattern for values that should be ignored by the validator.

{
  "plugins": [
    "stylelint-csstree-validator"
  ],
  "rules": {
    "csstree/validator": {
      "ignoreValue": "^pattern$"
    }
  }
}

For this example, the plugin will not report warnings for values which is matched the given pattern. However, warnings will still be reported for unknown properties.

RegExp patterns

In some cases a more general match patterns are needed instead of exact name matching. In such cases a RegExp pattern can be used.

Since CSS names are an indentifiers which can't contain a special characters used for RegExp's, a distinguishing between a CSS name and RegExp is a trivial problem. When the plugin encounters a string in a ignore pattern list containing any character other than a-z, A-Z, 0-9 or -, it produces a RegExp using the construction new RegExp('^(' + pattern + ')$', 'i'). In other words, the pattern should be fully matched case-insensitive.

To have a full control over a RegExp pattern, a regular RegExp instance or its stringified version (i.e. "/pattern/flags?") can be used.

  • "foo|bar" transforms into /^(foo|bar)$/i
  • "/foo|bar/i" transforms into /foo|bar/i (note: it's not the same as previous RegExp, since not requires a full match with a name)
  • /foo|bar/ used as is (note: with no i flag a matching will be case-sensitive which makes no sense in CSS)

License

MIT

changelog

3.0.0 (June 12, 2023)

  • Added support for stylelint 15 (#53)
  • Dropped support for Node.js below 14.13

2.1.0 (January 29, 2023)

  • Bumped css-tree to ^2.3.1
  • Extended ignoreAtrules and ignoreProperties options to accept RegExp patterns (#19, #45)
  • Fixed Sass's @else at-rule to allow have no a prelude (#46)
  • Changed at-rule prelude validation to emit no warnings when a prelude contains Sass/Less syntax extensions (#44)

2.0.0 (December 14, 2021)

  • Added syntaxExtensions option to specify syntax extensions, i.e. sass or/and less. For now the plugin validates CSS only by default
  • Added at-rule validation for name, prelude and descriptors
  • Added atrules option to extend or alter at-rule syntax definition dictionary or disable at-rule validation when false is passed as a value for the option
  • Added ignoreAtrules option to specify at-rule names which should not be validated
  • Used isStandardSyntax*() helpers from stylelint to reduce failures for non-standard syntax (e.g. Less or Sass)
  • Added support for Less & Sass namespaces, a value with such constructions are ignored now instead of failure (#39)
  • Added a column to mismatch error details
  • Renamed ignore option into ignoreProperties to be more clear what is ignoring; ignore option is still work but cause to a deprecation warning
  • Fixed ignoreValue option to apply for parse errors either (#43)
  • Fixed failure on a declaration with a Less variable reference, i.e. ignore such declarations for now
  • Package
    • Changed supported versions of Node.js to ^12.20.0, ^14.13.0 and >=15.0.0
    • Converted to ES modules. However, CommonJS is supported as well (dual module)
    • Bumped css-tree to 2.0 and latest mdn-data dictionaries

1.9.0 (October 27, 2020)

  • Bumped CSSTree to ^1.0.0 (mdn-data is bumped to 2.0.12)
  • Added properties and types options to extend syntax dictionary
  • Added ignoreValue option (#14)

1.8.0 (January 24, 2020)

  • Added support for stylelint 13 (#25, thanks to @limonte)

1.7.0 (November 25, 2019)

  • Added support for stylelint 12 (#24, #23, thanks to @limonte & @gforcada)
  • Bumped CSSTree to 1.0.0-alpha.38 (mdn-data is bumped to 2.0.6)

1.6.1 (October 6, 2019)

  • Fixed regression after CSSTree bump to 1.0.0-alpha.34

1.6.0 (October 6, 2019)

  • Added support for stylelint 11 (#21, thanks to @ntwb)
  • Bumped CSSTree to 1.0.0-alpha.34

1.5.2 (July 11, 2019)

1.5.1 (July 11, 2019)

1.5.0 (July 11, 2019)

1.4.1 (July 5, 2019)

  • Fixed missed console.log() (#18)

1.4.0 (July 3, 2019)

  • Added support for stylelint 10 (#17, thanks to @limonte)
  • Bumped CSSTree to 1.0.0-alpha.30

1.3.0 (May 30, 2018)

1.2.2 (February 19, 2018)

  • Bumped CSSTree to 1.0.0-alpha.28 (bug fixes)
  • Bumped stylelint to >=7.0.0 <10.0.0 and make it a peer dependency

1.2.1 (November 12, 2017)

  • Bumped CSSTree to 1.0.0-alpha.26 (improved parsing and bug fixes)

1.2.0 (September 4, 2017)

  • Bumped CSSTree to 1.0.0-alpha21 (improved parsing and updated property grammars)

1.1.1 (February 15, 2017)

  • Ignore any declaration which property name looks using a preprocessor interpolation (e.g. smth-@{foo} or smth-#{$foo})
  • Ignore values with Sass interpolation (#7)

1.1.0 (February 14, 2017)

  • Ignore Less and Sass var declarations that treats as regular declarations by PostCSS (#4)
  • Implemented ignore option to define a list of property names that should be ignored by the validator. It may be used as a workaround to avoid warnings about syntax extensions (#5)

1.0.6 (February 12, 2017)

  • Bump CSSTree to 1.0.0-alpha16
  • Ignore values with Less and Sass extensions (#3)

1.0.5 (January 19, 2017)

  • Bump CSSTree to 1.0.0-alpha12

1.0.4 (December 21, 2016)

  • Bump CSSTree to 1.0.0-alpha9

1.0.3 (November 21, 2016)

  • Bump CSSTree to 1.0.0-alpha8

1.0.2 (September 19, 2016)

  • Bump CSSTree to 1.0.0-alpha5

1.0.1 (September 17, 2016)

  • Tweak description files

1.0.0 (September 17, 2016)

  • Initial implementation