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

[DISCUSS] [tslint] Get rid of object-literal-sort-keys rule #19700

Closed
chrisdavies opened this issue Jun 6, 2018 · 20 comments · Fixed by #20274
Closed

[DISCUSS] [tslint] Get rid of object-literal-sort-keys rule #19700

chrisdavies opened this issue Jun 6, 2018 · 20 comments · Fixed by #20274

Comments

@chrisdavies
Copy link
Contributor

chrisdavies commented Jun 6, 2018

I've been writing some TypeScript lately, and this rule is driving me mad.

Our style guide says to put shorthand properties before any long-hand properties:

export const thing = {
  achoo,
  choo,
  bach: someFunctionHere(),
};

But this rule makes me have to do this:

export const thing = {
  achoo,
  bach: someFunctionHere(),
  choo,
};

Further, I often like to group my properties by logical association, or by normal English flow of words, especially for bigger objects:

{
   name: 'Oscar',
   address: 'Sesame Street',
   city: 'New York',
   state: 'SC',
   zip: 12324,
}

But this rule would make me do this:

{
   address: 'Sesame Street',
   city: 'New York',
   name: 'Oscar',
   state: 'SC',
   zip: 12324,
}

OK, maybe that's not the best example, as you'd probably split addresses out into their own object, but you get the point. I think alphabetical sorting is not terribly useful in general, but other groupings are often useful.

Thumbs up / down if you agree / disagree.

@chrisdavies chrisdavies changed the title [DISCUSS] Get rid of object-literal-sort-keys rule [DISCUSS] [tslint] Get rid of object-literal-sort-keys rule Jun 6, 2018
@chrisdavies chrisdavies added the dev label Jun 6, 2018
@lukasolson
Copy link
Member

Our JS style guide actually says to put shorthand variables first, so we either need to change that or get rid of this lint rule.

@chrisdavies
Copy link
Contributor Author

@lukasolson You're right. It's not encapsulated in our eslint rules. It probably should be in both eslint and tslint.

@cjcenizal
Copy link
Contributor

cjcenizal commented Jun 6, 2018

Can someone who is for this rule explain how it benefits them? I don’t feel strongly one way or the other but if this rule impedes teammates and doesn’t provide a huge benefit, then I think removing it is a low cost way to improve the DX.

@chrisdavies
Copy link
Contributor Author

Updated the description to include the style guide per Lucas's comment.

@kobelb
Copy link
Contributor

kobelb commented Jun 6, 2018

Can someone who is for this rule explain how it benefits them? I don’t feel strongly one way or the other but if this rule impedes teammates and doesn’t provide a huge benefit, then I think removing it is a low cost way to improve the DX.

I find that with time and enough fields being added "logical" ordering generally falls apart and I have generally resorted to alphabetical ordering. No longer requiring shorthand properties to be listed first reduces the amount of noise in a diff when switching from shorthand to longhand and vice-versa.

@bmcconaghy
Copy link
Contributor

I like how easy to understand the alphabetical ordering is. With the other ordering, I have to know what type of property it is in order to find it in a large list.

@epixa
Copy link
Contributor

epixa commented Jun 6, 2018

I don't have a strong opinion about alphabetical ordering, but I do think we should defer to the recommended linting rules wherever possible. We'll have exceptions to the recommended rules, but I'd much prefer they be considered further down the road once we've used them for a bit rather than in the days after the recommended rules were introduced in the first place.

Unquestionably the recommended rules will not be ideal to some people, just like the prettier styles aren't necessarily everyone's favorite. The point of these things is largely to take those decisions out of our hands to help us avoid inconsistencies and churn on our styles over time.

We did make an exception so far with not prefixing interfaces with I, but this was a style rule that had direct ramifications on our public plugin APIs, which made nipping it in the bud pretty important.

@stacey-gammon
Copy link
Contributor

If we do leave it in, is there any way to automatically fix it? Seems to take up a good bit of time when I'm trying to commit without errors to go fix it manually... but maybe I'm not aware of a special command (like prettier has the command) to auto fix it?

@epixa
Copy link
Contributor

epixa commented Jun 6, 2018

Does the --fix flag work for the eslint command? If so, then yes. And you can also set up your editor to run that automatically on files (which will do things like add the license header as well).

@chrisdavies
Copy link
Contributor Author

@stacey-gammon I don't know what editor you use, but VS Code automatically fixes it. You can click on the little yellow lightbulb and say "Fix all errors". So, it's not too egregious a rule from that perspective.

@bmcconaghy
Copy link
Contributor

@stacey-gammon also in vscode you can add this to your settings:

"eslint.autoFixOnSave": true,
 "eslint.options": { "configFile": ".eslintrc.js" },

@kobelb
Copy link
Contributor

kobelb commented Jun 6, 2018

Complete side-note, but the auto fix on save stuff makes working with the license headers so much nicer!

@azasypkin
Copy link
Member

I'd still keep this rule since "logical grouping" of properties sounds like a very subjective thing that works well only until another developer decides to change the code and they may have different perspective on what "logical grouping" is. It's quite common for large teams like we have.

Having stricter rules that are enforced automatically may also reduce unnecessary friction during code review. In case when "logical grouping" feels really important then maybe it's time to extract these properties into sub-object or something.

@rhoboat
Copy link

rhoboat commented Jun 8, 2018

Agree with keeping the object-literal-sort-keys rule, and updating the style guide to remove preferring shorthand properties at the top.

chrisdavies added a commit that referenced this issue Jun 11, 2018
This addresses the concerns raised by #19700, which pointed out that our style guide is inconsistent with the tslint rules when it comes to the ordering of properties.
@chrisdavies
Copy link
Contributor Author

We have 9 in favor and 6 opposed. That's probably too close to warrant making a change. Instead, I'm closing this, and we'll keep the tslint rules. I've opened a separate PR which will update our JS style guide to no longer specify a preference for property ordering.

@mattapperson
Copy link
Contributor

A note that this is not done with a --fix flag palantir/tslint#2583

@stacey-gammon
Copy link
Contributor

Can we re-open this discussion since this is not auto-fixed? Or at least it doesn't seem to be for me. The import order is auto fixed but not object literal order. Manually alphabetizing is painful.

@stacey-gammon stacey-gammon reopened this Jun 27, 2018
@mattapperson
Copy link
Contributor

@stacey-gammon worth noting that the prettier team looked into an auto-fix for this and found it was darn near impossible as it could and likely would cause bugs prettier/prettier#1096

@epixa
Copy link
Contributor

epixa commented Jun 27, 2018

I've changed my vote in favor of this change. I don't think this could be auto-fixed without resulting in some pretty gross code, which leads me to believe that this will never be autofixable.

At this point, I've seen this rule criticized at least a half dozen times on slack, along with an email thread and this issue. One of the reasons we want to use recommended rules is to prevent constant time dumped into discussing issues exactly like this. We should keep the recommend rules as much as we can, but if there is mostly overwhelming support for changing a rule and the only people that vote against it are doing that on ideological grounds, then that's pretty damning for the rule in question.

@stacey-gammon
Copy link
Contributor

Created #20274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants