Skip to content

Commit 66a28d1

Browse files
authored
Remove alignment of property keys with identation (#4390)
This fixes some visual issues like alignment of brackets, and also make it more consistent when keys are long (it does not take much, only need to be longer than ident which is 4). Since most of our tests have single letter keys this never showed up as much of a problem, but in the real world it becomes annoying. Before: ``` { hello: "world", z: { a: 1 } } ``` After: ``` { hello: "world", z: { a: 1 } } ```
1 parent 728325b commit 66a28d1

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

core/engine/src/value/display.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ fn print_obj_value_internals(
6060
let object = obj.borrow();
6161
if let Some(object) = object.prototype() {
6262
vec![format!(
63-
"{:>width$}: {}",
63+
"{:>width$}{}: {}",
64+
"",
6465
"__proto__",
6566
display_fn(
6667
&object.clone().into(),
@@ -72,7 +73,8 @@ fn print_obj_value_internals(
7273
)]
7374
} else {
7475
vec![format!(
75-
"{:>width$}: {}",
76+
"{:>width$}{}: {}",
77+
"",
7678
"__proto__",
7779
JsValue::null().display(),
7880
width = indent,
@@ -104,7 +106,8 @@ fn print_obj_value_props(
104106
if val.is_data_descriptor() {
105107
let v = &val.expect_value();
106108
result.push(format!(
107-
"{:>width$}: {}",
109+
"{:>width$}{}: {}",
110+
"",
108111
key,
109112
display_fn(v, encounters, indent.wrapping_add(4), print_internals),
110113
width = indent,

core/engine/src/value/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static TWO_E_63: LazyLock<BigInt> = LazyLock::new(|| {
8181
/// // Comments are allowed inside.
8282
/// "key": (js_string!("value"))
8383
/// }, context).display().to_string(),
84-
/// "{\n key: \"value\"\n}",
84+
/// "{\n key: \"value\"\n}",
8585
/// );
8686
/// ```
8787
pub use boa_macros::js_object;
@@ -103,7 +103,7 @@ pub use boa_macros::js_object;
103103
///
104104
/// assert_eq!(
105105
/// JsValue::from(value).display().to_string(),
106-
/// "{\n 3: null,\n key: \"value\",\nalsoKey: 1\n}"
106+
/// "{\n 3: null,\n key: \"value\",\n alsoKey: 1\n}"
107107
/// );
108108
/// ```
109109
pub use boa_macros::js_value;

core/engine/src/value/tests.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,30 @@ fn debug_object() {
400400
fn display_object() {
401401
const DISPLAY: &str = indoc! {r#"
402402
{
403-
a: "a"
403+
a: "a"
404404
}"#
405405
};
406406
run_test_actions([TestAction::assert_with_op("({a: 'a'})", |v, _| {
407407
v.display().to_string() == DISPLAY
408408
})]);
409409
}
410410

411+
/// Ensure indentation is always 4 and that the property key is not right-align with
412+
/// the brackets.
413+
#[test]
414+
fn display_object_indent() {
415+
const DISPLAY: &str = indoc! {r#"
416+
{
417+
a: "a",
418+
hello: "world"
419+
}"#
420+
};
421+
run_test_actions([TestAction::assert_with_op(
422+
"({a: 'a', hello: 'world'})",
423+
|v, _| v.display().to_string() == DISPLAY,
424+
)]);
425+
}
426+
411427
#[test]
412428
fn to_integer_or_infinity() {
413429
run_test_actions([TestAction::inspect_context(|ctx| {
@@ -1608,7 +1624,7 @@ mod js_value_macro {
16081624

16091625
assert_eq!(
16101626
format!("{}", o.display()),
1611-
"{\nfOne: \"Hello\",\nfield2: 42\n}"
1627+
"{\n fOne: \"Hello\",\n field2: 42\n}"
16121628
);
16131629
}
16141630
}

core/runtime/src/fetch/tests/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn response_json() {
8888
let response = response.as_promise().unwrap().await_blocking(ctx).unwrap();
8989
assert_eq!(
9090
format!("{}", response.display_obj(false)),
91-
"{\nhello world: 123\n}"
91+
"{\n hello world: 123\n}"
9292
);
9393
}),
9494
]);

0 commit comments

Comments
 (0)