Skip to content

Commit

Permalink
fix: schema and formatter language application (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Sep 15, 2023
1 parent 2b8dc0c commit 9aa5c4e
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 58 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ Read our [guidelines to categorize a change](https://biomejs.dev/internals/versi
New entries must be placed in a section entitled `Unreleased`.
Read our [guidelines for writing a good changelog entry](https://github.com/biomejs/biome/blob/main/CONTRIBUTING.md#changelog).

## Unreleased

### Analyzer
### CLI
### Configuration
### Editors
### Formatter
### JavaScript APIs
### Linter
### Parser
### VSCode

## 1.2.1 (2023-09-15)

### Configuration

- Fix an edge case where the formatter language configuration wasn't picked.
- Fix the configuration schema, where `json.formatter` properties weren't transformed in camel case.

## 1.2.0 (2023-09-15)

### CLI
Expand Down
94 changes: 94 additions & 0 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2241,3 +2241,97 @@ const a = {
result,
));
}

#[test]
fn should_apply_different_indent_style() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let biome_json = Path::new("biome.json");
fs.insert(
biome_json.into(),
r#"{
"formatter": {
"indentStyle": "space"
},
"javascript": {
"formatter": {
"lineWidth": 320,
"indentSize": 8,
"indentStyle": "tab"
}
},
"json": {
"formatter": {
"lineWidth": 80,
"indentSize": 2,
"indentStyle": "tab"
}
}
}"#,
);

let json_file_content = r#"
{
"array": ["lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum"]
}
"#;
let json_file = Path::new("input.json");
fs.insert(json_file.into(), json_file_content.as_bytes());

let js_file_content = r#"
const a = {
"array": ["lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum"]
}
"#;
let js_file = Path::new("input.js");
fs.insert(js_file.into(), js_file_content.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
("format"),
"--write",
json_file.as_os_str().to_str().unwrap(),
js_file.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

let mut file = fs
.open(js_file)
.expect("formatting target file was removed by the CLI");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");

assert!(content.contains('\t'), "should not contain tabs");

drop(file);

let mut file = fs
.open(json_file)
.expect("formatting target file was removed by the CLI");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");

assert!(content.contains('\t'), "should not contain tabs");

drop(file);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"should_apply_different_indent_style",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `biome.json`

```json
{
"formatter": {
"indentStyle": "space"
},
"javascript": {
"formatter": {
"lineWidth": 320,
"indentSize": 8,
"indentStyle": "tab"
}
},
"json": {
"formatter": {
"lineWidth": 80,
"indentSize": 2,
"indentStyle": "tab"
}
}
}
```

## `input.js`

```js
const a = {
array: ["lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum", "lorem ipsum"],
};

```

## `input.json`

```json
{
"array": [
"lorem ipsum",
"lorem ipsum",
"lorem ipsum",
"lorem ipsum",
"lorem ipsum",
"lorem ipsum",
"lorem ipsum"
]
}

```

# Emitted Messages

```block
Formatted 2 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct JavascriptFormatter {
deserialize_with = "deserialize_line_width",
serialize_with = "serialize_line_width"
)]
#[serde(skip_serializing_if = "Option::is_none")]
#[bpaf(long("javascript-formatter-line-width"), argument("NUMBER"), optional)]
pub line_width: Option<LineWidth>,
}
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_service/src/configuration/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl MergeWith<JsonParser> for JsonParser {

#[derive(Default, Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Bpaf)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(default, deny_unknown_fields)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct JsonFormatter {
/// Control the formatter for JSON (and its super languages) files.
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -84,6 +84,7 @@ pub struct JsonFormatter {
deserialize_with = "deserialize_line_width",
serialize_with = "serialize_line_width"
)]
#[serde(skip_serializing_if = "Option::is_none")]
#[bpaf(long("json-formatter-line-width"), argument("NUMBER"), optional)]
pub line_width: Option<LineWidth>,
}
Expand Down
33 changes: 12 additions & 21 deletions crates/biome_service/src/file_handlers/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,20 @@ impl Language for JsLanguage {
language: &JsFormatterSettings,
path: &RomePath,
) -> JsFormatOptions {
let indent_style = {
let indent_style = language.indent_style.unwrap_or_default();
if indent_style != IndentStyle::default() {
indent_style
} else {
global.indent_style.unwrap_or_default()
}
let indent_style = if let Some(indent_style) = language.indent_style {
indent_style
} else {
global.indent_style.unwrap_or_default()
};
let line_width = {
let line_width = language.line_width.unwrap_or_default();
if line_width != LineWidth::default() {
line_width
} else {
global.line_width.unwrap_or_default()
}
let line_width = if let Some(line_width) = language.line_width {
line_width
} else {
global.line_width.unwrap_or_default()
};
let indent_width = {
let indent_width = language.indent_width.unwrap_or_default();
if indent_width != IndentWidth::default() {
indent_width
} else {
global.indent_size.unwrap_or_default()
}
let indent_width = if let Some(indent_width) = language.indent_width {
indent_width
} else {
global.indent_size.unwrap_or_default()
};
JsFormatOptions::new(path.as_path().try_into().unwrap_or_default())
.with_indent_style(indent_style)
Expand Down
33 changes: 12 additions & 21 deletions crates/biome_service/src/file_handlers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,20 @@ impl Language for JsonLanguage {
language: &Self::FormatterSettings,
_path: &RomePath,
) -> Self::FormatOptions {
let indent_style = {
let indent_style = language.indent_style.unwrap_or_default();
if indent_style != IndentStyle::default() {
indent_style
} else {
global.indent_style.unwrap_or_default()
}
let indent_style = if let Some(indent_style) = language.indent_style {
indent_style
} else {
global.indent_style.unwrap_or_default()
};
let line_width = {
let line_width = language.line_width.unwrap_or_default();
if line_width != LineWidth::default() {
line_width
} else {
global.line_width.unwrap_or_default()
}
let line_width = if let Some(line_width) = language.line_width {
line_width
} else {
global.line_width.unwrap_or_default()
};
let indent_width = {
let indent_width = language.indent_width.unwrap_or_default();
if indent_width != IndentWidth::default() {
indent_width
} else {
global.indent_size.unwrap_or_default()
}
let indent_width = if let Some(indent_width) = language.indent_width {
indent_width
} else {
global.indent_size.unwrap_or_default()
};
JsonFormatOptions::default()
.with_indent_style(indent_style)
Expand Down
8 changes: 3 additions & 5 deletions editors/vscode/configuration_schema.json

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

2 changes: 1 addition & 1 deletion editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "biomejs",
"displayName": "Biome",
"description": "Biome LSP VS Code Extension",
"version": "1.4.0",
"version": "1.4.1",
"icon": "icon.png",
"activationEvents": [
"onLanguage:javascript",
Expand Down
6 changes: 3 additions & 3 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

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

8 changes: 3 additions & 5 deletions packages/@biomejs/biome/configuration_schema.json

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

2 changes: 1 addition & 1 deletion packages/@biomejs/biome/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@biomejs/biome",
"version": "1.2.0",
"version": "1.2.1",
"bin": "bin/biome",
"scripts": {
"postinstall": "node scripts/postinstall.js"
Expand Down
Loading

0 comments on commit 9aa5c4e

Please sign in to comment.