JavaScript: Keep comments position for assignment/variable - #7709
Conversation
| const v8 = /**@type{string} */(value); | ||
|
|
||
| const v9 = /** @type {string} */ | ||
| (value); |
There was a problem hiding this comment.
Maybe for @type comments (for all JSDoc comments) better to keep it in one line
/cc @thorn0
There was a problem hiding this comment.
Yeah, why not do this for all comments?
There was a problem hiding this comment.
Looks good. I'll fix.
// Input
const foo = /* foo */
bar;
// Expected output😄
const foo = /* foo */ bar;|
Besides, multiple comments inside assignments behave strangely: Prettier pr-7709 --parser babelInput: var a = /*1*/ /*2*/ /*3*/
// 4
x;
var a = /*1*/ /*2*/ /*3*/
x;
var a = /*1*/ /*2*/ /*3*/
/*4*/
x;Output: var a /*1*/ /*2*/ =
/*3*/
// 4
x;
var a /*1*/ /*2*/ = /*3*/ x;
var a /*1*/ /*2*/ =
/*3*/
/*4*/
x; |
| contents, | ||
| hasNewline(options.originalText, options.locEnd(comment)) ? hardline : " " | ||
| ]); | ||
| const leftNode = |
There was a problem hiding this comment.
Can we probably just use Checked that, turns out comment.precedingNode here instead of leftNode?precedingNode is already deleted when this function is called.
There was a problem hiding this comment.
How about making this solution more general:
const lineBreak = hasNewline(options.originalText, options.locEnd(comment))
? hasNewline(options.originalText, options.locStart(comment), {
backwards: true
})
? hardline
: line
: " ";?
I see this breaks some tests, but let's discuss those cases. Maybe this behavior is okay for them too.
There was a problem hiding this comment.
Input:
if (foo) /* baz */
bar();
if (foo)
/* baz */
bar();Current output:
if (foo)
/* baz */
bar();
if (foo)
/* baz */
bar();Output with my proposed change:
if (foo) /* baz */ bar();
if (foo)
/* baz */
bar();I would say it's okay to do this. If the user wants this comment to be on the next line, they can move it manually, and it will stay there.
There was a problem hiding this comment.
@thorn0 Thank you! I did not know backwards option. Could you tell me what does that mean?
There was a problem hiding this comment.
It determines the direction, in which hasNewLine searches for a new line. By default, it searches the source code forward from the given index. With backwards: true it searches backwards.
hasNewline(options.originalText, options.locStart(comment), { backwards: true })
This returns true if there is a new line between the comment and the last non-whitespace character before the comment.
There was a problem hiding this comment.
Would be good to hear from @evilebottnawi or @j-f1 if they're okay with this change.
|
Looking at the snapshots, I found some changes that don't look good to me: Input: const kochabCooieGameOnOboleUnweave = // ???
annularCooeedSplicesWalksWayWay;
const bifornCringerMoshedPerplexSawder = // !!!
glimseGlyphsHazardNoopsTieTie +
averredBathersBoxroomBuggyNurl -
anodyneCondosMalateOverateRetinol;Prettier 1.19.1 Output: const kochabCooieGameOnOboleUnweave = annularCooeedSplicesWalksWayWay; // ???
const bifornCringerMoshedPerplexSawder = // !!!
glimseGlyphsHazardNoopsTieTie +
averredBathersBoxroomBuggyNurl -
anodyneCondosMalateOverateRetinol;Prettier pr-7709 Output: const kochabCooieGameOnOboleUnweave =
// ???
annularCooeedSplicesWalksWayWay;
const bifornCringerMoshedPerplexSawder =
// !!!
glimseGlyphsHazardNoopsTieTie +
averredBathersBoxroomBuggyNurl -
anodyneCondosMalateOverateRetinol; |
|
I propose the following implementation of function handleVariableDeclaratorComments(
enclosingNode,
followingNode,
comment
) {
if (
enclosingNode &&
(enclosingNode.type === "VariableDeclarator" ||
enclosingNode.type === "AssignmentExpression") &&
followingNode &&
(followingNode.type === "ObjectExpression" ||
followingNode.type === "ArrayExpression" ||
followingNode.type === "TemplateLiteral" ||
followingNode.type === "TaggedTemplateExpression" ||
isBlockComment(comment))
) {
addLeadingComment(followingNode, comment);
return true;
}
return false;
} |
thorn0
left a comment
There was a problem hiding this comment.
Looks good now. Please add the snippet from #7709 (comment) to the tests.
|
@evilebottnawi @j-f1 Could you review again? |
Sorry, this is not related to 2.0
Fixes #6120
Currently, the use of almost expressions in assignments/variable-declarations containing BlockComment move the position of the comment to before
=. The behavior also break the Closure Compiler type casting comments(ref :#4124 (comment)).This PR fixes the behavior by making it consistent with array, object literal and template literal.
changelog_unreleased/*/pr-XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.✨Try the playground for this PR✨