array-bracket-newline
许多风格指南要求或禁止在方括号内换行。
规则详情
此规则强制在方括号的开头和结尾处换行。
选项
此规则具有字符串选项
"always"
要求在方括号内换行"never"
禁止在方括号内换行"consistent"
要求每对方括号的换行方式一致。如果一对方括号中一个有换行,另一个没有,则会报错。
或对象选项(如果满足任何属性,则要求换行。否则,禁止换行)
"multiline": true
(默认)如果元素内部或元素之间有换行,则要求换行。如果为 false,则禁用此条件。"minItems": null
(默认)如果元素数量至少为给定的整数,则要求换行。如果为 0,则此条件将与选项"always"
相同。如果为null
(默认),则禁用此条件。
always
使用 "always"
选项时,此规则的错误代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", "always"]*/
var a = [];
var b = [1];
var c = [1, 2];
var d = [1,
2];
var e = [function foo() {
dosomething();
}];
incorrect
使用 "always"
选项时,此规则的正确代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", "always"]*/
var a = [
];
var b = [
1
];
var c = [
1, 2
];
var d = [
1,
2
];
var e = [
function foo() {
dosomething();
}
];
correct
never
使用 "never"
选项时,此规则的错误代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", "never"]*/
var a = [
];
var b = [
1
];
var c = [
1, 2
];
var d = [
1,
2
];
var e = [
function foo() {
dosomething();
}
];
incorrect
使用 "never"
选项时,此规则的正确代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", "never"]*/
var a = [];
var b = [1];
var c = [1, 2];
var d = [1,
2];
var e = [function foo() {
dosomething();
}];
correct
consistent
使用 "consistent"
选项时,此规则的错误代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", "consistent"]*/
var a = [1
];
var b = [
1];
var c = [function foo() {
dosomething();
}
]
var d = [
function foo() {
dosomething();
}]
incorrect
使用 "consistent"
选项时,此规则的正确代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", "consistent"]*/
var a = [];
var b = [
];
var c = [1];
var d = [
1
];
var e = [function foo() {
dosomething();
}];
var f = [
function foo() {
dosomething();
}
];
correct
多行
使用默认 { "multiline": true }
选项时,此规则的错误代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", { "multiline": true }]*/
var a = [
];
var b = [
1
];
var c = [
1, 2
];
var d = [1,
2];
var e = [function foo() {
dosomething();
}];
incorrect
使用默认 { "multiline": true }
选项时,此规则的正确代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", { "multiline": true }]*/
var a = [];
var b = [1];
var c = [1, 2];
var d = [
1,
2
];
var e = [
function foo() {
dosomething();
}
];
correct
最小项数
使用 { "minItems": 2 }
选项时,此规则的错误代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", { "minItems": 2 }]*/
var a = [
];
var b = [
1
];
var c = [1, 2];
var d = [1,
2];
var e = [
function foo() {
dosomething();
}
];
incorrect
使用 { "minItems": 2 }
选项时,此规则的正确代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", { "minItems": 2 }]*/
var a = [];
var b = [1];
var c = [
1, 2
];
var d = [
1,
2
];
var e = [function foo() {
dosomething();
}];
correct
多行和最小项数
使用 { "multiline": true, "minItems": 2 }
选项时,此规则的错误代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/
var a = [
];
var b = [
1
];
var c = [1, 2];
var d = [1,
2];
var e = [function foo() {
dosomething();
}];
incorrect
使用 { "multiline": true, "minItems": 2 }
选项时,此规则的正确代码示例
js
/*eslint @stylistic/js/array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/
var a = [];
var b = [1];
var c = [
1, 2
];
var d = [
1,
2
];
var e = [
function foo() {
dosomething();
}
];
correct
何时不使用它
如果您不想在打开和关闭数组括号后强制换行,请不要启用此规则。