-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Description
When does this rule warn? Please describe and show example code:
Similar to the quote-props
rule, where it's possible to set the rule to consistent
and consistent-as-needed
, I'd like the object-shorthand
rule to include these options as well.
When object-shorthand
set to consistent
, the below should not be considered a problem:
let obj1 = {
foo: foo,
bar: 'This is a string',
};
No problem:
let obj2 = {
foo,
bar,
};
The following should be considered a problem:
let obj1 = {
foo,
bar: 'This is a string',
};
Problem:
let obj2 = {
a() {},
bar: foo,
};
The consistent-as-needed
is very similar, but if all keys are redundant, the following pattern is considered a problem:
let obj3 = {
foo: foo,
bar: bar,
};
The options apply to both properties and methods.
Is this rule preventing an error or is it stylistic?
Stylistic. Personally, I feel the shorthand syntax is great, but mixing it with the longform syntax makes it harder to scan/read code. In my rules, I want to allow the shorthand syntax, but only if it's consistent in that object literal.
Why is this rule a candidate for inclusion instead of creating a custom rule?
I think this is in line with other rules which also provide consistent and consistent-as-needed options (e.g. quote-props
).
Are you willing to create the rule yourself?
Yes.