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 collection of order related linting rules for stylelint (in a form of a plugin).
Installation
npm install stylelint-order
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/declaration-block-order": [
"custom-properties",
"declarations"
],
"order/declaration-block-properties-alphabetical-order": true
}
}
List of rules
Thanks
declaration-block-properties-specified-order
and declaration-block-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.