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

[formatter] bugs & fixes #21381

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions external-crates/move/tooling/prettier-move/src/cst/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Node } from '..';
import { MoveOptions, printFn, treeFn } from '../printer';
import { AstPath, doc, Doc } from 'prettier';
import { list, printIdentifier, shouldBreakFirstChild } from '../utilities';
const { group, join, line } = doc.builders;
const { group, join, line, indent, hardline } = doc.builders;

/**
* Creates a callback function to print common nodes.
Expand Down Expand Up @@ -159,10 +159,24 @@ export function printRefType(path: AstPath<Node>, _opt: MoveOptions, print: prin
*/
function printArgList(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
const nodes = path.node.nonFormattingChildren;
const children = path.map(print, 'nonFormattingChildren');

if (nodes.length === 1 && nodes[0]!.isBreakableExpression) {
return ['(', children[0]!, ')'];
const child = nodes[0]!;
const shouldBreak =
nodes[0]?.trailingComment?.type === 'line_comment' ||
nodes[0]?.leadingComment.some((e) => e.type === 'line_comment');

if (shouldBreak) {
return [
'(',
indent(hardline),
indent(path.call(print, 'nonFormattingChildren', 0)),
hardline,
')',
];
}

return ['(', path.call(print, 'nonFormattingChildren', 0), ')'];
}

return group(list({ path, print, options, open: '(', close: ')' }), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Node } from '../..';
import { MoveOptions, printFn, treeFn } from '../../printer';
import { AstPath, Doc, doc } from 'prettier';
import { printTrailingComment } from '../../utilities';
const { group, indent, line } = doc.builders;

/** The type of the node implemented in this file */
Expand All @@ -21,12 +22,29 @@ export default function (path: AstPath<Node>): treeFn | null {
* Print `assign_expression` node.
*/
function printAssignExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
return [
path.call(print, 'nonFormattingChildren', 0), // lhs
' =',
group([
indent(line),
indent(path.call(print, 'nonFormattingChildren', 1)), // rhs
]),
];
if (path.node.nonFormattingChildren.length !== 2) {
throw new Error('`assign_expression` must have 2 children');
}

const result: Doc[] = [];
let shouldBreak = false;

// together with the LHS we print trailing comment if there is one
result.push(path.call((lhs) => {
const hasComment = !!lhs.node.trailingComment;

if (lhs.node.trailingComment?.type == 'line_comment') {
shouldBreak = true;
const trailingLineComment = printTrailingComment(lhs, true);
lhs.node.disableTrailingComment();
return [print(lhs), ' =', indent(trailingLineComment)];
}

return [print(lhs), hasComment ? '=' : ' ='];
}, 'nonFormattingChildren', 0));

// then print the rhs
result.push(group([shouldBreak ? '' : indent(line), indent(path.call(print, 'nonFormattingChildren', 1))]));

return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ function printDotExpression(path: AstPath<Node>, options: MoveOptions, print: pr
path.node.nonFormattingChildren[0]!.type === NODE_TYPE ||
path.node.parent?.type === NODE_TYPE;

const isParentList = path.node.parent?.isList;

// if dot expression has a trailing comment and it breaks, we need to
// print it manually after the rhs
const trailing = lineSuffix(printTrailingComment(path));
path.node.disableTrailingComment();

const lhs = path.call(
(path) => printNode(path, options, print, false),
Expand All @@ -57,12 +58,14 @@ function printDotExpression(path: AstPath<Node>, options: MoveOptions, print: pr
if (!isChain) {
const right = path.node.nonFormattingChildren[1]!;
if (right.leadingComment.length > 0) {
path.node.disableTrailingComment();
return [lhs, indent(softline), indent(rhs), trailing];
}

return [lhs, rhs];
}

path.node.disableTrailingComment();
const parts = [lhs, ifBreak(indent(softline), ''), ifBreak(indent(rhs), rhs), trailing];

// group if parent is not `dot_expression`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ export default function (path: AstPath<Node>): treeFn | null {
*/
function printIfExpression(path: AstPath<Node>, options: MoveOptions, print: printFn): Doc {
const groupId = Symbol('if_expression');
const length = path.node.nonFormattingChildren.length;
const hasElse = path.node.children.some((e) => e.type == 'else');

const condition = path.node.nonFormattingChildren[0];
const trueBranch = path.node.nonFormattingChildren[1];
const result = [
const result: Doc[] = [
// condition group
group([
'if (',
Expand All @@ -67,31 +68,39 @@ function printIfExpression(path: AstPath<Node>, options: MoveOptions, print: pri
// body group
group(
[
trueBranch?.isList // || trueBranch?.isControlFlow
trueBranch?.isList
? [' ', path.call(print, 'nonFormattingChildren', 1)]
: [indent(line), (path.call(print, 'nonFormattingChildren', 1))],

: [indent(line), path.call(print, 'nonFormattingChildren', 1)],
],
{ id: groupId },
),
];

// else block
if (length === 3) {
// whether developer has put a newline character before `else` keyword
// if they did - we respect it and break the line intentionally
const shouldBreak = !!path.node.children.find((e) => e.type === 'else')?.startsOnNewLine;
const elseBranch = path.node.nonFormattingChildren[2];
if (hasElse) {
const isTrueBlock = trueBranch?.type === 'block';
const elseNode = path.node.nonFormattingChildren[2]!;
const printed = path.call(print, 'nonFormattingChildren', 2);
const shouldBreak =
elseNode.leadingComment.some((e) => e.type == 'line_comment') ||
elseNode.trailingComment?.type == 'line_comment';

result.push(
group(
elseBranch?.isList || trueBranch?.type === 'block'
? [' else ', printed]
: [ifBreak(' ', line, { groupId }), 'else ', printed],
{ shouldBreak },
),
);
if (elseNode.isList || elseNode.isControlFlow) {
result.push(group([
isTrueBlock ? ' ' : line,
'else ',
printed,
]))
} else {
result.push(
isTrueBlock ? ' ' : line,
'else',
group([
indent(line),
indent(path.call(print, 'nonFormattingChildren', 2)),
], { shouldBreak }),
);
}
}

return result;
Expand Down
Loading
Loading