如何在自动保存JS文件时禁用VS Code自动格式化并添加EOF换行符?

4

我正在使用 vscode,并启用了自动格式化配置,以在保存文件时格式化文件。但是我最近发现 vscode 编辑器总是在每个 js 文件的 EOF 处添加一个换行符,那么如何禁用这个功能呢?

我特别添加了配置 "files.insertFinalNewline": false, 但仍未生效。

vscode 信息:

Version: 1.33.1 (system setup)
Commit: 51b0b28134d51361cf996d2f0a1c698247aeabd8
Date: 2019-04-11T08:27:14.102Z
Electron: 3.1.6
Chrome: 66.0.3359.181
Node.js: 10.2.0
V8: 6.6.346.32
OS: Windows_NT x64 6.1.7601

vscode config.json

{
    "workbench.colorTheme": "Monokai",
    "editor.fontSize": 16,
    "editor.formatOnPaste": true,
    // Format a file on save.
    // A formatter must be available,
    // the file must not be auto-saved,
    // and editor must not be shutting down.
    "editor.formatOnSave": true,
    "debug.console.fontSize": 16,
    "terminal.integrated.fontSize": 14,
    "markdown.preview.fontSize": 14,
    "window.zoomLevel": 1,
    "editor.renderWhitespace": "all",
    "window.title": "${dirty}${activeEditorLong}${separator}${rootName}${separator}${appName}",
    "search.exclude": {
        "**/.gitignore": true,
        "**/.idea": true,
        "**/.vscode": true,
        "**/build": true,
        "**/dist": true,
        "**/tmp": true,
        "**/yarn.lock": true
    },
    "workbench.iconTheme": "material-icon-theme",
    "editor.wordWrapColumn": 110,
    "http.proxyStrictSSL": false,
    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": false,
    "files.trimFinalNewlines": false,
    "html.format.endWithNewline": false,
    "javascript.implicitProjectConfig.experimentalDecorators": true,
    // Enable/disable default JavaScript formatter (For Prettier)
    "javascript.format.enable": false,
    // Use 'prettier-eslint' instead of 'prettier'.
    // Other settings will only be fallbacks
    // in case they could not be inferred from eslint rules.
    "prettier.eslintIntegration": true,
    "prettier.tabWidth": 4,
    "prettier.singleQuote": true,
    "prettier.arrowParens": "always"
}


.eslintrc.json
{
    "extends": "airbnb",
    "plugins": [
        "react",
        "jsx-a11y",
        "import",
        "react-hooks"
    ],
    "parserOptions": {
        "sourceType": "module",
        "ecmaFeatures": {
            "legacyDecorators": true,
            "experimentalObjectRestSpread": true
        }
    },
    "env": {
        "browser": true,
        "es6": true
    },
    "parser": "babel-eslint",
    "globals": {
        "describe": true,
        "it": true,
        "inject": true,
        "beforeEach": true,
        "addProviders": true,
        "spyOn": true,
        "expect": true,
        "global": true,
        "require": true,
        "async": true,
        "ENVIRONMENT": true,
        "client": true
    },
    "rules": {
        "linebreak-style": 0,
        "quotes": [2, "single", {
            "avoidEscape": true,
            "allowTemplateLiterals": true
        }],
        "indent": [2, 4, {
            "SwitchCase": 1,
            "VariableDeclarator": 1
        }],
        "react/jsx-indent": [2, 4],
        "comma-dangle": ["error", "never"],
        "class-methods-use-this": 0,
        "import/newline-after-import": 0,
        "space-before-function-paren": ["error", "never"],
        "func-names": ["error", "never"],
        "consistent-return": [0],
        "eol-last": ["error", "never"],
        "no-script-url": ["off"],
        "react/jsx-indent-props": [2, 4],
        "react/forbid-prop-types": [2, {
            "forbid": []
        }],
        "jsx-a11y/anchor-is-valid": ["error", {
            "components": ["Link"],
            "specialLink": ["to"],
            "aspects": ["noHref"]
        }],
        "jsx-a11y/href-no-hash": "off",
        "jsx-a11y/no-static-element-interactions": "off",
        "jsx-a11y/click-events-have-key-events": "off",
        "import/no-unresolved": [
            "error",
            {
                "ignore": ["client/"]
            }
        ],
        "import/no-extraneous-dependencies": [
            "error",
            {
                "devDependencies": true
            }
        ],
        "import/extensions": 0,
        "max-len": [
            0, 110, 4
        ],
        "react/jsx-filename-extension": [
            1,
            {
                "extensions": [
                    ".js",
                    ".jsx"
                ]
            }
        ],
        "react-hooks/rules-of-hooks": "error"
    },
    "settings": {
        "import/resolver": {
            "node": {
                "extensions": [".js", ".jsx", ".json", ".css"]
            }
        }
    }
}

所以,如何正确配置VSCode?

看起来你正在使用 Prettier 作为你的格式化工具,所以 Prettier 可能有这个选项? - Rob Lourens
1
如果您正在使用prettier,那么他们似乎认为由于prettier是有主见的,它决定在EOF处始终添加换行符,而不考虑您可能有任何与此相矛盾的其他设置。 - B. Witter
1个回答

2

我曾经也遇到过同样的问题。要解决它,你需要进入你项目的文件夹。然后打开设置,即命令 + shift + p,输入settings,选择Preferences: open settings UI,在Text Editor选项卡下,选择Files,你会找到Eol选项卡,可以从\n、\r\n和auto中选择。我选择了auto,这样就会覆盖你指定的任何设置。


4
我做了这个,但是没有起作用,无论我怎么做都会添加新行。 - Manohar Reddy Poreddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接