Skip to content

Commit c00b4c0

Browse files
authored
Merge pull request #368 from stasm/print-braces
Print braces around missing references
2 parents 5593ceb + 24355cf commit c00b4c0

9 files changed

+146
-56
lines changed

fluent/src/builtins.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@
1111
* `FluentType`. Functions must return `FluentType` objects as well.
1212
*/
1313

14-
import { FluentNumber, FluentDateTime } from "./types.js";
15-
16-
export default {
17-
"NUMBER": ([arg], opts) =>
18-
new FluentNumber(arg.valueOf(), merge(arg.opts, opts)),
19-
"DATETIME": ([arg], opts) =>
20-
new FluentDateTime(arg.valueOf(), merge(arg.opts, opts)),
21-
};
14+
import { FluentNone, FluentNumber, FluentDateTime } from "./types.js";
2215

2316
function merge(argopts, opts) {
2417
return Object.assign({}, argopts, values(opts));
@@ -31,3 +24,25 @@ function values(opts) {
3124
}
3225
return unwrapped;
3326
}
27+
28+
export
29+
function NUMBER([arg], opts) {
30+
if (arg instanceof FluentNone) {
31+
return arg;
32+
}
33+
if (arg instanceof FluentNumber) {
34+
return new FluentNumber(arg.valueOf(), merge(arg.opts, opts));
35+
}
36+
return new FluentNone("NUMBER()");
37+
}
38+
39+
export
40+
function DATETIME([arg], opts) {
41+
if (arg instanceof FluentNone) {
42+
return arg;
43+
}
44+
if (arg instanceof FluentDateTime) {
45+
return new FluentDateTime(arg.valueOf(), merge(arg.opts, opts));
46+
}
47+
return new FluentNone("DATETIME()");
48+
}

fluent/src/resolver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
import { FluentType, FluentNone, FluentNumber, FluentDateTime }
3939
from "./types.js";
40-
import builtins from "./builtins.js";
40+
import * as builtins from "./builtins.js";
4141

4242
// Prevent expansion of too long placeables.
4343
const MAX_PLACEABLE_LENGTH = 2500;

fluent/src/types.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ export class FluentType {
4545
}
4646

4747
export class FluentNone extends FluentType {
48+
valueOf() {
49+
return null;
50+
}
51+
4852
toString() {
49-
return this.value || "???";
53+
return `{${this.value || "???"}}`;
5054
}
5155
}
5256

fluent/test/arguments_test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,49 @@ suite('Variables', function() {
101101
test('falls back to argument\'s name if it\'s missing', function() {
102102
const msg = bundle.getMessage('foo');
103103
const val = bundle.format(msg, {}, errs);
104-
assert.equal(val, '$arg');
104+
assert.equal(val, '{$arg}');
105105
assert(errs[0] instanceof ReferenceError); // unknown variable
106106
});
107107

108108
test('cannot be arrays', function() {
109109
const msg = bundle.getMessage('foo');
110110
const val = bundle.format(msg, { arg: [1, 2, 3] }, errs);
111-
assert.equal(val, '$arg');
111+
assert.equal(val, '{$arg}');
112112
assert(errs[0] instanceof TypeError); // unsupported variable type
113113
});
114114

115115
test('cannot be a dict-like object', function() {
116116
const msg = bundle.getMessage('foo');
117117
const val = bundle.format(msg, { arg: { prop: 1 } }, errs);
118-
assert.equal(val, '$arg');
118+
assert.equal(val, '{$arg}');
119119
assert(errs[0] instanceof TypeError); // unsupported variable type
120120
});
121121

122122
test('cannot be a boolean', function() {
123123
const msg = bundle.getMessage('foo');
124124
const val = bundle.format(msg, { arg: true }, errs);
125-
assert.equal(val, '$arg');
125+
assert.equal(val, '{$arg}');
126126
assert(errs[0] instanceof TypeError); // unsupported variable type
127127
});
128128

129129
test('cannot be undefined', function() {
130130
const msg = bundle.getMessage('foo');
131131
const val = bundle.format(msg, { arg: undefined }, errs);
132-
assert.equal(val, '$arg');
132+
assert.equal(val, '{$arg}');
133133
assert(errs[0] instanceof TypeError); // unsupported variable type
134134
});
135135

136136
test('cannot be null', function() {
137137
const msg = bundle.getMessage('foo');
138138
const val = bundle.format(msg, { arg: null }, errs);
139-
assert.equal(val, '$arg');
139+
assert.equal(val, '{$arg}');
140140
assert(errs[0] instanceof TypeError); // unsupported variable type
141141
});
142142

143143
test('cannot be a function', function() {
144144
const msg = bundle.getMessage('foo');
145145
const val = bundle.format(msg, { arg: () => null }, errs);
146-
assert.equal(val, '$arg');
146+
assert.equal(val, '{$arg}');
147147
assert(errs[0] instanceof TypeError); // unsupported variable type
148148
});
149149
});

fluent/test/functions_builtin_test.js

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ suite('Built-in functions', function() {
2222
let msg;
2323

2424
msg = bundle.getMessage('num-decimal');
25-
assert.equal(bundle.format(msg), 'NaN');
25+
assert.equal(bundle.format(msg), '{$arg}');
2626

2727
msg = bundle.getMessage('num-percent');
28-
assert.equal(bundle.format(msg), 'NaN');
28+
assert.equal(bundle.format(msg), '{$arg}');
2929

3030
msg = bundle.getMessage('num-bad-opt');
31-
assert.equal(bundle.format(msg), 'NaN');
31+
assert.equal(bundle.format(msg), '{$arg}');
3232
});
3333

3434
test('number argument', function() {
@@ -50,13 +50,42 @@ suite('Built-in functions', function() {
5050
let msg;
5151

5252
msg = bundle.getMessage('num-decimal');
53-
assert.equal(bundle.format(msg, args), 'NaN');
53+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
5454

5555
msg = bundle.getMessage('num-percent');
56-
assert.equal(bundle.format(msg, args), 'NaN');
56+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
5757

5858
msg = bundle.getMessage('num-bad-opt');
59-
assert.equal(bundle.format(msg, args), 'NaN');
59+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
60+
});
61+
62+
test('date argument', function() {
63+
const date = new Date('2016-09-29');
64+
const args = {arg: date};
65+
let msg;
66+
67+
msg = bundle.getMessage('num-decimal');
68+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
69+
70+
msg = bundle.getMessage('num-percent');
71+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
72+
73+
msg = bundle.getMessage('num-bad-opt');
74+
assert.equal(bundle.format(msg, args), '{NUMBER()}');
75+
});
76+
77+
test('invalid argument', function() {
78+
const args = {arg: []};
79+
let msg;
80+
81+
msg = bundle.getMessage('num-decimal');
82+
assert.equal(bundle.format(msg, args), '{$arg}');
83+
84+
msg = bundle.getMessage('num-percent');
85+
assert.equal(bundle.format(msg, args), '{$arg}');
86+
87+
msg = bundle.getMessage('num-bad-opt');
88+
assert.equal(bundle.format(msg, args), '{$arg}');
6089
});
6190
});
6291

@@ -74,13 +103,13 @@ suite('Built-in functions', function() {
74103
let msg;
75104

76105
msg = bundle.getMessage('dt-default');
77-
assert.equal(bundle.format(msg), 'Invalid Date');
106+
assert.equal(bundle.format(msg), '{$arg}');
78107

79108
msg = bundle.getMessage('dt-month');
80-
assert.equal(bundle.format(msg), 'Invalid Date');
109+
assert.equal(bundle.format(msg), '{$arg}');
81110

82111
msg = bundle.getMessage('dt-bad-opt');
83-
assert.equal(bundle.format(msg), 'Invalid Date');
112+
assert.equal(bundle.format(msg), '{$arg}');
84113
});
85114

86115
test('Date argument', function () {
@@ -107,5 +136,47 @@ suite('Built-in functions', function() {
107136
// Thu Sep 29 2016 02:00:00 GMT+0200 (CEST)
108137
assert.equal(bundle.format(msg, args), date.toString());
109138
});
139+
140+
test('number argument', function() {
141+
let args = {arg: 1};
142+
let msg;
143+
144+
msg = bundle.getMessage('dt-default');
145+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
146+
147+
msg = bundle.getMessage('dt-month');
148+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
149+
150+
msg = bundle.getMessage('dt-bad-opt');
151+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
152+
});
153+
154+
test('string argument', function() {
155+
let args = {arg: 'Foo'};
156+
let msg;
157+
158+
msg = bundle.getMessage('dt-default');
159+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
160+
161+
msg = bundle.getMessage('dt-month');
162+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
163+
164+
msg = bundle.getMessage('dt-bad-opt');
165+
assert.equal(bundle.format(msg, args), '{DATETIME()}');
166+
});
167+
168+
test('invalid argument', function() {
169+
let args = {arg: []};
170+
let msg;
171+
172+
msg = bundle.getMessage('dt-default');
173+
assert.equal(bundle.format(msg, args), '{$arg}');
174+
175+
msg = bundle.getMessage('dt-month');
176+
assert.equal(bundle.format(msg, args), '{$arg}');
177+
178+
msg = bundle.getMessage('dt-bad-opt');
179+
assert.equal(bundle.format(msg, args), '{$arg}');
180+
});
110181
});
111182
});

fluent/test/functions_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ suite('Functions', function() {
2323
test('falls back to the name of the function', function() {
2424
const msg = bundle.getMessage('foo');
2525
const val = bundle.format(msg, args, errs);
26-
assert.equal(val, 'MISSING()');
26+
assert.equal(val, '{MISSING()}');
2727
assert.equal(errs.length, 1);
2828
assert(errs[0] instanceof ReferenceError); // unknown function
2929
});

0 commit comments

Comments
 (0)