LibJS: Ensure GetDateTimeFormat returns nonnull for the best format

This is an editorial change in the Temporal proposal. See:
https://github.com/tc39/proposal-temporal/commit/8a52801
This commit is contained in:
Timothy Flynn 2024-12-06 09:03:34 -05:00 committed by Tim Flynn
parent 7104d724ee
commit 234f218803
Notes: github-actions[bot] 2024-12-06 16:13:59 +00:00
5 changed files with 67 additions and 5 deletions

View file

@ -373,11 +373,9 @@ Optional<Unicode::CalendarPattern> get_date_time_format(Unicode::CalendarPattern
// 17. If needDefaults is true, then
if (need_defaults) {
// a. If anyPresent is true, return null.
if (any_present) {
// FIXME: Spec issue: We can hit this when setting `bestFormat`, which should never be null. Don't return for now.
// https://github.com/tc39/proposal-temporal/issues/3049
}
// a. If anyPresent is true and inherit is RELEVANT, return null.
if (any_present && inherit == OptionInherit::Relevant)
return {};
// b. For each property name prop of defaultOptions, do
options.for_each_calendar_field_zipped_with(format_options, default_options, [&](auto const&, auto& format_option) {

View file

@ -75,6 +75,22 @@ describe("errors", () => {
"Cannot format Temporal.ZonedDateTime, use Temporal.ZonedDateTime.prototype.toLocaleString"
);
});
test("Temporal fields must overlap formatter", () => {
const yearFormatter = new Intl.DateTimeFormat([], { year: "numeric", calendar: "iso8601" });
const plainMonthDay = new Temporal.PlainMonthDay(1, 1);
const dayFormatter = new Intl.DateTimeFormat([], { day: "numeric", calendar: "iso8601" });
const plainYearMonth = new Temporal.PlainYearMonth(1972, 1);
expect(() => {
yearFormatter.format(plainMonthDay);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainMonthDay");
expect(() => {
dayFormatter.format(plainYearMonth);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainYearMonth");
});
});
const d0 = Date.UTC(2021, 11, 7, 17, 40, 50, 456);

View file

@ -112,6 +112,22 @@ describe("errors", () => {
"Cannot format a date-time range with different date-time types"
);
});
test("Temporal fields must overlap formatter", () => {
const yearFormatter = new Intl.DateTimeFormat([], { year: "numeric", calendar: "iso8601" });
const plainMonthDay = new Temporal.PlainMonthDay(1, 1);
const dayFormatter = new Intl.DateTimeFormat([], { day: "numeric", calendar: "iso8601" });
const plainYearMonth = new Temporal.PlainYearMonth(1972, 1);
expect(() => {
yearFormatter.formatRange(plainMonthDay, plainMonthDay);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainMonthDay");
expect(() => {
dayFormatter.formatRange(plainYearMonth, plainYearMonth);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainYearMonth");
});
});
const d0 = Date.UTC(1989, 0, 23, 7, 8, 9, 45);

View file

@ -112,6 +112,22 @@ describe("errors", () => {
"Cannot format a date-time range with different date-time types"
);
});
test("Temporal fields must overlap formatter", () => {
const yearFormatter = new Intl.DateTimeFormat([], { year: "numeric", calendar: "iso8601" });
const plainMonthDay = new Temporal.PlainMonthDay(1, 1);
const dayFormatter = new Intl.DateTimeFormat([], { day: "numeric", calendar: "iso8601" });
const plainYearMonth = new Temporal.PlainYearMonth(1972, 1);
expect(() => {
yearFormatter.formatRangeToParts(plainMonthDay, plainMonthDay);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainMonthDay");
expect(() => {
dayFormatter.formatRangeToParts(plainYearMonth, plainYearMonth);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainYearMonth");
});
});
const d0 = Date.UTC(1989, 0, 23, 7, 8, 9, 45);

View file

@ -71,6 +71,22 @@ describe("errors", () => {
"Cannot format Temporal.ZonedDateTime, use Temporal.ZonedDateTime.prototype.toLocaleString"
);
});
test("Temporal fields must overlap formatter", () => {
const yearFormatter = new Intl.DateTimeFormat([], { year: "numeric", calendar: "iso8601" });
const plainMonthDay = new Temporal.PlainMonthDay(1, 1);
const dayFormatter = new Intl.DateTimeFormat([], { day: "numeric", calendar: "iso8601" });
const plainYearMonth = new Temporal.PlainYearMonth(1972, 1);
expect(() => {
yearFormatter.formatToParts(plainMonthDay);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainMonthDay");
expect(() => {
dayFormatter.formatToParts(plainYearMonth);
}).toThrowWithMessage(TypeError, "Unable to determine format for Temporal.PlainYearMonth");
});
});
const d = Date.UTC(1989, 0, 23, 7, 8, 9, 45);