Enforce consistent spacing inside braces
Some problems reported by this rule are automatically fixable by the --fix
command line option
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 };
This rule enforces consistent spacing inside braces of object literals, destructuring assignments, and import/export specifiers.
This rule has two options, a string option and an object option.
String option:
"never"
(default) disallows spacing inside of braces"always"
requires spacing inside of braces (except {}
)Object option:
"arraysInObjects": true
requires spacing inside of braces of objects beginning and/or ending with an array element (applies when the first option is set to never
)"arraysInObjects": false
disallows spacing inside of braces of objects beginning and/or ending with an array element (applies when the first option is set to always
)"objectsInObjects": true
requires spacing inside of braces of objects beginning and/or ending with an object element (applies when the first option is set to never
)"objectsInObjects": false
disallows spacing inside of braces of objects beginning and/or ending with an object element (applies when the first option is set to always
)Examples of incorrect code for this rule with the default "never"
option:
Examples of correct code for this rule with the default "never"
option:
Examples of incorrect code for this rule with the "always"
option:
Examples of correct code for this rule with the "always"
option:
Examples of additional correct code for this rule with the "never", { "arraysInObjects": true }
options:
Examples of additional correct code for this rule with the "always", { "arraysInObjects": false }
options:
Examples of additional correct code for this rule with the "never", { "objectsInObjects": true }
options:
Examples of additional correct code for this rule with the "always", { "objectsInObjects": false }
options:
You can turn this rule off if you are not concerned with the consistency of spacing between curly braces.
This rule was introduced in ESLint v0.22.0.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/object-curly-spacing