-
-
Notifications
You must be signed in to change notification settings - Fork 555
add format-array (formats JSON arrays) #1968
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
Conversation
There are a few |
# in cases where the preference differs from prettier. | ||
# | ||
# It has only one mode of operation: | ||
# Run it without any arguments to format the files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may suggest that I could add a --check
flag to have it exit 1/0 depending on whether files need to be formatted, so that it can be incorporated into CI
Actually, my plan to incorporate it into CI was just to run it and check git diff
. The advantage of that is that we could even show the git diff
output in the CI, which will give contributors an idea of what changes they need to make. That seems to me to be more useful than just saying "you need to make some changes".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've done the same in the past in the v3-repo, you can probably copy parts of the format workflows there to set it up: https://github.com/exercism/v3/blob/main/.github/workflows/format-code.yml
3e38ab1
to
c384a84
Compare
"expected": { | ||
"error": "Number of input columns is not a multiple of three" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be (similar to the above change):
"expected": { | |
"error": "Number of input columns is not a multiple of three" | |
} | |
"expected": { "error": "Number of input columns is not a multiple of three" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would not be in line with prettier
. format-array
has no intention of formatting objects or other non-array items
@@ -66,7 +66,14 @@ | |||
"input": { | |||
"count": 6 | |||
}, | |||
"expected": [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]] | |||
"expected": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to also have the other expected values formatted with one element per line? That way one really gets a visual indication of the triangle shape
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with format-array
, very possible! I will add it to the list of things to be done after this PR. It will be as simple as having expected
be multi_line
.
# matches things of the form "key": [1, 2, 3] | ||
# because this is not a multi-line regex, | ||
# . does NOT match newlines. | ||
contents.scan(/^( +)"#{key}": (\[.*\],?$)/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically speaking, this (using a regex) could also match the text in a comment, but that is both highly unlikely and pretty noticeable in a PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a fascinating case to consider. Presumably it would be something like
{
"comment": [
"consider the following input",
"foo\": [1, 2, 3]"
]
}
That doesn't particularly look good in JSON, and for this to match you have to pass a key
that includes a \
... it's indeed pretty noticeable on both ends.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving based on the two mentioned commits.
Now that the other two PRs are merged (I have rebased it on main now that they are, so this PR now only contains the relevant commits), we could go about this two possible ways:
I prefer the latter so I will be doing that unless I hear otherwise. |
6467919
to
95242c8
Compare
|
I now think this is ready for review. See https://github.com/exercism/problem-specifications/runs/5430593239?check_suite_focus=true , which is the CI run for #1981, to see what happens now if a JSON file's formatting does not match prettier + format-array. |
@@ -336,4 +336,5 @@ Note: if you use VS Code as your editor, you can install the [prettier plugin][p | |||
[improve-exercise-metadata]: https://github.com/exercism/legacy-docs/blob/main/you-can-help/improve-exercise-metadata.md | |||
[legacy-docs]: https://github.com/exercism/legacy-docs | |||
[prettier]: https://prettier.io/ | |||
[format-array]: https://github.com/exercism/problem-specifications/blob/main/bin/format-array.rb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notice! this link does not work yet for the obvious reason that format-array
doesn't exist on main
yet! I am not sure if I have a better solution to offer for review, other than "you will just have to check this link very carefully"
Or, this change could be removed from this PR and only submitted in a separate PR after format-array.rb has been merged, so that all can verify that the link is correct, but I was not convinced that was a better solution.
notice! I have just found out that |
Recall that I also put out a call for anyone to put forth alternative ways of formatting arrays. I have added discussion of two potential options to the initial PR comment (https://www.npmjs.com/package/prettier-plugin-multiline-arrays, and the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
This has been lying dormant for a while. @exercism/reviewers could you maybe review this PR? |
All right! So, I have a weak preference for not squashing these commits. Nobody else who reviewed expressed an opinion either way, so that could mean nobody has an opinion or just that it wasn't on the forefront of their minds at the time, and that's fine. I know that I could ask Erik to merge it without squashing. So @ErikSchierboom I'm going to tentatively ask for that. If it seems better to just squash just say the word and that's fine with me. |
Ah, it turns out I don't have enough permissions to do a merge commit. @iHiD could you merge this without squashing? |
I don't have permission for that either :) Can I rebase+merge? |
I think that a rebase merge loses the pull request context? I'm not entirely sure. |
It's still inked in the UI but not the commit message(s), I think. But I'd just squash it tbh |
I can't claim to know @petertseng's current thoughts, but I remember them writing a nice post on this topic years ago. To expand a little, I'd agree that the best practice is a merge commit in this situation. Ideally after rebasing the PR branch on the most recent
I think that using the merge commit is the best of all worlds here because:
But please consider this post entirely non-blocking. There's a tradeoff between allowing the "best" merge method for every situation, and avoiding a "bad" merge method for the average situation. |
bowling has `previousRolls` on a single line. transpose has each element of `lines` on its own line. pull request: exercism#1968
format-array expresses our per-file preferences for array formatting, in cases where the preference differs from prettier. To see that this is true, the following procedure can be used: 1. Delete or truncate .pretterignore and run prettier on JSON files. prettier will have reformatted them. 2. run format-array. It will restore the files to their original state. pull request: exercism#1968
pull request: exercism#1968
with format-array running, we are now ready to allow prettier to format all. pull request: exercism#1968
pull request: exercism#1968
Thanks for linking to my previous post! I think it generally still accurately describes my views. I can also help the situation a little - I have now included a link to the pull request in the commit messages. That solves my complaint with rebase+merge (loses link to pull request), and I would say it'd be okay to rebase+merge in this case! Don't want to use up too much of everyone's valuable time trying to get things figured out. If we can't figure things out, just squashing is all right. Aside: To allow a merge commit, I am going to hazard a guess that one setting to look at would be to see whether "Require linear history" is checked in https://github.com/exercism/problem-specifications/settings/branches (the protection rule for |
I thought you were talking about a rebase merge. I thought we agreed that merge commits are not wanted at all some time ago? |
I think the conclusion was #1934 (comment):
|
No, I'm thinking of something older. Merge commits have (thankfully) been disabled across pretty much the entire org quite some time ago. |
@iHiD Could you rebase merge this? |
bowling has `previousRolls` on a single line. transpose has each element of `lines` on its own line. pull request: #1968
format-array expresses our per-file preferences for array formatting, in cases where the preference differs from prettier. To see that this is true, the following procedure can be used: 1. Delete or truncate .pretterignore and run prettier on JSON files. prettier will have reformatted them. 2. run format-array. It will restore the files to their original state. pull request: #1968
with format-array running, we are now ready to allow prettier to format all. pull request: #1968
suggested as a follow-up action to format-array being merged, in at least two separate occasions: exercism#1968 (comment) exercism#1966 (comment)
suggested as a follow-up action to format-array being merged, in at least two separate occasions: exercism#1968 (comment) exercism#1966 (comment) The reason for the suggestion is that helps visualise the human-readable form of the triangle that we are used to seeing.
suggested as a follow-up action to format-array being merged, on at least two separate occasions: exercism#1968 (comment) exercism#1966 (comment) The reason for the suggestion is that helps visualise the human-readable form of the triangle that we are used to seeing.
suggested as a follow-up action to format-array being merged, on at least two separate occasions: #1968 (comment) #1966 (comment) The reason for the suggestion is that helps visualise the human-readable form of the triangle that we are used to seeing.
format-array expresses our per-file preferences for array formatting, in
cases where the preference differs from prettier.
To see that this is true, the following procedure can
be used:
prettier will have reformatted them.
This is ready for review.
The below are historical blockers that had to be solved before it is ready for review, but they are now solved:
allow prettier to format more files #1966 and auto-format portions of some JSON files #1967 need to be merged first (notice that this PR includes commits from both of those PRs). Reviews of this PR should be limited solely to 0196b9e and c384a84. Reviews of other commits should go in their respective PRs.doneallergies: format #1978 needs to be merged firstdoneIt should be incorporated into CI before being merged (and that will need review when the time comes as well)donesuggested items for review:
book-store
could be inexercises/book-store/canonical-data.json
or some other file inexercises/book-store
). Advantage: If I'm just interested in editing a single exercise's JSON file, I only need to look in that JSON file to find the rules, rather than knowing that I need to look inbin/format-array.rb
as well.padded
, and it can't correctly formatdominoes
,triangle
, orsaddle-points
, where the multiline-ness should descend only one level deep into an array of arrays.--print-width
flag for prettier: I haven't explored this to find out whether we can specify a value forprint-width
that correctly does what we want. I suspect it would be a little difficult, since our decision is not based on line length or width.