Skip to content

Commit c4317b0

Browse files
anbaMs2ger
authored andcommitted
Ensure ToDateTimeFormattable is called for both arguments
1 parent f8231fc commit c4317b0

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-intl.datetimeformat.prototype.formatRange
6+
description: >
7+
ToDateTimeFormattable is called on both arguments before checking if the
8+
arguments have a different kind.
9+
info: |
10+
Intl.DateTimeFormat.prototype.formatRange ( startDate, endDate )
11+
12+
...
13+
4. Let x be ? ToNumberToDateTimeFormattable(startDate).
14+
5. Let y be ? ToNumberToDateTimeFormattable(endDate).
15+
6. Return ? FormatDateTimeRange(dtf, x, y).
16+
17+
ToDateTimeFormattable ( value )
18+
19+
1. If IsTemporalObject(value) is true, return value.
20+
2. Return ? ToNumber(value).
21+
22+
FormatDateTimeRange ( dateTimeFormat, x, y )
23+
24+
1. Let parts be ? PartitionDateTimeRangePattern(dateTimeFormat, x, y).
25+
...
26+
27+
PartitionDateTimeRangePattern ( dateTimeFormat, x, y )
28+
29+
...
30+
5. If IsTemporalObject(x) is true or IsTemporalObject(y) is true, then
31+
a. If SameTemporalType(x, y) is false, throw a TypeError exception.
32+
...
33+
features: [Temporal]
34+
---*/
35+
36+
var callCount = 0;
37+
38+
var invalidDateValue = {
39+
valueOf() {
40+
callCount += 1;
41+
42+
// TimeClip(NaN) throws a RangeError in HandleDateTimeOthers, so if we see
43+
// a RangeError below, it means the implementation incorrectly calls
44+
// HandleDateTimeOthers before checking for different argument kinds.
45+
return NaN;
46+
}
47+
};
48+
49+
var objects = [
50+
new Temporal.PlainDate(1970, 1, 1),
51+
new Temporal.PlainDateTime(1970, 1, 1),
52+
new Temporal.PlainTime(),
53+
new Temporal.PlainYearMonth(1970, 1),
54+
new Temporal.PlainMonthDay(1, 1),
55+
new Temporal.ZonedDateTime(0n, "UTC"),
56+
new Temporal.Instant(0n),
57+
];
58+
59+
var dtf = new Intl.DateTimeFormat();
60+
61+
for (var i = 0; i < objects.length; ++i) {
62+
var object = objects[i];
63+
64+
assert.sameValue(callCount, i * 2);
65+
66+
assert.throws(TypeError, function() {
67+
dtf.formatRange(invalidDateValue, object);
68+
});
69+
70+
assert.sameValue(callCount, i * 2 + 1);
71+
72+
assert.throws(TypeError, function() {
73+
dtf.formatRange(object, invalidDateValue);
74+
});
75+
76+
assert.sameValue(callCount, i * 2 + 2);
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-Intl.DateTimeFormat.prototype.formatRangeToParts
6+
description: >
7+
ToDateTimeFormattable is called on both arguments before checking if the
8+
arguments have a different kind.
9+
info: |
10+
Intl.DateTimeFormat.prototype.formatRangeToParts ( startDate, endDate )
11+
12+
...
13+
4. Let x be ? ToNumberToDateTimeFormattable(startDate).
14+
5. Let y be ? ToNumberToDateTimeFormattable(endDate).
15+
6. Return ? FormatDateTimeRangeToParts(dtf, x, y).
16+
17+
ToDateTimeFormattable ( value )
18+
19+
1. If IsTemporalObject(value) is true, return value.
20+
2. Return ? ToNumber(value).
21+
22+
FormatDateTimeRangeToParts ( dateTimeFormat, x, y )
23+
24+
1. Let parts be ? PartitionDateTimeRangePattern(dateTimeFormat, x, y).
25+
...
26+
27+
PartitionDateTimeRangePattern ( dateTimeFormat, x, y )
28+
29+
...
30+
5. If IsTemporalObject(x) is true or IsTemporalObject(y) is true, then
31+
a. If SameTemporalType(x, y) is false, throw a TypeError exception.
32+
...
33+
features: [Temporal]
34+
---*/
35+
36+
var callCount = 0;
37+
38+
var invalidDateValue = {
39+
valueOf() {
40+
callCount += 1;
41+
42+
// TimeClip(NaN) throws a RangeError in HandleDateTimeOthers, so if we see
43+
// a RangeError below, it means the implementation incorrectly calls
44+
// HandleDateTimeOthers before checking for different argument kinds.
45+
return NaN;
46+
}
47+
};
48+
49+
var objects = [
50+
new Temporal.PlainDate(1970, 1, 1),
51+
new Temporal.PlainDateTime(1970, 1, 1),
52+
new Temporal.PlainTime(),
53+
new Temporal.PlainYearMonth(1970, 1),
54+
new Temporal.PlainMonthDay(1, 1),
55+
new Temporal.ZonedDateTime(0n, "UTC"),
56+
new Temporal.Instant(0n),
57+
];
58+
59+
var dtf = new Intl.DateTimeFormat();
60+
61+
for (var i = 0; i < objects.length; ++i) {
62+
var object = objects[i];
63+
64+
assert.sameValue(callCount, i * 2);
65+
66+
assert.throws(TypeError, function() {
67+
dtf.formatRangeToParts(invalidDateValue, object);
68+
});
69+
70+
assert.sameValue(callCount, i * 2 + 1);
71+
72+
assert.throws(TypeError, function() {
73+
dtf.formatRangeToParts(object, invalidDateValue);
74+
});
75+
76+
assert.sameValue(callCount, i * 2 + 2);
77+
}

0 commit comments

Comments
 (0)