Skip to content
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

Enforce restrictions on selector expression per spec, fixes #416 #417

Merged
merged 1 commit into from
Aug 5, 2019
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
60 changes: 60 additions & 0 deletions fluent-bundle/test/fixtures_reference/select_expressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,66 @@
],
"attributes": {}
},
{
"id": "invalid-selector-select-expression",
"value": [
{
"type": "select",
"selector": {
"type": "num",
"value": 3,
"precision": 0
},
"variants": [
{
"key": {
"type": "str",
"value": "key"
},
"value": "default"
}
],
"star": 0
}
],
"attributes": {}
},
{
"id": "invalid-selector-nested-expression",
"value": [
{
"type": "select",
"selector": {
"type": "select",
"selector": {
"type": "var",
"name": "sel"
},
"variants": [
{
"key": {
"type": "str",
"value": "key"
},
"value": "value"
}
],
"star": 0
},
"variants": [
{
"key": {
"type": "str",
"value": "key"
},
"value": "default"
}
],
"star": 0
}
],
"attributes": {}
},
{
"id": "empty-variant",
"value": [
Expand Down
2 changes: 2 additions & 0 deletions fluent-syntax/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function getErrorMessage(code, args) {
return "Unbalanced closing brace in TextElement.";
case "E0028":
return "Expected an inline expression";
case "E0029":
return "Expected simple expression as selector";
default:
return code;
}
Expand Down
30 changes: 20 additions & 10 deletions fluent-syntax/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,26 @@ class FluentParser {
return selector;
}

if (selector.type === "MessageReference") {
if (selector.attribute === null) {
throw new ParseError("E0016");
} else {
throw new ParseError("E0018");
}
}

if (selector.type === "TermReference" && selector.attribute === null) {
throw new ParseError("E0017");
// Validate selector expression according to
// abstract.js in the Fluent specification
switch (selector.type) {
case "MessageReference":
if (selector.attribute === null) {
throw new ParseError("E0016");
} else {
throw new ParseError("E0018");
}
case "TermReference":
if (selector.attribute === null) {
throw new ParseError("E0017");
}
case "StringLiteral":
case "NumberLiteral":
case "VariableReference":
case "FunctionReference":
break;
default:
throw new ParseError("E0029");
}

ps.next();
Expand Down
14 changes: 14 additions & 0 deletions fluent-syntax/test/fixtures_reference/select_expressions.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ invalid-selector-term-variant =
*[key] value
}

# ERROR Nested expressions are not valid selectors
invalid-selector-select-expression =
{ { 3 } ->
*[key] default
}

# ERROR Select expressions are not valid selectors
invalid-selector-nested-expression =
{ { $sel ->
*[key] value
} ->
*[key] default
}

empty-variant =
{ $sel ->
*[key] {""}
Expand Down
18 changes: 18 additions & 0 deletions fluent-syntax/test/fixtures_reference/select_expressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@
"annotations": [],
"content": "invalid-selector-term-variant =\n { -term(case: \"nominative\") ->\n *[key] value\n }\n\n"
},
{
"type": "Comment",
"content": "ERROR Nested expressions are not valid selectors"
},
{
"type": "Junk",
"annotations": [],
"content": "invalid-selector-select-expression =\n { { 3 } ->\n *[key] default\n }\n\n"
},
{
"type": "Comment",
"content": "ERROR Select expressions are not valid selectors"
},
{
"type": "Junk",
"annotations": [],
"content": "invalid-selector-nested-expression =\n { { $sel ->\n *[key] value\n } ->\n *[key] default\n }\n\n"
},
{
"type": "Message",
"id": {
Expand Down