简体   繁体   English

如何让ESLint在Visual Studio代码中正确格式化和检查规则?

[英]How to get ESLint to format and check rules correctly in Visual Studio Code?

I've got node/npm/vue.js-cli installed with default website. 我在默认网站上安装了node / npm / vue.js-cli。

I have these two extensions installed: 我安装了这两个扩展:

在此输入图像描述

When I save, it checks ESlint rules and shows me errors: 当我保存时,它会检查ESlint规则并显示错误:

在此输入图像描述

The problem is, when I press CTRL-SHIFT-F to format the code, it (1) takes the space out from between the function name and the parentheses , but then when I save the file, ESLint (2) complains that the space is not there . 问题是,当我按CTRL-SHIFT-F格式化代码时,它(1) 从函数名和括号之间取出空格 ,但是当我保存文件时,ESLint(2) 抱怨空格不在那里

  1. How do I know what tool/extension is formatting my code? 我怎么知道什么工具/扩展名格式化我的代码?
  2. How do I know what tool/extension is checking rules when I save? 保存时如何知道检查规则的工具/扩展名是什么?
  3. How can I set these so that they are the same so that reformatting my code changes the code to rules that it checks when it saves? 如何设置它们以使它们相同,以便重新格式化我的代码会将代码更改为它在保存时检查的规则?

I was having same problem when I started dashboard building in vue.js. 当我在vue.js中启动仪表板构建时,我遇到了同样的问题。 Eventually I scraped internet for at least 2-3 hours. 最终我上网至少2-3个小时。 I found out one dev's code which had perfect configuration for .eslintrc file and package.json including all babel core packages. 我找到了一个开发代码,它有.eslintrc文件和package.json的完美配置,包括所有babel核心包。

.eslintrc file- .eslintrc文件 -

{
"env": {
    "browser": true
},
"plugins": [
    "html"
],
"settings": {
    "html/html-extensions": [".html", ".vue" ]
},
"parser": "babel-eslint",
"rules": {
    "array-bracket-spacing": [ "error", "always" ],
    "brace-style": [ "error", "1tbs" ],
    "comma-spacing": "error",
    "comma-style": "error",
    "computed-property-spacing": [ "error", "always" ],
    "constructor-super": "error",
    "dot-notation": "error",
    "eol-last": "error",
    "func-call-spacing": "error",
    "indent": [ "error", "tab", { "SwitchCase": 1 } ],
    "key-spacing": "error",
    "keyword-spacing": "error",
    "no-alert": "error",
    "no-bitwise": "error",
    "no-console": "error",
    "no-const-assign": "error",
    "no-debugger": "error",
    "no-dupe-args": "error",
    "no-dupe-class-members": "error",
    "no-dupe-keys": "error",
    "no-duplicate-case": "error",
    "no-duplicate-imports": "error",
    "no-else-return": "error",
    "no-eval": "error",
    "no-extra-semi": "error",
    "no-fallthrough": "error",
    "no-lonely-if": "error",
    "no-mixed-operators": "error",
    "no-mixed-spaces-and-tabs": "error",
    "no-multiple-empty-lines": [ "error", { "max": 1 } ],
    "no-multi-spaces": "error",
    "no-negated-in-lhs": "error",
    "no-nested-ternary": "error",
    "no-redeclare": "error",
    "no-shadow": "error",
    "no-undef-init": "error",
    "no-unreachable": "error",
    "no-unsafe-negation": "error",
    "no-use-before-define": [ "error", "nofunc" ],
    "no-useless-computed-key": "error",
    "no-useless-constructor": "error",
    "no-useless-return": "error",
    "no-var": "error",
    "no-whitespace-before-property": "error",
    "object-curly-spacing": [ "error", "always" ],
    "one-var": "off",
    "prefer-const": "error",
    "semi": "error",
    "semi-spacing": "error",
    "space-before-blocks": [ "error", "always" ],
    "space-before-function-paren": [ "error", "never" ],
    "space-in-parens": [ "error", "always" ],
    "space-infix-ops": [ "error", { "int32Hint": false } ],
    "space-unary-ops": [ "error", {
        "overrides": {
            "!": true
        }
    } ],
    "template-curly-spacing": [ "error", "always" ],
    "valid-jsdoc": [ "error", { "requireReturn": false } ],
    "valid-typeof": "error",
    "yoda": [0]
}
}

package.json file - package.json文件 -

{
"name": "theme1",
"version": "1.0.0",
"description": "A Vue.js project",
"author": "your email and name",
"private": true,
"scripts": {
  "dev": "node build/dev-server.js",
  "start": "node build/dev-server.js",
  "lint": "eslint .",
  "build": "node build/build.js"
},
"dependencies": {
  "vue": "^2.2.6",
  "vue-router": "^2.3.1",
  "vue-socket.io": "^2.1.1-a"
},
"devDependencies": {
  "autoprefixer": "^6.7.2",
  "babel-core": "^6.22.1",
  "babel-loader": "^6.2.10",
  "babel-eslint": "^6.1.2",
  "babel-plugin-transform-runtime": "^6.22.0",
  "babel-preset-env": "^1.3.2",
  "babel-preset-stage-2": "^6.22.0",
  "babel-register": "^6.22.0",
  "chalk": "^1.1.3",
  "connect-history-api-fallback": "^1.3.0",
  "copy-webpack-plugin": "^4.0.1",
  "css-loader": "^0.28.0",
  "eslint": "^3.19.0",
  "eslint-plugin-html": "^2.0.1",
  "eventsource-polyfill": "^0.9.6",
  "express": "^4.14.1",
  "extract-text-webpack-plugin": "^2.0.0",
  "file-loader": "^0.11.1",
  "friendly-errors-webpack-plugin": "^1.1.3",
  "html-webpack-plugin": "^2.28.0",
  "http-proxy-middleware": "^0.17.3",
  "jquery": "^3.2.1",
  "lightbox2": "^2.9.0",
  "opn": "^4.0.2",
  "optimize-css-assets-webpack-plugin": "^1.3.0",
  "ora": "^1.2.0",
  "rimraf": "^2.6.0",
  "semver": "^5.3.0",
  "shelljs": "^0.7.6",
  "url-loader": "^0.5.8",
  "vue-loader": "^11.3.4",
  "vue-parallaxy": "^1.0.2",
  "vue-resource": "^1.3.1",
  "vue-scrollto": "^2.6.4",
  "vue-slick": "^1.1.3",
  "vue-style-loader": "^2.0.5",
  "vue-swipe": "^2.0.3",
  "vue-template-compiler": "^2.2.6",
  "vue2-google-maps": "^0.7.5",
  "vuejs-carousel": "^0.6.0",
  "webpack": "^2.3.3",
  "webpack-bundle-analyzer": "^2.2.1",
  "webpack-dev-middleware": "^1.10.0",
  "webpack-hot-middleware": "^2.18.0",
  "webpack-merge": "^4.1.0"
},
"engines": {
  "node": ">= 4.0.0",
  "npm": ">= 3.0.0"
},
"browserslist": [
  "> 1%",
  "last 2 versions",
  "not ie <= 8"
]
}

.babelrc file- .babelrc文件 -

{
"presets": [
  ["env", { "modules": false }],
  "stage-2"
],
"plugins": ["transform-runtime"],
"comments": false,
"env": {
  "test": {
    "presets": ["env", "stage-2"],
    "plugins": [ "istanbul" ]
  }
}

Hope this both file helps for your project for eslint config. 希望这两个文件都有助于您的eslint配置项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio Code 中获取 eslint 以允许双引号? - How to get eslint in Visual Studio Code to allow double quotes? 使用 ESLint 和 Prettier 在 Visual Studio Code 中保存时无法获得正确的自动格式化 - Can't get correct autoformat on save in Visual Studio Code with ESLint and Prettier vscode如何格式化vue文件以符合eslint规则 - vscode how to format vue file to comply with eslint rules Vue中的ESLint代码格式 - ESLint code format in Vue 如何在 VS Code 编辑器中使用 ESLint + Airbnb 规则 + TypeScript + Stylelint 为 SCSS 配置 Vue CLI 4,并在保存时自动修复? - How to configure Vue CLI 4 with ESLint + Airbnb rules + TypeScript + Stylelint for SCSS, in VS Code editor with autofix on save? 如何处理 AirBNB 配置中冲突的 eslint 规则 - How to deal with conflicting eslint rules in AirBNB configuration 如何为 3 个空格配置 Eslint 缩进规则? - How to configure Eslint indent rules for 3 space? 如何在Visual Studio Code中自动完成Vue模板 - How to autocomplete Vue templates in Visual Studio Code 如何阻止 vue eslint 规则更改? - How do I stop vue eslint rules from changing? 如何使用 ESLint + Prettier + Airbnb 规则 + TypeScript + Vetur 配置 Vue CLI 4? - How to configure Vue CLI 4 with ESLint + Prettier + Airbnb rules + TypeScript + Vetur?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM