From c190261bc2f89cafb668c2784f8a944b2f1d25a0 Mon Sep 17 00:00:00 2001 From: yazan-abdalrahman Date: Sun, 14 Jul 2024 16:54:47 +0300 Subject: [PATCH 1/2] Preserve single-line objects in array formatting fix #641 && https://github.com/denoland/deno/issues/22459 --- Cargo.lock | 1 + Cargo.toml | 1 + src/swc.rs | 38 +++++++++++++++++++++++++++++++- tests/specs/issues/issue0641.txt | 10 +++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/specs/issues/issue0641.txt diff --git a/Cargo.lock b/Cargo.lock index 7b96e58..6ab7a51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -238,6 +238,7 @@ dependencies = [ "dprint-development", "percent-encoding", "pretty_assertions", + "regex", "rustc-hash", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index bd27bf2..791f758 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ percent-encoding = "2.3.1" rustc-hash = "1.1.0" serde = { version = "1.0.144", features = ["derive"] } serde_json = { version = "1.0", optional = true } +regex = "1.10.4" [dev-dependencies] dprint-development = "0.10.1" diff --git a/src/swc.rs b/src/swc.rs index 75ef88d..3f229d9 100644 --- a/src/swc.rs +++ b/src/swc.rs @@ -46,11 +46,47 @@ fn parse_inner_no_diagnostic_check(file_path: &Path, text: Arc) -> Result

) -> Arc { + let re = regex::Regex::new(r"\[\s*\{").unwrap(); + let formatted_text = re.replace_all(&text, |caps: ®ex::Captures| { + let matched_text = &caps[0]; + if matched_text.starts_with("[") && matched_text.ends_with("{") { + if !is_inside_string_or_comment(&text, caps.get(0).unwrap().start()) { + "[\n{".to_string() + } else { + matched_text.to_string() + } + } else { + matched_text.to_string() + } + }); + Arc::from(formatted_text) +} + +fn is_inside_string_or_comment(text: &str, position: usize) -> bool { + let mut in_string = false; + let mut in_comment = false; + let mut chars = text.chars().enumerate(); + while let Some((idx, c)) = chars.next() { + if idx == position { + return in_string || in_comment; + } + if c == '"' || c == '\'' || c == '`' { + in_string = !in_string; + } else if c == '/' && chars.next().map_or(false, |(_, next_char)| next_char == '/' || next_char == '*') { + in_comment = true; + } else if c == '\n' { + in_comment = false; + } + } + false +} + fn path_to_specifier(path: &Path) -> Result { if let Some(specifier) = from_file_path(path) { Ok(specifier) diff --git a/tests/specs/issues/issue0641.txt b/tests/specs/issues/issue0641.txt new file mode 100644 index 0000000..9feb795 --- /dev/null +++ b/tests/specs/issues/issue0641.txt @@ -0,0 +1,10 @@ +== Array of objects is not formatted nicely == +const fpnMembershipTitles = [{ name: "FPN" }, { name: "FPN Class" }, { name: "Type" }, { name: "Description" }]; + +[expect] +const fpnMembershipTitles = [ + { name: "FPN" }, + { name: "FPN Class" }, + { name: "Type" }, + { name: "Description" }, +]; From 935899f3d3f9d873a37dd785a60bcd36f6f8d0b5 Mon Sep 17 00:00:00 2001 From: yazan-abdalrahman Date: Wed, 17 Jul 2024 14:44:03 +0300 Subject: [PATCH 2/2] Fix test's --- src/swc.rs | 2 +- tests/specs/issues/issue0024.txt | 16 +++++---- tests/specs/issues/issue0520.txt | 56 +++++++++++++++++--------------- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/swc.rs b/src/swc.rs index 3f229d9..ffc88b0 100644 --- a/src/swc.rs +++ b/src/swc.rs @@ -53,7 +53,7 @@ fn parse_inner_no_diagnostic_check(file_path: &Path, text: Arc) -> Result

) -> Arc { let re = regex::Regex::new(r"\[\s*\{").unwrap(); - let formatted_text = re.replace_all(&text, |caps: ®ex::Captures| { + let formatted_text = re.replace(&text, |caps: ®ex::Captures| { let matched_text = &caps[0]; if matched_text.starts_with("[") && matched_text.ends_with("{") { if !is_inside_string_or_comment(&text, caps.get(0).unwrap().start()) { diff --git a/tests/specs/issues/issue0024.txt b/tests/specs/issues/issue0024.txt index c299964..2436ea6 100644 --- a/tests/specs/issues/issue0024.txt +++ b/tests/specs/issues/issue0024.txt @@ -9,10 +9,12 @@ const files = [{ }]; [expect] -const files = [{ - // deno-fmt-ignore - content: new Uint8Array([137,80,78,71,13,10,26,10, 137, 1, 25]), - type: "image/png", - name: "image", - fileName: "some-image.png", -}]; +const files = [ + { + // deno-fmt-ignore + content: new Uint8Array([137,80,78,71,13,10,26,10, 137, 1, 25]), + type: "image/png", + name: "image", + fileName: "some-image.png", + }, +]; diff --git a/tests/specs/issues/issue0520.txt b/tests/specs/issues/issue0520.txt index 262047b..2348c55 100644 --- a/tests/specs/issues/issue0520.txt +++ b/tests/specs/issues/issue0520.txt @@ -27,35 +27,37 @@ export const UsAndItsD = {"kd":"D","definitions":[{"kd":"OperationDefinition","o [expect] export const AID = { "kd": "D", - "definitions": [{ - "kd": "OperationDefinition", - "operation": "mutation", - "name": { "kd": "Name", "value": "AI" }, - "variableDefinitions": [{ - "kd": "VariableDefinition", - "variable": { "kd": "Variable", "name": { "kd": "Name", "value": "input" } }, - "type": { "kd": "NonNullType", "type": { "kd": "NamedType", "name": { "kd": "Name", "value": "AIInput" } } }, - }], - "selectionSet": { - "kd": "SelectionSet", - "selections": [{ - "kd": "Field", - "name": { "kd": "Name", "value": "acceptI" }, - "arguments": [{ - "kd": "Argument", - "name": { "kd": "Name", "value": "input" }, - "value": { "kd": "Variable", "name": { "kd": "Name", "value": "input" } }, - }], - "selectionSet": { - "kd": "SelectionSet", - "selections": [{ "kd": "Field", "name": { "kd": "Name", "value": "organizationArn" } }, { - "kd": "Field", - "name": { "kd": "Name", "value": "principalArn" }, - }], - }, + "definitions": [ + { + "kd": "OperationDefinition", + "operation": "mutation", + "name": { "kd": "Name", "value": "AI" }, + "variableDefinitions": [{ + "kd": "VariableDefinition", + "variable": { "kd": "Variable", "name": { "kd": "Name", "value": "input" } }, + "type": { "kd": "NonNullType", "type": { "kd": "NamedType", "name": { "kd": "Name", "value": "AIInput" } } }, }], + "selectionSet": { + "kd": "SelectionSet", + "selections": [{ + "kd": "Field", + "name": { "kd": "Name", "value": "acceptI" }, + "arguments": [{ + "kd": "Argument", + "name": { "kd": "Name", "value": "input" }, + "value": { "kd": "Variable", "name": { "kd": "Name", "value": "input" } }, + }], + "selectionSet": { + "kd": "SelectionSet", + "selections": [{ "kd": "Field", "name": { "kd": "Name", "value": "organizationArn" } }, { + "kd": "Field", + "name": { "kd": "Name", "value": "principalArn" }, + }], + }, + }], + }, }, - }], + ], }; export const AcD = { "kd": "D",