You can follow these articles to create a custom rule:
https://blog.scottlogic.com/2021/09/06/how-to-write-an-es-lint-rule-for-beginners.html
https://flexport.engineering/writing-custom-lint-rules-for-your-picky-developers-67732afa1803
I wrote a rule to enforce the description prop on a component called FormattedMessage
const ERROR_DESCRIPTION_MISSING =
"FormattedMessage component should have a `description` prop";
module.exports = {
meta: {
type: "problem",
schema: [],
},
create: (context) => {
return {
JSXOpeningElement: function (node) {
const nodeType = node.name.name;
if (nodeType !== "FormattedMessage") {
return;
}
if (!node.attributes.find((attr) => attr.name.name === "description")) {
context.report({
node: node,
message: ERROR_DESCRIPTION_MISSING,
});
}
},
};
},
};
This rule will apply to any component called FormattedMessage. I'm not sure if it is possible to identify the import where it comes from to check it is a react-intl component.
After creating your custom eslint plugin, you'll need to add this new rule to your project. That will depend on your project setup, but if you used CRA, you could follow this guide
Here you can see the rule working. Just clone it and move to the custom-eslint-formatted-message dir, and run npm i npm run lint. vscode also detects the rule and highlights the error.
