Skip to content

useConsistentObjectDefinition

Require the consistent declaration of object literals. Defaults to explicit definitions.

ECMAScript 6 provides two ways to define an object literal: {foo: foo} and {foo}. The two styles are functionally equivalent. Using the same style consistently across your codebase makes it easier to quickly read and understand object definitions.

{
"options": {
"syntax": "explicit"
}
}
let foo = 1;
let invalid = {
foo
};
code-block.js:3:5 lint/nursery/useConsistentObjectDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use shorthand object property syntax.

1 │ let foo = 1;
2 │ let invalid = {
> 3 │ foo
^^^
4 │ };
5 │

Using explicit object property syntax makes object definitions more readable and consistent.

let invalid = {
bar() { return "bar"; },
};
code-block.js:2:5 lint/nursery/useConsistentObjectDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use shorthand object property syntax.

1 │ let invalid = {
> 2 │ bar() { return “bar”; },
^^^^^^^^^^^^^^^^^^^^^^^
3 │ };
4 │

Using explicit object property syntax makes object definitions more readable and consistent.

let foo = 1;
let valid = {
foo: foo,
bar: function() { return "bar"; },
};
{
"options": {
"syntax": "shorthand"
}
}
let foo = 1;
let invalid = {
foo: foo
};
let invalid = {
bar: function() { return "bar"; },
};
let foo = 1;
let valid = {
foo,
bar() { return "bar"; },
};

Use the options to specify the syntax of object literals to enforce.

{
"options": {
"syntax": "explicit"
}
}

The syntax to use:

  • explicit: enforces the use of explicit object property syntax in every case.
  • shorthand: enforces the use of shorthand object property syntax when possible.

Default: explicit

biome.json
{
"linter": {
"rules": {
"nursery": {
"useConsistentObjectDefinition": "error"
}
}
}
}