vue项目中ESLint配置

2023-05-16

1.Eslint是用来干嘛的

ESLint最初是由Nicholas C. Zakas 于2013年6月创建的开源项目。它的目标是提供一个插件化的javascript代码检测工具。
在这里插入图片描述

2.为什么要使用Eslint

ESLint 是一个开源的 JavaScript 代码检查工具,。代码检查是一种静态的分析,常用于寻找有问题的模式或者代码,并且不依赖于具体的编码风格。对大多数编程语言来说都会有代码检查,一般来说编译程序会内置检查工具。
代码千万行,安全第一行;前端不规范,同事两行泪

  1. 统一团队编码规范(命名,众多格式等)
  2. 统一语法,毕竟es版本已经不少了(var/let…)
  3. 减少git不必要的提交(如果文件格式不一样,也会被git提交的)
  4. 避免低级错误 在编译时检查语法,而不是等JS引擎运行时才检查

3.手动下载配置(原理)

3.1 创建项目
创建一个测试文件夹:eslint_test
初始化项目:npm init -y (创建 package.json)
3.2 ESLint安装
直接在项目中安装eslint包 npm i eslint -D
注意安装结果:node_moduels 中下载了很多包

  • .bin/eslint.cmd 脚本文件,内部通过 nodejs 执行 eslint运行包 的代码
    在这里插入图片描述

  • @eslint包 用来规范eslint配置文件
    在这里插入图片描述

  • eslint开头的包 是 eslint运行包,包含eslint代码
    在这里插入图片描述
    3.3 生成ESLint配置文件
    使用 npx 来执行 (推荐):npx eslint --init
    在这里插入图片描述
    执行结果
    在这里插入图片描述
    在这里插入图片描述
    3.6 ESLint执行

  • 命令:npx eslint ./需要检查语法的文件路径

  • 如果违法规范,会将错误提示到 终端,说明 eslint 工作正常

在这里插入图片描述

4.ESLint配置文件入门

在这里插入图片描述
4.1 env节点

"env": {
    "browser": true,
    "commonjs": true,
    "es2021": true
}

由于 ESLint 的各种规范中,一般都不允许使用未在页面内声明的成员,所以告诉eslint,当前代码运行在那些环境中。
4.2 globals节点

当访问当前源文件内未定义的变量时,no-undef 规则将发出警告。如果你想在一个源文件里使用全局变量,推荐你在 ESLint 中定义这些全局变量,这样 ESLint 就不会发出警告了。你可以使用注释或在配置文件中定义全局变量。

 "globals": {
  "_": true  // 可以读取,可以修改
  "$": false // 可以读取,不能修改
 }

可以使用 globals 节点来手动配置全局成员
4.3 extends 节点
通过这个节点可以配置使用 内置规范 还是 第三方规范

"extends": [
  "standard" // "eslint-config-standard"
 ]

注意:配置 extends时,可以省略 eslint-config-,直接写成 standard
4.4 parserOptions 节点
ESLint 解析器 解析代码时,可以指定 用哪个 js 的版本

 "parserOptions": {
  "ecmaVersion": 12
 }

注意:这里是指定 检查时按照哪个js版本语法检查,但这里不包含 全局变量
4.5 rules 节点

 "rules": {
 }

两种用法:

  1. 不使用 extend 节点 配置 整套的规范,而是在 rules节点中直接配置
  2. 使用 extend 节点 配置 整套的规范,在 rules节点中修改部分规范的配置

5.ESLint检查语法的规则

5.1 ESLint语法规范本质
本质就是函数
我们可以通过 看 ESLint 源码查看:

  • eslint 内置285个规则,每条规则 都是一个 create函数
  • 在进行语法检查时,会将代码转成另一种

6. 语法规范包类型

a. ESLint内置规范包 :eslint-all / eslint-recommended

b. 标准规范包:eslint-config-standard

c. 第三方规范包(google/airbnb/facebook…)

6.1 内置规范包

已经随eslint一起下载:
eslint-all:使用全部280多个规则
eslint-recommended:只使用推荐的60个规则

6.2 标准规范包(需要下载)
包名:eslint-config-standard也使用了200多个规则
手动下载:
a. 查看需要下载的包和版本 npm info "eslint-config-airbnb-base@latest" peerDependencies
b.下载 相关包 npx install-peerdeps --dev eslint-config-airbnb-base

7. 配置规范包

7.1 修改 eslint 配置文件

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "es2021": true
    },
    // "extends": "eslint:all", // 内置:所有规则
    // "extends": "eslint:recommended", // 内置:推荐规则
    "extends": "eslint-config-standard", // 第三方:标准规则
    // "extends": "eslint-config-airbnb-base", // 第三方:airbnb公司规则
    "parserOptions": {
        "ecmaVersion": 12
    },
    "rules": {
    }
};

7.2 运行ESLint
注意:ESLint 默认只检查 js 文件代码,如果想 检查 vue 或 react文件,需要装其它插件包

npx eslint ./index.js // 检查 目标文件
npx eslint ./src // 检查 目标文件夹 中所有js文件

7.3 测试不同包检查相同代码
一段相同代码
在这里插入图片描述
在这里插入图片描述
注意:自己平时写代码还是要开启严格模式,要是去公司还得按照公司规范书写。

下面是我个人在项目中经常使用配置:
package.json

"devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.15",
    "@vue/cli-plugin-eslint": "^5.0.0",
    "@vue/cli-plugin-router": "~4.5.15",
    "@vue/cli-plugin-vuex": "~4.5.15",
    "@vue/cli-service": "~4.5.15",
    "babel-eslint": "^10.1.0",
    "eslint": "^7.22.0",
    "eslint-config-tpconfig": "^0.3.3",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-vue": "^7.7.0"
  },

.eslintrc.js

module.exports = {
    root: true,
    parserOptions: {
        parser: 'babel-eslint',
        sourceType: 'module'
    },
    env: {
        browser: true,
        node: true,
        es6: true,
        jquery: true
    },
    extends: ['plugin:vue/recommended', 'eslint:recommended'],
    rules: {
        'vue/no-mutating-props': 0, // 先不管对props的修改
        'vue/max-attributes-per-line': [
            2,
            {
                singleline: 20,
                multiline: {
                    max: 10,
                    allowFirstLine: false
                }
            }
        ],
        'vue/singleline-html-element-content-newline': 'off',
        'vue/html-self-closing': [
            'error',
            {
                html: {
                    void: 'always',
                    normal: 'never',
                    component: 'never'
                },
                svg: 'any',
                math: 'any'
            }
        ],
        'vue/multiline-html-element-content-newline': 'off',
        'vue/name-property-casing': ['error', 'PascalCase'],
        'vue/no-v-html': 'off',
        'vue/html-indent': ['error', 4, { baseIndent: 1 }],
        'vue/script-indent': ['error', 4, { baseIndent: 0 }],
        'accessor-pairs': 2,
        'arrow-spacing': [
            2,
            {
                before: true,
                after: true
            }
        ],
        'block-spacing': [2, 'always'],
        'brace-style': [
            2,
            '1tbs',
            {
                allowSingleLine: true
            }
        ],
        camelcase: [
            0,
            {
                properties: 'always'
            }
        ],
        'comma-dangle': [2, 'never'],
        'comma-spacing': [
            2,
            {
                before: false,
                after: true
            }
        ],
        'comma-style': [2, 'last'],
        'constructor-super': 2,
        curly: [2, 'multi-line'],
        'dot-location': [2, 'property'],
        'eol-last': 2,
        eqeqeq: ['error', 'always', { null: 'ignore' }],
        'generator-star-spacing': [
            2,
            {
                before: true,
                after: true
            }
        ],
        'handle-callback-err': [2, '^(err|error)$'],
        'jsx-quotes': [2, 'prefer-single'],
        'key-spacing': [
            2,
            {
                beforeColon: false,
                afterColon: true
            }
        ],
        'keyword-spacing': [
            2,
            {
                before: true,
                after: true
            }
        ],
        'new-cap': [
            2,
            {
                newIsCap: true,
                capIsNew: false
            }
        ],
        'new-parens': 2,
        'no-array-constructor': 2,
        'no-caller': 2,
        'no-class-assign': 2,
        'no-cond-assign': 2,
        'no-const-assign': 2,
        'no-control-regex': 0,
        'no-delete-var': 2,
        'no-dupe-args': 2,
        'no-dupe-class-members': 2,
        'no-dupe-keys': 2,
        'no-duplicate-case': 2,
        'no-empty-character-class': 2,
        'no-empty-pattern': 2,
        // 'no-eval': 2,
        'no-ex-assign': 2,
        'no-extend-native': 2,
        'no-extra-bind': 2,
        'no-extra-boolean-cast': 2,
        'no-extra-parens': [2, 'functions'],
        'no-fallthrough': 2,
        'no-floating-decimal': 2,
        'no-func-assign': 2,
        'no-implied-eval': 2,
        'no-inner-declarations': [2, 'functions'],
        'no-invalid-regexp': 2,
        'no-irregular-whitespace': 2,
        'no-iterator': 2,
        'no-label-var': 2,
        'no-labels': [
            2,
            {
                allowLoop: false,
                allowSwitch: false
            }
        ],
        'no-lone-blocks': 2,
        'no-mixed-spaces-and-tabs': 2,
        'no-multi-spaces': 2,
        'no-multi-str': 2,
        'no-multiple-empty-lines': [
            2,
            {
                max: 1
            }
        ],
        'no-native-reassign': 2,
        'no-negated-in-lhs': 2,
        'no-new-object': 2,
        'no-new-require': 2,
        'no-new-symbol': 2,
        'no-new-wrappers': 2,
        'no-obj-calls': 2,
        'no-octal': 2,
        'no-octal-escape': 2,
        'no-path-concat': 2,
        'no-proto': 2,
        'no-redeclare': 2,
        'no-regex-spaces': 2,
        'no-return-assign': [2, 'except-parens'],
        'no-self-assign': 2,
        'no-self-compare': 2,
        'no-sequences': 2,
        'no-shadow-restricted-names': 2,
        'no-spaced-func': 2,
        'no-sparse-arrays': 2,
        'no-this-before-super': 2,
        'no-throw-literal': 2,
        'no-trailing-spaces': 2,
        'no-undef': 2,
        'no-undef-init': 2,
        'no-unexpected-multiline': 2,
        'no-unmodified-loop-condition': 2,
        'no-unneeded-ternary': [
            2,
            {
                defaultAssignment: false
            }
        ],
        'no-unreachable': 2,
        'no-unsafe-finally': 2,
        'no-unused-vars': [
            2,
            {
                vars: 'all',
                args: 'none'
            }
        ],
        'no-useless-call': 2,
        'no-useless-computed-key': 2,
        'no-useless-constructor': 2,
        'no-useless-escape': 0,
        'no-whitespace-before-property': 2,
        'no-with': 2,
        'one-var': [
            2,
            {
                initialized: 'never'
            }
        ],
        'operator-linebreak': [
            2,
            'after',
            {
                overrides: {
                    '?': 'before',
                    ':': 'before'
                }
            }
        ],
        'padded-blocks': [2, 'never'],
        quotes: [
            2,
            'single',
            {
                avoidEscape: true,
                allowTemplateLiterals: true
            }
        ],
        semi: [2, 'always'],
        'semi-spacing': [
            2,
            {
                before: false,
                after: true
            }
        ],
        'space-before-blocks': [2, 'always'],
        'space-before-function-paren': [2, 'never'],
        'space-in-parens': [2, 'never'],
        'space-infix-ops': 2,
        'space-unary-ops': [
            2,
            {
                words: true,
                nonwords: false
            }
        ],
        'spaced-comment': [
            2,
            'always',
            {
                markers: ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']
            }
        ],
        'template-curly-spacing': [2, 'never'],
        'use-isnan': 2,
        'valid-typeof': 2,
        'wrap-iife': [2, 'any'],
        'yield-star-spacing': [2, 'both'],
        yoda: [2, 'never'],
        'prefer-const': 2,
        'object-curly-spacing': [
            2,
            'always',
            {
                objectsInObjects: false
            }
        ],
        'array-bracket-spacing': [2, 'never'],
        strict: 2, // 使用严格模式
        'max-statements': [2, 50, { ignoreTopLevelFunctions: true }],
        'no-console': process.env.NODE_ENV === 'production' ? 0 : 0,
        'no-debugger': process.env.NODE_ENV === 'production' ? 0 : 0
    },
    globals: { _: true, Base64: true, axios: true, BMap: true }
};

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue项目中ESLint配置 的相关文章

随机推荐

  • 最新解决git拉取远程仓库失败问题:Failed to connect to github.com port 443: Timed out.

    最新解决git拉取远程仓库失败问题 xff1a Failed to connect to github com port 443 Timed out 本地git拉取 pull 或抓取 fetch 远程github仓库出现 Failed to
  • 回溯算法及剪枝

    回溯算法及剪枝 理论基础模板框架实例思路 剪枝 回溯算法的本质是暴力穷举 xff0c 即使用递归控制for循环嵌套的数量 xff0c 本身不是一个高效的算法 尽管可以使用剪枝来提高效率 xff0c 但是还是改不了穷举的本质 回溯法 xff0
  • MySQL索引的底层实现原理

    索引的底层实现原理 数据库索引是存储在磁盘上的 xff0c 当数据量大时 xff0c 就不能把整个索引全部加载到内存了 xff0c 只能逐一加载每一个磁盘块 xff08 对应索引树的节点 xff09 xff0c 索引树越低 xff0c 越
  • MySQL事务

    事务概念 一个事务是由一条或者多条对数据库操作的SQL语句所组成的一个不可分割的单元 xff0c 只有当事务中的所有操作都正常执行完啦 xff0c 整个事务才会被提交给数据库 xff1b 如果有部分事务处理失败 xff0c 那么事务就要回退
  • MySQL行锁、表锁&间隙锁

    事务隔离级别的实现原理 xff1a 锁 表级锁 amp 行级锁 表级锁 xff1a 对整张表加锁 开销小 xff0c 加锁快 xff0c 不会出现死锁 xff1b 锁粒度大 xff0c 发生锁冲突的概率高 xff0c 并发度低 行级锁 xf
  • sql模糊查询多个条件写法

    单个模糊查询一般使用like xff0c 如果多个可以使用 OR 进行连接 xff0c 不过写样子写法很冗余 xff0c 而且如果多个条件是从表中 select出来的时候这种方法就不可行了 针对这种问题 xff0c 一般都提供了正则表达式的
  • Python datetime.fromtimestamp 遇到的一些坑

    背景 xff1a 调用腾讯某个接口返回的是时间戳的形式 xff0c 本地解析的时间跟腾讯端的时间不一致 xff0c 经过排查发现是本地没有转化为北京时间 xff0c 而腾讯端是默认转换为北京时间的 但是却有一个疑惑 x1f914 xff0c
  • 【ClickHouse】批量写入ClickHouse 的几种方式

    ClickHouse没有官方的Python接口 xff0c 有个第三方的库 xff0c 叫clickhouse driver xff0c 笔者所知道的将数据批量写入的方式不是很多 xff0c 这里列举最常见的3种方式 第一种方式 CSV文件
  • 【redis】redis简单操作(待更新。。)

    span class token keyword import span redis span class token comment 导入redis 模块 span pool span class token operator 61 sp
  • js--客户端存储问题

    1 sessionstorage 2 localstorage 3 例子 xff1a 存储名字 lt body gt lt input type 61 34 text 34 id 61 34 name 34 gt lt input type
  • 【Python】python操作mongo的简单示例(待更新。。)

    span class token comment usr bin python3 span span class token keyword import span pymongo myclient span class token ope
  • 【Linux】清理升级缓存以及无用包

    span class token function sudo span span class token function apt get span autoclean span class token function sudo span
  • jsp 与 servlet 之间传值

    jsp gt servlet 1 input jsp 定义name lt input type 61 34 text 34 name 61 34 cardnum 34 id 61 34 cardnum 34 gt servlet 通过获取
  • 关于oracle表空间不足原因及处理方法

    oracle表空间不足错误代码 xff1a ORA 01688 unable to extend table 等 xff1b 查看剩余表空间的大小 xff1a SELECT UPPER F TABLESPACE NAME 34 表空间名 3
  • 剪枝操作——回溯法的限界思想及其应用:圆排列问题

    我相信人都有自尊 xff0c 而尊重别人的自尊是一种及其高尚的精神 社会中有很多想不到的惨绝人寰的事情 xff0c 有时候发生到了自己身上 有时候发生在自己最亲密的人身上 有时候发生在自己周围人的身上等 这些无不露出一些类似于心理变态vs心
  • Archlinux安装(BIOS)教程

    Archlinux安装 xff08 BIOS xff09 记录一下 建议参考 xff1a ArchWiki 官方安装指导 下载镜像 官网 制作U盘启动盘 rufus 插入U盘 进入BIOS启动U盘 xff0c 选择第一个 连接wifi sp
  • 深度优先 求 图中强连通子图的个数

    说明 输入矩阵形式的图 xff0c matrix i j 值为 1 说明边 i 与边 j 相连 定义一个 visited 的 Boolean 数组 xff0c 为 true 表示此边已经访问过 算法时间复杂度 xff1a n的平方 可以优化
  • 解决办法:Ubuntu 16.04 【缺少依赖】导致出现该错误——ERROR: the following packages/stacks could not have their

    解决办法 xff1a Ubuntu 16 04 缺少依赖 导致出现该错误 ERROR the following packages stacks could not have their rosdep keys resolved to sy
  • Caused by: java.lang.NumberFormatException: For input string: “undefined“

    做的项目是前后端分离的 xff0c 刚完成一个列表功能 xff0c 写好了前端代码 xff0c 启动后端后 xff0c 前端没有报错 xff0c 但是在浏览器中控制台不印不出来 xff0c 显示如图 请求失败 xff01 xff01 xff
  • vue项目中ESLint配置

    1 Eslint是用来干嘛的 ESLint最初是由Nicholas C Zakas 于2013年6月创建的开源项目 它的目标是提供一个插件化的javascript代码检测工具 2 为什么要使用Eslint ESLint 是一个开源的 Jav