【发布时间】:2019-10-26 02:08:04
【问题描述】:
我有一个 .vue 文件,当我保存它时会自动格式化。这很好,但我有一个我无法理解的问题,也无法摆脱。 按照本指南 https://blog.jongallant.com/2019/02/vuejs-vetur-vscode-format-eslint-issues/ ,我设置了 VS 代码以使用 Vetur、beautify-js 和 ESLint。
在 .vue 文件中,我有一个简单的数组。当我保存文件时,它会像这样闪烁 100 毫秒:
然后像这样保存:
ESLint 然后抱怨两件事:
'预期缩进 8 个空格,但发现 0.eslint(indent)'
和
'不允许尾随空格.eslint(no-trailing-spaces)'
我在这里同意 ESLint 并且不想抑制这些错误。其他所有格式都可以,但 JS 不是数组。我相信这是 js-beautify 像这样格式化文档(因此链接的文章:)),但是当我禁用它时,文档仍然被格式化。但是如果我在 eslint.validate 的 vue 部分将 autofix 设置为 false (见下文),则文档没有被格式化。所以我在这里有点困惑。
这是我的 settings.json
{
"explorer.confirmDragAndDrop": false,
"vetur.validation.template": false,
"editor.formatOnSave": true,
"eslint.validate": [
{
"language": "vue",
"autoFix": true
},
{
"language": "html",
"autoFix": true
},
{
"language": "javascript",
"autoFix": true
}
],
"explorer.confirmDelete": false,
"eslint.autoFixOnSave": true,
"html.format.wrapLineLength": 200,
"editor.wordWrapColumn": 280,
"editor.wordWrap": "on",
"html.format.wrapAttributes": "force",
"editor.snippetSuggestions": "top",
"vetur.format.defaultFormatter.html": "js-beautify-html",
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
"wrap_attributes": "auto"
}
}
}
这是我的 .eslintrc.js
module.exports = {
root: true,
env: {
node: true,
},
plugins: ['es-beautifier'],
extends: ['plugin:vue/essential', 'plugin:es-beautifier/standard'],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'comma-dangle': ['error', 'never'],
// 'no-trailing-spaces': [0],
// indent: 'off',
'linebreak-style': 'off',
},
parserOptions: {
parser: 'babel-eslint',
},
};
谢谢!
【问题讨论】:
-
右下角是否显示“空格:8”?
-
它说“空格:2”。这不是被格式化程序覆盖的 VS 代码吗?
-
@Farsen 你有没有尝试过 Vetur 与 Prettier 而不是 beautify-js 的结合?
标签: vue.js visual-studio-code eslint js-beautify