diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-12-14 15:30:34 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-15 09:40:09 +0000 |
commit | a2cf026b306331f5e64d6658d34dfa474d412ae0 (patch) | |
tree | e38916c05f28c618779b57baa48ae7acf9ed50c5 /Userland/Libraries | |
parent | 010888aceca2c513b22abbf84c67916a5e6853ea (diff) | |
download | serenity-a2cf026b306331f5e64d6658d34dfa474d412ae0.zip |
LibJS: Throw a RangeError when when formatting strings in DurationFormat
This is a normative change in the Intl.DurationFormat proposal. See:
https://github.com/tc39/proposal-intl-duration-format/commit/2546080
Diffstat (limited to 'Userland/Libraries')
3 files changed, 19 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp index d1c28d3943..43f9b21bfa 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp @@ -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. diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.format.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.format.js index d7de5ff780..4b1b8c0b4f 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.format.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.format.js @@ -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"); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.formatToParts.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.formatToParts.js index be12c713a0..ffa9259d2e 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.formatToParts.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.prototype.formatToParts.js @@ -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"); |