array-bracket-newline
强制数组方括号后和前换行
🔧 可修复
此规则报告的一些问题可以通过 --fix
命令行 选项自动修复
许多风格指南要求或禁止在数组方括号内换行。
规则详情
此规则强制在数组方括号后和前换行。
选项
此规则具有字符串选项
"always"
要求方括号内换行"never"
禁止方括号内换行"consistent"
要求每对方括号对换行符的使用保持一致。如果一对括号中一个括号内有换行符,而另一个括号内没有,则会报告错误。
或对象选项(如果满足任何属性,则需要换行符。否则,禁止换行符)
"multiline": true
(默认)如果元素内部或元素之间存在换行符,则需要换行符。如果此项为 false,则禁用此条件。"minItems": null
(默认)如果元素数量至少为给定的整数,则需要换行符。如果此项为 0,则此条件的作用与"always"
选项相同。如果此项为null
(默认值),则禁用此条件。
always
使用 "always"
选项时,错误代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", "always"]*/
const a = ;
const b = 1;
const c = 1, 2;
const d = 1,
2;
const e = function foo() {
dosomething();
};
使用 "always"
选项时,正确代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", "always"]*/
const a = [
];
const b = [
1
];
const c = [
1, 2
];
const d = [
1,
2
];
const e = [
function foo() {
dosomething();
}
];
never
使用 "never"
选项时,错误代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", "never"]*/
const a =
;
const b =
1
;
const c =
1, 2
;
const d =
1,
2
;
const e =
function foo() {
dosomething();
}
;
使用 "never"
选项时,正确代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", "never"]*/
const a = [];
const b = [1];
const c = [1, 2];
const d = [1,
2];
const e = [function foo() {
dosomething();
}];
consistent
使用 "consistent"
选项时,错误代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", "consistent"]*/
const a = [1
;
const b = [
1;
const c = [function foo() {
dosomething();
}
const d = [
function foo() {
dosomething();
}
使用 "consistent"
选项时,正确代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", "consistent"]*/
const a = [];
const b = [
];
const c = [1];
const d = [
1
];
const e = [function foo() {
dosomething();
}];
const f = [
function foo() {
dosomething();
}
];
multiline
使用默认的 { "multiline": true }
选项时,错误代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", { "multiline": true }]*/
const a =
;
const b =
1
;
const c =
1, 2
;
const d = 1,
2;
const e = function foo() {
dosomething();
};
使用默认的 { "multiline": true }
选项时,正确代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", { "multiline": true }]*/
const a = [];
const b = [1];
const c = [1, 2];
const d = [
1,
2
];
const e = [
function foo() {
dosomething();
}
];
minItems
使用 { "minItems": 2 }
选项时,错误代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/
const a =
;
const b =
1
;
const c = 1, 2;
const d = 1,
2;
const e =
function foo() {
dosomething();
}
;
使用 { "minItems": 2 }
选项时,正确代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/
const a = [];
const b = [1];
const c = [
1, 2
];
const d = [
1,
2
];
const e = [function foo() {
dosomething();
}];
multiline 和 minItems
使用 { "multiline": true, "minItems": 2 }
选项时,错误代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/
const a =
;
const b =
1
;
const c = 1, 2;
const d = 1,
2;
const e = function foo() {
dosomething();
};
使用 { "multiline": true, "minItems": 2 }
选项时,正确代码示例
在 Playground 中打开
/*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/
const a = [];
const b = [1];
const c = [
1, 2
];
const d = [
1,
2
];
const e = [
function foo() {
dosomething();
}
];
何时不使用
如果您不想强制在数组方括号后和前换行,请不要启用此规则。
兼容性
相关规则
版本
此规则在 ESLint v4.0.0-alpha.1 中引入。