Enforcing a style rule with a linter that actually fails the build
Background I run a fleet of static sites that publish new content every day, mostly unattended. One of the house style rules is simple: no emoji anywhere in our own copy. That rule is impossible to hold by hand. A single site builds a few hundred HTML files, and emoji can slip into nav icons, button labels, <title> , the RSS feed, or JSON-LD (the JSON-formatted metadata embedded in a page to…
A static site publishing machine has a rule against using emoji in the content. A linter is used to enforce this rule. However, the linter fails the build when it encounters emoji in certain situations. This article discusses how the author solved this problem.
The linter checks each file for the presence of emoji using a regular expression. If an emoji is found, the check fails and stops the publish process. The linter was designed to catch emoji in nav icons, button labels, title, RSS feed, and JSON-LD metadata. However, there were cases where the linter incorrectly flagged content that should not have been flagged.
The first situation was when the linter flagged verbatim quotes from other people. The linter only flagged these quotes if they had a specific HTML attribute. The author rejected this approach because it would allow anyone to add a specific class to their content and bypass the rule.
The second situation was when the linter flagged proper nouns. The linter needed a different unit of exclusion for proper nouns. The author created a list of proper nouns that should not be flagged. If a proper noun was found in the content, the linter would mask it with spaces. This prevented the linter from incorrectly flagging proper nouns.
The third situation was when the linter was not decoding numeric character references before scanning for emoji. The author fixed this by decoding the numeric references first, then masking the emoji. This ensured that the linter only flagged actual emoji and not numeric references that looked like emoji.
The author added a feature to the linter that counted the number of exclusions made. This made it easier to see if the linter was passing because it was excluding too much content. The author also added a feature to the linter that would report any unused allow-list entries. This helped keep the allow-list from growing and becoming an escape hatch.
Overall, the linter successfully enforced the style rule that no emoji should be used in the content. The linter was able to exclude content that should not be flagged and report any exclusions made. This helped ensure that the site was publishing accurate content without any unnecessary flags.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.