Index

no-irregular-whitespace

禁止不规则空格

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

无效或不规则的空格会导致 ECMAScript 5 解析器出现问题,并且还会使代码更难调试,类似于混合制表符和空格。

🌐 Invalid or irregular whitespace causes issues with ECMAScript 5 parsers and also makes code harder to debug in a similar nature to mixed tabs and spaces.

程序员可能会错误地输入各种空白字符,例如通过复制或键盘快捷键。例如,在 macOS 上按下 Alt + Space 会添加一个不换行空格字符。

🌐 Various whitespace characters can be inputted by programmers by mistake for example from copying or keyboard shortcuts. Pressing Alt + Space on macOS adds in a non breaking space character for example.

解决这个问题的一个简单方法是从头重写出错的那一行。这也可能是由文本编辑器引起的问题:如果重写该行不能解决问题,尝试使用不同的编辑器。

🌐 A simple fix for this problem could be to rewrite the offending line from scratch. This might also be a problem introduced by the text editor: if rewriting the line does not fix it, try using a different editor.

这些空间导致的已知问题:

🌐 Known issues these spaces cause:

  • 奥甘空间标记
    • 是有效的标记分隔符,但在大多数字体中渲染为可见字形,这在源代码中可能会产生误导。
  • 蒙古语元音分隔符
    • 自 Unicode 6.3 起不再被视为空格分隔符。在当前解析器中,如果用它替代常规的标记分隔符,将导致语法错误。
  • 行分隔符和段落分隔符
    • 这些始终是有效的空白字符和行终止符,但在 ECMAScript 2019 之前的字符串字面量中被认为是非法的。
  • 零宽度空间
    • 不被认为是标记的分隔符,并且通常被解析为 Unexpected token ILLEGAL
    • 在现代浏览器中未显示,使得代码存储库软件有望解决可视化问题。

在 JSON 中,根据此规则列为不规则空白的任何字符都不能出现在字符串之外。

🌐 In JSON, none of the characters listed as irregular whitespace by this rule may appear outside of a string.

规则详情

🌐 Rule Details

此规则旨在捕获不是普通制表符和空格的无效空白字符。其中一些字符可能会在现代浏览器中引起问题,而其他字符则在调试时难以发现。

🌐 This rule is aimed at catching invalid whitespace that is not a normal tab and space. Some of these characters may cause issues in modern browsers and others will be a debugging issue to spot.

此规则不允许以下字符,除非选项允许:

🌐 This rule disallows the following characters except where the options allow:

\u000B - Line Tabulation (\v) - <VT>
\u000C - Form Feed (\f) - <FF>
\u0085 - Next Line - <NEL>
\u00A0 - No-Break Space - <NBSP>
\u1680 - Ogham Space Mark - <OGSP>
\u180E - Mongolian Vowel Separator - <MVS>
\u2000 - En Quad - <NQSP>
\u2001 - Em Quad - <MQSP>
\u2002 - En Space - <ENSP>
\u2003 - Em Space - <EMSP>
\u2004 - Three-Per-Em - <THPMSP> - <3/MSP>
\u2005 - Four-Per-Em - <FPMSP> - <4/MSP>
\u2006 - Six-Per-Em - <SPMSP> - <6/MSP>
\u2007 - Figure Space - <FSP>
\u2008 - Punctuation Space - <PUNCSP>
\u2009 - Thin Space - <THSP>
\u200A - Hair Space - <HSP>
\u200B - Zero Width Space - <ZWSP>
\u2028 - Line Separator - <LS> - <LSEP>
\u2029 - Paragraph Separator - <PS> - <PSEP>
\u202F - Narrow No-Break Space - <NNBSP>
\u205F - Medium Mathematical Space - <MMSP>
\u3000 - Ideographic Space - <IDSP>
\uFEFF - Zero Width No-Break Space - <BOM>

选项

🌐 Options

此规则有一个异常对象选项:

🌐 This rule has an object option for exceptions:

  • "skipStrings": true(默认)允许字符串字面量中存在任意空白字符
  • "skipComments": true 允许在注释中使用任意空白字符
  • "skipRegExps": true 允许在正则表达式字面量中使用任意空白字符
  • "skipTemplates": true 允许模板字面量中包含任意空白字符
  • "skipJSXText": true 允许在 JSX 文本中使用任意空白字符

skipStrings

使用默认 { "skipStrings": true } 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default { "skipStrings": true } option:

在线运行
/*eslint no-irregular-whitespace: "error"*/

const thing = function() /*<NBSP>*/{
    return 'test';
}

const foo = function( /*<NBSP>*/){
    return 'test';
}

const bar = function /*<NBSP>*/(){
    return 'test';
}

const baz = function/*<Ogham Space Mark>*/(){
    return 'test';
}

const qux = function() {
    return 'test';/*<ENSP>*/
}

const quux = function() {
    return 'test'; /*<NBSP>*/
}

const item = function() {
    // Description <NBSP>: some descriptive text
}

/*
Description <NBSP>: some descriptive text
*/

const func = function() {
    return / <NBSP>regexp/;
}

const myFunc = function() {
    return `template <NBSP>string`;
}

使用默认 { "skipStrings": true } 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default { "skipStrings": true } option:

在线运行
/*eslint no-irregular-whitespace: "error"*/

const thing = function() {
    return ' <NBSP>thing';
}

const foo = function() {
    return '​<ZWSP>thing';
}

const bar = function() {
    return 'th <NBSP>ing';
}

skipComments

使用 { "skipComments": true } 选项时,此规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the { "skipComments": true } option:

在线运行
/*eslint no-irregular-whitespace: ["error", { "skipComments": true }]*/

function thing() {
    // Description <NBSP>: some descriptive text
}

/*
Description <NBSP>: some descriptive text
*/

skipRegExps

使用 { "skipRegExps": true } 选项时,此规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the { "skipRegExps": true } option:

在线运行
/*eslint no-irregular-whitespace: ["error", { "skipRegExps": true }]*/

function thing() {
    return / <NBSP>regexp/;
}

skipTemplates

使用 { "skipTemplates": true } 选项时,此规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the { "skipTemplates": true } option:

在线运行
/*eslint no-irregular-whitespace: ["error", { "skipTemplates": true }]*/

function thing() {
    return `template <NBSP>string`;
}

skipJSXText

使用 { "skipJSXText": true } 选项时,该规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the { "skipJSXText": true } option:

在线运行
/*eslint no-irregular-whitespace: ["error", { "skipJSXText": true }]*/

function Thing() {
    return <div>text in JSX</div>; // <NBSP> before `JSX`
}

何时不使用

🌐 When Not To Use It

如果你决定在应用中使用除制表符和空格以外的空格以外的空格。

🌐 If you decide that you wish to use whitespace other than tabs and spaces outside of strings in your application.

版本

此规则是在 ESLint v0.9.0 中引入。

进阶读物

资源