At some point on a long-lived project, the rough edges start to show and it becomes worth spending effort on tooling that guards the codebase. The hard part is timing: too early is premature optimization, but waiting until everything is a mess is no better.

A good deal of that tooling is cheap enough to set up immediately. Formatters such as Prettier fall into the as-you-code bucket, and so does a whole family of linters covering accessibility, compatibility and security. Webhint collects several of those checks into a single package.

Other protection comes from code you write yourself. Tests are the obvious case, and they can be wired to run while you work. This article, though, is about a narrower variant of that idea: custom linting rules, prompted by two write-ups that crossed the desk recently — Benedict Quinn’s “How to write a custom ESLint rule” and Omri Lavi’s “Custom Stylelint Rules — Writing Your First one”. Both ESLint and Stylelint are already in use in the main codebase, so the topic was relevant.

One caution: authoring rules for either tool is not easy. It requires familiarity with an Abstract Syntax Tree — nothing close to a quick conditional like if (rules.find.selector.startsWith("old")) throw("Deprecated selector.").

Linting selectors your team has retired

The interest here connects to a question that came in from a team maintaining an old project. They want to eliminate their oldest and buggiest CSS selectors. Concretely, someone opening an HTML file might find an element carrying a class named deprecated-selector, and the team wants the IDE to flag it outright as a linting error saying “This is a deprecated selector, use .ui-fresh__selector instead.”

I work on a development team working on an old project, and we want to get of rid many of our oldest and buggiest CSS selectors. For example, one of us might open a HTML file and see an element with a class name of deprecated-selector, our goal is to have our IDE literally mark it as a linting error and say like “This is a deprecated selector, use .ui-fresh__selector instead”.

A custom Stylelint rule aimed at known-deprecated selectors was the first idea, but Stylelint lints CSS, and the reported problem is mostly in HTML. html-inspector does support writing your own rules, though it has aged enough that it is unclear whether that route works out.

CSS-Tricks star logo

Sponsor CSS-Tricks!

We've got lots of ways to help you connect with the greatest front-end community in all the land.

CSS-Tricks Newsletter

Sign up to get front-end roundups, browser updates, and exclusive content sent to your inbox bi-weekly.

Ads are self-hosted