From ace36681ffa50ef994244fed13dcae859e96f076 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sun, 23 Jan 2022 17:18:10 +0200 Subject: [PATCH] LibJS+LibIMAP: Use the new Optional(Optional) constructor These look much nicer than these repeated ternaries :^) --- .../LibWeb/WrapperGenerator.cpp | 4 ++-- Userland/Libraries/LibIMAP/Parser.cpp | 23 +++++++++---------- .../Runtime/Temporal/AbstractOperations.cpp | 10 +++----- .../Runtime/Temporal/DurationPrototype.cpp | 2 +- .../Temporal/PlainDateTimePrototype.cpp | 4 ++-- .../Runtime/Temporal/PlainTimePrototype.cpp | 4 ++-- .../Temporal/ZonedDateTimePrototype.cpp | 4 ++-- 7 files changed, 23 insertions(+), 28 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp index 6390a860392..6c4b86c9412 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp @@ -725,9 +725,9 @@ static NonnullOwnPtr parse_interface(StringView filename, StringView DictionaryMember member { required, move(type), - move(name), + name, move(extended_attributes), - default_value.has_value() ? default_value.value() : Optional {}, + Optional(move(default_value)), }; dictionary.members.append(move(member)); } diff --git a/Userland/Libraries/LibIMAP/Parser.cpp b/Userland/Libraries/LibIMAP/Parser.cpp index 89e6c02cbfe..6275d75f750 100644 --- a/Userland/Libraries/LibIMAP/Parser.cpp +++ b/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()); + fetch_response.add_body_data(move(data_item), Optional(move(body))); break; } } @@ -434,8 +434,8 @@ BodyStructure Parser::parse_one_part_body() auto data = BodyStructureData { type, subtype, - id.has_value() ? Optional(id.value()) : Optional(), - description.has_value() ? Optional(description.value()) : Optional(), + Optional(move(id)), + Optional(move(description)), encoding, params, num_octets, @@ -496,8 +496,8 @@ BodyStructure Parser::parse_one_part_body() BodyStructureData data { type, subtype, - id.has_value() ? Optional(id.value()) : Optional(), - description.has_value() ? Optional(description.value()) : Optional(), + Optional(move(id)), + Optional(move(description)), encoding, params, num_octets, @@ -522,8 +522,8 @@ BodyStructure Parser::parse_one_part_body() BodyStructureData data { type, subtype, - id.has_value() ? Optional(id.value()) : Optional(), - description.has_value() ? Optional(description.value()) : Optional(), + Optional(move(id)), + Optional(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(name.value()) : Optional(); + address.name = Optional(move(name)); consume(" "); auto source_route = parse_nstring(); - address.source_route = source_route.has_value() ? Optional(source_route.value()) : Optional(); + address.source_route = Optional(move(source_route)); consume(" "); auto mailbox = parse_nstring(); - address.mailbox = mailbox.has_value() ? Optional(mailbox.value()) : Optional(); + address.mailbox = Optional(move(mailbox)); consume(" "); auto host = parse_nstring(); - address.host = host.has_value() ? Optional(host.value()) : Optional(); + address.host = Optional(move(host)); consume(")"); return address; } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 4e12280cffd..8105544cfb9 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -1220,7 +1220,7 @@ ThrowCompletionOr parse_iso_date_time(GlobalObject& global_object, return vm.throw_completion(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() }; + return ISODateTime { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond, .calendar = Optional(move(calendar_part)) }; } // 13.34 ParseTemporalInstantString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstantstring @@ -1626,10 +1626,6 @@ ThrowCompletionOr parse_temporal_time_zone_string(GlobalObject { auto& vm = global_object.vm(); - auto optional_string_view_to_optional_string = [](Optional const& s) { - return s.has_value() ? String { *s } : Optional {}; - }; - // 1. Assert: Type(isoString) is String. // 2. Let parseResult be ParseText(! StringToCodePoints(isoString), TemporalTimeZoneString). @@ -1655,7 +1651,7 @@ ThrowCompletionOr 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(move(name)) }; } // 8. If offsetString is empty, then @@ -1665,7 +1661,7 @@ ThrowCompletionOr 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(move(offset_string)), .name = Optional(move(name)) }; } // 13.44 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp index 3629277041d..1716aa3fa7a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/DurationPrototype.cpp +++ b/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 {}, false)); + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *round_to, Optional(maximum), false)); // 20. Let relativeTo be ? ToRelativeTemporalObject(roundTo). auto relative_to = TRY(to_relative_temporal_object(global_object, *round_to)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 248c5718332..834029816e3 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/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 {}, false)); + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional(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 {}, false)); + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional(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)); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp index 311fc34fccf..78d3b9daeac 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp +++ b/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 {}, false)); + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional(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 {}, false)); + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional(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()); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp index 3b2ac142011..ac96633157c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp +++ b/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 {}, false)); + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional(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 {}, false)); + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, Optional(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)) {