Skip to content

Commit 599c482

Browse files
committed
Don't drop to newline when patterns start with special chars
1 parent a001103 commit 599c482

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

fluent-syntax/src/serializer.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as AST from "./ast.js";
22

3-
function indent(content: string): string {
3+
function indentExceptFirstLine(content: string): string {
44
return content.split("\n").join("\n ");
55
}
66

@@ -136,19 +136,29 @@ function serializeTerm(term: AST.Term): string {
136136

137137

138138
function serializeAttribute(attribute: AST.Attribute): string {
139-
const value = indent(serializePattern(attribute.value));
139+
const value = indentExceptFirstLine(serializePattern(attribute.value));
140140
return `\n .${attribute.id.name} =${value}`;
141141
}
142142

143143

144144
function serializePattern(pattern: AST.Pattern): string {
145145
const content = pattern.elements.map(serializeElement).join("");
146-
const startOnNewLine =
146+
const maybeStartOnNewLine =
147147
pattern.elements.some(isSelectExpr) ||
148148
pattern.elements.some(includesNewLine);
149149

150-
if (startOnNewLine) {
151-
return `\n ${indent(content)}`;
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+
161+
return `\n ${indentExceptFirstLine(content)}`;
152162
}
153163

154164
return ` ${content}`;
@@ -228,7 +238,7 @@ export function serializeExpression(expr: AST.Expression): string {
228238

229239
function serializeVariant(variant: AST.Variant): string {
230240
const key = serializeVariantKey(variant.key);
231-
const value = indent(serializePattern(variant.value));
241+
const value = indentExceptFirstLine(serializePattern(variant.value));
232242

233243
if (variant.default) {
234244
return `\n *[${key}]${value}`;

fluent-syntax/test/serializer_test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,30 @@ suite("Serialize resource", function() {
188188
assert.strictEqual(pretty(input), input);
189189
});
190190

191+
test("multiline starting inline", function() {
192+
const input = ftl`
193+
foo = Foo
194+
Bar
195+
196+
`;
197+
const output = ftl`
198+
foo =
199+
Foo
200+
Bar
201+
202+
`;
203+
assert.strictEqual(pretty(input), output);
204+
});
205+
206+
test("multiline starting inline with a special char", function() {
207+
const input = ftl`
208+
foo = *Foo
209+
Bar
210+
211+
`;
212+
assert.strictEqual(pretty(input), input);
213+
});
214+
191215
test("multiline with placeable", function() {
192216
const input = ftl`
193217
foo =
@@ -338,6 +362,17 @@ suite("Serialize resource", function() {
338362
assert.strictEqual(pretty(input), output);
339363
});
340364

365+
test("select expression in inline value starting with a special char", function() {
366+
const input = ftl`
367+
foo = .Foo { $sel ->
368+
*[a] A
369+
[b] B
370+
}
371+
372+
`;
373+
assert.strictEqual(pretty(input), input);
374+
});
375+
341376
test("select expression in multiline value", function() {
342377
const input = ftl`
343378
foo =

0 commit comments

Comments
 (0)