Skip to content

Commit

Permalink
Fix reformatting of attributes with empty arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule committed Jan 11, 2022
1 parent 154f494 commit d0b0d1e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
7 changes: 5 additions & 2 deletions libs/datamodel/core/src/ast/reformat/reformatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,11 @@ impl<'a> Reformatter<'a> {
}
Self::reformat_expression(&mut builder, &current);
}
Rule::doc_comment | Rule::doc_comment_and_new_line => {
panic!("Comments inside attribute argument list not supported yet.")
Rule::empty_argument => {
if !builder.line_empty() {
builder.write(", ");
}
Self::reformat_attribute_arg(&mut builder, &current);
}
_ => Self::reformat_generic_token(target, &current),
};
Expand Down
44 changes: 44 additions & 0 deletions libs/datamodel/core/tests/reformat/reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,3 +1276,47 @@ fn test_composite_types_in_models() {
let result = Reformatter::new(input).reformat_to_string();
expected.assert_eq(&result);
}

#[test]
fn empty_arguments_reformat_properly() {
let schema = r#"
/// Post including an author and content.
model Post {
id Int @id @default(autoincrement())
content String? @default(map: "")
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id], onDelete: )
authorId Int?
}
// Documentation for this model.
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
"#;

let expected = expect![[r#"
/// Post including an author and content.
model Post {
id Int @id @default(autoincrement())
content String? @default(map: "")
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id], onDelete: )
authorId Int?
}
// Documentation for this model.
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
"#]];

let result = Reformatter::new(schema).reformat_to_string();
expected.assert_eq(&result);
}

0 comments on commit d0b0d1e

Please sign in to comment.