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

fix(fmt): Preserve single-line objects in array formatting #649

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 37 additions & 1 deletion src/swc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,47 @@ fn parse_inner_no_diagnostic_check(file_path: &Path, text: Arc<str>) -> Result<P
maybe_syntax: Some(syntax),
media_type,
scope_analysis: false,
text,
text: format_array_of_objects(text),
})
.map_err(|diagnostic| anyhow!("{:#}", &diagnostic))
}

fn format_array_of_objects(text: Arc<str>) -> Arc<str> {
let re = regex::Regex::new(r"\[\s*\{").unwrap();
let formatted_text = re.replace(&text, |caps: &regex::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<ModuleSpecifier> {
if let Some(specifier) = from_file_path(path) {
Ok(specifier)
Expand Down
16 changes: 9 additions & 7 deletions tests/specs/issues/issue0024.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
];
56 changes: 29 additions & 27 deletions tests/specs/issues/issue0520.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions tests/specs/issues/issue0641.txt
Original file line number Diff line number Diff line change
@@ -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" },
];