@@ -13,6 +13,29 @@ function isSelectExpr(elem: AST.PatternElement): boolean {
13
13
&& elem . expression instanceof AST . SelectExpression ;
14
14
}
15
15
16
+ function shouldStartOnNewLine ( pattern : AST . Pattern ) : boolean {
17
+ const isMultiline =
18
+ pattern . elements . some ( isSelectExpr ) ||
19
+ pattern . elements . some ( includesNewLine ) ;
20
+
21
+ if ( isMultiline ) {
22
+ const firstElement = pattern . elements [ 0 ] ;
23
+ if ( firstElement instanceof AST . TextElement ) {
24
+ const firstChar = firstElement . value [ 0 ] ;
25
+ // Due to the indentation requirement these text characters may not appear
26
+ // as the first character on a new line.
27
+ if ( firstChar === "[" || firstChar === "." || firstChar === "*" ) {
28
+ return false ;
29
+ }
30
+ }
31
+
32
+ return true ;
33
+ }
34
+
35
+ return false ;
36
+ }
37
+
38
+
16
39
// Bit masks representing the state of the serializer.
17
40
export const HAS_ENTRIES = 1 ;
18
41
@@ -143,25 +166,12 @@ function serializeAttribute(attribute: AST.Attribute): string {
143
166
144
167
function serializePattern ( pattern : AST . Pattern ) : string {
145
168
const content = pattern . elements . map ( serializeElement ) . join ( "" ) ;
146
- const maybeStartOnNewLine =
147
- pattern . elements . some ( isSelectExpr ) ||
148
- pattern . elements . some ( includesNewLine ) ;
149
-
150
- if ( maybeStartOnNewLine ) {
151
- const firstElement = pattern . elements [ 0 ] ;
152
- if ( firstElement instanceof AST . TextElement ) {
153
- const firstChar = firstElement . value [ 0 ] ;
154
- // Due to the indentation requirement these text characters may not appear
155
- // as the first character on a new line.
156
- if ( firstChar === "[" || firstChar === "." || firstChar === "*" ) {
157
- return ` ${ indentExceptFirstLine ( content ) } ` ;
158
- }
159
- }
160
169
170
+ if ( shouldStartOnNewLine ( pattern ) ) {
161
171
return `\n ${ indentExceptFirstLine ( content ) } ` ;
162
172
}
163
173
164
- return ` ${ content } ` ;
174
+ return ` ${ indentExceptFirstLine ( content ) } ` ;
165
175
}
166
176
167
177
0 commit comments