Skip to content

Commit cb22f54

Browse files
committed
(fluent) Enforce the default variant during parsing
1 parent df1de5e commit cb22f54

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ERROR No blanks are allowed between * and [.
2+
err01 = { $sel ->
3+
* [key] Value
4+
}
5+
6+
# ERROR Missing default variant.
7+
err02 = { $sel ->
8+
[key] Value
9+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"type": "Resource",
3+
"body": [
4+
{
5+
"type": "Comment",
6+
"content": "ERROR No blanks are allowed between * and [.",
7+
"span": {
8+
"type": "Span",
9+
"start": 0,
10+
"end": 46
11+
}
12+
},
13+
{
14+
"type": "Junk",
15+
"annotations": [
16+
{
17+
"type": "Annotation",
18+
"code": "E0011",
19+
"args": [],
20+
"message": "Expected at least one variant after \"->\"",
21+
"span": {
22+
"type": "Span",
23+
"start": 69,
24+
"end": 69
25+
}
26+
}
27+
],
28+
"content": "err01 = { $sel ->\n * [key] Value\n}\n\n",
29+
"span": {
30+
"type": "Span",
31+
"start": 47,
32+
"end": 87
33+
}
34+
},
35+
{
36+
"type": "Comment",
37+
"content": "ERROR Missing default variant.",
38+
"span": {
39+
"type": "Span",
40+
"start": 87,
41+
"end": 119
42+
}
43+
},
44+
{
45+
"type": "Junk",
46+
"annotations": [
47+
{
48+
"type": "Annotation",
49+
"code": "E0010",
50+
"args": [],
51+
"message": "Expected one of the variants to be marked as default (*)",
52+
"span": {
53+
"type": "Span",
54+
"start": 154,
55+
"end": 154
56+
}
57+
}
58+
],
59+
"content": "err02 = { $sel ->\n [key] Value\n}\n",
60+
"span": {
61+
"type": "Span",
62+
"start": 120,
63+
"end": 156
64+
}
65+
}
66+
],
67+
"span": {
68+
"type": "Span",
69+
"start": 0,
70+
"end": 156
71+
}
72+
}

fluent/src/resource.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,15 @@ export default class FluentResource extends Map {
367367
variants[count++] = {key, value};
368368
}
369369

370-
return count > 0 ? {variants, star} : null;
370+
if (count === 0) {
371+
return null;
372+
}
373+
374+
if (star === undefined) {
375+
throw new FluentError("Expected default variant");
376+
}
377+
378+
return {variants, star};
371379
}
372380

373381
function parseVariantKey() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)