Skip to content
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
12 changes: 12 additions & 0 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@
"object.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
"spaceSurroundingProperties": {
"description": "Whether to add a space surrounding the properties of single line objects.",
"type": "boolean",
"default": true,
"oneOf": [{
"const": true,
"description": "Ex. `{ \"key\": \"value\" }`"
}, {
"const": false,
"description": "Ex. `{\"key\": \"value\"}`"
}]
},
"trailingCommas": {
"description": "Whether to use trailing commas.",
"type": "string",
Expand Down
9 changes: 8 additions & 1 deletion src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl ConfigurationBuilder {
self.insert("object.preferSingleLine", value.into())
}

/// Whether to add a space surrounding the properties of single line objects.
/// Default: `true`
pub fn space_surrounding_properties(&mut self, value: bool) -> &mut Self {
self.insert("spaceSurroundingProperties", value.into())
}

/// Whether to use trailing commas.
///
/// Default: `TrailingCommaKind::Jsonc`
Expand Down Expand Up @@ -155,12 +161,13 @@ mod tests {
.prefer_single_line(true)
.array_prefer_single_line(true)
.object_prefer_single_line(false)
.space_surrounding_properties(false)
.trailing_commas(TrailingCommaKind::Always)
.json_trailing_comma_files(vec!["tsconfig.json".to_string(), ".vscode/settings.json".to_string()])
.ignore_node_comment_text("deno-fmt-ignore");

let inner_config = config.get_inner_config();
assert_eq!(inner_config.len(), 11);
assert_eq!(inner_config.len(), 12);
let diagnostics = resolve_config(inner_config, &GlobalConfiguration::default()).diagnostics;
assert_eq!(diagnostics.len(), 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/configuration/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Configuration {
pub array_prefer_single_line: bool,
#[serde(rename = "object.preferSingleLine")]
pub object_prefer_single_line: bool,
pub space_surrounding_properties: bool,
pub trailing_commas: TrailingCommaKind,
pub json_trailing_comma_files: Vec<String>,
}
1 change: 1 addition & 0 deletions src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub fn resolve_config(
prefer_single_line,
&mut diagnostics,
),
space_surrounding_properties: get_value(&mut config, "spaceSurroundingProperties", true, &mut diagnostics),
trailing_commas: get_value(
&mut config,
"trailingCommas",
Expand Down
4 changes: 2 additions & 2 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ fn gen_object<'a>(obj: &'a Object, context: &mut Context<'a, '_>) -> PrintItems
prefer_hanging: false,
force_use_new_lines: force_multi_lines,
allow_blank_lines: true,
single_line_space_at_start: true,
single_line_space_at_end: true,
single_line_space_at_start: context.config.space_surrounding_properties,
single_line_space_at_end: context.config.space_surrounding_properties,
custom_single_line_separator: None,
multi_line_options: ir_helpers::MultiLineOptions::surround_newlines_indented(),
force_possible_newline_at_start: false,
Expand Down
30 changes: 30 additions & 0 deletions tests/specs/object/Object_SpaceSurroundingProperties_False.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
~~ spaceSurroundingProperties: false ~~
== should not add a space surrounding the properties of single line objects ==
{ "prop": 5, "prop2": { "prop3": "test" } }

[expect]
{"prop": 5, "prop2": {"prop3": "test"}}

== should not affect multi-line objects ==
{
"prop": 5,
"prop2": { "prop3": "test" }
}

[expect]
{
"prop": 5,
"prop2": {"prop3": "test"}
}

== should not affect empty objects ==
{ "prop": {} }

[expect]
{"prop": {}}

== should handle block comments adjacent to the braces ==
{ /* a */ "prop": 5 /* b */ }

[expect]
{/* a */ "prop": 5 /* b */}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
~~ spaceSurroundingProperties: false, object.preferSingleLine: true, lineWidth: 40 ~~

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure if this test was necessary, since object.preferSingleLine is the default.

== should not add a space surrounding the properties when collapsing to a single line ==
{
"prop": {
"test": 2
}
}

[expect]
{"prop": {"test": 2}}
12 changes: 12 additions & 0 deletions tests/specs/object/Object_SpaceSurroundingProperties_True.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
~~ spaceSurroundingProperties: true ~~
== should add a space surrounding the properties of single line objects ==
{"prop": 5, "prop2": {"prop3": "test"}}

[expect]
{ "prop": 5, "prop2": { "prop3": "test" } }

== should handle block comments adjacent to the braces ==
{/* a */ "prop": 5 /* b */}

[expect]
{ /* a */ "prop": 5 /* b */ }