为自己的项目添加eslint并给vscode添加保存自动按照eslint格式化

4次阅读

共计 2720 个字符,预计需要花费 7 分钟才能阅读完成。

一、安装 eslint 相关依赖

`npm install eslint eslint-config-airbnb-base babel-eslint eslint-plugin-import eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-html --save-dev`

二、配置 eslint 文件

放在项目根目录下
.eslintrc.js 文件配置信息

module.exports = {
    env: {
        browser: true,
        node: true,
        es6: true,
    },
    plugins: [
        'react',
        'react-hooks',
    ],
    parser: 'babel-eslint', // include eslint-plugin-import
    parserOptions: {
        ecmaVersion: 7,
        sourceType: 'module',
        ecmaFeatures: {
            jsx: true,
            modules: true,
        }
    },
    extends: 'airbnb-base',
    rules: {
        // off
        'no-use-before-define': 0,
        'no-console': 0,
        'no-alert': 0,
        'no-plusplus': 0,
        'no-unused-expressions': 0,
        'func-names': 0,
        'eqeqeq': 0,
        'no-underscore-dangle': 0,
        'no-param-reassign': 0,
        'no-new': 0,
        'import/no-unresolved': 0,
        'import/no-extraneous-dependencies': 0,
        'linebreak-style': 0,
        'no-nested-ternary': 0,
        'arrow-body-style': 0,
        'prefer-const': 0,

        // warn
        'import/prefer-default-export': 1,
        'no-unused-vars': 1,

        // error
        indent: [2, 4, {
            SwitchCase: 1,
            VariableDeclarator: 1,
            outerIIFEBody: 1,
            FunctionDeclaration: {
                parameters: 1,
                body: 1,
            },
            FunctionExpression: {
                parameters: 1,
                body: 1,
            },
        }],
        'space-before-function-paren': [2, 'never'],
        'wrap-iife': [2, 'inside', { functionPrototypeMethods: true}],
        'max-len': [2, 120, 4, {
            ignoreUrls: true,
            ignoreComments: false,
            ignoreRegExpLiterals: true,
            ignoreStrings: true,
            ignoreTemplateLiterals: true,
        }],

        // react
        'react/jsx-uses-react': 2,
        'react/jsx-uses-vars': 2,
        'react-hooks/rules-of-hooks': 2,
        // 使用后会添加不希望出现的变量到依赖
        // 'react-hooks/exhaustive-deps': 1,

        // no debugger
        'no-debugger': 2,
    }
};

.eslintignore 文件配置信息

dist/*
**/node_modules/*

三、添加 vscode 设置

vscode 安装插件 eslint
ctrl+ P 输入 setting,打开 setting.json
添加以下参数

{
    // vscode 默认启用了根据文件类型自动设置 tabsize 的选项
    "editor.detectIndentation": false,
    // 重新设定 tabsize
    "editor.tabSize": 4,
    // #每次保存的时候自动格式化 
    "editor.formatOnSave": true,
    // #每次保存的时候将代码按 eslint 格式进行修复
    "eslint.autoFixOnSave": true,
    // 添加 vue 支持
    "eslint.validate": [
        "javascript",
        "javascriptreact",
        {
            "language": "vue",
            "autoFix": true
        }
    ],
    //  #让 prettier 使用 eslint 的代码格式进行校验 
    "prettier.eslintIntegration": true,
    //  #去掉代码结尾的分号 
    "prettier.semi": false,
    //  #使用带引号替代双引号 
    "prettier.singleQuote": true,
    //  #让函数 (名) 和后面的括号之间加个空格
    "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
    // #这个按用户自身习惯选择 
    "vetur.format.defaultFormatter.html": "js-beautify-html",
    // #让 vue 中的 js 按编辑器自带的 ts 格式进行格式化 
    "vetur.format.defaultFormatter.js": "vscode-typescript",
    "vetur.format.defaultFormatterOptions": {
        "js-beautify-html": {
            "wrap_attributes": "force-aligned"
            // #vue 组件中 html 代码格式化样式
        }
    },
    // 格式化 stylus, 需安装 Manta's Stylus Supremacy 插件"stylusSupremacy.insertColons": false, // 是否插入冒号"stylusSupremacy.insertSemicolons": false, // 是否插入分好"stylusSupremacy.insertBraces": false, // 是否插入大括号"stylusSupremacy.insertNewLineAroundImports": false, // import 之后是否换行"stylusSupremacy.insertNewLineAroundBlocks": false,"editor.codeActionsOnSave": {"source.fixAll.eslint": true},
    "files.autoSave": "off" // 两个选择器中是否换行
}

四、重启 vscode

重启 vscode 之后,测试保存有没有自动格式化。如果提示配置冲突,选 yes 就行

正文完
 0