What is stylelint-order?
The stylelint-order npm package is a plugin for stylelint that provides order related linting rules for CSS properties within declaration blocks. It helps in enforcing a consistent order for CSS properties, which can make code easier to read and maintain.
What are stylelint-order's main functionalities?
Specifying the order of content within declaration blocks
This feature allows you to define the order in which different types of content within a declaration block should appear. For example, you can specify that custom properties should be declared before any other declarations.
{
"plugins": [
"stylelint-order"
],
"rules": {
"order/order": [
"custom-properties",
"declarations"
],
"order/properties-order": [
"position",
"top",
"right",
"bottom",
"left"
]
}
}
Specifying the order of properties within declaration blocks
This feature allows you to define a specific order for CSS properties within a declaration block. You can list the properties in the order you want them to be linted. This can be a custom order or alphabetical order.
{
"plugins": [
"stylelint-order"
],
"rules": {
"order/properties-order": [
"position",
"z-index",
"top",
"right",
"bottom",
"left"
],
"order/properties-alphabetical-order": {}
}
}
Grouping and separating properties
This feature allows you to group related properties together and enforce an empty line before the group or between groups. This can improve readability and organization of CSS rules.
{
"plugins": [
"stylelint-order"
],
"rules": {
"order/properties-order": [
[
"position",
"z-index"
],
{
"emptyLineBefore": "always",
"properties": [
"color",
"font-size",
"font-weight"
]
}
]
}
}
Other packages similar to stylelint-order
csscomb
CSScomb is a coding style formatter for CSS. Similar to stylelint-order, it can sort CSS properties in a specific order. However, CSScomb is not a plugin for stylelint and has its own configuration and usage patterns.
postcss-sorting
postcss-sorting is a PostCSS plugin that sorts CSS properties according to specified order. It is similar to stylelint-order in functionality but is used within the PostCSS ecosystem, which is a tool for transforming CSS with JavaScript plugins.
sass-lint
sass-lint is a tool to help you enforce consistent conventions and avoid errors in your stylesheets. It is similar to stylelint-order in that it can enforce property order, but it is specifically designed for linting Sass files, not standard CSS.
stylelint-order
A plugin pack of order related linting rules for stylelint. Every rule support autofixing (stylelint --fix
).
Installation
First, install stylelint:
npm install stylelint --save-dev
Then install plugin:
npm install stylelint-order --save-dev
Usage
Add stylelint-order
to your stylelint config plugins array, then add rules you need to the rules list. All rules from stylelint-order need to be namespaced with order
.
Like so:
{
"plugins": [
"stylelint-order"
],
"rules": {
"order/order": [
"custom-properties",
"declarations"
],
"order/properties-alphabetical-order": true
}
}
List of rules
Autofixing
Every rule support autofixing (stylelint --fix
). postcss-sorting is using internally for order autofixing.
Automatic sortings has some limitation, which are described for every rule if any. Please, take a look at how comments are handled by postcss-sorting.
CSS-in-JS styles with template interpolation could be ignored by autofixing to avoid styles corruption.
Autofixing is enabled by default if it's enabled in stylelint configuration. Autofixing can be disabled on per rule basis using disableFix: true
secondary option. E. g.:
{
"rules": {
"order/order": [
[
"custom-properties",
"declarations"
],
{
"disableFix": true
}
]
}
}
Less isn't supported. It might work, but haven't tested.
Thanks
properties-order
and properties-alphabetical-order
code and readme are based on declaration-block-properties-order
rule which was a stylelint's core rule prior stylelint 8.0.0.