Skip to content
← Back to rules

eslint/no-irregular-whitespace Correctness

This rule is turned on by default.

它的作用

禁止在代码中使用不规则空白字符。

为什么这很糟糕?

不规则空白字符对大多数编辑器来说是不可见的,可能会导致意外行为,使代码更难调试和维护。
它们还可能导致代码格式化和解析方面的问题。

示例

以下是此规则的错误代码示例:

javascript
// 包含不规则空白字符(不可见)
function example() {
  var foo = "bar"; // 在 'bar' 前有不规则空白
}

以下是此规则的正确代码示例:

javascript
function example() {
  var foo = "bar"; // 仅使用普通空格
}

配置

此规则接受一个包含以下属性的配置对象:

skipComments

type: boolean

default: true

是否跳过注释中的不规则空白。

skipJSXText

type: boolean

default: true

是否跳过 JSX 文本中的不规则空白。

skipRegExps

type: boolean

default: true

是否跳过正则表达式字面量中的不规则空白。

skipStrings

type: boolean

default: true

是否跳过字符串字面量中的不规则空白。

skipTemplates

type: boolean

default: true

是否跳过模板字面量中的不规则空白。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-irregular-whitespace": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-irregular-whitespace": "error",
  },
});
bash
oxlint --deny no-irregular-whitespace

版本

此规则在 v0.1.1 中添加。

参考资料