如何将JavaScript文件格式化在Visual Studio代码中?
我正在尝试将我的JavaScript文件格式化在保存上,但它不起作用。
我使用的一些设置正在工作&如预期的那样显示错误(带格式,半彩色等):
当我保存此文件时,我希望将这些格式化错误自动固定。
我尝试添加“ editor.formatonsave”:true,
settings.json
,&重新启动VS代码,但这不起作用。
这是我的settings.json
文件:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript",
"javascriptreact",
"json",
"jsonc",
"json5",
"markdown"
],
"cSpell.words": [],
"cSpell.minWordLength": 4,
"cSpell.showStatus": true,
"cSpell.allowCompoundWords": true,
"compile-hero.disable-compile-files-on-did-save-code": true,
"breadcrumbs.enabled": false,
"editor.cursorBlinking": "blink",
"editor.lineDecorationsWidth": "30",
"editor.folding": false,
"editor.hideCursorInOverviewRuler": true,
"editor.occurrencesHighlight": false,
"editor.roundedSelection": false,
"editor.selectionHighlight": true,
"editor.scrollBeyondLastLine": false,
"explorer.openEditors.visible": 0,
"window.zoomLevel": 0,
"workbench.activityBar.visible": true,
"workbench.editor.showIcons": true,
"workbench.startupEditor": "none",
"workbench.tree.renderIndentGuides": "none",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"workbench.editor.highlightModifiedTabs": true,
"files.trimFinalNewlines": true,
"editor.acceptSuggestionOnEnter": "off",
"cypressHelper.menuItems.OpenCypress": false,
"editor.bracketPairColorization.enabled": true,
"editor.guides.bracketPairs": "active",
"editor.guides.indentation": false,
"cucumberautocomplete.smartSnippets": true,
"cucumberautocomplete.steps": "cypress/support/StepDefs/*.spec.js",
"cucumberautocomplete.syncfeatures": "cypress/integration/*.feature",
"cucumberautocomplete.strictGherkinCompletion": false,
"cucumberautocomplete.strictGherkinValidation": false,
"cucumberautocomplete.skipDocStringsFormat": false,
"cucumberautocomplete.onTypeFormat": true,
"editor.quickSuggestions": {
"comments": false,
"strings": true,
"other": true
},
"conf.view.indentSymbol": "space",
"conf.view.identsBefore.Feature": 0,
"conf.view.identsBefore.Scenario": 2,
"conf.view.identsBefore.Background": 2,
"conf.view.identsBefore.ScenarioOutline": 2,
"conf.view.identsBefore.Given": 4,
"conf.view.identsBefore.When": 4,
"conf.view.identsBefore.Then": 4,
"conf.view.identsBefore.And": 4,
"conf.view.identsBefore.Tag": "relative",
"conf.view.identsBefore.Table": 6,
"conf.view.identsBefore.Examples": 4,
"conf.view.table.autoformat": true,
"cSpell.ignoreWords": [],
"git.ignoreLimitWarning": true,
"[json]": {
"editor.formatOnSave": true
}
}
这是我的.eslintrc.json
file:
{
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false
},
"overrides": [
{
"files": ["*.json", "*.json5", "*.jsonc"],
"parser": "jsonc-eslint-parser"
}
],
"rules": {
"complexity": "off",
"max-len":"off",
"max-params": [2, 4],
"max-statements":"off",
"max-depth": "off",
"max-nested-callbacks": "off",
"indent": [2, 2],
"linebreak-style": "off",
"space-before-function-paren": [2, "always"],
"curly": [2, "all"],
"brace-style": [2, "1tbs"],
"quotes": [1, "single", "avoid-escape"],
"eqeqeq": [2, "allow-null"],
"semi": [2, "always"],
"new-cap": 0,
"no-console": "off",
"no-empty": 0,
"no-unused-vars": 1,
"space-before-blocks": [2, "always"],
"keyword-spacing": [
2,
{
"before": true,
"after": true,
"overrides": {}
}
],
"no-trailing-spaces": [
2, {
"skipBlankLines": false
}
],
"cypress/no-unnecessary-waiting": "off",
"eol-last": ["error", "never"],
"jsonc/array-bracket-newline": [
"error",
{
"multiline": true,
"minItems": null
}
],
"jsonc/object-property-newline": [
"error", {
"allowAllPropertiesOnSameLine": false
}
],
"jsonc/object-curly-newline": [
"error",
{
"multiline": true,
"minProperties": 1
}
],
"jsonc/array-bracket-spacing": ["error", "never"],
"jsonc/array-element-newline": ["error", "consistent"],
"jsonc/object-curly-spacing": 2,
"jsonc/indent": ["error", 2, {}]
},
"env": {
"node": true
},
"extends": [
"eslint:recommended",
"plugin:jsonc/recommended-with-json"
]
}
这是我在package.json
中的依赖项:
"dependencies": {
"@babel/core": "^7.16.0",
"@babel/eslint-parser": "^7.16.3",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/plugin-proposal-object-rest-spread": "^7.16.7",
"@babel/preset-env": "^7.16.8",
"eslint": "^8.9.0",
"eslint-plugin-jsonc": "^2.2.1"
},
请让我知道我是否可以提供更多信息来帮助回答问题。
I am trying to format my JavaScript files on save, but it is not working.
Some of the settings I am using are working & showing errors (with formatting, semi-colons, etc.) as expected:
When I save this file, I want those formatting errors to be auto-fixed.
I have tried adding "editor.formatOnSave": true,
to settings.json
, & restarting VS Code, but that doesn't work.
Here is my settings.json
file:
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": [
"javascript",
"javascriptreact",
"json",
"jsonc",
"json5",
"markdown"
],
"cSpell.words": [],
"cSpell.minWordLength": 4,
"cSpell.showStatus": true,
"cSpell.allowCompoundWords": true,
"compile-hero.disable-compile-files-on-did-save-code": true,
"breadcrumbs.enabled": false,
"editor.cursorBlinking": "blink",
"editor.lineDecorationsWidth": "30",
"editor.folding": false,
"editor.hideCursorInOverviewRuler": true,
"editor.occurrencesHighlight": false,
"editor.roundedSelection": false,
"editor.selectionHighlight": true,
"editor.scrollBeyondLastLine": false,
"explorer.openEditors.visible": 0,
"window.zoomLevel": 0,
"workbench.activityBar.visible": true,
"workbench.editor.showIcons": true,
"workbench.startupEditor": "none",
"workbench.tree.renderIndentGuides": "none",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"workbench.editor.highlightModifiedTabs": true,
"files.trimFinalNewlines": true,
"editor.acceptSuggestionOnEnter": "off",
"cypressHelper.menuItems.OpenCypress": false,
"editor.bracketPairColorization.enabled": true,
"editor.guides.bracketPairs": "active",
"editor.guides.indentation": false,
"cucumberautocomplete.smartSnippets": true,
"cucumberautocomplete.steps": "cypress/support/StepDefs/*.spec.js",
"cucumberautocomplete.syncfeatures": "cypress/integration/*.feature",
"cucumberautocomplete.strictGherkinCompletion": false,
"cucumberautocomplete.strictGherkinValidation": false,
"cucumberautocomplete.skipDocStringsFormat": false,
"cucumberautocomplete.onTypeFormat": true,
"editor.quickSuggestions": {
"comments": false,
"strings": true,
"other": true
},
"conf.view.indentSymbol": "space",
"conf.view.identsBefore.Feature": 0,
"conf.view.identsBefore.Scenario": 2,
"conf.view.identsBefore.Background": 2,
"conf.view.identsBefore.ScenarioOutline": 2,
"conf.view.identsBefore.Given": 4,
"conf.view.identsBefore.When": 4,
"conf.view.identsBefore.Then": 4,
"conf.view.identsBefore.And": 4,
"conf.view.identsBefore.Tag": "relative",
"conf.view.identsBefore.Table": 6,
"conf.view.identsBefore.Examples": 4,
"conf.view.table.autoformat": true,
"cSpell.ignoreWords": [],
"git.ignoreLimitWarning": true,
"[json]": {
"editor.formatOnSave": true
}
}
And here is my .eslintrc.json
file:
{
"parser": "@babel/eslint-parser",
"parserOptions": {
"requireConfigFile": false
},
"overrides": [
{
"files": ["*.json", "*.json5", "*.jsonc"],
"parser": "jsonc-eslint-parser"
}
],
"rules": {
"complexity": "off",
"max-len":"off",
"max-params": [2, 4],
"max-statements":"off",
"max-depth": "off",
"max-nested-callbacks": "off",
"indent": [2, 2],
"linebreak-style": "off",
"space-before-function-paren": [2, "always"],
"curly": [2, "all"],
"brace-style": [2, "1tbs"],
"quotes": [1, "single", "avoid-escape"],
"eqeqeq": [2, "allow-null"],
"semi": [2, "always"],
"new-cap": 0,
"no-console": "off",
"no-empty": 0,
"no-unused-vars": 1,
"space-before-blocks": [2, "always"],
"keyword-spacing": [
2,
{
"before": true,
"after": true,
"overrides": {}
}
],
"no-trailing-spaces": [
2, {
"skipBlankLines": false
}
],
"cypress/no-unnecessary-waiting": "off",
"eol-last": ["error", "never"],
"jsonc/array-bracket-newline": [
"error",
{
"multiline": true,
"minItems": null
}
],
"jsonc/object-property-newline": [
"error", {
"allowAllPropertiesOnSameLine": false
}
],
"jsonc/object-curly-newline": [
"error",
{
"multiline": true,
"minProperties": 1
}
],
"jsonc/array-bracket-spacing": ["error", "never"],
"jsonc/array-element-newline": ["error", "consistent"],
"jsonc/object-curly-spacing": 2,
"jsonc/indent": ["error", 2, {}]
},
"env": {
"node": true
},
"extends": [
"eslint:recommended",
"plugin:jsonc/recommended-with-json"
]
}
And here are my dependencies in package.json
:
"dependencies": {
"@babel/core": "^7.16.0",
"@babel/eslint-parser": "^7.16.3",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/plugin-proposal-object-rest-spread": "^7.16.7",
"@babel/preset-env": "^7.16.8",
"eslint": "^8.9.0",
"eslint-plugin-jsonc": "^2.2.1"
},
Please let me know if I can provide more info to help answer the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论