Skip to content

Proposal addition to object-shorthand: add consistent and consistent-as-needed #5438

@martijndeh

Description

@martijndeh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionenhancementThis change enhances an existing feature of ESLinthelp wantedThe team would welcome a contribution from the community for this issueruleRelates to ESLint's core rules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions