当前位置:网站首页>eslint 简单配置
eslint 简单配置
2022-06-22 20:52:00 【前端粉刷匠】
Eslint
esLint是一个开源的JavaScript代码检查工具,代码检查是一种静态的分析,常用语寻找有问题的模式或者代码,并不依赖于具体的编码风格,对于大多数的语言来说都会有代码检查功能,一般来说编译程序内会内置检查工具。
esLint所有的规则都设计成可插入的,eslint的默认规则与其他插件并没有什么区别,规则本身和测试可以依赖同样的模式,因此可配置,插件式式eslint的最大特点。
配置方式
- 使用JavaScript注释把配置信息直接嵌入到一个代码源文件中。
- 配置文件式
上面两种方式中’配置文件式‘是最常见的配置方式,使用JavaScript,JSON,或者YAML文件为整个目录(处理你的主目录),和它的子目录指定配置信息,Eslint会自动查找他们自动读取他们,再者你可以在命令行运行的时候指定一个任意的配置文件。
Eslint支持几种格式的配置文件
- JavaScript使用.eslintrc.js然后输出一个配置对象。
- YAML 使用.eslintrc.yaml或者.eslintrc.yml去定义配置结构。
- JSON 使用.eslintrc.json去定义配置的结构,eslint的JSON文件允许JavaScript风格的注释。
- (弃用)使用eslintrc,可以使JSON也可以是YAML,
- package.json 在page.json中创建一个eslintConfig属性,在那里定义你的配置。如果同一个目录下有多个配置,eslint只会使用一个,优先级的顺序如下。
- .eslintrc.js
- .eslintrc.yaml
- .eslintrc.yml
- .eslintrc.json
- .eslintrc
- .package.json
综上:我们尽量选择js文件来当做配置文件,因为js里面方便书写逻辑,并且优先级比较。
常用的配置项
parser
- Eslint默认使用Espree作为其解析器,可以在配置文件中指定不同的解析器,
- Esprima
- Babel-ESlint 一个对babel解析器的包装。使其能与eslint兼容
- typescript-eslint-parser(实验),一个把TS转换成ESTree兼容格式的解析器,这样它就可以在eslint中使用了。这样做的目的是通过eslint来解析TypeScript文件(尽管不一定必须通过所有的eslint规则),注意在使用自定义解析器的时候,为了让eslint处理非ES5特性时正常工作,配置parserOptions仍然是必须的。解析器会被传入parserOptions,但是不一定会使用他们来决定功能特性的开关。
Prettier
- 代码格式化工具,能够按照我们的规则,将我们的代码格式化。
eslint检查指令:
- lint --fix 检查所有文件 (一般情况下都用的是全局检测)
- eslint app.js --fix 检查制定文件
如果在代码中有些不能避免的问题可以使用下列方法来避免检查
- 忽略 no-undef 检查:/* eslint-disable no-undef */
- 忽略 no-new 检查:/* eslint-disable no-new */
配置项解读
/** * AlloyTeam ESLint 规则 - React * * 包含所有 ESLint 规则,以及所有 eslint-plugin-react 规则 * 使用 babel-eslint 作为解析器 * * @fixable 表示此配置支持 --fix * @off 表示此配置被关闭了,并且后面说明了关闭的原因 */
module.exports = {
extends: [
'./index.js',
],
plugins: [
'react'
],
rules: {
// 布尔值类型的 propTypes 的 name 必须为 is 或 has 开头
// @off 不强制要求写 propTypes
'react/boolean-prop-naming': 'off',
// 一个 defaultProps 必须有对应的 propTypes
// @off 不强制要求写 propTypes
'react/default-props-match-prop-types': 'off',
// 组件必须有 displayName 属性
// @off 不强制要求写 displayName
'react/display-name': 'off',
// 禁止在自定义组件中使用一些指定的 props
// @off 没必要限制
'react/forbid-component-props': 'off',
// 禁止使用一些指定的 elements
// @off 没必要限制
'react/forbid-elements': 'off',
// 禁止使用一些指定的 propTypes
// @off 不强制要求写 propTypes
'react/forbid-prop-types': 'off',
// 禁止直接使用别的组建的 propTypes
// @off 不强制要求写 propTypes
'react/forbid-foreign-prop-types': 'off',
// 禁止使用数组的 index 作为 key
// @off 太严格了
'react/no-array-index-key': 'off',
// 禁止使用 children 做 props
'react/no-children-prop': 'error',
// 禁止使用 dangerouslySetInnerHTML
// @off 没必要限制
'react/no-danger': 'off',
// 禁止在使用了 dangerouslySetInnerHTML 的组建内添加 children
'react/no-danger-with-children': 'error',
// 禁止使用已废弃的 api
'react/no-deprecated': 'error',
// 禁止在 componentDidMount 里面使用 setState
// @off 同构应用需要在 didMount 里写 setState
'react/no-did-mount-set-state': 'off',
// 禁止在 componentDidUpdate 里面使用 setState
'react/no-did-update-set-state': 'error',
// 禁止直接修改 this.state
'react/no-direct-mutation-state': 'error',
// 禁止使用 findDOMNode
'react/no-find-dom-node': 'error',
// 禁止使用 isMounted
'react/no-is-mounted': 'error',
// 禁止在一个文件创建两个组件
// @off 有一个 bug https://github.com/yannickcr/eslint-plugin-react/issues/1181
'react/no-multi-comp': 'off',
// 禁止在 PureComponent 中使用 shouldComponentUpdate
'react/no-redundant-should-component-update': 'error',
// 禁止使用 ReactDOM.render 的返回值
'react/no-render-return-value': 'error',
// 禁止使用 setState
// @off setState 很常用
'react/no-set-state': 'off',
// 禁止拼写错误
'react/no-typos': 'error',
// 禁止使用字符串 ref
'react/no-string-refs': 'error',
// 禁止在组件的内部存在未转义的 >, ", ' 或 }
'react/no-unescaped-entities': 'error',
// @fixable 禁止出现 HTML 中的属性,如 class
'react/no-unknown-property': 'error',
// 禁止出现未使用的 propTypes
// @off 不强制要求写 propTypes
'react/no-unused-prop-types': 'off',
// 定义过的 state 必须使用
// @off 没有官方文档,并且存在很多 bug: https://github.com/yannickcr/eslint-plugin-react/search?q=no-unused-state&type=Issues&utf8=%E2%9C%93
'react/no-unused-state': 'off',
// 禁止在 componentWillUpdate 中使用 setState
'react/no-will-update-set-state': 'error',
// 必须使用 Class 的形式创建组件
'react/prefer-es6-class': [
'error',
'always'
],
// 必须使用 pure function
// @off 没必要限制
'react/prefer-stateless-function': 'off',
// 组件必须写 propTypes
// @off 不强制要求写 propTypes
'react/prop-types': 'off',
// 出现 jsx 的地方必须 import React
// @off 已经在 no-undef 中限制了
'react/react-in-jsx-scope': 'off',
// 非 required 的 prop 必须有 defaultProps
// @off 不强制要求写 propTypes
'react/require-default-props': 'off',
// 组件必须有 shouldComponentUpdate
// @off 没必要限制
'react/require-optimization': 'off',
// render 方法中必须有返回值
'react/require-render-return': 'error',
// @fixable 组件内没有 children 时,必须使用自闭和写法
// @off 没必要限制
'react/self-closing-comp': 'off',
// @fixable 组件内方法必须按照一定规则排序
'react/sort-comp': 'error',
// propTypes 的熟悉必须按照字母排序
// @off 没必要限制
'react/sort-prop-types': 'off',
// style 属性的取值必须是 object
'react/style-prop-object': 'error',
// HTML 中的自闭和标签禁止有 children
'react/void-dom-elements-no-children': 'error',
// @fixable 布尔值的属性必须显式的写 someprop={true}
// @off 没必要限制
'react/jsx-boolean-value': 'off',
// @fixable 自闭和标签的反尖括号必须与尖括号的那一行对齐
'react/jsx-closing-bracket-location': [
'error',
{
nonEmpty: false,
selfClosing: 'line-aligned'
}
],
// @fixable 结束标签必须与开始标签的那一行对齐
// @off 已经在 jsx-indent 中限制了
'react/jsx-closing-tag-location': 'off',
// @fixable 大括号内前后禁止有空格
'react/jsx-curly-spacing': [
'error',
{
when: 'never',
attributes: {
allowMultiline: true
},
children: true,
spacing: {
objectLiterals: 'never'
}
}
],
// @fixable props 与 value 之间的等号前后禁止有空格
'react/jsx-equals-spacing': [
'error',
'never'
],
// 限制文件后缀
// @off 没必要限制
'react/jsx-filename-extension': 'off',
// @fixable 第一个 prop 必须得换行
// @off 没必要限制
'react/jsx-first-prop-new-line': 'off',
// handler 的名称必须是 onXXX 或 handleXXX
// @off 没必要限制
'react/jsx-handler-names': 'off',
// @fixable jsx 的 children 缩进必须为四个空格
'react/jsx-indent': [
'error',
4
],
// @fixable jsx 的 props 缩进必须为四个空格
'react/jsx-indent-props': [
'error',
4
],
// 数组中的 jsx 必须有 key
'react/jsx-key': 'error',
// @fixable 限制每行的 props 数量
// @off 没必要限制
'react/jsx-max-props-per-line': 'off',
// jsx 中禁止使用 bind
// @off 太严格了
'react/jsx-no-bind': 'off',
// 禁止在 jsx 中使用像注释的字符串
'react/jsx-no-comment-textnodes': 'error',
// 禁止出现重复的 props
'react/jsx-no-duplicate-props': 'error',
// 禁止在 jsx 中出现字符串
// @off 没必要限制
'react/jsx-no-literals': 'off',
// 禁止使用 target="_blank"
// @off 没必要限制
'react/jsx-no-target-blank': 'off',
// 禁止使用未定义的 jsx elemet
'react/jsx-no-undef': 'error',
// 禁止使用 pascal 写法的 jsx,比如 <TEST_COMPONENT>
'react/jsx-pascal-case': 'error',
// @fixable props 必须排好序
// @off 没必要限制
'react/jsx-sort-props': 'off',
// @fixable jsx 的开始和闭合处禁止有空格
'react/jsx-tag-spacing': [
'error',
{
closingSlash: 'never',
beforeSelfClosing: 'always',
afterOpening: 'never'
}
],
// jsx 文件必须 import React
'react/jsx-uses-react': 'error',
// 定义了的 jsx element 必须使用
'react/jsx-uses-vars': 'error',
// @fixable 多行的 jsx 必须有括号包起来
// @off 没必要限制
'react/jsx-wrap-multilines': 'off'
}
};
边栏推荐
- Case 2 of SQL performance degradation caused by modifying implicit parameters
- Is it safe to open a securities account by downloading the qiniu app? Is there a risk?
- Greedy distribution problem (2)
- 获取当前所在周的起始和结束的日期
- A case of misuse of append
- Grafana report display of sentinel based high availability current limiting system
- . Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
- 5 minutes to quickly launch web applications and APIs (vercel)
- 一个spark app demo
- Redis big key problem
猜你喜欢

MySQL master-slave synchronization and its basic process of database and table division

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试

Introduction to database access tools

2020-12-04

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

2021-08-21

SOA Service Oriented Architecture

In the third week of June, the main growth ranking list (BiliBili platform) of station B single feigua data up was released!

How to quickly build an enterprise knowledge base at low cost?

leetcode. 11 --- container with the most water
随机推荐
A spark app demo
Why is yuancosmos so popular? Is the 10trillion yuan shouted by the market boasting or the truth?
Technology cloud report: East to West computing is not only about "computing", but also needs "new storage"
China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users
Spark RDD Programming Guide(2.4.3)
2021-04-16
Spark RDD Programming Guide(2.4.3)
How to change the dial on the apple Watch
MySQL multi table operation
2021-08-21
Pycharm configuring remote connection server development environment
Greedy interval problem (3)
R language data preprocessing, converting type variables into factor variables, converting data sets into H2O format, and dividing data sets (training set, test set, verification set)
2021-05-02
Reasons for the failure of digital transformation and the way to success
Core and semiconductor "RF eda/ filter design platform" shines ims2022
A SQL optimization case using order by and rownum
Using the hbuilder x editor to install a solution for terminal window plug-ins that are not responding
Relationship between adau1452 development system interface and code data
Do not know how to choose the development of digital currency wallet?