useConsistentObjectDefinition
Summary
Section titled Summary- Diagnostic Category:
lint/nursery/useConsistentObjectDefinition
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
object-shorthand
- Inspired from
Description
Section titled DescriptionRequire 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.
Example
Section titled ExampleInvalid
Section titled Invalid{ "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.
Valid
Section titled Validlet foo = 1;let valid = { foo: foo, bar: function() { return "bar"; },};
Invalid
Section titled Invalid{ "options": { "syntax": "shorthand" }}
let foo = 1;let invalid = { foo: foo};
let invalid = { bar: function() { return "bar"; },};
Valid
Section titled Validlet foo = 1;let valid = { foo, bar() { return "bar"; },};
Options
Section titled OptionsUse the options to specify the syntax of object literals to enforce.
{ "options": { "syntax": "explicit" }}
syntax
Section titled syntaxThe 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
How to configure
Section titled How to configure{ "linter": { "rules": { "nursery": { "useConsistentObjectDefinition": "error" } } }}