src/content/docs/linter/rules/use-consistent-object-definitions.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/style/useConsistentObjectDefinitions`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Inspired from [`object-shorthand`](https://eslint.org/docs/latest/rules/object-shorthand){
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": "error"
}
}
}
}
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.
{
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": {
"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"; },
};
{
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": {
"options": {
"syntax": "explicit"
}
}
}
}
}
}
let foo = 1;
let invalid = {
foo
};
let invalid = {
bar() { return "bar"; },
};
let foo = 1;
let valid = {
foo: foo,
bar: function() { return "bar"; },
};
Use the options to specify the syntax of object literals to enforce.
{
"linter": {
"rules": {
"style": {
"useConsistentObjectDefinitions": {
"options": {
"syntax": "explicit"
}
}
}
}
}
}
The syntax to use:
shorthand: enforces the use of shorthand object property syntax when possible.explicit: enforces the use of explicit object property syntax in every case.Default: shorthand