operator-linebreak
强制对操作符使用一致的换行符样式
此规则报告的一些问题可以通过 --fix
命令行 选项自动修复。
此规则已在 ESLint v8.53.0 中弃用。请使用 相应规则 in @stylistic/eslint-plugin-js
。
当语句过长而无法在一行中容纳时,通常会在分隔表达式的操作符旁边插入换行符。首先想到的样式是在操作符后放置换行符,遵循英文标点符号规则。
var fullHeight = borderTop +
innerHeight +
borderBottom;
一些开发人员发现,将操作符放在行首可以使代码更易读。
var fullHeight = borderTop
+ innerHeight
+ borderBottom;
规则详细信息
此规则强制对操作符使用一致的换行符样式。
选项
此规则有两个选项,一个字符串选项和一个对象选项。
字符串选项
"after"
要求在操作符后放置换行符"before"
要求在操作符前放置换行符"none"
禁止在操作符两侧放置换行符
对象选项
"overrides"
覆盖指定操作符的全局设置
默认配置是 "after", { "overrides": { "?": "before", ":": "before" } }
after
使用 "after"
选项时,此规则的错误代码示例
/*eslint operator-linebreak: ["error", "after"]*/
foo = 1
2;
foo = 1
2;
foo
5;
if (someCondition
otherCondition) {
}
answer = everything
42
foo;
class Foo {
a
1;
[b]
2;
[c
]
3;
}
使用 "after"
选项时,此规则的正确代码示例
/*eslint operator-linebreak: ["error", "after"]*/
foo = 1 + 2;
foo = 1 +
2;
foo =
5;
if (someCondition ||
otherCondition) {
}
answer = everything ?
42 :
foo;
class Foo {
a =
1;
[b] =
2;
[c
] =
3;
d = 4;
}
before
使用 "before"
选项时,此规则的错误代码示例
/*eslint operator-linebreak: ["error", "before"]*/
foo = 1
2;
foo
5;
if (someCondition
otherCondition) {
}
answer = everything
42
foo;
class Foo {
a
1;
[b]
2;
[c
]
3;
}
使用 "before"
选项时,此规则的正确代码示例
/*eslint operator-linebreak: ["error", "before"]*/
foo = 1 + 2;
foo = 1
+ 2;
foo
= 5;
if (someCondition
|| otherCondition) {
}
answer = everything
? 42
: foo;
class Foo {
a
= 1;
[b]
= 2;
[c
]
= 3;
d = 4;
}
none
使用 "none"
选项时,此规则的错误代码示例
/*eslint operator-linebreak: ["error", "none"]*/
foo = 1
2;
foo = 1
2;
if (someCondition
otherCondition) {
}
if (someCondition
otherCondition) {
}
answer = everything
42
foo;
answer = everything
42
foo;
class Foo {
a
1;
[b]
2;
[c
]
3;
d
4;
[e]
5;
[f
]
6;
}
使用 "none"
选项时,此规则的正确代码示例
/*eslint operator-linebreak: ["error", "none"]*/
foo = 1 + 2;
foo = 5;
if (someCondition || otherCondition) {
}
answer = everything ? 42 : foo;
class Foo {
a = 1;
[b] = 2;
[c
] = 3;
d = 4;
[e] = 5;
[f
] = 6;
}
overrides
使用 { "overrides": { "+=": "before" } }
选项时,此规则的附加错误代码示例
/*eslint operator-linebreak: ["error", "after", { "overrides": { "+=": "before" } }]*/
var thing = 'thing';
thing
's';
使用 { "overrides": { "+=": "before" } }
选项时,此规则的附加正确代码示例
/*eslint operator-linebreak: ["error", "after", { "overrides": { "+=": "before" } }]*/
var thing = 'thing';
thing
+= 's';
使用 { "overrides": { "?": "ignore", ":": "ignore" } }
选项时,此规则的附加正确代码示例
/*eslint operator-linebreak: ["error", "after", { "overrides": { "?": "ignore", ":": "ignore" } }]*/
answer = everything ?
42
: foo;
answer = everything
?
42
:
foo;
使用默认的 "after", { "overrides": { "?": "before", ":": "before" } }
选项时,此规则的错误代码示例
/*eslint operator-linebreak: ["error", "after", { "overrides": { "?": "before", ":": "before" } }]*/
foo = 1
2;
foo = 1
2;
foo
5;
if (someCondition
otherCondition) {
}
answer = everything
42
foo;
使用默认的 "after", { "overrides": { "?": "before", ":": "before" } }
选项时,此规则的正确代码示例
/*eslint operator-linebreak: ["error", "after", { "overrides": { "?": "before", ":": "before" } }]*/
foo = 1 + 2;
foo = 1 +
2;
foo =
5;
if (someCondition ||
otherCondition) {
}
answer = everything
? 42
: foo;
何时不使用它
如果您的项目不使用通用的操作符换行符样式,请关闭此规则。
相关规则
版本
此规则是在 ESLint v0.19.0 中引入的。