object-curly-spacing
在大括号内强制执行一致的间距
此规则报告的一些问题可通过 --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 or disallow spaces between curly braces in the following situations:
// simple object literals
var obj = { foo: "bar" };
// nested object literals
var obj = { foo: { zoo: "bar" } };
// destructuring assignment (EcmaScript 6)
var { x, y } = y;
// import/export declarations (EcmaScript 6)
import { foo } from "bar";
export { foo };
规则详情
¥Rule Details
此规则强制在对象字面量、解构赋值和导入/导出说明符的大括号内保持一致的间距。
¥This rule enforces consistent spacing inside braces of object literals, destructuring assignments, and import/export specifiers.
选项
¥Options
该规则有两个选项,一个字符串选项和一个对象选项。
¥This rule has two options, a string option and an object option.
字符串选项:
¥String option:
-
"never"
(默认)不允许大括号内有间距¥
"never"
(default) disallows spacing inside of braces -
"always"
需要大括号内有间距({}
除外)¥
"always"
requires spacing inside of braces (except{}
)
对象选项:
¥Object option:
-
"arraysInObjects": true
要求以数组元素开头和/或结尾的对象大括号内部有间距(当第一个选项设置为never
时适用)¥
"arraysInObjects": true
requires spacing inside of braces of objects beginning and/or ending with an array element (applies when the first option is set tonever
) -
"arraysInObjects": false
不允许以数组元素开头和/或结尾的对象大括号内的间距(当第一个选项设置为always
时适用)¥
"arraysInObjects": false
disallows spacing inside of braces of objects beginning and/or ending with an array element (applies when the first option is set toalways
) -
"objectsInObjects": true
要求以对象元素开头和/或结尾的对象大括号内部有间距(当第一个选项设置为never
时适用)¥
"objectsInObjects": true
requires spacing inside of braces of objects beginning and/or ending with an object element (applies when the first option is set tonever
) -
"objectsInObjects": false
不允许以对象元素开头和/或结尾的对象大括号内的间距(当第一个选项设置为always
时适用)¥
"objectsInObjects": false
disallows spacing inside of braces of objects beginning and/or ending with an object element (applies when the first option is set toalways
)
never
使用默认 "never"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default "never"
option:
/*eslint object-curly-spacing: ["error", "never"]*/
var obj = { 'foo': 'bar' };
var obj = {'foo': 'bar' };
var obj = { baz: {'foo': 'qux'}, bar};
var obj = {baz: { 'foo': 'qux'}, bar};
var {x } = y;
import { foo } from 'bar';
使用默认 "never"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default "never"
option:
/*eslint object-curly-spacing: ["error", "never"]*/
var obj = {'foo': 'bar'};
var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'};
var obj = {
'foo': 'bar'
};
var obj = {'foo': 'bar'
};
var obj = {
'foo':'bar'};
var obj = {};
var {x} = y;
import {foo} from 'bar';
always
使用 "always"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "always"
option:
/*eslint object-curly-spacing: ["error", "always"]*/
var obj = {'foo': 'bar'};
var obj = {'foo': 'bar' };
var obj = { baz: {'foo': 'qux'}, bar};
var obj = {baz: { 'foo': 'qux' }, bar};
var obj = {'foo': 'bar'
};
var obj = {
'foo':'bar'};
var {x} = y;
import {foo } from 'bar';
使用 "always"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "always"
option:
/*eslint object-curly-spacing: ["error", "always"]*/
var obj = {};
var obj = { 'foo': 'bar' };
var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' };
var obj = {
'foo': 'bar'
};
var { x } = y;
import { foo } from 'bar';
arraysInObjects
使用 "never", { "arraysInObjects": true }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "never", { "arraysInObjects": true }
options:
/*eslint object-curly-spacing: ["error", "never", { "arraysInObjects": true }]*/
var obj = {"foo": [ 1, 2 ] };
var obj = {"foo": [ "baz", "bar" ] };
使用 "always", { "arraysInObjects": false }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "always", { "arraysInObjects": false }
options:
/*eslint object-curly-spacing: ["error", "always", { "arraysInObjects": false }]*/
var obj = { "foo": [ 1, 2 ]};
var obj = { "foo": [ "baz", "bar" ]};
objectsInObjects
使用 "never", { "objectsInObjects": true }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "never", { "objectsInObjects": true }
options:
/*eslint object-curly-spacing: ["error", "never", { "objectsInObjects": true }]*/
var obj = {"foo": {"baz": 1, "bar": 2} };
使用 "always", { "objectsInObjects": false }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "always", { "objectsInObjects": false }
options:
/*eslint object-curly-spacing: ["error", "always", { "objectsInObjects": false }]*/
var obj = { "foo": { "baz": 1, "bar": 2 }};
何时不使用
¥When Not To Use It
如果你不关心大括号之间间距的一致性,你可以关闭此规则。
¥You can turn this rule off if you are not concerned with the consistency of spacing between curly braces.
相关规则
版本
此规则是在 ESLint v0.22.0 中引入。