This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
Change quote-props rule to "consistent-as-needed" #39
Open
wincent
wants to merge
2
commits into
master
Choose a base branch
from
wincent/quote-props
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I just got bitten by this while preparing this PR: liferay/liferay-js-themes-toolkit#247 Basically, given an object containing some version numbers properties: ``` { '6.2': true, '7.0': true, '7.1': true, } ``` we currently have ESLint configured via "quote-props" to require quotes waround property names "as-needed"; producing this: ``` { 6.2: true, '7.0': true, 7.1: true, } ``` This looks pretty terrible. I noticed that Prettier didn't have an opinion on this, so I changed them all to skip the quotes: ``` { 6.2: true, 7.0: true, 7.1: true, } ``` of course, that is wrong too, because `7.0` actually gets stringified to `'7'`, meaning the object is equivalent to: ``` { '6.2': true, '7': true, '7.1': true, } ``` This breaks checks of the form `if (object['7.0'] === true)`. So I added a lint suppression and put strings back on all the keys, but we should just configure ESLint to use "consistent-as-needed" instead. The docs: https://eslint.org/docs/rules/quote-props): describe it like so: > requires quotes around all object literal property names if any name > strictly requires quotes, otherwise disallows quotes around object > property names Test plan: tweak my local copy of eslint-config-liferay in the repo with the PR I linked at the top and confirm that all-quotes are enforced in that object but not in other places. It's not a total bed of roses, unfortunately. I found that if you start with an object like this: ``` { a: 1, 'b-c': 2, } ``` it will get changed to: ``` { "a": 1, 'b-c': 2, } ``` on the bright side, if you then change it to use single quotes, it stays that way. Closest ticket I can find for an issue like this is: microsoft/vscode-eslint#209 So, it's a wrinkle, but I still think we're better off with this change than without it. Closes: https://github.com/liferay/eslint-config-liferay/issues/38
Note: don't want to merge this until Prettier 1.17 comes out, otherwise we'll have a conflict:
|
Prettier 1.17 is out, so I am going to try this out. |
As noted in this comment: #39 (comment) we need the latest Prettier (1.17.0) in order to make use of the "consistent-as-needed" rule, and in our Prettier config, we'll need to use `"quote-props": "consistent"`. So, I updated everything using `yarn upgrade-interactive` and added the necessary rule to the ".prettierrc.json" file; other projects will need to do the same.
Tried it, but there is still a bug, so probably best to hold off until it is resolved: |
At last, looks like we will be able to merge this with the next Prettier release, thanks to: prettier/prettier#8508 |
How did you remember about this? |
I'm subscribed to the Prettier issue, so I go an email about it. Wasn't that hard to remember the connection. 😁 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I just got bitten by this while preparing this PR.
Basically, given an object containing some version numbers properties:
we currently have ESLint configured via "quote-props" to require quotes around property names "as-needed"; producing this:
This looks pretty terrible. I noticed that Prettier didn't have an opinion on this, so I changed them all to skip the quotes:
of course, that is wrong too, because
7.0
actually gets stringified to'7'
, meaning the object is equivalent to:This breaks checks of the form
if (object['7.0'] === true)
. So I added a lint suppression and put strings back on all the keys, but we should just configure ESLint to use "consistent-as-needed" instead. The docs) describe it like so:Test plan: tweak my local copy of eslint-config-liferay in the repo with the PR I linked at the top and confirm that all-quotes are enforced in that object but not in other places. It's not a total bed of roses, unfortunately. I found that if you start with an object like this:
it will get changed to:
on the bright side, if you then change it to use single quotes, it stays that way. Closest ticket I can find for an issue like this is:
microsoft/vscode-eslint#209
So, it's a wrinkle, but I still think we're better off with this change than without it.
Closes: https://github.com/liferay/eslint-config-liferay/issues/38