no-irregular-whitespace
禁止不规则空格
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
无效或不规则的空格会导致 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:
-
奥甘空间标记
¥Ogham Space Mark
-
是有效的标记分隔符,但在大多数字体中渲染为可见字形,这在源代码中可能会产生误导。
¥Is a valid token separator, but is rendered as a visible glyph in most typefaces, which may be misleading in source code.
-
-
蒙古语元音分隔符
¥Mongolian Vowel Separator
-
自 Unicode 6.3 起不再被视为空格分隔符。当使用它代替常规标记分隔符时,它将导致当前解析器中出现语法错误。
¥Is no longer considered a space separator since Unicode 6.3. It will result in a syntax error in current parsers when used in place of a regular token separator.
-
-
行分隔符和段落分隔符
¥Line Separator and Paragraph Separator
-
这些始终是有效的空白字符和行终止符,但在 ECMAScript 2019 之前的字符串字面量中被认为是非法的。
¥These have always been valid whitespace characters and line terminators, but were considered illegal in string literals prior to ECMAScript 2019.
-
-
零宽度空间
¥Zero Width Space
-
不被视为标记的分隔符,并且通常被解析为
Unexpected token ILLEGAL
。¥Is NOT considered a separator for tokens and is often parsed as an
Unexpected token ILLEGAL
. -
在现代浏览器中未显示,使得代码存储库软件有望解决可视化问题。
¥Is NOT shown in modern browsers making code repository software expected to resolve the visualization.
-
在 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>
\u00A0 - No-Break Space - <NBSP>
\u0085 - Next Line - <NEL>
\u1680 - Ogham Space Mark - <OGSP>
\u180E - Mongolian Vowel Separator - <MVS>
\ufeff - Zero Width No-Break Space - <BOM>
\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>
选项
¥Options
此规则有一个异常对象选项:
¥This rule has an object option for exceptions:
-
"skipStrings": true
(默认)允许字符串字面量中的任何空白字符¥
"skipStrings": true
(default) allows any whitespace characters in string literals -
"skipComments": true
允许注释中包含任何空白字符¥
"skipComments": true
allows any whitespace characters in comments -
"skipRegExps": true
允许正则表达式字面量中的任何空白字符¥
"skipRegExps": true
allows any whitespace characters in regular expression literals -
"skipTemplates": true
允许模板字面量中存在任何空白字符¥
"skipTemplates": true
allows any whitespace characters in template literals -
"skipJSXText": true
允许 JSX 文本中存在任何空白字符¥
"skipJSXText": true
allows any whitespace characters in JSX text
skipStrings
使用默认 { "skipStrings": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "skipStrings": true }
option:
/*eslint no-irregular-whitespace: "error"*/
var thing = function() /*<NBSP>*/{
return 'test';
}
var thing = function( /*<NBSP>*/){
return 'test';
}
var thing = function /*<NBSP>*/(){
return 'test';
}
var thing = function /*<Ogham Space Mark>*/(){
return 'test';
}
var thing = function() {
return 'test'; /*<ENSP>*/
}
var thing = function() {
return 'test'; /*<NBSP>*/
}
var thing = function() {
// Description <NBSP>: some descriptive text
}
/*
Description <NBSP>: some descriptive text
*/
var thing = function() {
return / <NBSP>regexp/;
}
var thing = 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"*/
var thing = function() {
return ' <NBSP>thing';
}
var thing = function() {
return '<ZWSP>thing';
}
var thing = 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 中引入。