VSCode格式化ESLint方法

背景:

由于在项目进行过程中,发现项目组有ESLint的代码规范,并在各项目中配置了ESLint

      导致使用vscode各种报错,如下图

解决方案:

1、安装插件

(1)ESLint:JavaScript代码格式化工具

(2)vetur:可以格式化html,标准css,标准js,vue文件

2、确认package.json中的配置项

这是前提,确认在package.json中安装了eslint依赖,不然配置无用

3、.eslinttrc.js文件配置

首先,在我们项目跟目录添加.eslintrc.js 文件,用于校验代码,编写eslint相关规则,关于eslint的一些具体规则,请查看eslint文档

下面列一下我们项目中常用的eslint规则:

  1 module.exports = {
  2   root: true,
  3   parser: ‘babel-eslint‘,
  4   parserOptions: {
  5   sourceType: ‘module‘,
  6   "allowImportExportEverywhere": true  //ignore eslint error: ‘import‘ and ‘export‘ may only appear at the top level
  7   },
  8   env: {
  9     browser: true,
 10     node: true,
 11     es6: true,
 12   },
 13   extends: ‘eslint:recommended‘,
 14   // required to lint *.vue files
 15   plugins: [
 16     ‘html‘,
 17     ‘vue‘
 18   ],
 19   // add your custom rules here
 20   //it is base on https://github.com/vuejs/eslint-config-vue
 21   ‘rules‘: {
 22     ‘accessor-pairs‘: 2,
 23     ‘arrow-spacing‘: [2, {
 24       ‘before‘: true,
 25       ‘after‘: true
 26     }],
 27     ‘block-spacing‘: [2, ‘always‘],
 28     ‘brace-style‘: [2, ‘1tbs‘, {
 29       ‘allowSingleLine‘: true
 30     }],
 31     ‘camelcase‘: [0, {
 32       ‘properties‘: ‘always‘
 33     }],
 34     ‘comma-dangle‘: [2, ‘never‘],
 35     ‘comma-spacing‘: [2, {
 36       ‘before‘: false,
 37       ‘after‘: true
 38     }],
 39     ‘comma-style‘: [2, ‘last‘],
 40     ‘constructor-super‘: 2,
 41     ‘curly‘: [2, ‘multi-line‘],
 42     ‘dot-location‘: [2, ‘property‘],
 43     ‘eol-last‘: 2,
 44     ‘eqeqeq‘: [2, ‘allow-null‘],
 45     ‘generator-star-spacing‘: [2, {
 46       ‘before‘: true,
 47       ‘after‘: true
 48     }],
 49     ‘handle-callback-err‘: [2, ‘^(err|error)$‘],
 50     ‘indent‘: [2, 2, {
 51       ‘SwitchCase‘: 1
 52     }],
 53     ‘jsx-quotes‘: [2, ‘prefer-single‘],
 54     ‘key-spacing‘: [2, {
 55       ‘beforeColon‘: false,
 56       ‘afterColon‘: true
 57     }],
 58     ‘keyword-spacing‘: [2, {
 59       ‘before‘: true,
 60       ‘after‘: true
 61     }],
 62     ‘new-cap‘: [2, {
 63       ‘newIsCap‘: true,
 64       ‘capIsNew‘: false
 65     }],
 66     ‘new-parens‘: 2,
 67     ‘no-array-constructor‘: 2,
 68     ‘no-caller‘: 2,
 69     ‘no-console‘: ‘off‘,
 70     ‘no-class-assign‘: 2,
 71     ‘no-cond-assign‘: 2,
 72     ‘no-const-assign‘: 2,
 73     ‘no-control-regex‘: 0,
 74     ‘no-delete-var‘: 2,
 75     ‘no-dupe-args‘: 2,
 76     ‘no-dupe-class-members‘: 2,
 77     ‘no-dupe-keys‘: 2,
 78     ‘no-duplicate-case‘: 2,
 79     ‘no-empty-character-class‘: 2,
 80     ‘no-empty-pattern‘: 2,
 81     ‘no-eval‘: 2,
 82     ‘no-ex-assign‘: 2,
 83     ‘no-extend-native‘: 2,
 84     ‘no-extra-bind‘: 2,
 85     ‘no-extra-boolean-cast‘: 2,
 86     ‘no-extra-parens‘: [2, ‘functions‘],
 87     ‘no-fallthrough‘: 2,
 88     ‘no-floating-decimal‘: 2,
 89     ‘no-func-assign‘: 2,
 90     ‘no-implied-eval‘: 2,
 91     ‘no-inner-declarations‘: [2, ‘functions‘],
 92     ‘no-invalid-regexp‘: 2,
 93     ‘no-irregular-whitespace‘: 2,
 94     ‘no-iterator‘: 2,
 95     ‘no-label-var‘: 2,
 96     ‘no-labels‘: [2, {
 97       ‘allowLoop‘: false,
 98       ‘allowSwitch‘: false
 99     }],
100     ‘no-lone-blocks‘: 2,
101     ‘no-mixed-spaces-and-tabs‘: 2,
102     ‘no-multi-spaces‘: 2,
103     ‘no-multi-str‘: 2,
104     ‘no-multiple-empty-lines‘: [2, {
105       ‘max‘: 1
106     }],
107     ‘no-native-reassign‘: 2,
108     ‘no-negated-in-lhs‘: 2,
109     ‘no-new-object‘: 2,
110     ‘no-new-require‘: 2,
111     ‘no-new-symbol‘: 2,
112     ‘no-new-wrappers‘: 2,
113     ‘no-obj-calls‘: 2,
114     ‘no-octal‘: 2,
115     ‘no-octal-escape‘: 2,
116     ‘no-path-concat‘: 2,
117     ‘no-proto‘: 2,
118     ‘no-redeclare‘: 2,
119     ‘no-regex-spaces‘: 2,
120     ‘no-return-assign‘: [2, ‘except-parens‘],
121     ‘no-self-assign‘: 2,
122     ‘no-self-compare‘: 2,
123     ‘no-sequences‘: 2,
124     ‘no-shadow-restricted-names‘: 2,
125     ‘no-spaced-func‘: 2,
126     ‘no-sparse-arrays‘: 2,
127     ‘no-this-before-super‘: 2,
128     ‘no-throw-literal‘: 2,
129     ‘no-trailing-spaces‘: 2,
130     ‘no-undef‘: 2,
131     ‘no-undef-init‘: 2,
132     ‘no-unexpected-multiline‘: 2,
133     ‘no-unmodified-loop-condition‘: 2,
134     ‘no-unneeded-ternary‘: [2, {
135       ‘defaultAssignment‘: false
136     }],
137     ‘no-unreachable‘: 2,
138     ‘no-unsafe-finally‘: 2,
139     ‘no-unused-vars‘: [2, {
140       ‘vars‘: ‘all‘,
141       ‘args‘: ‘none‘
142     }],
143     ‘no-useless-call‘: 2,
144     ‘no-useless-computed-key‘: 2,
145     ‘no-useless-constructor‘: 2,
146     ‘no-useless-escape‘: 0,
147     ‘no-whitespace-before-property‘: 2,
148     ‘no-with‘: 2,
149     ‘one-var‘: [2, {
150       ‘initialized‘: ‘never‘
151     }],
152     ‘operator-linebreak‘: [2, ‘after‘, {
153       ‘overrides‘: {
154         ‘?‘: ‘before‘,
155         ‘:‘: ‘before‘
156       }
157     }],
158     ‘padded-blocks‘: [2, ‘never‘],
159     ‘quotes‘: [2, ‘single‘, {
160       ‘avoidEscape‘: true,
161       ‘allowTemplateLiterals‘: true
162     }],
163     ‘semi‘: [2, ‘never‘],
164     ‘semi-spacing‘: [2, {
165       ‘before‘: false,
166       ‘after‘: true
167     }],
168     ‘space-before-blocks‘: [2, ‘always‘],
169     ‘space-before-function-paren‘: [2, ‘never‘],
170     ‘space-in-parens‘: [2, ‘never‘],
171     ‘space-infix-ops‘: 2,
172     ‘space-unary-ops‘: [2, {
173       ‘words‘: true,
174       ‘nonwords‘: false
175     }],
176     ‘spaced-comment‘: [2, ‘always‘, {
177       ‘markers‘: [‘global‘, ‘globals‘, ‘eslint‘, ‘eslint-disable‘, ‘*package‘, ‘!‘, ‘,‘]
178     }],
179     ‘template-curly-spacing‘: [2, ‘never‘],
180     ‘use-isnan‘: 2,
181     ‘valid-typeof‘: 2,
182     ‘wrap-iife‘: [2, ‘any‘],
183     ‘yield-star-spacing‘: [2, ‘both‘],
184     ‘yoda‘: [2, ‘never‘],
185     ‘prefer-const‘: 2,
186     ‘no-debugger‘: process.env.NODE_ENV === ‘production‘ ? 2 : 0,
187     ‘object-curly-spacing‘: [2, ‘always‘, {
188       objectsInObjects: false
189     }],
190     ‘array-bracket-spacing‘: [2, ‘never‘]
191   }
192 }

由于接手项目之前,项目组已经配置了相应的ESLint规则,所以我这边的操作是将上面代码中rule的代码全部放入之后,再将原先的ESLint配置项加在 rule 后面

4、配置settings.json文件

在其中加入:

 1 // 添加 vue 支持
 2 "eslint.validate": [
 3     "javascript",
 4     "javascriptreact",
 5     {
 6         "language": "vue",
 7         "autoFix": true
 8     }
 9 ],
10 //  #让prettier使用eslint的代码格式进行校验
11 "prettier.eslintIntegration": true,
12 "vetur.format.defaultFormatter.html": "js-beautify-html",
13 // #让vue中的js按编辑器自带的ts格式进行格式化
14 "vetur.format.defaultFormatter.js": "vscode-typescript",
15 "vetur.format.defaultFormatterOptions": {
16     "js-beautify-html": {
17         "wrap_attributes": "force-aligned"
18         // #vue组件中html代码格式化样式
19     }
20 },

之后就可以发现,这些报错冒红啦~~

让我们来修复它,点击快速修复

大功告成

最后跑一下项目看看

好的,终于没有error提示啦~~~

原文地址:https://www.cnblogs.com/haishen/p/10737655.html

时间: 2024-08-30 05:23:54

VSCode格式化ESLint方法的相关文章

Visual Studio Code 格式化ESlint 的方法

既然要格式化ESlint,就不得不先介绍一下什么是ESlint.后面再介绍格式化的方法 1.ESlint ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 JSLint.JSHint 相似,除了少数的例外: ESLint 使用 Espree 解析 JavaScript. ESLint 使用 AST 去分析代码中的模式 ESLint 是完全插件化的.每一个规则都是一个插件并且你可以在运行时添加更多

Python中格式化format()方法详解

Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参数使用{NUM}进行表示,0, 表示第一个参数,1, 表示第二个参数, 以后顺次递加; 使用":", 指定代表元素需要的操作, 如":.3"小数点三位, ":8"占8个字符空间等; 还可以添加特定的字母, 如: 'b' - 二进制. 将数字以2为基数

u盘格式化后数据能恢复吗,格式化数据恢复方法

u盘格式化后数据能恢复吗,格式化数据恢复方法.对于数据的存储设备来说,格式化的操作都是因为需要清理位置来另作他用,在格式化之前需要做好充分的备份工作的,如果说未备份的数据被格式化后该怎么恢复? 数据在被格式化的操作删除之后,是作永久删除操作了,所以想要恢复格式化的数据,一般的方式是不可能的, 下面呢笔者就来和各位细说下具体的恢复教程吧. 教程一:强力数据恢复软件 第一步:在电脑的常用浏览器上下载到强力数据恢复软件的安装包,之后完成后续的安装等操作,将U盘连接到电脑上.点击'立即体验'按钮运行软件

VSCODE 配置eslint规则和自动修复

VSCODE 配置eslint规则和自动修复 vscode安装以下插件 EsLint.vetur.Prettier - Code formatter vscode设置了添加配置项,,默认会去查找你项目中的eslint配置文件 { "workbench.iconTheme": "material-icon-theme", "explorer.confirmDragAndDrop": false, "explorer.confirmDele

让vscode按照eslint进行格式化

1.安装eslint依赖 在package.json中用npm安装eslint的依赖 "eslint": "^4.19.1", "eslint-friendly-formatter": "^4.0.1", "eslint-loader": "^2.0.0", "eslint-plugin-html": "^4.0.3", 2.生成eslint配置文

VSCode配置Vue自动格式化ESlint格式

安装插件Vetur,ESlint. 打开用户设置setting.json,将下面内容复制粘贴. { // tab 大小为2个空格 "editor.tabSize": 2, // 编辑器换行 "editor.wordWrap": "off", // 保存时格式化 "editor.formatOnSave": true, // 开启 vscode 文件路径导航 "breadcrumbs.enabled": tr

VScode 格式化代码保存时使用ESlint修复代码

前言 eslint  vs code 新买的电脑啊啊西 装VScode 配置格式化代码保存时使用ESlint修复代码头快炸了,不建议初学者用,太费时间了: 终于搞定---再也不要担心缩进,函数(名)和后面的括号之间没空格啦,代码末尾有分号了, 首先 下载插件  Prettier - Code formatter 然后 设置中配置settings.json 代码: { "editor.lineNumbers": "on", //开启行数提示 "editor.

完全离线安装VSCode插件--Eslint

最近折腾了一番,总算把Eslint插件在离线的情况下安装好了.之前查了挺多,但是很多方法还是在没有完全离线的情况下进行的.之所以想完全离线安装,主要是因为我们工作的地方是禁止访问外网的,所以像直接执行npm命令来进行一些包的安装,其实对我来说是用不了的. 好了,现在进入正题,说下我的解决方式吧. 1.在无法联网的电脑上先把VSCode安装好. 2.在家里有网的电脑上,在vsCode里面的扩展直接安装eslint插件,然后在该路径(C:\Users\Administrator\.vscode\ex

vscode中eslint插件的配置

用vue-cli构建vue项目,会有个eslint代码检测的安装 可vscode自带代码格式化是prettier格式(右键有格式化文件或alt+shift+f) 这时候要在vscode上装一个eslint插件 装完后在文件----->首选项------->设置里找到settings.json(快捷键忘了) eslint配置如下,(配置的·具体详情还有待研究,我也是网上copy的,不过亲测可用).保存配置后每次写.vue文件只要control+s保存,就可以把代码格式改成符合eslint标准 &