vscode 配置 eslint 自动格式化问题;或者项目根目录配置.esliintc.js

news/2024/7/10 1:11:23 标签: vscode, vue, eslint, .esliintc.js

前端合作开发有个很严重的问题,就是大多数人的开发风格是不太一样的,那么合作开发时如何保持统一呢?

我们可以借助eslint,开发代码尽可能的规范化,那么风格自然也就大同小异了。

vscode__4">vscode 商店

首选去vscode商店,安装esLint
在这里插入图片描述

一、引入 Eslint

npm 引入

npm install --save-dev babel-eslint eslint eslint-friendly-formatter eslint-loader eslint-plugin-html eslint-config-standard eslint-plugin-promise  eslint-plugin-standard
eslint-plugin-import eslint-plugin-node eslint-plugin-flow-vars eslint-plugin-react eslint-config-vue eslint-plugin-vue

vue-cli 默认配置

vue create eslint-demo

在这里插入图片描述

vscode_27">二、配置vscode

引入eslint发现开发的时候,虽然规范了代码,但是每当保存时,稍不注意控制台就一堆警告⚠️,那么如何可以让编辑器自动格式化呢?

a、cdoe --> 首选项 --> 设置

在这里插入图片描述

b、进入setting.json,并配置

在这里插入图片描述

//setting.json
{
  "files.associations": {
    "*.cjson": "jsonc",
    "*.wxss": "css",
    "*.wxs": "javascript"
  },
  "emmet.includeLanguages": {
    "wxml": "html"
  },
  "minapp-vscode.disableAutoConfig": true,
  "[vue]": {
    "editor.defaultFormatter": "octref.vetur"
  },
  "[javascript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features"
  },
  "window.zoomLevel": 0,
  "diffEditor.ignoreTrimWhitespace": false,
  "eslint.alwaysShowStatus": true,
  "eslint.options": {
    "extensions": [
      ".js",
      ".vue"
    ]
  },
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "vue",
    "html",
    "typescriptreact",
    "typescript"
    // {
    //   "language": "vue",
    //   "autoFix": true
    // }
    // {
    //   "language": "typescript",
    //   "autoFix": true
    // }
  ],
  "eslint.run": "onSave",
  // "eslint.autoFixOnSave": true,
  "editor.formatOnSave": false,
  "eslint.enable": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.codeAction.showDocumentation": {
    "enable": true
  },
  "gitlens.gitCommands.closeOnFocusOut": true,
  "editor.fontSize": 14,
  "git.confirmSync": false,
  "editor.tabSize": 2,
  "editor.suggestSelection": "first",
  "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
  "sonarlint.rules": {
    "javascript:S1128": {
      "level": "off"
    },
    "javascript:S1763": {
      "level": "off"
    }
  },
  "[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
  },
  "leek-fund.immersiveBackground": true,
  "leek-fund.stockSort": 0,
  "leek-fund.stocksRemind": {},
  "extensions.ignoreRecommendations": true,
  "editor.minimap.enabled": false,
  "breadcrumbs.enabled": false,
}

三、项目根目录配置.esliintc.js,没有的新建一下

默认执行.esliintc.js文件,没有执行vscode 的配置

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

module.exports = {

  //此项是用来告诉eslint找当前配置文件不能往父级查找
  root: true,

  //此项是用来指定eslint解析器的,解析器必须符合规则,babel-eslint解析器是对babel解析器的包装使其与ESLint解析
  parser: 'babel-eslint',

  //此项是用来指定javaScript语言类型和风格,sourceType用来指定js导入的方式,默认是script,此处设置为module,指某块导入方式
  parserOptions: {
    // 设置 script(默认) 或 module,如果代码是在ECMASCRIPT中的模块
    sourceType: 'module',
    "ecmaVersion": 6,
    "ecmaFeatures": {
      "jsx": true
    }
  },

  // 此项指定环境的全局变量,下面的配置指定为浏览器环境
  env: {
    "browser": true,
    "node": true,
    "commonjs": true,
    "es6": true,
    "amd": true
  },
  // https://github.com/standard/standard/blob/master/docs/RULES-en.md
  // 此项是用来配置标准的js风格,就是说写代码的时候要规范的写,如果你使用vs-code我觉得应该可以避免出错
  extends: 'vue',
  // 此项是用来提供插件的,插件名称省略了eslint-plugin-,下面这个配置是用来规范html的
  plugins: [
    'html',
    "flow-vars", 
    "react"
  ],
  /* 
   下面这些rules是用来设置从插件来的规范代码的规则,使用必须去掉前缀eslint-plugin-
    主要有如下的设置规则,可以设置字符串也可以设置数字,两者效果一致
    "off" -> 0 关闭规则
    "warn" -> 1 开启警告规则
    "error" -> 2 开启错误规则
  */
  rules: {
    // 不需要
    "space-before-function-paren": 0,  // 函数定义时括号前面要不要有空格
    "eol-last": 0,  // 文件以单一的换行符结束
    "no-extra-semi": 0, // 可以多余的冒号
    "semi": 0,  // 语句可以不需要分号结尾
    "eqeqeq": 0, // 必须使用全等
    "one-var": 0, // 连续声明
    "no-undef": 1, // 可以 有未定义的变量

    // 警告
    "no-extra-boolean-cast": 1, // 不必要的bool转换
    "no-extra-parens": 1, // 非必要的括号
    "no-empty": 1, // 块语句中的内容不能为空
    "no-use-before-define": [1, "nofunc"], // 未定义前不能使用
    "complexity": [1, 10], // 循环复杂度
    "no-unused-vars": 1, // 不能有声明后未被使用的变量或参数
    // vue
    "flow-vars/define-flow-type": 1,
    "flow-vars/use-flow-type": 1,

    // react
    "react/jsx-uses-react": 2,
    "react/jsx-uses-vars": 2,

    // 错误
    "comma-dangle": [2, "never"], // 对象字面量项尾不能有逗号
    "no-debugger": 2, // 禁止使用debugger
    "no-constant-condition": 2, // 禁止在条件中使用常量表达式 if(true) if(1)
    "no-dupe-args": 2, // 函数参数不能重复
    "no-dupe-keys": 2, // 在创建对象字面量时不允许键重复 {a:1,a:1}
    "no-duplicate-case": 2, // switch中的case标签不能重复
    "no-empty-character-class": 2, // 正则表达式中的[]内容不能为空
    "no-invalid-regexp": 2, // 禁止无效的正则表达式
    "no-func-assign": 2, // 禁止重复的函数声明
    "valid-typeof": 2,  // 必须使用合法的typeof的值
    "no-unreachable": 2, // 不能有无法执行的代码
    "no-unexpected-multiline": 2, // 避免多行表达式
    "no-sparse-arrays": 2, // 禁止稀疏数组, [1,,2]
    "no-shadow-restricted-names": 2, // 严格模式中规定的限制标识符不能作为声明时的变量名使用
    "no-cond-assign": 2, // 禁止在条件表达式中使用赋值语句
    "no-native-reassign": 2, // 不能重写native对象

    // 代码风格
    "no-else-return": 1, // 如果if语句里面有return,后面不能跟else语句
    "no-multi-spaces": 1, // 不能用多余的空格
    "key-spacing": [1, {  // 对象字面量中冒号的前后空格
      "beforeColon": false,
      "afterColon": true
    }],
    "block-scoped-var": 2, // 块语句中使用var
    "consistent-return": 2, // return 后面是否允许省略
    "accessor-pairs": 2, // 在对象中使用getter/setter
    "dot-location": [2, "property"], // 对象访问符的位置,换行的时候在行首还是行尾
    "no-lone-blocks": 2, // 禁止不必要的嵌套块
    "no-labels": 2, // 禁止标签声明
    "no-extend-native": 2, // 禁止扩展native对象
    "no-floating-decimal": 2, // 禁止省略浮点数中的0 .5 3.
    "no-loop-func": 2, // 禁止在循环中使用函数(如果没有引用外部变量不形成闭包就可以)
    "no-new-func": 2,  // 禁止使用new Function
    "no-self-compare": 2, // 不能比较自身
    "no-sequences": 2, // 禁止使用逗号运算符
    "no-throw-literal": 2, // 禁止抛出字面量错误 throw "error";
    "no-return-assign": [2, "always"], // return 语句中不能有赋值表达式
    "no-redeclare": [2, {   // 禁止重复声明变量
      "builtinGlobals": true
    }],
    "no-unused-expressions": [2, {  // 禁止无用的表达式
      "allowShortCircuit": true,
      "allowTernary": true
    }],
    "no-useless-call": 2, // 禁止不必要的call和apply
    "no-useless-concat": 2,
    "no-void": 2, // 禁用void操作符
    "no-with": 2, // 禁用with
    "space-infix-ops": 2, // 中缀操作符周围要不要有空格
    "valid-jsdoc": [2, { // jsdoc规则
      "requireParamDescription": true,
      "requireReturnDescription": true
    }],
    "no-warning-comments": [2, {  // 不能有警告备注
      "terms": ["todo", "fixme", "any other term"],
      "location": "anywhere"
    }],
    "curly": 1, // 必须使用 if(){} 中的{}

    // common js
    "no-duplicate-imports": 1
  }
}

http://www.niftyadmin.cn/n/1558103.html

相关文章

DeepMind新成果:不可解释的神经元并不比可解释神经元作用小

作者 | Debra编辑 | EmilyAI 前线导读:深度神经网络由许多单独的神经元组成,它们以复杂且违反直觉的方式组合起来,以解决各种具有挑战性的任务。这种复杂性赋予了神经网络神奇的力量,但也给它们带来了不好的名声:混乱、…

VuePress生成器,以md文件生成文档说明

开发一个自己的组件库,那么如何提供说明文档呢? 大家当然可以动手开发一个,但是代价无疑有点大,那么有什么速成的方法吗? 当然有!!!,VuePress就可以很好的实现我们的诉求…

Oracle集合

1 --union 并集2 select * from emp where ename like %A% union3 select * from emp where ename like %M%;4 --union all 集并 公共部分 会包含二次5 select * from emp where ename like %A% 6 union all 7 select * from emp where ename like %M%; 8 --interser…

ajax、axios、fetch区别

随着互联网的高速发展以及IT开发技术的升级,前后端分离已成为互联网项目开发的业界标准使用方式。 那么前后端如何通讯建立连接呢? 这时候就需要用到ajax、axios、fetch。 但是他们又有什么不同呢?我们一起来学习一下。 一、ajax学习 aja…

控制input只能输入数字和两位小数

<input type"text" name"je" οnkeyup"clearNoNum(this)" /> function clearNoNum(obj){ obj.value obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符 obj.value obj.value.replace(/^\./g…

mac 更新node,不要在去删除下载了,使用nvm 切换不同node版本

首先安装Homebrew 是一款Mac OS平台下的软件包管理工具&#xff0c;拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令&#xff0c;就可以实现包管理&#xff0c;而不用你关心各种依赖和文件路径的情况&#xff0c;十分方便快捷。 Homebrew的安装比较费劲&a…

修改Mac终端Terminal的提示文字,修改zshrc、bashrc

打开终端&#xff0c;我们会发现 左下角有一段丑丑的提示&#xff0c;大体意思是什么呢&#xff1f; 查看终端提示文字的环境变量 echo $PS1大家可以看到终端显示 \h:\W \u$ 那么如何可以修改一下呢&#xff1f; 毕竟主机名和用户名&#xff0c;其实我们并不太需要。 修改ba…