Skip to content

Commit 99d472b

Browse files
committed
shouldStartOnNewLine
1 parent 599c482 commit 99d472b

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

fluent-syntax/src/serializer.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@ function isSelectExpr(elem: AST.PatternElement): boolean {
1313
&& elem.expression instanceof AST.SelectExpression;
1414
}
1515

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+
1639
// Bit masks representing the state of the serializer.
1740
export const HAS_ENTRIES = 1;
1841

@@ -143,25 +166,12 @@ function serializeAttribute(attribute: AST.Attribute): string {
143166

144167
function serializePattern(pattern: AST.Pattern): string {
145168
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-
}
160169

170+
if (shouldStartOnNewLine(pattern)) {
161171
return `\n ${indentExceptFirstLine(content)}`;
162172
}
163173

164-
return ` ${content}`;
174+
return ` ${indentExceptFirstLine(content)}`;
165175
}
166176

167177

0 commit comments

Comments
 (0)