question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

More granular config for 'object-shorthand'

See original GitHub issue

_Duplicating the rationale from https://github.com/palantir/tslint/issues/3778_

The object-shorthand rule currently does not offer granularity for enforcing/disallowing shorthand separately for property assignment vs method declarations.

I would prefer to allow (but not enforce!) shorthand for method declarations, but disallow shorthand for property assignments.

Here’s an example of how I would like to configure the object-shorthand rule:

const someValue = 20;

const myObj = { 
    // error: shorthand property assignment not allowed
    someValue,
    // valid
    anotherValue: someValue,
    // valid
    someMethod(): number {
        return 42;
    },
    // also valid
    anotherMethod: (): number => {
       return 42;
    }
};

The motivation for this is that shorthand method declaration is very convenient and improves readability, while shorthand property assignment creates a coupling between the object’s property name and a local variable name that can lead to bugs when refactoring code later.

What rule do you want to change?

object-shorthand

Does this change cause the rule to produce more or fewer warnings?

Number of warnings stays the same

How will the change be implemented? (New option, new default behavior, etc.)?

More granular config options similar to TSLint version

Are you willing to submit a pull request to implement this change?

Yes

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pablobirukovcommented, Nov 4, 2019

@katerberg thanks, I remember that, and that’s what I’m waiting for 👍

0reactions
kaicataldocommented, Jun 12, 2020

The team has made the difficult decision to freeze stylistic rules (please see https://eslint.org/blog/2020/05/changes-to-rules-policies for more details), and we unfortunately won’t be able to accept this proposal.

Thanks for contributing to ESLint and for being willing to implement your proposal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

object-literal-shorthand - Rule
With "never" option provided, any shorthand object literal syntax causes an error. The rule can be configured in a more granular way.
Read more >
object-shorthand - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
[sort-keys] Add option to allow shorthand in object literals to be ...
I was envisioning something more along the lines of a single { separateShorthand: true } option that would allow/require shorthand and regular ...
Read more >
eslint object-shorthand error with variable passed in
This rule enforces the use of the shorthand syntax. This applies to all methods (including generators) defined in object literals and any ...
Read more >
babel/plugin-transform-classes
Some built-in functions (like Array ) always return a new object. Instead of returning it, Babel should treat it as the new this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found