Skip to content

Commit 0c3849d

Browse files
committed
fix(temporal): align Duration toLocaleString with spec fallback and improve valueOf error
1 parent 5b0f62a commit 0c3849d

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

core/engine/src/builtins/temporal/duration/mod.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,15 @@ impl Duration {
11151115
Ok(JsString::from(result).into())
11161116
}
11171117

1118-
/// 7.3.24 `Temporal.Duration.prototype.toLocaleString ( )`
1118+
/// 7.3.24 `Temporal.Duration.prototype.toLocaleString ( [ locales [ , options ] ] )`
1119+
///
1120+
/// When the implementation includes ECMA-402, this method is defined by the Intl specification
1121+
/// (typically via `Intl.DurationFormat`). Boa does not implement `Intl.DurationFormat` yet, so
1122+
/// we use the Temporal proposal **non-ECMA-402** fallback: `TemporalDurationToString(duration, auto)`
1123+
/// — the same string as [`Self::to_json`].
1124+
///
1125+
/// The `locales` and `options` arguments are accepted for API compatibility but are ignored
1126+
/// until locale-sensitive duration formatting is implemented.
11191127
///
11201128
/// More information:
11211129
///
@@ -1126,23 +1134,10 @@ impl Duration {
11261134
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/toLocaleString
11271135
pub(crate) fn to_locale_string(
11281136
this: &JsValue,
1129-
_: &[JsValue],
1130-
_: &mut Context,
1137+
_args: &[JsValue],
1138+
context: &mut Context,
11311139
) -> JsResult<JsValue> {
1132-
// TODO: Update for ECMA-402 compliance
1133-
let object = this.as_object();
1134-
let duration = object
1135-
.as_ref()
1136-
.and_then(JsObject::downcast_ref::<Self>)
1137-
.ok_or_else(|| {
1138-
JsNativeError::typ().with_message("this value must be a Duration object.")
1139-
})?;
1140-
1141-
let result = duration
1142-
.inner
1143-
.as_temporal_string(ToStringRoundingOptions::default())?;
1144-
1145-
Ok(JsString::from(result).into())
1140+
Self::to_json(this, &[], context)
11461141
}
11471142

11481143
/// 7.3.25 `Temporal.Duration.prototype.valueOf ( )`
@@ -1156,7 +1151,11 @@ impl Duration {
11561151
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/valueOf
11571152
pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
11581153
Err(JsNativeError::typ()
1159-
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
1154+
.with_message(
1155+
"Cannot convert a Temporal.Duration to a primitive value. \
1156+
Use Temporal.Duration.compare() for comparison or \
1157+
Temporal.Duration.prototype.toString() for a string representation.",
1158+
)
11601159
.into())
11611160
}
11621161
}

core/engine/src/builtins/temporal/duration/tests.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{TestAction, run_test_actions};
1+
use crate::{JsNativeErrorKind, TestAction, run_test_actions};
22

33
#[test]
44
fn duration_constructor() {
@@ -70,3 +70,23 @@ fn basic() {
7070
TestAction::assert_eq("dur.nanoseconds", 0),
7171
]);
7272
}
73+
74+
#[test]
75+
fn duration_to_locale_string_matches_to_json_until_intl_duration_format() {
76+
run_test_actions([
77+
TestAction::run("let dur = Temporal.Duration.from('P1Y2M3DT4H5M6.007008009S')"),
78+
TestAction::assert("dur.toLocaleString() === dur.toJSON()"),
79+
TestAction::assert(
80+
"dur.toLocaleString('en-US', { style: 'narrow' }) === dur.toJSON()",
81+
),
82+
]);
83+
}
84+
85+
#[test]
86+
fn duration_value_of_throws_type_error_with_compare_hint() {
87+
run_test_actions([TestAction::assert_native_error(
88+
"Temporal.Duration.from('P1D').valueOf()",
89+
JsNativeErrorKind::Type,
90+
"Cannot convert a Temporal.Duration to a primitive value. Use Temporal.Duration.compare() for comparison or Temporal.Duration.prototype.toString() for a string representation.",
91+
)]);
92+
}

0 commit comments

Comments
 (0)