At Testim.io we care about code quality and UX. For this reason, we use various tools that make development easier and more…
By Omri Lavi,
Share on
At Testim.io we care about code quality and UX. For this reason, we use various tools that make development easier and more accurate. Among others, we use Stylelint to lint our SCSS and CSS files. One of the powerful features of stylelint is the ability to add rules of your own, which will fit your project’s needs. While stylelint provides an API for adding custom rules, it can be a bit confusing when doing so for the first time.
In this blog post, we will go through the steps for adding a new custom stylelint rule to your project:
Declaring and creating the plugin
Adding a rule to the plugin
Implementing the linting function along with auto-fix support
Integrating the plugin with your project’s Stylelint configuration
Background story
In our application’s codebase, we store commonly used colors in variables. This makes color theme changes very quick and easy. While developing a visual component, we use Invision’s mockups as a reference. Invision isn’t aware of the color variables we use. Thus, Copying styles from Invision involves searching for the colors’ variables (e.g. replace #ff6363 with $color-red1).
This seems like an easy thing to do: use “find anywhere” and look for this color. Yet, the process becomes cumbersome — especially if we include many colors. Additionally, in some cases of legacy code, the variable ($color-red1) was not used. Hence, we can find the same color (#ff6363) scattered in the codebase. In such cases, the developer has to find the correct declaration in all search results.
In order to solve this inconvenience, we wrote a custom stylelint rule. It suggests the correct variable name, and even auto-fixes the errors. How convenient!
In the following paragraphs, we will describe the steps for adding a custom rule of your own. As an abbreviated example, we’ll write a rule which will replace the expression blue with the hex value #0000FF.
Writing the Plugin — First Steps
Let’s start with creating a file that will contain the rule itself — in our case, it’s no-color-blue.js.
Next, you’ll need to define your rule. This way stylelint will know what to run when the rule appears in the configurations:
// no-color-blue.js
const stylelint = require('stylelint');
const { ruleMessages, validateOptions } = stylelint.utils;
const ruleName = 'testim-plugin/no-color-blue';
const messages = ruleMessages(ruleName, {
//… linting messages can be specified here
});
More stories we think you will like
Testim Mobile: Elevating Mobile App Quality and Speed
We're proud to announce the latest update to Testim Mobile, which brings unparalleled test automation support for custom mobile apps.…
mobile
AI and quality assurance: Self-healing processes to improve engineer experience
If you have been in QA for any length of time, you can probably recall a project where the test…
QA
Discover How QA Can Enhance the Developer Experience With AI
DevOps is no longer a new concept. Companies have embraced the term. However, it may appear that some are not…