Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_js_formatter): regression of call arguments #2812

Merged
merged 5 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/rome_js_formatter/src/js/expressions/call_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@ impl FormatNodeRule<JsCallArguments> for FormatJsCallArguments {
)
});

let mut null_buffer = f.inspect_null();
let arguments_break = separated.iter().map(|element| {
let mut will_break_buffer = null_buffer.inspect_will_break();
write!(will_break_buffer, [element]).ok();
will_break_buffer.will_break()
});
let mut any_breaks = false;
let an_argument_breaks = arguments_break.enumerate().any(|(index, will_break)| {
any_breaks |= will_break;
if should_group_first_argument && index > 0
|| (should_group_last_argument && index < args.len() - 1)
{
will_break
} else {
false
}
});

if an_argument_breaks {
return write!(f, [all_arguments_expanded]);
}

if any_breaks {
write!(f, [expand_parent()])?;
}

write!(
f,
[best_fitting![
Expand Down
5 changes: 3 additions & 2 deletions crates/rome_js_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ mod test {
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"
"hol\a"
'hol\c'
test.expect(t => {
t.true(a);
}, false);
"#;
let syntax = SourceType::tsx();
let tree = parse(src, 0, syntax);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/rome_js_formatter/tests/spec_test.rs
assertion_line: 257
assertion_line: 259
expression: arrow_nested.js
---
# Input
Expand Down Expand Up @@ -62,12 +62,13 @@ const promiseFromCallback = (fn) =>
}),
);

runtimeAgent.getProperties(objectId, false, false, false, (
// ownProperties // accessorPropertiesOnly // generatePreview
error,
properties,
internalProperties,
) => {
return 1;
});
runtimeAgent.getProperties(
objectId,
false, // ownProperties
false, // accessorPropertiesOnly
false, // generatePreview
(error, properties, internalProperties) => {
return 1;
},
);

Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ test.expect(t => {

test.expect(t => {
t.true(a)
}, false, /* something */)
}, false,
// comment
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/rome_js_formatter/tests/spec_test.rs
assertion_line: 256
assertion_line: 259
expression: call_expression.js
---
# Input
Expand Down Expand Up @@ -49,7 +49,9 @@ test.expect(t => {

test.expect(t => {
t.true(a)
}, false, /* something */)
}, false,
// comment
)
=============================
# Outputs
## Output 1
Expand Down Expand Up @@ -99,5 +101,7 @@ test.expect((t) => {

test.expect((t) => {
t.true(a);
}, false /* something */);
}, false
// comment
);

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ deepCopyAndAsyncMapLeavesB(
{ valueMapper, overwriteExistingKeys }
)

// rome-ignore format: shut down regression
deepCopyAndAsyncMapLeavesC(
{ source: sourceValue, destination: destination[sourceKey] },
1337,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ deepCopyAndAsyncMapLeavesB(
{ valueMapper, overwriteExistingKeys }
)

// rome-ignore format: shut down regression
deepCopyAndAsyncMapLeavesC(
{ source: sourceValue, destination: destination[sourceKey] },
1337,
Expand Down Expand Up @@ -72,10 +73,12 @@ deepCopyAndAsyncMapLeavesB(
{ valueMapper, overwriteExistingKeys },
);

deepCopyAndAsyncMapLeavesC({
source: sourceValue,
destination: destination[sourceKey],
}, 1337, { valueMapper, overwriteExistingKeys });
// rome-ignore format: shut down regression
deepCopyAndAsyncMapLeavesC(
{ source: sourceValue, destination: destination[sourceKey] },
1337,
{ valueMapper, overwriteExistingKeys }
)

function someFunction(url) {
return get(url).then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ runtimeAgent.getProperties(

# Output
```js
runtimeAgent.getProperties(objectId, false, false, false, (
// ownProperties // accessorPropertiesOnly // generatePreview
error,
properties,
internalProperties,
) => {
return 1;
});
runtimeAgent.getProperties(
objectId,
false, // ownProperties
false, // accessorPropertiesOnly
false, // generatePreview
(error, properties, internalProperties) => {
return 1;
},
);

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,37 @@ db.collection('indexOptionDefault').createIndex({ a: 1 }, {
//https://github.com/prettier/prettier/issues/3002
beep
.boop()
.baz("foo", {
some: {
thing: {
nested: true,
.baz(
"foo",
{
some: {
thing: {
nested: true,
},
},
},
}, { another: { thing: true } }, () => {});
{ another: { thing: true } },
() => {},
);

//https://github.com/prettier/prettier/issues/2984
db
.collection("indexOptionDefault")
.createIndex({ a: 1 }, {
indexOptionDefaults: true,
w: 2,
wtimeout: 1000,
}, function (err) {
test.equal(null, err);
test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern);
.createIndex(
{ a: 1 },
{
indexOptionDefaults: true,
w: 2,
wtimeout: 1000,
},
function (err) {
test.equal(null, err);
test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern);

client.close();
done();
});
client.close();
done();
},
);

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,14 @@ stuff.doThing(someStuff, -1, {
accept: (node) => doSomething(node),
});

doThing(someOtherStuff,
// This is important
true, {
decline: (creditCard) => takeMoney(creditCard),
});
doThing(
someOtherStuff,
// This is important
true,
{
decline: (creditCard) => takeMoney(creditCard),
},
);

func(() => {
thing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ a(
a("long-nested-value", "long-nested-value2", "long-nested-value3"),
);

a.b().c({
d,
}, () => {});
a.b().c(
{
d,
},
() => {},
);

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ const bar3 = [1, 2, 3].reduce((carry, value) => {
return [...carry, value];
}, ([1, 2, 3] as unknown) as number[]);

const bar4 = [1, 2, 3].reduce((carry, value) => {
return [...carry, value];
}, <Array<number>>[1, 2, 3]);
const bar4 = [1, 2, 3].reduce(
(carry, value) => {
return [...carry, value];
},
<Array<number>>[1, 2, 3],
);

const bar5 = [1, 2, 3].reduce((carry, value) => {
return { ...carry, [value]: true };
Expand All @@ -69,9 +72,12 @@ const bar7 = [1, 2, 3].reduce((carry, value) => {
return { ...carry, [value]: true };
}, ({ 1: true } as unknown) as { [key: number]: boolean });

const bar8 = [1, 2, 3].reduce((carry, value) => {
return { ...carry, [value]: true };
}, <{ [key: number]: boolean }>{ 1: true });
const bar8 = [1, 2, 3].reduce(
(carry, value) => {
return { ...carry, [value]: true };
},
<{ [key: number]: boolean }>{ 1: true },
);

```

Expand Down