ESLint:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的29个中间版本)
第39行: 第39行:
npm install eslint-config-standard eslint-plugin-standard eslint-plugin-promise
npm install eslint-config-standard eslint-plugin-standard eslint-plugin-promise
eslint --init</syntaxhighlight>
eslint --init</syntaxhighlight>
== 配置文件 ==
ESLint 支持多种格式配置文件,同一目录下多个配置文件的优先级顺序如下:
# JavaScript - <code>.eslintrc.js</code>。
# YAML - <code>.eslintrc.yaml</code>/<code>.eslintrc.yml</code>。
# JSON - <code>.eslintrc.json</code>,允许 JavaScript 风格注释。
# Deprecated - <code>.eslintrc</code>,文本格式为 JSON/YAML。
# package.json - 创建 <code>eslintConfig</code> 属性。
初始化和生成.eslintrc.*文件。
{{Shc_out|<syntaxhighlight lang="bash">
eslint --init
</syntaxhighlight>|<syntaxhighlight lang="bash">
You can also run this command directly using 'npm init @eslint/config'.
Need to install the following packages:
  @eslint/create-config
Ok to proceed? (y) y
✔ How would you like to use ESLint? · problems
✔ What type of modules does your project use? · esm
✔ Which framework does your project use? · react
✔ Does your project use TypeScript? · No / Yes
✔ Where does your code run? · browser
✔ What format do you want your config file to be in? · YAML
The config that you've selected requires the following dependencies:
eslint-plugin-react@latest
✔ Would you like to install them now with npm? · No / Yes
Installing eslint-plugin-react@latest
Successfully created .eslintrc.yml file in /Users/kangjian/360/medici/client
</syntaxhighlight>|➜  client git:(master_sketch_version) ✗}}
=== IDE ===
vscode:
# 打开 extensions 搜索 ESLint 并安装。
# 点击 code 菜单,选中 setting/extensions,点击 edit in settings.json。
<syntaxhighlight lang="javascript">
{
    "eslint.codeAction.showDocumentation": {
        "enable": true
    },
    "eslint.format.enable": true,
    "eslint.options": {
    },
    "eslint.probe": [
        "javascript",
        "javascriptreact",
        "html"
    ],
}</syntaxhighlight>
== 命令 ==
== 命令 ==
初始化和生成.eslintrc.*文件
<syntaxhighlight lang="bash">
eslint --init</syntaxhighlight>
执行检查命令
执行检查命令
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
第79行: 第125行:
{| class="wikitable"
{| class="wikitable"
|-
|-
| [[/规则 |规则]]
| [[/规则|规则]] || [[/eslint-plugin-react|eslint-plugin-react]]
|}
|}


== 示例 ==
== 示例 ==
{| class="wikitable"
|-
| [[/提交指定文件|提交指定文件]] || 示例 || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
=== 我的配置示例 ===
{| class="wikitable"
|-
| [[/我的配置-前端-react|我的配置-前端-react]]
|}
== 技巧 ==
== 技巧 ==
{| class="wikitable"
|-
| [[/忽略校验|忽略校验]] || [[/全局变量|全局变量]] || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
== 修复代码 ==
{{hc|package.json|<syntaxhighlight lang="javascript">
  "scripts": {
    "lint": "eslint --fix --ext .js,.vue --ignore-path .gitignore .",
  },
</syntaxhighlight>}}
运行 <syntaxhighlight lang="bash">
npm run lint</syntaxhighlight> 修复代码样式
== 参数 ==
{| class="wikitable"
|-
|  --ignore-path || 指定忽略配置文件,配置此参数,则 <code>.eslintignore</code> 失效 || <syntaxhighlight lang="bash">
eslint –ignore-path .gitignore</syntaxhighlight>
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
== .eslintignore ==
[[/.eslintignore|.eslintignore]]
== error ==
== error ==
=== config ===
* ESLint: Expected indentation of 10 spaces but found 20 spaces.(
* ESLint: Expected indentation of 10 spaces but found 20 spaces.(
*: <syntaxhighlight lang="javascript">
*: <syntaxhighlight lang="javascript">
rules: {
rules: {
         indent: ["error", 4, { SwitchCase: 1 }],</syntaxhighlight>
         indent: ["error", 4, { SwitchCase: 1 }],</syntaxhighlight>
* ESLint:please specify Node.js interpreter correctly
*: webstorm 没有配置 nodejs 环境,我使用的是 wsl(windows subsystem for linux),所以没有找到命令
* eslint: please specify path to eslint package
*: <code>npm i eslint</code>
* ESLint: Unexpected side effect in "renderDataList" computed property.(vue/no-side-effects-in-computed-properties)
*: 此规则旨在防止在计算属性中产生副作用的代码,报错代码如下:
*: <syntaxhighlight lang="javascript">
renderDataList() {
  const HSF = this.list
  const dataFilters = HSF[0].filters
  this.isCountry = dataFilters[0].key
  this.isDepartment = dataFilters[1].key
  this.isCountryValue = dataFilters[0].options[0].value
  this.isDepartmentValue = dataFilters[1].options[0].value
  return {
    searchFilters: dataFilters
  }
}</syntaxhighlight>
*: 更改为如下
*: <syntaxhighlight lang="javascript">
computed: {
  renderDataList: {
    get() {
      const HSF = this.list
      if (!HSF[0].filters) {
        return []
      }
      return {
        searchFilters: HSF[0].filters
      }
    },
    set() {
      const param = this.renderDataList.searchFilters
      this.isCountry = param[0].key
      this.isDepartment = param[1].key
      this.isCountryValue = param[0].options[0].value
      this.isDepartmentValue = param[1].options[0].value
    }
  }
},</syntaxhighlight>
* Parsing error: setter must have exactly one formal parameter
*  'util.isArray' was deprecated since v4.0.0. Use 'Array.isArray()' instead.(node/no-deprecated-api)
* Arrow function has a complexity of 30. Maximum allowed is 20.(complexity)
*:
* Type of the default value for 'searchHotList' prop must be a function
*: <syntaxhighlight lang="javascript">
props: {
  searchHotList: {
    type: Array,
    default: function () { return [] }
  }
}</syntaxhighlight>
*: <syntaxhighlight lang="javascript">
props: {
  searchHotList: {
    type: Array,
    default: () => [] // es6的箭头函数
  }
}</syntaxhighlight>
* ESLint: Missing accessibility modifier on method definition constructor.(@typescript-eslint/explicit-member-accessibility)
*: 缺少可访问的修饰符
* Environment key "es2021" is unknown
* The eslint library loaded from /Users/kangjian/360/medici/client/node_modules/eslint/lib/api.js doesn't export a CLIEngine. You need at least eslint@1.0.0
*: {{Shc_out|<syntaxhighlight lang="bash">
which eslint</syntaxhighlight>|<syntaxhighlight lang="bash">
/usr/local/bin/eslint
</syntaxhighlight>|➜  client git:(master_sketch_version) ✗}}
=== react ===
* Component definition is missing display name
== 雁阵规则 ==
== 雁阵规则 ==