useConsistentObjectDefinition
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useConsistentObjectDefinition
- This rule has a safe fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
object-shorthand
- Inspired from
Description
Section titled “Description”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.
Example
Section titled “Example”Invalid
Section titled “Invalid”{ "options": { "syntax": "explicit" }}
let foo = 1;let invalid = { foo};
code-block.js:3:5 lint/nursery/useConsistentObjectDefinition 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/nursery/useConsistentObjectDefinition 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”;·},
│ +++++++++++
let 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"; },};
let foo = 1;let valid = { foo, bar() { return "bar"; },};
Options
Section titled “Options”Use the options to specify the syntax of object literals to enforce.
{ "options": { "syntax": "explicit" }}
syntax
Section titled “syntax”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
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useConsistentObjectDefinition": "error" } } }}