zoukankan      html  css  js  c++  java
  • 项目常用eslint配置(Vue/React/TypeScript)

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

    记录一下常用的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'] // 指定数组的元素之间要以空格隔开(,后面)
        }
    }
    
  • 相关阅读:
    Example [mybatis] 的用法
    开发中可能会用到的几个 jQuery 小提示和技巧
    setInterval 与 clearInterval详解
    15个华丽的扁平风格登录界面
    原生js模拟jquery写法
    纯色扁平化网站
    javascript刷新页面的集中办法
    javascript闭包中循环问题
    20个实用javascript技巧及实践(二)
    20个实用的javascript技巧及实践(一)
  • 原文地址:https://www.cnblogs.com/yayoi/p/12448429.html
Copyright © 2011-2022 走看看