Skip to content

Commit fe9584c

Browse files
authored
(fluent-syntax) Export serializeExpression, serializeVariantKey (#350)
1 parent bbcb340 commit fe9584c

File tree

4 files changed

+73
-20
lines changed

4 files changed

+73
-20
lines changed

fluent-syntax/src/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import FluentParser from "./parser";
2-
import FluentSerializer from "./serializer";
1+
import {FluentParser} from "./parser";
2+
import {FluentSerializer} from "./serializer";
33

44
export * from "./ast";
5-
export { FluentParser, FluentSerializer };
5+
export * from "./parser";
6+
export * from "./serializer";
67
export * from "./visitor";
78

89
export function parse(source, opts) {

fluent-syntax/src/parser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function withSpan(fn) {
3030
}
3131

3232

33-
export default class FluentParser {
33+
export
34+
class FluentParser {
3435
constructor({
3536
withSpans = true,
3637
} = {}) {

fluent-syntax/src/serializer.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ function isSelectExpr(elem) {
1313
&& elem.expression.type === "SelectExpression";
1414
}
1515

16+
export
1617
// Bit masks representing the state of the serializer.
17-
export const HAS_ENTRIES = 1;
18+
const HAS_ENTRIES = 1;
1819

19-
export default class FluentSerializer {
20+
export
21+
class FluentSerializer {
2022
constructor({ withJunk = false } = {}) {
2123
this.withJunk = withJunk;
2224
}
@@ -68,10 +70,6 @@ export default class FluentSerializer {
6870
throw new Error(`Unknown entry type: ${entry.type}`);
6971
}
7072
}
71-
72-
serializeExpression(expr) {
73-
return serializeExpression(expr);
74-
}
7573
}
7674

7775

@@ -208,6 +206,7 @@ function serializePlaceable(placeable) {
208206
}
209207

210208

209+
export
211210
function serializeExpression(expr) {
212211
switch (expr.type) {
213212
case "StringLiteral":
@@ -281,11 +280,14 @@ function serializeNamedArgument(arg) {
281280
}
282281

283282

283+
export
284284
function serializeVariantKey(key) {
285285
switch (key.type) {
286286
case "Identifier":
287287
return key.name;
288+
case "NumberLiteral":
289+
return key.value;
288290
default:
289-
return serializeExpression(key);
291+
throw new Error(`Unknown variant key type: ${key.type}`);
290292
}
291293
}

fluent-syntax/test/serializer_test.js

+58-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import assert from "assert";
22
import { ftl } from "./util";
33

4-
import { FluentParser, FluentSerializer } from "../src";
4+
import {
5+
FluentParser, FluentSerializer, serializeExpression, serializeVariantKey
6+
} from "../src";
57

68

79
suite("Serialize resource", function() {
@@ -486,29 +488,25 @@ suite("Serialize resource", function() {
486488
});
487489
});
488490

489-
suite("Serialize expression", function() {
491+
suite("serializeExpression", function() {
490492
let pretty;
491493

492494
setup(function() {
493495
const parser = new FluentParser();
494-
const serializer = new FluentSerializer({
495-
withJunk: false
496-
});
497496

498497
pretty = function pretty(text) {
499498
const {value: {elements: [placeable]}} = parser.parseEntry(text);
500-
return serializer.serializeExpression(placeable.expression);
499+
return serializeExpression(placeable.expression);
501500
}
502501
});
503502

504503
test("invalid expression", function() {
505-
const serializer = new FluentSerializer();
506504
assert.throws(
507-
() => serializer.serializeExpression(null),
505+
() => serializeExpression(null),
508506
/Cannot read property 'type'/
509507
);
510508
assert.throws(
511-
() => serializer.serializeExpression({}),
509+
() => serializeExpression({}),
512510
/Unknown expression type/
513511
);
514512
});
@@ -632,3 +630,54 @@ suite("Serialize padding around comments", function() {
632630
assert.equal(pretty(input), input);
633631
});
634632
});
633+
634+
suite("serializeVariantKey", function() {
635+
let prettyVariantKey;
636+
637+
setup(function() {
638+
let parser = new FluentParser();
639+
640+
prettyVariantKey = function(text, index) {
641+
let pattern = parser.parseEntry(text).value;
642+
let variants = pattern.elements[0].expression.variants;
643+
return serializeVariantKey(variants[index].key);
644+
}
645+
});
646+
647+
test("invalid expression", function() {
648+
assert.throws(
649+
() => serializeVariantKey(null),
650+
/Cannot read property 'type'/
651+
);
652+
assert.throws(
653+
() => serializeVariantKey({}),
654+
/Unknown variant key type/
655+
);
656+
});
657+
658+
test("identifiers", function() {
659+
const input = ftl`
660+
foo = { $num ->
661+
[one] One
662+
*[other] Other
663+
}
664+
`;
665+
assert.equal(prettyVariantKey(input, 0), "one");
666+
assert.equal(prettyVariantKey(input, 1), "other");
667+
});
668+
669+
test("number literals", function() {
670+
const input = ftl`
671+
foo = { $num ->
672+
[-123456789] Minus a lot
673+
[0] Zero
674+
*[3.14] Pi
675+
[007] James
676+
}
677+
`;
678+
assert.equal(prettyVariantKey(input, 0), "-123456789");
679+
assert.equal(prettyVariantKey(input, 1), "0");
680+
assert.equal(prettyVariantKey(input, 2), "3.14");
681+
assert.equal(prettyVariantKey(input, 3), "007");
682+
});
683+
});

0 commit comments

Comments
 (0)