You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置ESLint --fix将单行if语句格式化为标准块级样式?

ESLint格式化单行if语句不符合预期的解决办法

问题场景

我使用ESLint强制禁用单行if语句,但执行eslint --fix时,原本的代码:

if (constrant) return 'result';

被格式成了这种不符合预期的样式:

if (constrant)
{return 'result';}

我想要的标准格式化结果是:

if (constrant) {
  return 'result';
}

尝试过多种配置调整都没解决问题,当前的.eslintrc配置如下:

{
  "plugins": ["@typescript-eslint"],
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module",
    "lib": [
      "dom",
      "dom.iterable",
      "es2021"
    ]
  },
  "noInlineConfig": true,
  "extends": ["plugin:@typescript-eslint/recommended"],
  "rules": {
    "array-callback-return": "error",
    "for-direction": "error",
    "getter-return": "error",
    "no-async-promise-executor": "error",
    "no-await-in-loop": "error",
    "no-class-assign": "error",
    "no-compare-neg-zero": "error",
    "no-cond-assign": "error",
    "no-const-assign": "error",
    "no-constant-binary-expression": "error",
    "no-constant-condition": "error",
    "no-constructor-return": "error",
    "no-dupe-args": "error",
    "no-dupe-class-members": "error",
    "no-dupe-else-if": "error",
    "no-dupe-keys": "error",
    "no-duplicate-case": "error",
    "no-duplicate-imports": "error",
    "no-empty-pattern": "error",
    "no-ex-assign": "error",
    "no-fallthrough": "error",
    "no-func-assign": "error",
    "no-import-assign": "error",
    "no-invalid-regexp": "error",
    "no-irregular-whitespace": "error",
    "no-new-native-nonconstructor": "error",
    "no-obj-calls": "error",
    "no-promise-executor-return": "error",
    "no-self-assign": "error",
    "no-self-compare": "error",
    "no-setter-return": "error",
    "no-sparse-arrays": "error",
    "no-template-curly-in-string": "error",
    "no-this-before-super": "error",
    "no-undef": "error",
    "no-unexpected-multiline": "error",
    "no-unmodified-loop-condition": "error",
    "no-unreachable": "error",
    "no-unreachable-loop": "error",
    "no-unsafe-finally": "error",
    "no-unsafe-negation": "error",
    "no-unsafe-optional-chaining": "error",
    "no-unused-private-class-members": "error",
    "no-use-before-define": "warn",
    "no-useless-backreference": "error",
    "require-atomic-updates": "error",
    "arrow-body-style": ["error", "as-needed"],
    "block-scoped-var": "error",
    "camelcase": "error",
    "complexity": ["warn", 3],
    "consistent-return": "error",
    "curly": "error",
    "default-case": "error",
    "default-case-last": "error",
    "dot-notation": "error",
    "eqeqeq": "error",
    "func-names": ["error", "always"],
    "func-style": ["error", "declaration", { "allowArrowFunctions": true }],
    "grouped-accessor-pairs": "error",
    "id-denylist": ["error", "data", "callback"],
    "max-classes-per-file": ["error", 1],
    "max-depth": ["error", 1],
    "max-lines-per-function": "error",
    "max-nested-callbacks": ["error", 2],
    "new-cap": ["error", { "newIsCap": true }],
    "no-alert": "error",
    "no-caller": "error",
    "no-confusing-arrow": "error",
    "no-continue": "error",
    "no-delete-var": "error",
    "no-div-regex": "error",
    "no-else-return": "error",
    "no-empty": "error",
    "no-empty-function": ["error", { "allow": ["constructors"] }],
    "no-empty-static-block": "error",
    "no-eq-null": "error",
    "no-extend-native": "error",
    "no-extra-bind": "error",
    "no-extra-boolean-cast": "error",
    "no-extra-label": "error",
    "no-extra-semi": "error",
    "no-floating-decimal": "error",
    "no-global-assign": "error",
    "no-implicit-coercion": "error",
    "no-implied-eval": "error",
    "no-invalid-this": "error",
    "no-iterator": "error",
    "no-label-var": "error",
    "no-labels": "error",
    "no-lone-blocks": "error",
    "no-lonely-if": "error",
    "no-loop-func": "error",
    "no-magic-numbers": ["error", { "enforceConst": true, "ignoreClassFieldInitialValues": true }],
    "no-eval": "error",
    "no-mixed-operators": "error",
    "no-multi-assign": "error",
    "no-multi-str": "error",
    "no-nested-ternary": "error",
    "no-new": "error",
    "no-new-func": "error",
    "no-new-object": "error",
    "no-new-wrappers": "error",
    "no-nonoctal-decimal-escape": "error",
    "no-octal": "error",
    "no-octal-escape": "error",
    "no-param-reassign": "error",
    "no-proto": "error",
    "no-redeclare": "error",
    "no-regex-spaces": "error",
    "no-restricted-exports": "error",
    "no-restricted-globals": "error",
    "no-restricted-imports": "error",
    "no-restricted-properties": "error",
    "no-return-assign": "error",
    "no-return-await": "error",
    "no-script-url": "error",
    "no-shadow-restricted-names": "error",
    "no-throw-literal": "error",
    "no-undef-init": "error",
    "no-undefined": "error",
    "no-underscore-dangle": "error",
    "no-unneeded-ternary": "error",
    "no-unused-labels": "error",
    "no-useless-call": "error",
    "no-useless-catch": "error",
    "no-useless-computed-key": "error",
    "no-useless-concat": "error",
    "no-useless-constructor": "off",
    "no-useless-escape": "error",
    "no-useless-rename": "error",
    "no-useless-return": "error",
    "object-shorthand": "error",
    "one-var-declaration-per-line": "error",
    "operator-assignment": "error",
    "prefer-const": "error",
    "prefer-promise-reject-errors": "error",
    "max-statements-per-line": ["error"],
    "new-parens": "error",
    "no-mixed-spaces-and-tabs": "error",
    "no-multi-spaces": "error",
    "no-multiple-empty-lines": "error",
    "no-trailing-spaces": "error",
    "no-whitespace-before-property": "error",
    "nonblock-statement-body-position": ["error", "below"],
    "object-property-newline": ["error", {
      "allowAllPropertiesOnSameLine": true
    }],
    "indent": ["warn", 2, {
      "SwitchCase": 1,
      "ignoredNodes": ["PropertyDefinition"],
      "outerIIFEBody": 0
    }],
    "rest-spread-spacing": "error",
    "semi": "error",
    "semi-spacing": "error",
    "semi-style": "error",
    "space-infix-ops": "error",
    "space-unary-ops": "error",
    "switch-colon-spacing": "error",
    "template-curly-spacing": "error"
  }
}

解决办法

在.eslintrc的rules字段中添加@typescript-eslint/brace-style规则,配置如下:

"@typescript-eslint/brace-style": ["error", "1tbs", { "allowSingleLine": false }]

添加完成后重新执行eslint --fix,单行if语句就会被格式化为预期的标准样式。

内容的提问来源于stack exchange,提问作者Gilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:32:07