Skip to content

Commit

Permalink
refactor(reformat): completely separate relation additions from AST r…
Browse files Browse the repository at this point in the history
…eformatting

That way we can do pure AST reformatting if we want, and reuse the logic
for relation and relation scalar field additions in code actions. As a
side-effect, that makes the formatter idempotent.

There will be a follow-up PR to split the two functionalities in two
different modules, but I stopped here for readability in this PR.

closes prisma/prisma#12726
  • Loading branch information
tomhoule committed Jun 29, 2022
1 parent 46cd7ed commit 64b00fa
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 449 deletions.
624 changes: 189 additions & 435 deletions libs/datamodel/core/src/reformat.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions libs/datamodel/core/src/reformat/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use schema_ast::{parser::Rule, renderer::LineWriteable};

pub type Token<'a> = pest::iterators::Pair<'a, Rule>;
pub(super) type Token<'a> = pest::iterators::Pair<'a, Rule>;

pub trait TokenExtensions {
pub(super) trait TokenExtensions {
fn is_top_level_element(&self) -> bool;
}

Expand All @@ -19,15 +19,15 @@ impl TokenExtensions for Token<'_> {
}
}

pub fn comment(target: &mut dyn LineWriteable, comment_text: &str) {
pub(super) fn comment(target: &mut dyn LineWriteable, comment_text: &str) {
let trimmed = strip_new_line(comment_text);
let trimmed = trimmed.trim();

target.write(trimmed);
target.end_line();
}

pub fn strip_new_line(str: &str) -> &str {
pub(super) fn strip_new_line(str: &str) -> &str {
if str.ends_with('\n') {
&str[0..str.len() - 1] // slice away line break.
} else {
Expand Down
2 changes: 1 addition & 1 deletion libs/datamodel/core/tests/attributes/composite_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn reformat() {
"#};

let datamodel = with_header(schema, crate::Provider::Mongo, &["fullTextIndex"]);
let result = datamodel::reformat(&datamodel, 2).unwrap_or_else(|_| datamodel.to_owned());
let result = datamodel::reformat(&datamodel, 2).unwrap_or_else(|| datamodel.to_owned());

let expected = expect![[r#"
datasource test {
Expand Down
4 changes: 4 additions & 0 deletions libs/datamodel/core/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ pub use indoc::{formatdoc, indoc};
use datamodel::{diagnostics::*, Configuration, StringFromEnvVar};
use pretty_assertions::assert_eq;

pub(crate) fn reformat(input: &str) -> String {
datamodel::reformat(input, 2).unwrap_or_else(|| input.to_owned())
}

pub(crate) trait DatasourceAsserts {
fn assert_name(&self, name: &str) -> &Self;
fn assert_url(&self, url: StringFromEnvVar) -> &Self;
Expand Down
76 changes: 75 additions & 1 deletion libs/datamodel/core/tests/reformat/reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::*;
use indoc::indoc;

fn reformat(input: &str) -> String {
datamodel::reformat(input, 2).unwrap_or_else(|_| input.to_owned())
datamodel::reformat(input, 2).unwrap_or_else(|| input.to_owned())
}

#[test]
Expand Down Expand Up @@ -1376,3 +1376,77 @@ fn reformatting_optional_list_fields_works() {

expected.assert_eq(&reformat(schema));
}

#[test]
fn attribute_arguments_reformatting_is_idempotent() {
let schema = r#"
generator client {
provider = "prisma-client-js"
previewFeatures = "mongodb"
}
datasource db {
provider = "mongodb"
url = "m...ty"
}
model Foo {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
json Json
bar Bar
bars Bar[]
baz Baz @relation(fields: [bazId], references: [id])
bazId String @db.ObjectId
list String[]
jsonList Json[]
}
type Bar {
label String
number Int
}
model Baz {
id String @id @default(auto()) @map("_id") @db.ObjectId
foo Foo?
}
"#;

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
previewFeatures = "mongodb"
}
datasource db {
provider = "mongodb"
url = "m...ty"
}
model Foo {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
json Json
bar Bar
bars Bar[]
baz Baz @relation(fields: [bazId], references: [id])
bazId String @db.ObjectId
list String[]
jsonList Json[]
}
type Bar {
label String
number Int
}
model Baz {
id String @id @default(auto()) @map("_id") @db.ObjectId
foo Foo?
}
"#]];
let reformatted = reformat(schema);
expected.assert_eq(&reformatted);
assert_eq!(reformatted, reformat(&reformatted)); // it's idempotent
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use expect_test::expect;
use indoc::indoc;

fn reformat(input: &str) -> String {
datamodel::reformat(input, 2).unwrap_or_else(|_| input.to_owned())
}
use crate::common::*;

#[test]
fn native_types_in_missing_back_relation_fields() {
Expand Down
2 changes: 1 addition & 1 deletion prisma-fmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn run(opts: FormatOpts) {
}
};

let reformatted = reformat(&datamodel_string, opts.tabwidth).unwrap_or_else(|err: &str| err.to_owned());
let reformatted = reformat(&datamodel_string, opts.tabwidth).unwrap_or(datamodel_string);
match opts.output {
Some(file_name) => {
let file = File::open(&file_name).unwrap_or_else(|_| panic!("Unable to open file {}", file_name.display()));
Expand Down
2 changes: 1 addition & 1 deletion prisma-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn format(schema: &str, params: &str) -> String {
}
};

datamodel::reformat(schema, params.options.tab_size as usize).unwrap_or_else(|err| err.to_owned())
datamodel::reformat(schema, params.options.tab_size as usize).unwrap_or_else(|| schema.to_owned())
}

pub fn lint(schema: String) -> String {
Expand Down

0 comments on commit 64b00fa

Please sign in to comment.