object-curly-newline
在打开大括号之后和关闭大括号之前强制执行一致的换行符
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
许多风格指南要求或不允许在对象大括号和其他标记内换行。
🌐 A number of style guides require or disallow line breaks inside of object braces and other tokens.
规则详情
🌐 Rule Details
此规则要求或禁止在 { 与其后续标记之间,以及在 } 与对象字面量或解构赋值的前置标记之间换行。
🌐 This rule requires or disallows a line break between { and its following token, and between } and its preceding token of object literals or destructuring assignments.
选项
🌐 Options
此规则有一个字符串选项:
🌐 This rule has either a string option:
"always"在开括号后和闭括号前需要换行"never"不允许在打开和关闭大括号后换行
或对象选项:
🌐 Or an object option:
"multiline": true要求在属性内部或属性之间有换行时必须使用换行。否则,不允许换行。"minProperties"需要在属性数量至少达到给定整数时换行。默认情况下,如果对象包含换行符且属性数量少于给定整数,也会报告错误。然而,如果consistent选项设置为true,第二种行为将被禁用。"consistent": true(默认)要求要么大括号两端都直接包含换行符,要么都不包含。请注意,启用此选项还会更改minProperties选项的行为。(详见上文的minProperties)
你可以为对象字面、解构赋值以及命名导入和导出指定不同的选项:
🌐 You can specify different options for object literals, destructuring assignments, and named imports and exports:
{
"object-curly-newline": ["error", {
"ObjectExpression": "always",
"ObjectPattern": { "multiline": true },
"ImportDeclaration": "never",
"ExportDeclaration": { "multiline": true, "minProperties": 3 }
}]
}
"ObjectExpression"对象字面量的配置"ObjectPattern"对象模式解构赋值的配置"ImportDeclaration"的命名导入配置"ExportDeclaration"的命名导出配置
always
使用 "always" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "always" option:
/*eslint object-curly-newline: ["error", "always"]*/
let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
bar: 2};
let e = {foo() {
dosomething();
}};
let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
j} = obj;
let {k = function() {
dosomething();
}} = obj;
使用 "always" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always" option:
/*eslint object-curly-newline: ["error", "always"]*/
let a = {
};
let b = {
foo: 1
};
let c = {
foo: 1, bar: 2
};
let d = {
foo: 1,
bar: 2
};
let e = {
foo: function() {
dosomething();
}
};
let {
} = obj;
let {
f
} = obj;
let {
g, h
} = obj;
let {
i,
j
} = obj;
let {
k = function() {
dosomething();
}
} = obj;
never
使用 "never" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "never" option:
/*eslint object-curly-newline: ["error", "never"]*/
let a = {
};
let b = {
foo: 1
};
let c = {
foo: 1, bar: 2
};
let d = {
foo: 1,
bar: 2
};
let e = {
foo: function() {
dosomething();
}
};
let {
} = obj;
let {
f
} = obj;
let {
g, h
} = obj;
let {
i,
j
} = obj;
let {
k = function() {
dosomething();
}
} = obj;
使用 "never" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "never" option:
/*eslint object-curly-newline: ["error", "never"]*/
let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
bar: 2};
let e = {foo: function() {
dosomething();
}};
let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
j} = obj;
let {k = function() {
dosomething();
}} = obj;
multiline
使用 { "multiline": true } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "multiline": true } option:
/*eslint object-curly-newline: ["error", { "multiline": true }]*/
let a = {
};
let b = {
foo: 1
};
let c = {
foo: 1, bar: 2
};
let d = {foo: 1,
bar: 2};
let e = {foo: function() {
dosomething();
}};
let {
} = obj;
let {
f
} = obj;
let {
g, h
} = obj;
let {i,
j} = obj;
let {k = function() {
dosomething();
}} = obj;
使用 { "multiline": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "multiline": true } option:
/*eslint object-curly-newline: ["error", { "multiline": true }]*/
let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {
foo: 1,
bar: 2
};
let e = {
foo: function() {
dosomething();
}
};
let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {
i,
j
} = obj;
let {
k = function() {
dosomething();
}
} = obj;
minProperties
使用 { "minProperties": 2 } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "minProperties": 2 } option:
/*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/
let a = {
};
let b = {
foo: 1
};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
bar: 2};
let e = {
foo: function() {
dosomething();
}
};
let {
} = obj;
let {
f
} = obj;
let {g, h} = obj;
let {i,
j} = obj;
let {
k = function() {
dosomething();
}
} = obj;
使用 { "minProperties": 2 } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "minProperties": 2 } option:
/*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/
let a = {};
let b = {foo: 1};
let c = {
foo: 1, bar: 2
};
let d = {
foo: 1,
bar: 2
};
let e = {foo: function() {
dosomething();
}};
let {} = obj;
let {f} = obj;
let {
g, h
} = obj;
let {
i,
j
} = obj;
let {k = function() {
dosomething();
}} = obj;
consistent
使用默认 { "consistent": true } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "consistent": true } option:
/*eslint object-curly-newline: ["error", { "consistent": true }]*/
let a = {foo: 1
};
let b = {
foo: 1};
let c = {foo: 1, bar: 2
};
let d = {
foo: 1, bar: 2};
let e = {foo: function() {
dosomething();
}
};
let f = {
foo: function() {
dosomething();}};
let {g
} = obj;
let {
h} = obj;
let {i, j
} = obj;
let {k, l
} = obj;
let {
m, n} = obj;
let {
o, p} = obj;
let {q = function() {
dosomething();
}
} = obj;
let {
r = function() {
dosomething();
}} = obj;
使用默认 { "consistent": true } 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default { "consistent": true } option:
/*eslint object-curly-newline: ["error", { "consistent": true }]*/
let empty1 = {};
let empty2 = {
};
let a = {foo: 1};
let b = {
foo: 1
};
let c = {
foo: 1, bar: 2
};
let d = {
foo: 1,
bar: 2
};
let e = {foo: function() {dosomething();}};
let f = {
foo: function() {
dosomething();
}
};
let {} = obj;
let {
} = obj;
let {g} = obj;
let {
h
} = obj;
let {i, j} = obj;
let {
k, l
} = obj;
let {m,
n} = obj;
let {
o,
p
} = obj;
let {q = function() {dosomething();}} = obj;
let {
r = function() {
dosomething();
}
} = obj;
ObjectExpression 和 ObjectPattern
🌐 ObjectExpression and ObjectPattern
使用 { "ObjectExpression": "always", "ObjectPattern": "never" } 选项违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "ObjectExpression": "always", "ObjectPattern": "never" } options:
/*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
bar: 2};
let e = {foo: function() {
dosomething();
}};
let {
} = obj;
let {
f
} = obj;
let {
g, h
} = obj;
let {
i,
j
} = obj;
let {
k = function() {
dosomething();
}
} = obj;
使用 { "ObjectExpression": "always", "ObjectPattern": "never" } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ObjectExpression": "always", "ObjectPattern": "never" } options:
/*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
let a = {
};
let b = {
foo: 1
};
let c = {
foo: 1, bar: 2
};
let d = {
foo: 1,
bar: 2
};
let e = {
foo: function() {
dosomething();
}
};
let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
j} = obj;
let {k = function() {
dosomething();
}} = obj;
ImportDeclaration 和 ExportDeclaration
🌐 ImportDeclaration and ExportDeclaration
使用 { "ImportDeclaration": "always", "ExportDeclaration": "never" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "ImportDeclaration": "always", "ExportDeclaration": "never" } options:
/*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
import {foo, bar} from 'foo-bar';
import {foo as f, baz} from 'foo-bar';
import {qux,
foobar} from 'foo-bar';
export {
foo,
bar
};
export {
foo as f,
baz
} from 'foo-bar';
使用 { "ImportDeclaration": "always", "ExportDeclaration": "never" } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ImportDeclaration": "always", "ExportDeclaration": "never" } options:
/*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
import {
foo,
bar
} from 'foo-bar';
import {
baz, qux
} from 'foo-bar';
import {
foo as f,
foobar
} from 'foo-bar';
export { foo, bar } from 'foo-bar';
export { foo as f, baz } from 'foo-bar';
何时不使用
🌐 When Not To Use It
如果你不想在打开大括号之后和关闭大括号之前强制执行一致的换行符,那么禁用此规则是安全的。
🌐 If you don’t want to enforce consistent line breaks after opening and before closing braces, then it’s safe to disable this rule.
兼容性
🌐 Compatibility
相关规则
版本
此规则是在 ESLint v2.12.0 中引入。