Fix nested conditional expression indent
MODIFY
packages/@romejs/js-formatter/builders/comments.ts
packages/@romejs/js-formatter/builders/comments.ts
@@ -58,7 +58,9 @@ function printCommentSeparator(left: AnyNode, right: AnyNode): Token {
const linesBetween = getLinesBetween(left, right);
return linesBetween === 0
? space
- : linesBetween === 1 ? hardline : concat([hardline, hardline]);
+ : linesBetween === 1
+ ? hardline
+ : concat([hardline, hardline]);
}
export function printLeadingComment(node: AnyComment, next: AnyNode): Token {

MODIFY
packages/@romejs/js-formatter/builders/expressions/ConditionalExpression.ts
packages/@romejs/js-formatter/builders/expressions/ConditionalExpression.ts
* LICENSE file in the root directory of this source tree.
*/
-import {ConditionalExpression} from '@romejs/js-ast';
+import {AnyNode, ConditionalExpression} from '@romejs/js-ast';
import Builder from '../../Builder';
import {Token, concat, group, indent, lineOrSpace, space} from '../../tokens';
export default function ConditionalExpression(
builder: Builder,
node: ConditionalExpression,
+ parent: AnyNode,
): Token {
return printConditionalExpression(
builder.tokenize(node.test, node),
builder.tokenize(node.consequent, node),
builder.tokenize(node.alternate, node),
+ parent,
+ node.consequent,
+ node.alternate,
+ );
+}
+
+function isConditionalExpression(node: AnyNode): boolean {
+ return (
+ node.type === 'ConditionalExpression' || node.type === 'TSConditionalType'
);
}
@@ -24,12 +34,33 @@ export function printConditionalExpression(
test: Token,
consequent: Token,
alternate: Token,
+ parentNode: AnyNode,
+ consequentNode: AnyNode,
+ alternateNode: AnyNode,
): Token {
- return group(
- concat([
+ const printed = concat([
test,
- indent(concat([lineOrSpace, '?', space, indent(consequent)])),
- indent(concat([lineOrSpace, ':', space, indent(alternate)])),
+ indent(
+ concat([
+ lineOrSpace,
+ '?',
+ space,
+ isConditionalExpression(consequentNode)
+ ? consequent
+ : indent(consequent),
]),
- );
+ ),
+ indent(
+ concat([
+ lineOrSpace,
+ ':',
+ space,
+ isConditionalExpression(alternateNode) ? alternate : indent(alternate),
+ ]),
+ ),
+ ]);
+
+ // Do not group nested conditional expressions. By doing so, if a conditional
+ // expression breaks, the hole chain breaks.
+ return isConditionalExpression(parentNode) ? printed : group(printed);
}

MODIFY
packages/@romejs/js-formatter/builders/typescript/TSConditionalType.ts
packages/@romejs/js-formatter/builders/typescript/TSConditionalType.ts
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
- */
-
-import {TSConditionalType} from '@romejs/js-ast';
+ */ import {AnyNode, TSConditionalType} from '@romejs/js-ast';
import {Builder} from '@romejs/js-formatter';
import {Token, concat, space} from '../../tokens';
import {printConditionalExpression} from '../expressions/ConditionalExpression';
@@ -13,6 +11,7 @@ import {printConditionalExpression} from '../expressions/ConditionalExpression';
export default function TSConditionalType(
builder: Builder,
node: TSConditionalType,
+ parent: AnyNode,
): Token {
return printConditionalExpression(
concat([
@@ -24,5 +23,8 @@ export default function TSConditionalType(
]),
builder.tokenize(node.trueType, node),
builder.tokenize(node.falseType, node),
+ parent,
+ node.trueType,
+ node.falseType,
);
}

MODIFY
packages/@romejs/js-parser/parser/lval.ts
packages/@romejs/js-parser/parser/lval.ts
@@ -1014,7 +1014,9 @@ export function checkLVal(
description: descriptions.JS_PARSER.INVALID_PARENTEHSIZED_LVAL(
expr.type === 'BindingObjectPattern'
? 'object'
- : expr.type === 'BindingArrayPattern' ? 'array' : undefined,
+ : expr.type === 'BindingArrayPattern'
+ ? 'array'
+ : undefined,
),
loc: expr.loc,
});

Fix nested conditional expression indent