Skip to content

(fluent-syntax) Remove serializeReferenceExpression #351

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
Mar 26, 2019
Merged
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
62 changes: 26 additions & 36 deletions fluent-syntax/src/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,13 @@ function serializeElement(element) {

function serializePlaceable(placeable) {
const expr = placeable.expression;

switch (expr.type) {
case "Placeable":
return `{${serializePlaceable(expr)}}`;
case "SelectExpression":
// Special-case select expression to control the whitespace around the
// opening and the closing brace.
return `{ ${serializeSelectExpression(expr)}}`;
return `{ ${serializeExpression(expr)}}`;
default:
return `{ ${serializeExpression(expr)} }`;
}
Expand All @@ -186,15 +185,32 @@ function serializeExpression(expr) {
return expr.value;
case "VariableReference":
return `$${expr.id.name}`;
case "TermReference":
let term = {...expr, id: {name: `-${expr.id.name}`}};
return serializeReferenceExpression(term);
case "MessageReference":
return serializeReferenceExpression(expr);
case "TermReference": {
let out = `-${expr.id.name}`;
if (expr.attribute) {
out += `.${expr.attribute.name}`;
}
if (expr.arguments) {
out += serializeCallArguments(expr.arguments);
}
return out;
}
case "MessageReference": {
let out = expr.id.name;
if (expr.attribute) {
out += `.${expr.attribute.name}`;
}
return out;
}
case "FunctionReference":
return serializeReferenceExpression(expr);
case "SelectExpression":
return serializeSelectExpression(expr);
return `${expr.id.name}${serializeCallArguments(expr.arguments)}`;
case "SelectExpression": {
let out = `${serializeExpression(expr.selector)} ->`;
for (let variant of expr.variants) {
out += serializeVariant(variant);
}
return `${out}\n`;
}
case "Placeable":
return serializePlaceable(expr);
default:
Expand All @@ -203,32 +219,6 @@ function serializeExpression(expr) {
}


function serializeReferenceExpression(expr) {
let parts = [expr.id.name];
if (expr.attribute) {
parts.push(`.${expr.attribute.name}`);
}
if (expr.arguments) {
parts.push(serializeCallArguments(expr.arguments));
}
return parts.join("");
}


function serializeSelectExpression(expr) {
const parts = [];
const selector = `${serializeExpression(expr.selector)} ->`;
parts.push(selector);

for (const variant of expr.variants) {
parts.push(serializeVariant(variant));
}

parts.push("\n");
return parts.join("");
}


function serializeVariant(variant) {
const key = serializeVariantKey(variant.key);
const value = indent(serializePattern(variant.value));
Expand Down