Improve TSMappedType formatting
MODIFY
packages/@romejs/js-formatter/builders/types/IntersectionTypeAnnotation.ts
packages/@romejs/js-formatter/builders/types/IntersectionTypeAnnotation.ts
@@ -61,6 +61,8 @@ export default function IntersectionTypeAnnotation(
function isObjectType(node: AnyNode): boolean {
return (
- node.type === 'FlowObjectTypeAnnotation' || node.type === 'TSTypeLiteral'
+ node.type === 'FlowObjectTypeAnnotation' ||
+ node.type === 'TSMappedType' ||
+ node.type === 'TSTypeLiteral'
);
}

MODIFY
packages/@romejs/js-formatter/builders/typescript/TSMappedType.ts
packages/@romejs/js-formatter/builders/typescript/TSMappedType.ts
import {TSMappedType} from '@romejs/js-ast';
import {Builder} from '@romejs/js-formatter';
-import {Token, concat, space} from '../../tokens';
+import {Token, concat, group, indent, softline, space} from '../../tokens';
export default function TSMappedType(
builder: Builder,
node: TSMappedType,
): Token {
- const tokens: Array<Token> = ['{', space];
+ const tokens: Array<Token> = [];
if (node.readonly) {
tokens.push(tokenIfPlusMinus(builder, node.readonly), 'readonly', space);
@@ -38,15 +38,18 @@ export default function TSMappedType(
tokens.push(':', space, builder.tokenize(node.typeAnnotation, node));
}
- tokens.push(space, '}');
-
- return concat(tokens);
+ return group(
+ concat(['{', indent(concat([softline, concat(tokens)])), softline, '}']),
+ );
}
function tokenIfPlusMinus(builder: Builder, token: string | true): Token {
- if (token !== true) {
+ switch (token) {
+ case '+':
+ case '-':
return token;
- } else {
+
+ default:
return '';
}
}

MODIFY
packages/@romejs/js-formatter/builders/typescript/TSTypeParameterDeclaration.ts
packages/@romejs/js-formatter/builders/typescript/TSTypeParameterDeclaration.ts
@@ -21,7 +21,9 @@ export default function TSTypeParameterDeclaration(
const shouldInline =
params.length === 1 &&
params[0].type !== 'IntersectionTypeAnnotation' &&
- params[0].type !== 'UnionTypeAnnotation';
+ params[0].type !== 'UnionTypeAnnotation' &&
+ params[0].type !== 'TSIndexedAccessType' &&
+ params[0].type !== 'TSMappedType';
if (shouldInline) {
return concat(['<', builder.tokenize(params[0], node), '>']);

Improve TSMappedType formatting