Allow group formatting to skip flat mode
MODIFY
packages/@romejs/js-formatter/Printer.ts
packages/@romejs/js-formatter/Printer.ts
@@ -101,6 +101,15 @@ function print(token: Token, state: State, options: PrinterOptions): void {
}
case 'Group': {
+ if (token.shouldBreak) {
+ if (state.flat) {
+ throw new BreakError();
+ } else {
+ stack.push([token.contents, state]);
+ break;
+ }
+ }
+
if (state.flat) {
stack.push([token.contents, state]);
} else {

MODIFY
packages/@romejs/js-formatter/builders/objects/ObjectExpression.ts
packages/@romejs/js-formatter/builders/objects/ObjectExpression.ts
@@ -58,15 +58,14 @@ export default function ObjectExpression(
// If the first property is not one the same line as the opening brace,
// the object is printed on multiple lines.
- const forceBreak =
+ const shouldBreak =
node.loc !== undefined &&
props.length > 0 &&
props[0].loc !== undefined &&
props[0].loc.start.line !== node.loc.start.line;
- // HACK: When a hardline is output on flat mode, the group is split
- // TODO: Add a `break` property on group tokens to force this behavior
- const line = forceBreak ? hardline : softline;
-
- return group(concat(['{', indent(concat([line, concat(tokens)])), line, '}']));
+ return group(
+ concat(['{', indent(concat([softline, concat(tokens)])), softline, '}']),
+ shouldBreak,
+ );
}

MODIFY
packages/@romejs/js-formatter/builders/utils.ts
packages/@romejs/js-formatter/builders/utils.ts
@@ -180,6 +180,7 @@ export function printTSBraced(
hardline,
'}',
]),
+ true,
);
}

MODIFY
packages/@romejs/js-formatter/tokens.ts
packages/@romejs/js-formatter/tokens.ts
@@ -29,6 +29,7 @@ export type IndentToken = {
export type GroupToken = {
type: 'Group';
contents: Token;
+ shouldBreak: boolean;
};
export type CommentToken = {
@@ -84,10 +85,11 @@ export const space: SpaceToken = {
type: 'Space',
};
-export function group(contents: Token): GroupToken {
+export function group(contents: Token, shouldBreak: boolean = false): GroupToken {
return {
type: 'Group',
contents,
+ shouldBreak,
};
}

MODIFY
packages/@romejs/project/types.ts
packages/@romejs/project/types.ts
@@ -87,7 +87,9 @@ export type ProjectConfigTarget = {
// This is a project config that contains only things that can be JSON serializable
// This is used to transport and reserialize projects in workers
export type ProjectConfigJSON = ProjectConfigJSONObjectReducer<ProjectConfigBase> & {
- [ObjectKey in keyof ProjectConfigObjects]: ProjectConfigJSONPropertyReducer<ProjectConfigObjects[ObjectKey]>
+ [ObjectKey in keyof ProjectConfigObjects]: ProjectConfigJSONPropertyReducer<
+ ProjectConfigObjects[ObjectKey]
+ >
};
// Weird way to get the value type from a map
@@ -110,7 +112,9 @@ type ProjectConfigJSONPropertyReducer<Type> = Type extends AbsoluteFilePath
: Type;
type ProjectConfigJSONObjectReducer<Object> = {
- [PropertyKey in keyof Object]: ProjectConfigJSONPropertyReducer<Object[PropertyKey]>
+ [PropertyKey in keyof Object]: ProjectConfigJSONPropertyReducer<
+ Object[PropertyKey]
+ >
};
// Base of a project config without any objects
@@ -122,7 +126,9 @@ type ProjectConfigBase = {
// Data structure we pass around when normalizing and merging project configs
export type PartialProjectConfig = Partial<ProjectConfigBase> & {
- [Key in keyof ProjectConfigObjects]: PartialProjectValue<ProjectConfigObjects[Key]>
+ [Key in keyof ProjectConfigObjects]: PartialProjectValue<
+ ProjectConfigObjects[Key]
+ >
};
// rome-suppress-next-line lint/noExplicitAny

Allow group formatting to skip flat mode