Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More granular config for 'object-shorthand' #12351

Closed
pablobirukov opened this issue Oct 1, 2019 · 11 comments
Closed

More granular config for 'object-shorthand' #12351

pablobirukov opened this issue Oct 1, 2019 · 11 comments
Assignees
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules

Comments

@pablobirukov
Copy link

Duplicating the rationale from palantir/tslint#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

@pablobirukov pablobirukov added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Oct 1, 2019
@kaicataldo kaicataldo added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Oct 1, 2019
@kaicataldo
Copy link
Member

kaicataldo commented Oct 1, 2019

I'm generally supportive of this! I just took a look at the current options and they're really not very intuitive. I think it would make sense to deprecate "methods" and "properties" and allow for the first config option to be an object that has the following options (in addition to the current string options):

{
    "object-shorthand": ["error", 
        "always" | 
        "never" |
        "consistent" |
        "consistent-as-needed" |
        {
            "property": "always" | "never",
            "method": "always" | "never"
        }
    ]
}

I don't think it makes sense to allow setting property or method to consistent or consistent-as-needed.

@pablobirukov
Copy link
Author

@kaicataldo thanks for the reply.

I quite like the proposed syntax. Can't be sure about consistent or consistent-as-needed though, as I don't see a use for them in the first place.

Do we need someone else's approval to make sure my PR will be approved?

@kaicataldo
Copy link
Member

We recommend waiting until consensus is reached before working on it so that people don't spend time working on a change that the team feels isn't a good fit for the project. Consensus is reached when a member of the team champions the change and three other team members support it (with a 👍 reaction). You can read more details about the process here.

@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label Nov 3, 2019
@eslint-deprecated
Copy link

Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that issues failing to reach accepted status after 21 days tend to
never be accepted, and as such, we close those issues.
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

@kaicataldo kaicataldo self-assigned this Nov 4, 2019
@kaicataldo kaicataldo removed the auto closed The bot closed this issue label Nov 4, 2019
@kaicataldo
Copy link
Member

I'll champion this.

@kaicataldo kaicataldo reopened this Nov 4, 2019
@pablobirukov
Copy link
Author

I could have pushed a PR by now. All I want is to know it won't be rejected

@kaicataldo
Copy link
Member

@pablobirukov Please see #12351 (comment). I'll try to drum up support for this.

@pablobirukov
Copy link
Author

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

@jsphstls
Copy link

I am also interested in a new config for this rule. My case is likely the opposite of the originally posted example. I would like to disallow the methods shorthand only, but there is no current configuration that does this. The current documentation did not make that configuration seem impossible, so I only found out after time with failed attempts.

My (personal) reasoning for preventing the method shorthand is that it encourages method creation inside object brackets which leads to more activity at a nested level. With a combination of other rules the fat arrow => is the primary visual identifier for functions. Method shorthand does not permit the fat arrow.

Less better:

const something = {
  add (a, b) {
     return a + b
  },
  otherStuff
}

More better:

const add = (a,b) => a + b

const something = {
  add,
  otherStuff
}

@Nautigsam
Copy link

Nautigsam commented Apr 21, 2020

I think more granular config is also needed for enforcing arrow functions. For now arrow functions with body are considered invalid with avoidExplicitReturnArrows = true while short arrow functions are not enforced at all. It would be great to configure which level we want. For example, I would need to totally forbid arrow functions:

const o = {
  // considered valid
  accepted() {
    return {};
  },
  // enforced by object-shortand with avoidExplicitReturnArrows option
  invalid: () => {
    return {};
  },
  // not enforced by object-shortand
  alsoInvalid: () => ({})
}

@kaicataldo
Copy link
Member

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.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Dec 10, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

4 participants