项目常用eslint配置(Vue/React/TypeScript)

更新日期: 2020-03-11阅读: 2.9k标签: eslint

记录一下常用的eslint配置。


vue项目常用eslint配置

需要安装依赖(Vue这里使用standard扩展和vue插件,所以需要安装)

{  
    "devDependencies": {
        "babel-eslint": "^10.0.2",
        "eslint": "^6.1.0",
        "eslint-config-imperative-es6": "^2.1.0",
        "eslint-config-standard": "^10.2.1",
        "eslint-plugin-import": "^2.17.2",
        "eslint-plugin-node": "^5.2.0",
        "eslint-plugin-promise": "^3.4.0",
        "eslint-plugin-standard": "^3.0.1",
        "eslint-plugin-vue": "^4.7.1" // vue插件
    }
}

.eslintrc.js文件配置

// https://eslint.org/docs/user-guide/configuring

module.exports = {
    // 默认情况下,ESLint 会在所有父级目录里寻找配置文件,一直到根目录。ESLint 一旦发现配置文件中有 "root": true,它就会停止在父级目录中寻找。
    root: true, 
    parserOptions: { // 解析器选项
        parser: 'babel-eslint' // 一个对Babel解析器的包装,使其能够与 ESLint 兼容
    },
    // 环境的全局变量
    env: {  
        browser: true,
        node: true,
        jquery: true
    },
    // 配置文件可以被基础配置中的已启用的规则继承。
    extends: [
        // https://github.com/guidesmiths/eslint-config-imperative-es6
        'imperative-es6',
        // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
        // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
        'plugin:vue/essential',
        // https://github.com/standard/standard/blob/master/docs/RULES-en.md
        'standard'  // 使用eslint-plugin-standard扩展(挺好用的
    ],
    // 配置插件名字,可以省略'eslint-plugin-'前缀。使用前要用npm安装。
    plugins: [
        'vue'
    ],
    // 添加规则。配置定义在插件中的一个规则的时候,你必须使用 插件名/规则ID 的形式。
    // 常用规则官网:http://eslint.cn/docs/rules/
    rules: {
        'indent': ['error', 4],
        'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
        'linebreak-style': 'off', // 强制使用一致的换行分隔 LF or CRLF
        'array-callback-return': 'off'
    }
    // 若要禁用一组文件的配置文件中的规则,请使用 overrides 和 files。例如:
    // overrides: 
    // [{
    //     "files": ["*-test.js","*.spec.js"],
    //     "rules": {
    //         "no-unused-expressions": "off"
    //     }
    // }]
}


react项目常用eslint配置

同样安装依赖,React这里使用的airbnb扩展。安装eslint-import-resolver-webpack用来解决webpack中设置的别名eslint无法识别报错的问题。

{  
    "devDependencies": {
        "babel-eslint": "^10.0.2",
        "eslint": "^6.1.0",
        "eslint-config-airbnb": "^18.0.1",
        "eslint-import-resolver-webpack": "^0.12.1",
        "eslint-plugin-import": "^2.20.1",
        "eslint-plugin-jsx-a11y": "^6.2.3",
        "eslint-plugin-node": "^11.0.0",
        "eslint-plugin-promise": "^4.2.1",
        "eslint-plugin-react": "^7.14.3"
    }
}

.eslintrc.js文件配置

module.exports={
    env: {
        browser: true,
        commonjs: true,
        node: true,
        es6: true
    },
    extends: [
        'airbnb' // airbnb扩展
    ],
    globals: {
        $: true,
        process: true,
        __dirname: true
    },
    parser: 'babel-eslint',
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        },
        sourceType: 'module'
    },
    plugins: [
        'react' // react插件
    ],
    // 用来处理webpack中指定别名但eslint未能识别报的错
    settings: {
        "import/resolver": {
            webpack: {
                config: './build/webpack.base.conf.js'  // 指定webpack配置文件路径
            }
        }
    },
    // https://github.com/yannickcr/eslint-plugin-react
    rules: {
        "no-console": 0, //不禁用console
        "no-irregular-whitespace": 0, //不规则的空白不允许
        "react/jsx-filename-extension": [1, {"extensions": [".js", ".jsx"]}],//文件是.js还是.jsx
        "no-underscore-dangle": 0,
        "array-bracket-spacing": [2, 'never'], // 指定数组的元素之间要以空格隔开(,后面)
        "comma-dangle": 2, // 数组和对象键值对最后一个逗号, never参数:不能带末尾的逗号, always参数:必须带末尾的逗号
    }
}

附上rules其他rules规则:

{
    "rules": {
        // off=0, warn=1, error=2, 如果是数组, 第二项表示参数option
        "indent": [2, 2], // 控制缩进为2
        "eqeqeq": 1,// 警告使用全等
        "quotes": [2, "single"], //单引号
        "no-console": 0, //不禁用console
        "no-debugger": 1, //警告debugger
        "no-var": 2, //对var禁止
        "semi": 2, //强制使用分号
        "semi-spacing": [2, {"before": false, "after": true}], // 强制分号前后不允许空格
        "no-irregular-whitespace": 0, //不规则的空白不允许
        "no-trailing-spaces": 'error', //一行结束后面有空格就发出警告
        "eol-last": 0, //文件以单一的换行符结束
        "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不能有声明后未被使用的变量或参数
        "no-underscore-dangle": 0, //标识符不能以_开头或结尾
        "no-alert": 2, //禁止使用alert confirm prompt
        "no-lone-blocks": 0, //禁止不必要的嵌套块
        "no-class-assign": 2, //禁止给类赋值
        "no-cond-assign": 2, //禁止在条件表达式中使用赋值语句
        "no-const-assign": 2, //禁止修改const声明的变量
        "no-delete-var": 2, //不能对var声明的变量使用delete操作符
        "no-dupe-keys": 2, //在创建对象字面量时不允许键重复
        "no-duplicate-case": 2, //switch中的case标签不能重复
        "no-dupe-args": 2, //函数参数不能重复
        "no-empty": 2, //块语句中的内容不能为空
        "no-func-assign": 2, //禁止重复的函数声明
        "no-invalid-this": 0, //禁止无效的this,只能用在构造器,类,对象字面量
        "no-redeclare": 2, //禁止重复声明变量
        "no-spaced-func": 2, //函数调用时 函数名与()之间不能有空格
        "no-this-before-super": 0, //在调用super()之前不能使用this或super
        "no-undef": 2, //不能有未定义的变量
        "no-use-before-define": 2, //未定义前不能使用
        "camelcase": 0, //强制驼峰法命名
        "jsx-quotes": [2, "prefer-double"], //强制在JSX属性(jsx-quotes)中一致使用双引号
        "react/display-name": 0, //防止在React组件定义中丢失displayName
        "react/forbid-prop-types": 0, //禁止某些propTypes
        "react/jsx-boolean-value": 0, //在JSX中强制布尔属性符号
        "react/jsx-closing-bracket-location": 0, //在JSX中验证右括号位置
        "react/jsx-curly-spacing": [2, {"when": "never", "children": true}], //在JSX属性和表达式中加强或禁止大括号内的空格。
        "react/jsx-indent": [2,2], // 语法缩进控制
        "react/jsx-indent-props": [2, 2], //验证JSX中的props缩进是否为2个
        "react/jsx-key": 2, //在数组或迭代器中验证JSX具有key属性
        "react/jsx-max-props-per-line": 0, // 限制JSX中单行上的props的最大数量
        "react/jsx-no-bind": 0, //JSX中不允许使用箭头函数和bind
        "react/jsx-no-duplicate-props": 2, //防止在JSX中重复的props
        "react/jsx-no-literals": 0, //防止使用未包装的JSX字符串
        "react/jsx-no-undef": 1, //在JSX中禁止未声明的变量
        "react/jsx-pascal-case": 0, //为用户定义的JSX组件强制使用PascalCase
        "react/jsx-sort-props": 0, //强化props按字母排序
        "react/jsx-uses-react": 1, //防止反应被错误地标记为未使用
        "react/jsx-uses-vars": 2, //防止在JSX中使用的变量被错误地标记为未使用
        "react/no-danger": 0, //防止使用危险的JSX属性
        "react/no-did-mount-set-state": 0, //防止在componentDidMount中使用setState
        "react/no-did-update-set-state": 1, //防止在componentDidUpdate中使用setState
        "react/no-direct-mutation-state": 2, //防止this.state的直接变异
        "react/no-multi-comp": 0, //防止每个文件有多个组件定义
        "react/no-set-state": 0, //防止使用setState
        "react/no-unknown-property": 2, //防止使用未知的dom属性
        "react/prefer-es6-class": 2, //为React组件强制执行ES5或ES6类
        "react/prop-types": 0, //防止在React组件定义中丢失props验证
        "react/react-in-jsx-scope": 2, //使用JSX时防止丢失React
        "react/self-closing-comp": 0, //防止没有children的组件的额外结束标签
        "react/sort-comp": 2, //强制组件方法顺序
        "no-extra-boolean-cast": 0, //禁止不必要的bool转换
        "react/no-array-index-key": 0, //防止在数组中遍历中使用数组key做索引
        "react/no-deprecated": 1, //不使用弃用的方法
        "react/jsx-equals-spacing": 2, //在JSX属性中强制或禁止等号周围的空格
        "no-unreachable": 1, //不能有无法执行的代码
        "comma-dangle": [2, "never"], //对象字面量项尾必须有逗号
        "no-mixed-spaces-and-tabs": 0, //禁止混用tab和空格
        "prefer-arrow-callback": 0, //比较喜欢箭头回调
        "arrow-parens": 0, //箭头函数用小括号括起来
        "arrow-spacing": [
            'error',
            {
                before: true,
                after: true
            }
        ],
        "prefer-const": ["error", {
            "destructuring": "all"
            }
        ],
        "prefer-destructuring": ["error", {
            "VariableDeclarator": {
                "array": false,
                "object": true
            },
            "AssignmentExpression": {
                "array": false,
                "object": false
            }
        }, {
            "enforceForRenamedProperties": false
            }
        ],
        "use-isnan": 2,//禁止比较时使用NaN,只能用isNaN()
        // @fixable 代码块如果在一行内,那么大括号内的首尾必须有空格,比如 function () { alert('Hello') }
        'block-spacing': [
            'error',
            'always'
        ],
        // @fixable 函数名和执行它的括号之间禁止有空格
        'func-call-spacing': [
            'error',
            'never'
        ],
        // @fixable if, function 等的大括号之前必须要有空格,比如 if (a) {
        'space-before-blocks': [
            'error',
            'always'
        ],
        // @fixable function 的小括号之前必须要有空格
        'space-before-function-paren': [
            'error',
            {
                anonymous: 'ignore',
                named: 'never',
                asyncArrow: 'always'
            }
        ],
        // @fixable 小括号内的首尾禁止有空格
        'space-in-parens': [
            'error',
            'never'
        ],
        // @fixable 操作符左右必须有空格,比如 let sum = 1 + 2;
        'space-infix-ops': 'error',
        // @fixable new, typeof 等后面必须有空格,++, -- 等禁止有空格,比如:
        // let foo = new Person();
        // bar = bar++;
        'space-unary-ops': [
            'error',
            {
                words: true,
                nonwords: false
            }
        ],
        'switch-colon-spacing': [
            'error',
            {
                after: true,
                before: false
            }
        ],
        'arrow-spacing': [
            'error',
            {
                before: true,
                after: true
            }
        ],
        'comma-spacing': [
            'error',
            {
                before: false,
                after: true
            }
        ],
        'keyword-spacing': [
            'error',
            {
                before: true,
                after: true
            }
        ]
    }
}


TypeScript+React项目常用eslint配置

需要安装依赖@typescript-eslint(前提是已经安装了typescript)

{  
    "devDependencies": {
        "@typescript-eslint/eslint-plugin": "^2.23.0",
        "@typescript-eslint/parser": "^2.23.0",
        "eslint": "^6.8.0",
        "eslint-config-airbnb-typescript": "^7.0.0",
        "eslint-import-resolver-webpack": "^0.12.1",
        "eslint-plugin-import": "^2.20.1",
        "eslint-plugin-jsx-a11y": "^6.2.3",
        "eslint-plugin-react": "^7.19.0",
        "eslint-plugin-react-hooks": "^1.7.0",`
    }
}

.eslintrc.js文件配置

module.exports = {
    root: true,
    parser: '@typescript-eslint/parser',
    parserOptions: {
        project: './tsconfig.json',
    },
    plugins: [
        '@typescript-eslint',
    ],
    extends: [
        'airbnb-typescript',
        'eslint:recommended',
        'plugin:@typescript-eslint/eslint-recommended',
        'plugin:@typescript-eslint/recommended',
    ],
    // 用来处理webpack中指定别名但eslint未能识别报的错
    settings: {
        "import/resolver": {
            webpack: {
                config: './build/webpack.base.conf.js'  // 指定webpack配置文件路径
            }
        }
    },
    rules: {
        "react/state-in-constructor": 0,
        "comma-dangle": [2, 'always-multiline'],
        "no-console": 0, //不禁用console
        "no-irregular-whitespace": 0, //不规则的空白不允许
        "no-underscore-dangle": 0,
        "array-bracket-spacing": [2, 'never'] // 指定数组的元素之间要以空格隔开(,后面)
    }
}

链接: https://www.fly63.com/article/detial/8216

你不知道的Vuejs - 使用ESLint检查代码质量

Javascript 是一门弱类型语言,所以语法检查变得尤为重要。因此对 Javascript 进行语法检查的工具应运而生,目前 ESLint 使用最为广泛。这篇将讲解如何将 ESLint 集成到我们的项目中。

vue中关于eslint,以及如何在vue项目中关闭或使用ESLint

ESLint是一个用来识别 ECMAScript 并且按照规则给出报告的代码检测工具,使用它可以避免低级错误和统一代码的风格。 由于每个人写代码的习惯都会有所不同,所以统一代码风格在团队协作中尤为重要。 这篇文章主要讲解如何在vue项目中关闭或使用ESLint

ESLint_JS代码检查工具

ESLint是一个JavaScript代码静态检查工具,可以检查JavaScript的语法错误,提示潜在的bug,可以有效提高代码质量,维持前端团队高度一致的编码风格。ESLint不但提供一些默认的规则,也提供用户自定义规则来约束所写的JavaScript代码。本文将详细介绍ESLint

Eslint规则_ESLint常见命令

详解 ESLint 规则,规范你的代码,ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误。

ESLint规则配置说明

有的时候多人开发,代码的风格,用的代码编辑器都各不相同,所以为了大家能保持一种统一的风格,ESLint就可以帮我们检查代码的格式,和风格。

vue-cli3 取消eslint 校验代码

vue create hello-world创建项目的时候选择了Linter / Formatter,所以写代码的时候会有代码规范检查,怎么才能关闭这个校验呢?

如何在 React 项目中整合 Eslint 和 Prettier?

首先,我们使用官方提供的脚手架 create-react-app 来创建一个项目:Eslint 是一个可以检验代码,并给出报告的工具。它的目标是保证代码的一致性,避免错误。Eslint 为我们提供了 ECMAScript/JavaScript 规范的代码校验

webpack插件ProvidePlugin的使用方法和eslint配置

配置 webpack.config.js文件里 plugins 属性,配置完以后就可以在代码里直接使用 _ ,而不再需要 import; 注意:(如果不配置eslint,浏览器就会报错:\\\\\\\'_\\\\\\\' is not defined no-undef)

什么是ESLint?

官网上告诉我们,ESLint 是一个用来识别 ECMAScript/JavaScript 并且按照规则给出报告的代码检测工具,哦,所以我们可以知道,ESLint 就是一个工具,而且是一个用来检查代码的工具。代码检查是一种静态的分析,常用于寻找有问题的模式或者代码

vue开发之代码规范eslint

ESLint不管是多人合作还是个人项目,代码规范都是很重要的。这样做不仅可以很大程度地避免基本语法错误,也保证了代码的可读性。

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!