LibJS: Reject relativeTo string such as "2022-08-18T17:01Z"

This is a normative change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/2dc20bf
This commit is contained in:
Linus Groh 2022-10-17 09:20:38 +02:00
parent 57162ad510
commit 4567ded8e4
Notes: sideshowbarker 2024-07-17 05:39:03 +09:00
5 changed files with 22 additions and 2 deletions

View file

@ -254,6 +254,8 @@
M(TemporalInvalidPlainMonthDay, "Invalid plain month day") \
M(TemporalInvalidPlainTime, "Invalid plain time") \
M(TemporalInvalidPlainYearMonth, "Invalid plain year month") \
M(TemporalInvalidRelativeToStringUTCDesignatorWithoutBracketedTimeZone, "Invalid relativeTo string '{}': must not contain a UTC " \
"designator without bracketed time zone") \
M(TemporalInvalidTime, "Invalid time") \
M(TemporalInvalidTimeString, "Invalid time string '{}'") \
M(TemporalInvalidTimeStringUTCDesignator, "Invalid time string '{}': must not contain a UTC designator") \

View file

@ -1609,12 +1609,18 @@ ThrowCompletionOr<TemporalMonthDay> parse_temporal_month_day_string(VM& vm, Stri
// 13.36 ParseTemporalRelativeToString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalrelativetostring
ThrowCompletionOr<ISODateTime> parse_temporal_relative_to_string(VM& vm, String const& iso_string)
{
// 1. If ParseText(StringToCodePoints(isoString), TemporalDateTimeString) is a List of errors, throw a RangeError exception.
// 1. Let parseResult be ParseText(StringToCodePoints(isoString), TemporalDateTimeString).
auto parse_result = parse_iso8601(Production::TemporalDateTimeString, iso_string);
// 2. If parseResult is a List of errors, throw a RangeError exception.
if (!parse_result.has_value())
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidDateTimeString, iso_string);
// 2. Return ? ParseISODateTime(isoString).
// 3. If parseResult contains a UTCDesignator ParseNode but no TimeZoneBracketedAnnotation Parse Node, throw a RangeError exception.
if (parse_result->utc_designator.has_value() && !parse_result->time_zone_bracketed_annotation.has_value())
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidRelativeToStringUTCDesignatorWithoutBracketedTimeZone, iso_string);
// 4. Return ? ParseISODateTime(isoString).
return parse_iso_date_time(vm, *parse_result);
}

View file

@ -830,6 +830,7 @@ bool ISO8601Parser::parse_time_zone_bracketed_annotation()
return false;
if (!m_state.lexer.consume_specific(']'))
return false;
m_state.parse_result.time_zone_bracketed_annotation = transaction.parsed_string_view();
transaction.commit();
return true;
}

View file

@ -24,6 +24,7 @@ struct ParseResult {
Optional<StringView> time_fraction;
Optional<StringView> calendar_name;
Optional<StringView> utc_designator;
Optional<StringView> time_zone_bracketed_annotation;
Optional<StringView> time_zone_numeric_utc_offset;
Optional<StringView> time_zone_utc_offset_sign;
Optional<StringView> time_zone_utc_offset_hour;

View file

@ -100,4 +100,14 @@ describe("errors", () => {
Temporal.Duration.compare(duration, duration, { relativeTo: zonedDateTime });
}).toThrowWithMessage(TypeError, "null is not a function");
});
test("UTC designator only allowed with bracketed time zone", () => {
const duration = new Temporal.Duration();
expect(() => {
Temporal.Duration.compare(duration, duration, { relativeTo: "2022-08-18T17:01Z" });
}).toThrowWithMessage(
RangeError,
"Invalid relativeTo string '2022-08-18T17:01Z': must not contain a UTC designator without bracketed time zone"
);
});
});