简体   繁体   English

如何使用 eslint 在 FormattedMessage 组件中强制使用 description prop

[英]How to make description prop mandatory in FormattedMessage components with eslint

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.我正在使用react-intl并且需要FormattedMessage所有用途来包含description道具,以便翻译人员获得有关该字符串的一些上下文。 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.我还需要使用eslint规则强制执行它,以便始终提醒整个团队为每个可翻译字符串提供描述。 How can I setup that?我该如何设置?

You can use PropTypes.您可以使用 PropTypes。 It was earlier a part of React but now it has its own npm package, https://www.npmjs.com/package/prop-types .它早期是 React 的一部分,但现在它有自己的 npm 包, 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.它也很有用,因为如果你错过了它们,linter 会警告你。 https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prop-types.md 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,
};

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 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我写了一个规则来在一个名为FormattedMessage的组件上强制执行description prop

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 .此规则将适用于任何名为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.我不确定是否可以确定导入的来源以检查它是否是react-intl组件。

After creating your custom eslint plugin, you'll need to add this new rule to your project.创建自定义 eslint 插件后,您需要将此新规则添加到您的项目中。 That will depend on your project setup, but if you used CRA, you could follow this guide这将取决于您的项目设置,但如果您使用 CRA,则可以遵循本指南

Here you can see the rule working.在这里您可以看到规则在起作用。 Just clone it and move to the custom-eslint-formatted-message dir, and run npm i npm lint .只需克隆它并移动到custom-eslint-formatted-message目录,然后运行npm i npm lint vscode also detects the rule and highlights the error. vscode 还会检测规则并突出显示错误。 在此处输入图片说明

There are a few ways to do this.有几种方法可以做到这一点。

The first way is to use the eslint-plugin-react-intl.第一种方法是使用 eslint-plugin-react-intl。 This plugin will automatically add the description prop to any FormattedMessage instances it finds.此插件会自动将 description 属性添加到它找到的任何 FormattedMessage 实例。

The second way is to use the react-intl-enforce-description ESLint rule.第二种方法是使用 react-intl-enforce-description ESLint 规则。 This rule will check that all FormattedMessage instances have a description prop, and will error if they don't.此规则将检查所有 FormattedMessage 实例是否具有 description 属性,如果没有,则会出错。

The third way is to use the eslint-plugin-react-intl-display-translations ESLint rule.第三种方式是使用 eslint-plugin-react-intl-display-translations ESLint 规则。 This rule will check that all FormattedMessage instances are being used for displaying translations, and will error if they're not.此规则将检查所有 FormattedMessage 实例是否都用于显示翻译,如果不是,则会出错。 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.此规则对于在仅显示翻译的组件中强制使用 FormattedMessage 实例很有用,如果所有组件都使用 FormattedMessage 实例,则不需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM