I'm using react-intl
and need all uses of FormattedMessage
to contain a description
prop so that the translators get some context about that string. I also need to enforce it with an eslint
rule so the entire team is always reminded to provide a description with each translatable string. How can I setup that?

- 148
- 2
- 15
3 Answers
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.

- 19,277
- 4
- 32
- 49
There are a few ways to do this.
The first way is to use the
eslint-plugin-react-intl
. This plugin will automatically add the description prop to any FormattedMessage instances it finds.The second way is to use the
react-intl-enforce-description
ESLint rule. This rule will check that all FormattedMessage instances have a description prop, and will error if they don't.The third way is to use the
eslint-plugin-react-intl-display-translations
ESLint rule. This rule will check that all FormattedMessage instances are being used for displaying translations, and will error if they're not. This rule is useful for enforcing the use of FormattedMessage instances in components that only display translations, and is not necessary if all components are using FormattedMessage instances.

- 1,778
- 1
- 11
- 26

- 81
- 6
You can use PropTypes. It was earlier a part of React but now it has its own npm package, https://www.npmjs.com/package/prop-types. This will give you a runtime error if there props are not provided. It's also useful, because linters can warn you if you miss them. https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prop-types.md
import React from 'react';
import PropTypes from 'prop-types';
const MyComponent = (props) => (
<div className={props.name}>
{props.description}
</div>
);
MyComponent.propTypes = {
name: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
};

- 428
- 5
- 16
-
Thanks for the answer but I guess defining a required prop with PropTypes is not quite what I'm looking for: 1 - FormattedMessage is a component from the react-intl package. I'd need to create a wrapper on it and define my own propTypes? I'd be interested in a simpler solution 2 - A build time validation would be more useful than a runtime error – amoedoamorim Oct 28 '21 at 17:30