useConsistentObjectDefinitions
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/style/useConsistentObjectDefinitions¥Diagnostic Category:
lint/style/useConsistentObjectDefinitions -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
灵感来自
object-shorthand¥Inspired from
object-shorthand
-
¥How to configure
{ "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
{ "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
{ "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.
{ "linter": { "rules": { "style": { "useConsistentObjectDefinitions": { "options": { "syntax": "explicit" } } } } }}syntax
Section titled “syntax”使用的语法:
¥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
Biome v2.1 中文网 - 粤ICP备13048890号