Browse Source

LibJS+LibIMAP: Use the new Optional<U>(Optional<T>) constructor

These look much nicer than these repeated ternaries :^)
Idan Horowitz 3 years ago
parent
commit
ace36681ff

+ 2 - 2
Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp

@@ -725,9 +725,9 @@ static NonnullOwnPtr<Interface> parse_interface(StringView filename, StringView
             DictionaryMember member {
                 required,
                 move(type),
-                move(name),
+                name,
                 move(extended_attributes),
-                default_value.has_value() ? default_value.value() : Optional<String> {},
+                Optional<String>(move(default_value)),
             };
             dictionary.members.append(move(member));
         }

+ 11 - 12
Userland/Libraries/LibIMAP/Parser.cpp

@@ -315,7 +315,7 @@ FetchResponseData Parser::parse_fetch_response()
             break;
         case FetchCommand::DataItemType::BodySection: {
             auto body = parse_nstring();
-            fetch_response.add_body_data(move(data_item), body.has_value() ? body.release_value() : Optional<String>());
+            fetch_response.add_body_data(move(data_item), Optional<String>(move(body)));
             break;
         }
         }
@@ -434,8 +434,8 @@ BodyStructure Parser::parse_one_part_body()
         auto data = BodyStructureData {
             type,
             subtype,
-            id.has_value() ? Optional<String>(id.value()) : Optional<String>(),
-            description.has_value() ? Optional<String>(description.value()) : Optional<String>(),
+            Optional<String>(move(id)),
+            Optional<String>(move(description)),
             encoding,
             params,
             num_octets,
@@ -496,8 +496,8 @@ BodyStructure Parser::parse_one_part_body()
         BodyStructureData data {
             type,
             subtype,
-            id.has_value() ? Optional<String>(id.value()) : Optional<String>(),
-            description.has_value() ? Optional<String>(description.value()) : Optional<String>(),
+            Optional<String>(move(id)),
+            Optional<String>(move(description)),
             encoding,
             params,
             num_octets,
@@ -522,8 +522,8 @@ BodyStructure Parser::parse_one_part_body()
         BodyStructureData data {
             type,
             subtype,
-            id.has_value() ? Optional<String>(id.value()) : Optional<String>(),
-            description.has_value() ? Optional<String>(description.value()) : Optional<String>(),
+            Optional<String>(move(id)),
+            Optional<String>(move(description)),
             encoding,
             params,
             num_octets,
@@ -802,18 +802,17 @@ Address Parser::parse_address()
 {
     consume("(");
     auto address = Address();
-    // I hate this so much. Why is there no Optional.map??
     auto name = parse_nstring();
-    address.name = name.has_value() ? Optional<String>(name.value()) : Optional<String>();
+    address.name = Optional<String>(move(name));
     consume(" ");
     auto source_route = parse_nstring();
-    address.source_route = source_route.has_value() ? Optional<String>(source_route.value()) : Optional<String>();
+    address.source_route = Optional<String>(move(source_route));
     consume(" ");
     auto mailbox = parse_nstring();
-    address.mailbox = mailbox.has_value() ? Optional<String>(mailbox.value()) : Optional<String>();
+    address.mailbox = Optional<String>(move(mailbox));
     consume(" ");
     auto host = parse_nstring();
-    address.host = host.has_value() ? Optional<String>(host.value()) : Optional<String>();
+    address.host = Optional<String>(move(host));
     consume(")");
     return address;
 }

+ 3 - 7
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -1220,7 +1220,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(GlobalObject& global_object,
         return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidTime);
 
     // 20. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond, [[Calendar]]: calendar }.
-    return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = calendar_part.has_value() ? *calendar_part : Optional<String>() };
+    return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = Optional<String>(move(calendar_part)) };
 }
 
 // 13.34 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring
@@ -1626,10 +1626,6 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
 {
     auto& vm = global_object.vm();
 
-    auto optional_string_view_to_optional_string = [](Optional<StringView> const& s) {
-        return s.has_value() ? String { *s } : Optional<String> {};
-    };
-
     // 1. Assert: Type(isoString) is String.
 
     // 2. Let parseResult be ParseText(! StringToCodePoints(isoString), TemporalTimeZoneString).
@@ -1655,7 +1651,7 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
     // 7. If z is not empty, then
     if (z.has_value()) {
         // a. Return the Record { [[Z]]: true, [[OffsetString]]: undefined, [[Name]]: name }.
-        return TemporalTimeZone { .z = true, .offset_string = {}, .name = optional_string_view_to_optional_string(name) };
+        return TemporalTimeZone { .z = true, .offset_string = {}, .name = Optional<String>(move(name)) };
     }
 
     // 8. If offsetString is empty, then
@@ -1665,7 +1661,7 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject
     // NOTE: No-op.
 
     // 10. Return the Record { [[Z]]: false, [[OffsetString]]: offsetString, [[Name]]: name }.
-    return TemporalTimeZone { .z = false, .offset_string = optional_string_view_to_optional_string(offset_string), .name = optional_string_view_to_optional_string(name) };
+    return TemporalTimeZone { .z = false, .offset_string = Optional<String>(move(offset_string)), .name = Optional<String>(move(name)) };
 }
 
 // 13.44 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp

@@ -425,7 +425,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::round)
     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
 
     // 19. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo, maximum, false).
-    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *round_to, maximum.has_value() ? *maximum : Optional<double> {}, false));
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *round_to, Optional<double>(maximum), false));
 
     // 20. Let relativeTo be ? ToRelativeTemporalObject(roundTo).
     auto relative_to = TRY(to_relative_temporal_object(global_object, *round_to));

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp

@@ -544,7 +544,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::until)
     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
 
     // 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
-    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
 
     // 13. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options).
     auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), *largest_unit, options));
@@ -601,7 +601,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::since)
     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
 
     // 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
-    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
 
     // 14. Let diff be ? DifferenceISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], dateTime.[[Calendar]], largestUnit, options).
     auto diff = TRY(difference_iso_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), date_time->calendar(), *largest_unit, options));

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp

@@ -281,7 +281,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::until)
     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
 
     // 10. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
-    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
 
     // 11. Let result be ! DifferenceTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]]).
     auto result = difference_time(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond());
@@ -328,7 +328,7 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::since)
     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
 
     // 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
-    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
 
     // 12. Let result be ! DifferenceTime(other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]], temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]]).
     auto result = difference_time(other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond(), temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond());

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp

@@ -982,7 +982,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::until)
     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
 
     // 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
-    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
 
     // 13. If largestUnit is not one of "year", "month", "week", or "day", then
     if (!largest_unit->is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {
@@ -1059,7 +1059,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::since)
     auto maximum = maximum_temporal_duration_rounding_increment(*smallest_unit);
 
     // 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, maximum, false).
-    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, maximum.has_value() ? *maximum : Optional<double> {}, false));
+    auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional<double>(maximum), false));
 
     // 14. If largestUnit is not one of "year", "month", "week", or "day", then
     if (!largest_unit->is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {