Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psl: fix newline boundary search in relation reformatting #3457

Merged
merged 1 commit into from
Dec 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 7 additions & 9 deletions psl/psl-core/src/reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn push_missing_relation_attribute(inline_relation: walkers::InlineRelationWalke
content.push(')');

ctx.missing_bits.push(MissingBit {
position: before_newline(forward.ast_field().span().end, ctx.original_schema),
position: after_type(forward.ast_field().field_type.span().end, ctx.original_schema),
content,
})
}
Expand Down Expand Up @@ -328,12 +328,10 @@ fn references_argument(inline: walkers::InlineRelationWalker<'_>) -> String {
format!("references: [{}]", field_names.join(", "))
}

/// Assuming the last characters before span_end are newlines. We can fix this more thoroughly by
/// not including the newline in field spans.
fn before_newline(span_end: usize, original_schema: &str) -> usize {
assert!(&original_schema[span_end - 1..span_end] == "\n");
match &original_schema[span_end - 2..span_end - 1] {
"\r" => span_end - 2,
_ => span_end - 1,
}
fn after_type(type_span_end: usize, original_schema: &str) -> usize {
original_schema[type_span_end..]
.chars()
.position(|chr| !['[', ']', '?', '!'].contains(&chr))
.map(|pos| type_span_end + pos)
.unwrap_or(type_span_end)
}
2 changes: 2 additions & 0 deletions psl/psl/tests/reformat_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ fn run_reformat_test(test_file_path: &str) {
}

if reformat(&reformatted_text) != reformatted_text {
println!("=== reformatted ===\n{reformatted_text}");
println!("=== reformatted again ===\n{}", reformat(&reformatted_text));
panic!("Reformatting this schema is not idempotent.");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "./generated/client"
}

datasource db {
provider = "sqlserver"
url = "***"
}

model somemodel {
frownyFaceId String
frownyFace frownyface // 囧

id Int @id
}

model frownyface {
somes somemodel[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "./generated/client"
}

datasource db {
provider = "sqlserver"
url = "***"
}

model somemodel {
frownyFaceId String
frownyFace frownyface @relation(fields: [], references: []) // 囧

id Int @id
}

model frownyface {
somes somemodel[]
}