Skip to content

Commit e00b643

Browse files
committed
(third) Use an empty keys array in single-variant messages
1 parent 4227a28 commit e00b643

File tree

6 files changed

+25
-48
lines changed

6 files changed

+25
-48
lines changed

experiments/stasm/third/example/example_glossary.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ console.log("==== English ====");
111111
lang: "en",
112112
id: "accord",
113113
phrases: {},
114-
selectors: [{expr: null, default: {type: "StringLiteral", value: "default"}}],
114+
selectors: [],
115115
variants: [
116116
{
117-
keys: [{type: "StringLiteral", value: "default"}],
117+
keys: [],
118118
value: [
119119
{type: "StringLiteral", value: "The "},
120120
{
@@ -157,10 +157,10 @@ console.log("==== English ====");
157157
lang: "en",
158158
id: "you-see",
159159
phrases: {},
160-
selectors: [{expr: null, default: {type: "StringLiteral", value: "default"}}],
160+
selectors: [],
161161
variants: [
162162
{
163-
keys: [{type: "StringLiteral", value: "default"}],
163+
keys: [],
164164
value: [
165165
{type: "StringLiteral", value: "You see "},
166166
{
@@ -188,10 +188,10 @@ console.log("==== English ====");
188188
lang: "en",
189189
id: "they-wave",
190190
phrases: {},
191-
selectors: [{expr: null, default: {type: "StringLiteral", value: "default"}}],
191+
selectors: [],
192192
variants: [
193193
{
194-
keys: [{type: "StringLiteral", value: "default"}],
194+
keys: [],
195195
value: [
196196
{
197197
type: "FunctionCall",
@@ -221,10 +221,10 @@ console.log("==== polski ====");
221221
lang: "pl",
222222
id: "accord",
223223
phrases: {},
224-
selectors: [{expr: null, default: {type: "StringLiteral", value: "default"}}],
224+
selectors: [],
225225
variants: [
226226
{
227-
keys: [{type: "StringLiteral", value: "default"}],
227+
keys: [],
228228
value: [
229229
{
230230
type: "FunctionCall",
@@ -261,10 +261,10 @@ console.log("==== polski ====");
261261
lang: "pl",
262262
id: "you-see",
263263
phrases: {},
264-
selectors: [{expr: null, default: {type: "StringLiteral", value: "default"}}],
264+
selectors: [],
265265
variants: [
266266
{
267-
keys: [{type: "StringLiteral", value: "default"}],
267+
keys: [],
268268
value: [
269269
{type: "StringLiteral", value: "Widzisz "},
270270
{
@@ -292,10 +292,10 @@ console.log("==== polski ====");
292292
lang: "pl",
293293
id: "they-wave",
294294
phrases: {},
295-
selectors: [{expr: null, default: {type: "StringLiteral", value: "default"}}],
295+
selectors: [],
296296
variants: [
297297
{
298-
keys: [{type: "StringLiteral", value: "default"}],
298+
keys: [],
299299
value: [
300300
{
301301
type: "FunctionCall",

experiments/stasm/third/example/example_number.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ console.log("==== English ====");
99
lang: "en",
1010
id: "transferred",
1111
phrases: {},
12-
selectors: [
13-
{
14-
expr: null,
15-
default: {type: "StringLiteral", value: "default"},
16-
},
17-
],
12+
selectors: [],
1813
variants: [
1914
{
20-
keys: [{type: "StringLiteral", value: "default"}],
15+
keys: [],
2116
value: [
2217
{type: "StringLiteral", value: "Transferred "},
2318
{
@@ -49,15 +44,10 @@ console.log("==== French ====");
4944
lang: "fr",
5045
id: "transferred",
5146
phrases: {},
52-
selectors: [
53-
{
54-
expr: null,
55-
default: {type: "StringLiteral", value: "default"},
56-
},
57-
],
47+
selectors: [],
5848
variants: [
5949
{
60-
keys: [{type: "StringLiteral", value: "default"}],
50+
keys: [],
6151
value: [
6252
{
6353
type: "FunctionCall",

experiments/stasm/third/example/example_opaque.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@ console.log("==== English ====");
2424
lang: "en",
2525
id: "submit",
2626
phrases: {},
27-
selectors: [
28-
{
29-
expr: null,
30-
default: {type: "StringLiteral", value: "default"},
31-
},
32-
],
27+
selectors: [],
3328
variants: [
3429
{
35-
keys: [{type: "StringLiteral", value: "default"}],
30+
keys: [],
3631
value: [
3732
{type: "StringLiteral", value: "Ready? Then "},
3833
{

experiments/stasm/third/example/example_phrases.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ console.log("==== English ====");
5858
],
5959
},
6060
},
61-
selectors: [{expr: null, default: {type: "StringLiteral", value: "default"}}],
61+
selectors: [],
6262
variants: [
6363
{
64-
keys: [{type: "StringLiteral", value: "default"}],
64+
keys: [],
6565
value: [
6666
{type: "VariableReference", name: "userName"},
6767
{type: "StringLiteral", value: " "},

experiments/stasm/third/impl/context.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,12 @@ export class FormattingContext {
6565

6666
selectVariant(variants: Array<Variant>, selectors: Array<Selector>): Variant {
6767
interface ResolvedSelector {
68-
value: RuntimeValue<unknown> | null;
68+
value: RuntimeValue<unknown>;
6969
default: string;
7070
}
7171

7272
let resolved_selectors: Array<ResolvedSelector> = [];
7373
for (let selector of selectors) {
74-
if (selector.expr === null) {
75-
// A special selector which only selects its default value. Used in the
76-
// data model of single-variant messages.
77-
resolved_selectors.push({
78-
value: null,
79-
default: selector.default.value,
80-
});
81-
continue;
82-
}
83-
8474
switch (selector.expr.type) {
8575
case "VariableReference": {
8676
resolved_selectors.push({
@@ -107,7 +97,7 @@ export class FormattingContext {
10797
let selector = resolved_selectors[idx];
10898
switch (key.type) {
10999
case "StringLiteral": {
110-
if (key.value === selector.value?.value) {
100+
if (key.value === selector.value.value) {
111101
return true;
112102
}
113103
break;
@@ -131,6 +121,8 @@ export class FormattingContext {
131121
}
132122

133123
for (let variant of variants) {
124+
// When keys is an empty array, every() always returns true. This is
125+
// used single-variant messages to return their only variant.
134126
if (variant.keys.every(matches_corresponding_selector)) {
135127
return variant;
136128
}

experiments/stasm/third/impl/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface Phrase {
1212
}
1313

1414
export interface Selector {
15-
expr: VariableReference | FunctionCall | null;
15+
expr: VariableReference | FunctionCall;
1616
default: StringLiteral;
1717
}
1818

0 commit comments

Comments
 (0)