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
Fix empty line check between array elements #14736
Conversation
Honestly, I didn't expect you to change this part ... It's also a public API. But before that, we need to think about what will be affected. |
This case is not format correctly Prettier pr-14736 --parser babel Input: [
(a=b
),
b, //
]; Output: [
(a = b),
b, //
]; |
I tried a few other approaches, but they caused tests to fail. if you have any ideas I'm open to suggestions!
as for this, my understanding of skipToLineEnd is that it supposes that the only non-comment characters between the last node and EOL are in the character class of "\t ,; ", which would mean that since ")" can appear in there, this is not actually a behavior change so much as a bug fix. that's why I didn't make a changelog, but I can add one if you believe it's important as for:
I'm not sure the information required to fix this is present in the AST? the existing code uses the next line being whitespace as a sort of hacky heuristic here, but as you've demonstrated that is inadequate. any robust solution would require a fair bit of code to determine what is and isn't part of a multiline expression. perhaps this is better off raised as a followup issue, as it doesn't seem idempotency is violated in this example and fixing this beyond all edge cases might be a nontrivial effort requiring discussion? |
I decided to force-push a different version of this change. it solves the edge case mentioned by @fisker and doesn't change any public APIs. if anyone knows a shorter way to do this I'm all ears but otherwise I think this is an improvement over the last solution |
I noticed there was a trivial merge conflict (printArrayItems was renamed to printArrayElements while I was working on it) so I fixed that up and force-pushed the change. it should be automatically mergeable now |
src/language-js/print/array.js
Outdated
} else { | ||
const nextCandidate = | ||
text[currentIdx] == ")" ? currentIdx + 1 : candidateIdx; | ||
return skipParens(skipIfComment(currentIdx + 1), nextCandidate); |
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.
Can skip
function do the job?
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.
I think it can't replace skipParens
(and I think I'll rename skipParens
to indexAfterFurthestParen
for clarity) however after looking into it skipIfComment
can be implemented with skip
calls which cuts down a fair bit of code, so I'll make that change
while working editing the code, I realized instead of skipping the parens I can just skip directly to the comma. this is a lot less code overall however, it did trigger one change in the tests where
used to format as |
thinking about the change, I guess the root question is, for this input:
which of these outputs is most appropriate?
looking at it now, it probably would be the second example. the comment gets separated otherwise. I can push 13017-2 onto this branch if you think that'd be an improvement |
Expected output should be _ = [
(a = b), //comment
2,
3, //
]; It's bug in comment attach. Should be done in a separate PR. The blank line check is correct here. |
I see. well, setting aside the comment then, for this input:
which of these is preferred?
|
_ = [
(a = b),
2,
3, //
]; Because there is a blank line between comma after first element and second element. |
alright then, I'm fairly confident that this (the branch this PR is attached to) is the correct change. solving the root issue (i.e. #13017) without changing any snapshots would require writing code that then needs to be thrown out later. if you find anything else in the PR that needs changing, let me know, but otherwise I think it's ready |
…etween the element and the comma
oops, saw the lint failure, fixed it and ran lint locally too so it should be solved for good |
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.
LGTM, thanks!
ah, sorry for the sudden force push, I accidentally pushed the button to merge changes from main into this branch, but I rolled that back. is there something else I need to do or will these changes be merged at some point in the future? I'm not a maintainer on this project, so I cannot merge it myself |
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.
LGTM! Please check one comment.
src/language-js/print/array.js
Outdated
@@ -170,6 +171,18 @@ function isConciselyPrintedArray(node, options) { | |||
); | |||
} | |||
|
|||
function isLineAfterElementEmpty(path, { originalText: text }) { |
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.
Can we use destructuring assignment here?
function isLineAfterElementEmpty(path, { originalText: text }) { | |
function isLineAfterElementEmpty({ node }, { originalText: text }) { |
src/language-js/print/array.js
Outdated
@@ -9,17 +9,18 @@ import { | |||
fill, | |||
} from "../../document/builders.js"; | |||
import hasNewline from "../../utils/has-newline.js"; | |||
import isNextLineEmptyAfterIndex from "../../utils/is-next-line-empty.js"; | |||
import { skipInlineComment, skipTrailingComment } from "../../utils/public.js"; |
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.
Missed this on previous review. Do not import from public.js
, please import from the actual file directly.
I've implemented the suggested changes! |
oops, forgot a semicolon I guess. it's fixed now |
Co-authored-by: ☀️ <sol@solfox.online> Co-authored-by: fisker Cheung <lionkay@gmail.com>
Description
fixes #13017
it seems that parens are not included in the start-end range a node has for referencing the source text. this can cause skipToLineEnd to compute incorrectly because it isn't expecting to encounter any trailing ")"s. adding ")" to the list of characters it skips fixes this
Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨