Stylelint Rule: No Utility Selector Styling
This Stylelint rule, `stylelint-selector-no-utility`, is designed to enforce a specific coding standard by disallowing the direct styling of utility classes within CSS. It serves to protect the immutability of single-purpose utility styles, preventing unintended side effects that can arise from altering their definitions. The rule is highly specialized and is intended for use with `@primer/css`, the CSS framework developed by GitHub. The current stable release is version 4.0.0, which was published in March 2019. Due to its close integration with the Primer CSS ecosystem, the package's release cadence is not frequent and is typically tied to updates or changes within Primer CSS itself. Its primary differentiator lies in its targeted application to enforce best practices within a utility-first CSS architecture, specifically for projects utilizing `@primer/css`. It acts as a guardrail against common anti-patterns that can compromise the predictability and maintainability of utility-based styling.
Common errors
-
Unknown rule primer/selector-no-utility
cause The stylelint configuration is attempting to use the rule `primer/selector-no-utility` but the plugin `stylelint-selector-no-utility` has not been correctly registered in the `plugins` array.fixAdd `'stylelint-selector-no-utility'` to the `plugins` array in your Stylelint configuration file (e.g., `.stylelintrc.json` or `stylelint.config.js`). -
Error: Cannot find module 'stylelint-selector-no-utility'
cause The `stylelint-selector-no-utility` package has not been installed in the project's dependencies.fixInstall the package using npm or yarn: `npm install --save-dev stylelint-selector-no-utility` or `yarn add --dev stylelint-selector-no-utility`. -
A peer dependency of stylelint-selector-no-utility, @primer/css, is not installed.
cause The required peer dependency `@primer/css` (version `>=12`) is missing from the project's `node_modules`.fixInstall the `@primer/css` package: `npm install @primer/css@^12` or `yarn add @primer/css@^12`.
Warnings
- breaking Starting with v4.0.0, `@primer/css` is a required peer dependency for `stylelint-selector-no-utility` to function correctly. Previous versions might have bundled or relied on specific older versions implicitly.
- breaking For `stylelint-selector-no-utility` v3.0.1, users were required to install a pre-release (`canary`) version of `@primer/css` due to its migration out of the Primer CSS monorepo.
- gotcha This plugin is highly specific to Primer CSS. While it could be forked or adapted, its built-in logic and checks are tailored to Primer's utility class patterns.
Install
-
npm install stylelint-selector-no-utility -
yarn add stylelint-selector-no-utility -
pnpm add stylelint-selector-no-utility
Imports
- Plugin registration in stylelint.config.js
module.exports = { plugins: ['stylelint-selector-no-utility'], rules: { 'primer/selector-no-utility': true } }; - Plugin registration in .stylelintrc.json
{ "plugins": [ "stylelint-selector-no-utility", "primer/selector-no-utility" // Incorrect: plugin list should only contain the package name ] }{ "plugins": [ "stylelint-selector-no-utility" ], "rules": { "primer/selector-no-utility": true } } - Rule name usage
rules: { 'selector-no-utility': true }rules: { 'primer/selector-no-utility': true }
Quickstart
{
"plugins": [
"stylelint-selector-no-utility"
],
"rules": {
"primer/selector-no-utility": true
}
}