From 41df4c6353186b508ccda677f3ddb056f535e953 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 28 Mar 2022 12:29:17 -0400 Subject: [PATCH] LibJS: Modernize InitializeDateTimeFormat and simplify error handling This is an editorial change in the Intl spec. See: https://github.com/tc39/ecma402/commit/4cf1d2c --- .../Intl/DateTimeFormatConstructor.cpp | 84 ++++++++++--------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp index 9f53c36bcd..dbb5527c72 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp @@ -90,7 +90,7 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales). auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value)); - // 2. Let options be ? ToDateTimeOptions(options, "any", "date"). + // 2. Set options to ? ToDateTimeOptions(options, "any", "date"). auto* options = TRY(to_date_time_options(global_object, options_value, OptionRequired::Any, OptionDefaults::Date)); // 3. Let opt be a new Record. @@ -136,7 +136,7 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo // 14. If hour12 is not undefined, then if (!hour12.is_undefined()) { - // a. Let hourCycle be null. + // a. Set hourCycle to null. hour_cycle = js_null(); } @@ -151,7 +151,7 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo // 18. Set dateTimeFormat.[[Locale]] to r.[[locale]]. date_time_format.set_locale(move(result.locale)); - // 19. Let calendar be r.[[ca]]. + // 19. Set calendar to r.[[ca]]. // 20. Set dateTimeFormat.[[Calendar]] to calendar. if (result.ca.has_value()) date_time_format.set_calendar(result.ca.release_value()); @@ -234,7 +234,7 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo // 33. If timeZone is undefined, then if (time_zone_value.is_undefined()) { - // a. Set timeZone to DefaultTimeZone(). + // a. Set timeZone to ! DefaultTimeZone(). time_zone = Temporal::default_time_zone(); } // 34. Else, @@ -242,21 +242,25 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo // a. Set timeZone to ? ToString(timeZone). time_zone = TRY(time_zone_value.to_string(global_object)); - // b. If the result of IsValidTimeZoneName(timeZone) is false, then + // b. If the result of ! IsValidTimeZoneName(timeZone) is false, then if (!Temporal::is_valid_time_zone_name(time_zone)) { // i. Throw a RangeError exception. return vm.throw_completion(global_object, ErrorType::OptionIsNotValidValue, time_zone, vm.names.timeZone); } - // c. Set timeZone to CanonicalizeTimeZoneName(timeZone). + // c. Set timeZone to ! CanonicalizeTimeZoneName(timeZone). time_zone = Temporal::canonicalize_time_zone_name(time_zone); } // 35. Set dateTimeFormat.[[TimeZone]] to timeZone. date_time_format.set_time_zone(move(time_zone)); - // 36. For each row of Table 6, except the header row, in table order, do - TRY(for_each_calendar_field(global_object, format_options, [&](auto& option, auto const& property, auto const& defaults) -> ThrowCompletionOr { + // 36. Let hasExplicitFormatComponents be false. + // NOTE: Instead of using a boolean, we track any explicitly provided component name for nicer exception messages. + PropertyKey const* explicit_format_component = nullptr; + + // 37. For each row of Table 6, except the header row, in table order, do + TRY(for_each_calendar_field(global_object, format_options, [&](auto& option, auto const& property, auto const& values) -> ThrowCompletionOr { using ValueType = typename RemoveReference::ValueType; // a. Let prop be the name given in the Property column of the row. @@ -267,61 +271,65 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo auto value = TRY(get_number_option(global_object, *options, property, 1, 3, {})); // d. Set formatOptions.[[]] to value. - if (value.has_value()) + if (value.has_value()) { option = static_cast(value.value()); + + // e. If value is not undefined, then + // i. Set hasExplicitFormatComponents to true. + explicit_format_component = &property; + } } // c. Else, else { - // i. Let value be ? GetOption(options, prop, "string", « the strings given in the Values column of the row », undefined). - auto value = TRY(get_option(global_object, *options, property, Value::Type::String, defaults, Empty {})); + // i. Let values be a List whose elements are the strings given in the Values column of the row. + // ii. Let value be ? GetOption(options, prop, "string", values, undefined). + auto value = TRY(get_option(global_object, *options, property, Value::Type::String, values, Empty {})); // d. Set formatOptions.[[]] to value. - if (!value.is_undefined()) + if (!value.is_undefined()) { option = Unicode::calendar_pattern_style_from_string(value.as_string().string()); + + // e. If value is not undefined, then + // i. Set hasExplicitFormatComponents to true. + explicit_format_component = &property; + } } return {}; })); - // 37. Let matcher be ? GetOption(options, "formatMatcher", "string", « "basic", "best fit" », "best fit"). + // 38. Let matcher be ? GetOption(options, "formatMatcher", "string", « "basic", "best fit" », "best fit"). matcher = TRY(get_option(global_object, *options, vm.names.formatMatcher, Value::Type::String, AK::Array { "basic"sv, "best fit"sv }, "best fit"sv)); - // 38. Let dateStyle be ? GetOption(options, "dateStyle", "string", « "full", "long", "medium", "short" », undefined). + // 39. Let dateStyle be ? GetOption(options, "dateStyle", "string", « "full", "long", "medium", "short" », undefined). auto date_style = TRY(get_option(global_object, *options, vm.names.dateStyle, Value::Type::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {})); - // 39. Set dateTimeFormat.[[DateStyle]] to dateStyle. + // 40. Set dateTimeFormat.[[DateStyle]] to dateStyle. if (!date_style.is_undefined()) date_time_format.set_date_style(date_style.as_string().string()); - // 40. Let timeStyle be ? GetOption(options, "timeStyle", "string", « "full", "long", "medium", "short" », undefined). + // 41. Let timeStyle be ? GetOption(options, "timeStyle", "string", « "full", "long", "medium", "short" », undefined). auto time_style = TRY(get_option(global_object, *options, vm.names.timeStyle, Value::Type::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {})); - // 41. Set dateTimeFormat.[[TimeStyle]] to timeStyle. + // 42. Set dateTimeFormat.[[TimeStyle]] to timeStyle. if (!time_style.is_undefined()) date_time_format.set_time_style(time_style.as_string().string()); Optional best_format {}; - // 42. If dateStyle is not undefined or timeStyle is not undefined, then + // 43. If dateStyle is not undefined or timeStyle is not undefined, then if (date_time_format.has_date_style() || date_time_format.has_time_style()) { - // a. For each row in Table 6, except the header row, do - TRY(for_each_calendar_field(global_object, format_options, [&](auto const& option, auto const& property, auto const&) -> ThrowCompletionOr { - // i. Let prop be the name given in the Property column of the row. - // ii. Let p be formatOptions.[[]]. - // iii. If p is not undefined, then - if (option.has_value()) { - // 1. Throw a TypeError exception. - return vm.throw_completion(global_object, ErrorType::IntlInvalidDateTimeFormatOption, property, "dateStyle or timeStyle"sv); - } - - return {}; - })); + // a. If hasExplicitFormatComponents is true, then + if (explicit_format_component != nullptr) { + // i. Throw a TypeError exception. + return vm.throw_completion(global_object, ErrorType::IntlInvalidDateTimeFormatOption, *explicit_format_component, "dateStyle or timeStyle"sv); + } // b. Let styles be dataLocaleData.[[styles]].[[]]. // c. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles). best_format = date_time_style_format(data_locale, date_time_format); } - // 43. Else, + // 44. Else, else { // a. Let formats be dataLocaleData.[[formats]].[[]]. auto formats = Unicode::get_calendar_available_formats(data_locale, date_time_format.calendar()); @@ -338,7 +346,7 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo } } - // 44. For each row in Table 6, except the header row, in table order, do + // 45. For each row in Table 6, except the header row, in table order, do date_time_format.for_each_calendar_field_zipped_with(*best_format, [&](auto& date_time_format_field, auto const& best_format_field, auto) { // a. Let prop be the name given in the Property column of the row. // b. If bestFormat has a field [[]], then @@ -352,13 +360,13 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo String pattern; Vector range_patterns; - // 45. If dateTimeFormat.[[Hour]] is undefined, then + // 46. If dateTimeFormat.[[Hour]] is undefined, then if (!date_time_format.has_hour()) { // a. Set dateTimeFormat.[[HourCycle]] to undefined. date_time_format.clear_hour_cycle(); } - // 46. If dateTimeformat.[[HourCycle]] is "h11" or "h12", then + // 47. If dateTimeformat.[[HourCycle]] is "h11" or "h12", then if ((hour_cycle_value == Unicode::HourCycle::H11) || (hour_cycle_value == Unicode::HourCycle::H12)) { // a. Let pattern be bestFormat.[[pattern12]]. if (best_format->pattern12.has_value()) { @@ -372,7 +380,7 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo // b. Let rangePatterns be bestFormat.[[rangePatterns12]]. range_patterns = Unicode::get_calendar_range12_formats(data_locale, date_time_format.calendar(), best_format->skeleton); } - // 47. Else, + // 48. Else, else { // a. Let pattern be bestFormat.[[pattern]]. pattern = move(best_format->pattern); @@ -381,13 +389,13 @@ ThrowCompletionOr initialize_date_time_format(GlobalObject& glo range_patterns = Unicode::get_calendar_range_formats(data_locale, date_time_format.calendar(), best_format->skeleton); } - // 48. Set dateTimeFormat.[[Pattern]] to pattern. + // 49. Set dateTimeFormat.[[Pattern]] to pattern. date_time_format.set_pattern(move(pattern)); - // 49. Set dateTimeFormat.[[RangePatterns]] to rangePatterns. + // 50. Set dateTimeFormat.[[RangePatterns]] to rangePatterns. date_time_format.set_range_patterns(move(range_patterns)); - // 50. Return dateTimeFormat. + // 51. Return dateTimeFormat. return &date_time_format; }