Skip to content

useConsistentObjectDefinitions

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": "error"
}
}
}
}

¥Description

要求对象字面量声明方式一致。默认使用显式定义。

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

ECMAScript 6 提供了两种定义对象字面量的方法:{foo: foo}{foo}。这两种风格在功能上是等效的。在整个代码库中始终使用相同的风格,可以更轻松地快速阅读和理解对象定义。

¥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.

¥Example

¥Invalid

biome.json
{
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": {
"options": {
"syntax": "shorthand"
}
}
}
}
}
}
let foo = 1;
let invalid = {
foo: foo
};
code-block.js:3:5 lint/style/useConsistentObjectDefinitions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use explicit object property syntax when shorthand syntax is possible.

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

Using shorthand object property syntax makes object definitions more concise.

Safe fix: Use shorthand object property syntax.

3 │ ····foo:·foo
-----
let invalid = {
bar: function() { return "bar"; },
};
code-block.js:2:5 lint/style/useConsistentObjectDefinitions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Do not use explicit object property syntax when shorthand syntax is possible.

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

Using shorthand object property syntax makes object definitions more concise.

Safe fix: Use shorthand object property syntax.

2 │ ····bar:·function()·{·return·“bar”;·},
----------

¥Valid

let foo = 1;
let valid = {
foo,
bar() { return "bar"; },
};

¥Invalid

biome.json
{
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": {
"options": {
"syntax": "explicit"
}
}
}
}
}
}
let foo = 1;
let invalid = {
foo
};
code-block.js:3:5 lint/style/useConsistentObjectDefinitions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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.

Safe fix: Use explicit object property syntax.

3 │ ····foo:·foo
+++++
let invalid = {
bar() { return "bar"; },
};
code-block.js:2:5 lint/style/useConsistentObjectDefinitions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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.

Safe fix: Use explicit object property syntax.

2 │ ····bar:·function·()·{·return·“bar”;·},
+++++++++++

¥Valid

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

¥Options

使用以下选项指定要强制执行的对象字面量语法。

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

biome.json
{
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": {
"options": {
"syntax": "explicit"
}
}
}
}
}
}

使用的语法:

¥The syntax to use:

  • shorthand:尽可能强制要求使用简写对象属性语法。

    ¥shorthand: enforces the use of shorthand object property syntax when possible.

  • explicit:强制要求在任何情况下都使用显式对象属性语法。

    ¥explicit: enforces the use of explicit object property syntax in every case.

默认:shorthand

¥Default: shorthand

¥Related links