space-infix-ops
要求中缀运算符周围有间距
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
虽然格式偏好是非常个人化的,但许多风格指南需要在运算符周围留有空格,例如:
¥While formatting preferences are very personal, a number of style guides require spaces around operators, such as:
var sum = 1 + 2;
该规则的支持者认为,它使代码更易于阅读,并且可以更轻松地突出潜在错误,例如:
¥Proponents of this rule believe that it makes code easier to read and can more easily highlight potential errors, such as:
var sum = i+++2;
虽然这是有效的 JavaScript 语法,但很难确定作者的意图。
¥While this is valid JavaScript syntax, it is hard to determine what the author intended.
规则详情
¥Rule Details
此规则旨在确保中缀运算符周围有空格。
¥This rule is aimed at ensuring there are spaces around infix operators.
选项
¥Options
此规则接受具有以下默认值的单个选项参数:
¥This rule accepts a single options argument with the following defaults:
"space-infix-ops": ["error", { "int32Hint": false }]
int32Hint
将 int32Hint
选项设置为 true
(默认为 false
)以允许在没有空格的情况下写入 a|0
。
¥Set the int32Hint
option to true
(default is false
) to allow write a|0
without space.
var foo = bar|0; // `foo` is forced to be signed 32 bit integer
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint space-infix-ops: "error"*/
a+b
a+ b
a +b
a?b:c
const a={b:1};
var {b=0}=bar;
function foo(a=0) { }
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint space-infix-ops: "error"*/
a + b
a + b
a ? b : c
const a = {b:1};
var {b = 0} = bar;
function foo(a = 0) { }
何时不使用
¥When Not To Use It
如果你不关心中缀运算符周围间距的一致性,你可以关闭此规则。
¥You can turn this rule off if you are not concerned with the consistency of spacing around infix operators.
版本
此规则是在 ESLint v0.2.0 中引入。