Skip to content

Commit

Permalink
LibJS: Throw a RangeError when when formatting strings in DurationFormat
Browse files Browse the repository at this point in the history
This is a normative change in the Intl.DurationFormat proposal. See:
tc39/proposal-intl-duration-format@2546080
  • Loading branch information
trflynn89 authored and linusg committed Dec 15, 2022
1 parent 010888a commit a2cf026
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
11 changes: 9 additions & 2 deletions Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,16 @@ StringView DurationFormat::display_to_string(Display display)
// 1.1.3 ToDurationRecord ( input ), https://tc39.es/proposal-intl-duration-format/#sec-todurationrecord
ThrowCompletionOr<Temporal::DurationRecord> to_duration_record(VM& vm, Value input)
{
// 1. If Type(input) is not Object, throw a TypeError exception.
if (!input.is_object())
// 1. If Type(input) is not Object, then
if (!input.is_object()) {
// a. If Type(input) is String, throw a RangeError exception.
if (input.is_string())
return vm.throw_completion<RangeError>(ErrorType::NotAnObject, input);

// b. Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::NotAnObject, input);
}

auto& input_object = input.as_object();

// 2. Let result be a new Duration Record with each field set to 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ describe("correct behavior", () => {

describe("errors", () => {
test("non-object duration records", () => {
[-100, Infinity, NaN, "hello", 152n, Symbol("foo")].forEach(value => {
expect(() => {
new Intl.DurationFormat().format("hello");
}).toThrowWithMessage(RangeError, "is not an object");

[-100, Infinity, NaN, 152n, Symbol("foo"), true, null, undefined].forEach(value => {
expect(() => {
new Intl.DurationFormat().format(value);
}).toThrowWithMessage(TypeError, "is not an object");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ describe("correct behavior", () => {

describe("errors", () => {
test("non-object duration records", () => {
[-100, Infinity, NaN, "hello", 152n, Symbol("foo")].forEach(value => {
expect(() => {
new Intl.DurationFormat().formatToParts("hello");
}).toThrowWithMessage(RangeError, "is not an object");

[-100, Infinity, NaN, 152n, Symbol("foo"), true, null, undefined].forEach(value => {
expect(() => {
new Intl.DurationFormat().formatToParts(value);
}).toThrowWithMessage(TypeError, "is not an object");
Expand Down

0 comments on commit a2cf026

Please sign in to comment.