eslint使用

  • 在vscode的配置
  1. 下载eslint插件
  2. 下载eslint全局插件
js 复制代码
npm install -g eslint

// 执行命令
esllint --init // 根据需要选择即可
  1. 配置eslint
js 复制代码
"eslint.enable": true, // 启用/禁用ESLint。
    "eslint.alwaysShowStatus": true,
    "eslint.debug": true, // 启用ESLint的调试模式
    "eslint.format.enable": true, // 启用ESLint作为已验证文件的格式化程序 快速修复
    "eslint.lintTask.enable": true,
    "editor.codeActionsOnSave": {
        // "source.fixAll.eslint": true // 设置保存时自动更改eslint错误
    },
    "eslint.codeAction.showDocumentation": {
        "enable": true
    },
    "eslint.options": { //指定eslint配置文件位置
        "configFile": ".eslintrc.js" //指定项目根目录中的eslint配置文件
    },
    "eslint.validate": [
        "javascript", "javascriptreact", "html", "vue", "typescript", "jsx",
    ],
}

更多在vscode中关于eslint的配置可查看文档

  • 在项目中的配置 在根目录添加.eslintrc.js .eslintignore两个文件
js 复制代码
// 在eslintrc.js中 这是vue的 eslint --init 都会自带的有 
module.exports = {
    root: true,
    "env": {
        "browser": true,
        "es2020": true
    },
    "parserOptions": {
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "vue"
    ],
    'extends': [
        'plugin:vue/essential',
        '@vue/standard',
        "eslint:recommended",
        "plugin:vue/essential"
    ],
    rules: { // 对项目进行规则控制
        // allow async-await
        // 'generator-star-spacing': 'off',
        // allow debugger during development
        // 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
        // 'vue/no-parsing-error': [2, { 'x-invalid-end-tag': false }],
        // 'no-undef': 'off',
        // 'camelcase': 'off'
    },
    parserOptions: {
        parser: 'babel-eslint'
    }
};


// 在.eslintignore 中
/src/*.js // 写路径即可

在vue.config.js 中开启项目的eslint

js 复制代码
// Type: boolean | 'warning' | 'default' | 'error'
module.exports = {
  lintOnSave: process.env.NODE_ENV !== 'production',
  
  devServer: {//也可以通过设置让浏览器 overlay 同时显示警告和错误:
    overlay: {
      warnings: true,
      errors: true
    }
  } 
}

相关文档

  1. vue-loader.vuejs.org/zh/guide/li...
  2. eslint.org/docs/user-g...
  3. eslint.cn/
  4. github.com/vuejs/vue-d...
  5. github.com/ecmadao/Cod...

有错之处 还请不吝赐教 万分感谢

相关推荐
前端工作日常15 小时前
ESLint 配置深度解析:parserOptions、env 和 flowtype 的核心作用与实战指南
typescript·eslint
前端工作日常15 小时前
我学习到的ESLint 配置中root作用
eslint
angelQ6 天前
针对"@antfu/eslint-config": "^4.17.0"最新版本使用报错Unexpected token 'with'的解决方法
前端·eslint
jason_yang10 天前
代码规范-3大利器 prettier eslint husky
代码规范·eslint
拾光拾趣录14 天前
ESLint:从代码扫描到自动修复
前端·eslint
namehu18 天前
从 ESLint 到 Oxlint:一次提速百倍的前端 Lint 工具链升级实战
前端·javascript·eslint
光头程序员1 个月前
自定义 eslint 规则
eslint
non_hana2 个月前
一些 linter & formatter 配置最佳实践
typescript·node.js·eslint
锈儿海老师2 个月前
AST 工具大PK!Biome 的 GritQL 插件 vs. ast-grep,谁是你的菜?
前端·javascript·eslint
去伪存真2 个月前
提交规范靠吼没用,看我用“shell+husky螺丝刀”,一键给40多个项目上锁
前端·eslint