@ackee/eslint-config-react-app 中文文档教程
eslint-config-react-app
此软件包包含 Create React App 使用的可共享 ESLint 配置。
请参阅其文档:
- Getting Started – How to create a new app.
- User Guide – How to develop apps bootstrapped with Create React App.
Usage in Create React App Projects
使用此配置的最简单方法是使用 Create React App,默认包含它.
在Create React App项目中不需要单独安装。
Usage Outside of Create React App
如果你想在一个不是用Create React App构建的项目中使用这个ESLint配置,你可以通过以下步骤安装它。
首先,安装这个包和 ESLint。
npm install --save-dev eslint-config-react-app eslint@^8.0.0
然后在项目的根文件夹中创建一个名为 .eslintrc.json
的文件,其中包含以下内容:
{
"extends": "react-app"
}
就是这样! 您可以通过编辑 .eslintrc.json
文件来覆盖 eslint-config-react-app
中的设置。 在 ESLint 网站上了解有关配置 ESLint 的更多信息。
Jest rules
此配置还附带可选的 ESLint Jest 规则(基于 eslint-plugin-jest
< /a>)。
您可以通过将 Jest 配置添加到 ESLint 配置中的 extends
数组来启用这些规则。
{
"extends": ["react-app", "react-app/jest"]
}
Accessibility Checks
eslint-plugin-jsx-a11y 插件的以下规则被激活:
- alt-text
- anchor-has-content
- aria-activedescendant-has-tabindex
- aria-props
- aria-proptypes
- aria-role
- aria-unsupported-elements
- heading-has-content
- href-no-hash
- iframe-has-title
- img-redundant-alt
- no-access-key
- no-distracting-elements
- no-redundant-roles
- role-has-required-aria-props
- role-supports-aria-props
- scope
如果你想启用甚至更多可访问性规则,您可以在项目的根目录中使用以下内容创建一个 .eslintrc.json
文件:
{
"extends": ["react-app", "plugin:jsx-a11y/recommended"],
"plugins": ["jsx-a11y"]
}
但是,如果您使用的是 Create React App 并没有弹出,任何额外的规则只会显示在 IDE 集成,但不在浏览器或终端中。
eslint-config-react-app
This package includes the shareable ESLint configuration used by Create React App.
Please refer to its documentation:
- Getting Started – How to create a new app.
- User Guide – How to develop apps bootstrapped with Create React App.
Usage in Create React App Projects
The easiest way to use this configuration is with Create React App, which includes it by default.
You don’t need to install it separately in Create React App projects.
Usage Outside of Create React App
If you want to use this ESLint configuration in a project not built with Create React App, you can install it with the following steps.
First, install this package and ESLint.
npm install --save-dev eslint-config-react-app eslint@^8.0.0
Then create a file named .eslintrc.json
with following contents in the root folder of your project:
{
"extends": "react-app"
}
That's it! You can override the settings from eslint-config-react-app
by editing the .eslintrc.json
file. Learn more about configuring ESLint on the ESLint website.
Jest rules
This config also ships with optional Jest rules for ESLint (based on eslint-plugin-jest
).
You can enable these rules by adding the Jest config to the extends
array in your ESLint config.
{
"extends": ["react-app", "react-app/jest"]
}
Accessibility Checks
The following rules from the eslint-plugin-jsx-a11y plugin are activated:
- alt-text
- anchor-has-content
- aria-activedescendant-has-tabindex
- aria-props
- aria-proptypes
- aria-role
- aria-unsupported-elements
- heading-has-content
- href-no-hash
- iframe-has-title
- img-redundant-alt
- no-access-key
- no-distracting-elements
- no-redundant-roles
- role-has-required-aria-props
- role-supports-aria-props
- scope
If you want to enable even more accessibility rules, you can create an .eslintrc.json
file in the root of your project with this content:
{
"extends": ["react-app", "plugin:jsx-a11y/recommended"],
"plugins": ["jsx-a11y"]
}
However, if you are using Create React App and have not ejected, any additional rules will only be displayed in the IDE integrations, but not in the browser or the terminal.