@18f/18f-eslint 中文文档教程
18F eslint
此应用包装了 TTS 工程实践指南中描述的 eslint 配置, 使入门更容易。 要使用,先安装:
npm install @18f/eslint
这将安装所有适当的基本配置、插件和 更漂亮。 (从 eslint 6 开始,在你的本地项目中安装这些是 推荐,即使您正在使用全局安装的 eslint 或运行 npx
.) 它还会创建(或更新)一个包含 base 的 .eslintrc
文件 配置并将 prettier
配置添加到您的 `package.json. (这是 主要是为了使代码编辑器能够选择它。)
要在您的应用程序上运行 18F 配置的 eslint,您可以使用 npx
, 或将其作为脚本添加到您的 package.json
。 默认情况下,它会全部检查 eslint 支持的文件从当前目录开始,但你也可以 在命令行参数中指定 lint 的路径:
# Lint all the things
npx @18f/18f-eslint
# Lint some of the things
npx @18f/18f-eslint src
或在 package.json
{
...
"scripts": {
"lint": "18f-eslint"
}
}
中:定义脚本后,您可以使用 npm run lint
运行它。
命令行参数传递给 eslint lintFiles
方法。 它们可以是文件名、目录名或 glob 模式的组合。 笔记 如果你使用的是 glob,它们应该用引号引起来,这样它们 不由 shell 评估。
Configuration
默认情况下,您的应用程序将配置为扩展 airbnb
和 更漂亮
基本配置。 它还将启用 es6
环境。 如果 你的项目使用了 React,它也会扩展 prettier/react
配置 并启用浏览器
环境; 否则,它将启用 node
环境。
所有这些配置都可以由您的本地人覆盖或扩展 .eslintrc.*
文件。 该库提供的配置呈现给 eslint 作为基础,您的本地项目级配置将取代 它。 默认情况下,不定义 env
或 extends
可能是最安全的 您自己的配置文件中的属性,但如果需要,您当然可以。
如果您的项目是为 ES5 或更低版本编写的,请安装 eslint-config-airbnb -基础/遗产) 并更新您的 eslintrc
文件以将其包含在 extends
属性中。
该脚本通过以下方式确定您的项目项目是否为 React 项目 找到最近的 package.json
并在你的任一个中寻找 react
依赖关系或开发依赖关系。
GitHub Action
有一个 GitHub Action 让 将 18F-eslint 添加到您的 CI/CD 管道很容易。 此外,这个包可以 为您配置操作:
npx -p @18f/18f-eslint install-action
这将为每个 package.json
添加一个 18F-eslint GitHub 操作到您的 repo 文件。 如果你有一个 monorepo,它应该或多或少能正常工作。 它默认为 对每个路径下的所有 Javascript 文件进行 linting,但您可以调整工作流程 以满足您的需求。 查看文档 有关配置操作的更多信息。
Under the hood
18F eslint 包装器导入我们推荐的 eslint 规则和插件, Airbnb JavaScript 风格指南 指定的那些。
- For all projects
- eslint-config-prettier
- prettier
- For React projects:
- eslint-config-airbnb
- eslint-plugin-import
- eslint-plugin-jsx-a11y
- eslint-plugin-react
- For ES6/2015 projects that don't use React:
- eslint-config-airbnb-base
- eslint-plugin-import
Public domain
该项目属于全球公共领域。 如中所述 CONTRIBUTING:
该项目在美国属于公共领域,版权所有 和世界范围内的工作相关权利通过放弃 CC0 1.0 通用公共领域贡献。
所有对该项目的贡献都将在 CC0 奉献精神下发布。 通过提交拉取请求,您同意遵守此放弃 版权权益。
18F eslint
This app wraps the eslint configuration described in the TTS engineering practices guide, to make it easier to get started. To use, first install:
npm install @18f/eslint
This will install all the appropriate base configurations, plugins, and prettier. (Since eslint 6, installing these in your local project is recommended, even if you're using a globally-installed eslint or running with npx
.) It will also create (or update) a .eslintrc
file with the base configuration and add a prettier
configuration to your `package.json. (This is primarily to enable code editors to pick it up.)
To run the 18F-configured eslint on your application, you can either use npx
, or add it as a script to your package.json
. By default, it will lint all files supported by eslint starting at the current directory, but you can also specify paths to lint in the command line arguments:
# Lint all the things
npx @18f/18f-eslint
# Lint some of the things
npx @18f/18f-eslint src
Or in package.json
:
{
...
"scripts": {
"lint": "18f-eslint"
}
}
Once the script is defined, you can run it with npm run lint
.
The command line arguments are passed to the eslint lintFiles
method. They can be a combination of file names, directory names, or glob patterns. Note that if you're using globs, they should be wrapped in quotes so that they are not evaluated by the shell.
Configuration
By default, your application will be configured to extend the airbnb
and prettier
base configurations. It will also enable the es6
environment. If your project uses React, it will also extend the prettier/react
configuration and enable the browser
environment; otherwise, it will enable the node
environment.
All of these configurations can be overridden or extended by your local .eslintrc.*
file. The configuration provided by this library is presented to eslint as a base, and your local, project-level configurations will supercede it. By default, it's probably safest not to define an env
or extends
property in your own config file, but you certainly can if necessary.
If your project is written for ES5 or below, install eslint-config-airbnb-base/legacy) and update your eslintrc
file to include it in the extends
property.
The script determins whether or not your project project is a React project by finding the nearest package.json
and looking for react
in either your dependencies or dev-dependencies.
GitHub Action
There is a GitHub Action that makes it easy to add 18F-eslint to your CI/CD pipeline. In addition, this package can configure the action for you:
npx -p @18f/18f-eslint install-action
This will add an 18F-eslint GitHub action to your repo for each package.json
file. If you have a monorepo, it should more-or-less just work. It defaults to linting all Javascript files under each path, but you can tweak the workflow to suit your needs. See the documentation for more info about configuration the action.
Under the hood
The 18F eslint wrapper imports our recommended eslint rules and plugins, those specified by the Airbnb JavaScript style guide.
- For all projects
- eslint-config-prettier
- prettier
- For React projects:
- eslint-config-airbnb
- eslint-plugin-import
- eslint-plugin-jsx-a11y
- eslint-plugin-react
- For ES6/2015 projects that don't use React:
- eslint-config-airbnb-base
- eslint-plugin-import
Public domain
This project is in the worldwide public domain. As stated in CONTRIBUTING:
This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication.
All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.