Skip to content

Expose FluentSerializer.serializeExpression #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions fluent-syntax/src/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export default class FluentSerializer {
}

serialize(resource) {
if (resource.type !== 'Resource') {
throw new Error(`Unknown resource type: ${resource.type}`);
}

const parts = [];

if (resource.comment) {
Expand Down Expand Up @@ -66,6 +70,10 @@ export default class FluentSerializer {
throw new Error(`Unknown entry type: ${entry.type}`);
}
}

serializeExpression(expr) {
return serializeExpression(expr);
}
}


Expand Down Expand Up @@ -176,7 +184,13 @@ function serializePlaceable(placeable) {
case 'Placeable':
return `{${serializePlaceable(expr)}}`;
case 'SelectExpression':
return `{${serializeSelectExpression(expr)}}`;
// Special-case select expression to control the whitespace around the
// opening and the closing brace.
return expr.expression
// A select expression with a selector.
? `{ ${serializeSelectExpression(expr)}}`
// A variant list without a selector.
: `{${serializeSelectExpression(expr)}}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use a single space before and after the expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is special-cased so that there aren't any spaces around it. Consider:

-brand-name =
    {    ← no space after the opening {
       *[nominative] Firefox
    }    ← no extra space before the closing {, just the 4-space indent

default:
return `{ ${serializeExpression(expr)} }`;
}
Expand All @@ -199,6 +213,8 @@ function serializeExpression(expr) {
return serializeVariantExpression(expr);
case 'CallExpression':
return serializeCallExpression(expr);
case 'SelectExpression':
return serializeSelectExpression(expr);
default:
throw new Error(`Unknown expression type: ${expr.type}`);
}
Expand Down Expand Up @@ -229,7 +245,7 @@ function serializeSelectExpression(expr) {
const parts = [];

if (expr.expression) {
const selector = ` ${serializeExpression(expr.expression)} ->`;
const selector = `${serializeExpression(expr.expression)} ->`;
parts.push(selector);
}

Expand Down
120 changes: 113 additions & 7 deletions fluent-syntax/test/serializer_test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import assert from 'assert';
import { ftl } from './util';

import { parse, serialize } from '../src';
import { FluentParser, FluentSerializer } from '../src';


function pretty(text) {
const res = parse(text);
return serialize(res, {
withJunk: false
suite('Serialize resource', function() {
let pretty;

setup(function() {
const parser = new FluentParser();
const serializer = new FluentSerializer({
withJunk: false
});

pretty = function pretty(text) {
const res = parser.parse(text);
return serializer.serialize(res);
}
});

test('invalid resource', function() {
const serializer = new FluentSerializer();
assert.throws(
() => serializer.serialize(null),
/Cannot read property 'type'/
);
assert.throws(
() => serializer.serialize({}),
/Unknown resource type/
);
});
}

suite('Serializer', function() {
test('simple message', function() {
const input = ftl`
foo = Foo
Expand Down Expand Up @@ -400,3 +419,90 @@ suite('Serializer', function() {
assert.equal(pretty(input), input);
});
});

suite('Serialize expression', function() {
let pretty;

setup(function() {
const parser = new FluentParser();
const serializer = new FluentSerializer({
withJunk: false
});

pretty = function pretty(text) {
const {value: {elements: [placeable]}} = parser.parseEntry(text);
return serializer.serializeExpression(placeable.expression);
}
});

test('invalid expression', function() {
const serializer = new FluentSerializer();
assert.throws(
() => serializer.serializeExpression(null),
/Cannot read property 'type'/
);
assert.throws(
() => serializer.serializeExpression({}),
/Unknown expression type/
);
});

test('string expression', function() {
const input = ftl`
foo = { "str" }
`;
assert.equal(pretty(input), '"str"');
});

test('number expression', function() {
const input = ftl`
foo = { 3 }
`;
assert.equal(pretty(input), '3');
});

test('message reference', function() {
const input = ftl`
foo = { msg }
`;
assert.equal(pretty(input), 'msg');
});

test('external argument', function() {
const input = ftl`
foo = { $ext }
`;
assert.equal(pretty(input), '$ext');
});

test('attribute expression', function() {
const input = ftl`
foo = { msg.attr }
`;
assert.equal(pretty(input), 'msg.attr');
});

test('variant expression', function() {
const input = ftl`
foo = { -msg[variant] }
`;
assert.equal(pretty(input), '-msg[variant]');
});

test('call expression', function() {
const input = ftl`
foo = { BUILTIN(3.14, kwarg: "value") }
`;
assert.equal(pretty(input), 'BUILTIN(3.14, kwarg: "value")');
});

test('select expression', function() {
const input = ftl`
foo =
{ $num ->
*[one] One
}
`;
assert.equal(pretty(input), '$num ->\n *[one] One\n');
});
});