Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_cli): issue #3175 #3189

Merged
merged 3 commits into from
Sep 9, 2022
Merged
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
58 changes: 27 additions & 31 deletions crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,11 @@ pub(crate) fn apply_format_settings_from_cli(
let mut configuration = if let Some(configuration) = configuration {
configuration
} else {
Configuration {
formatter: Some(FormatterConfiguration::default()),
javascript: Some(JavascriptConfiguration {
formatter: Some(JavascriptFormatter::default()),
globals: None,
}),
..Configuration::default()
}
Configuration::default()
};
let formatter = configuration
.formatter
.get_or_insert_with(FormatterConfiguration::default);

let size = session
.args
Expand All @@ -105,21 +101,19 @@ pub(crate) fn apply_format_settings_from_cli(
source,
})?;

if let Some(formatter) = configuration.formatter.as_mut() {
match indent_style {
Some(IndentStyle::Tab) => {
formatter.indent_style = PlainIndentStyle::Tab;
}
Some(IndentStyle::Space(default_size)) => {
formatter.indent_style = PlainIndentStyle::Space;
formatter.indent_size = size.unwrap_or(default_size);
}
None => {}
match indent_style {
Some(IndentStyle::Tab) => {
formatter.indent_style = PlainIndentStyle::Tab;
}

if let Some(line_width) = line_width {
formatter.line_width = line_width;
Some(IndentStyle::Space(default_size)) => {
formatter.indent_style = PlainIndentStyle::Space;
formatter.indent_size = size.unwrap_or(default_size);
}
None => {}
}

if let Some(line_width) = line_width {
formatter.line_width = line_width;
}

let quote_properties = session
Expand All @@ -137,18 +131,20 @@ pub(crate) fn apply_format_settings_from_cli(
argument: "--quote-style",
source,
})?;
if let Some(javascript) = configuration

let javascript = configuration
.javascript
.as_mut()
.and_then(|j| j.formatter.as_mut())
{
if let Some(quote_properties) = quote_properties {
javascript.quote_properties = quote_properties;
}
.get_or_insert_with(JavascriptConfiguration::default);
let javascript_formatter = javascript
.formatter
.get_or_insert_with(JavascriptFormatter::default);

if let Some(quote_style) = quote_style {
javascript.quote_style = quote_style;
}
if let Some(quote_properties) = quote_properties {
javascript_formatter.quote_properties = quote_properties;
}

if let Some(quote_style) = quote_style {
javascript_formatter.quote_style = quote_style;
}

Ok(configuration)
Expand Down
16 changes: 16 additions & 0 deletions crates/rome_cli/tests/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,19 @@ pub const CONFIG_INCORRECT_GLOBALS_V2: &str = r#"{
}
}
}"#;

pub const CONFIG_ISSUE_3175_1: &str = r#"{
"formatter": {
"indentStyle": "space",
"indentSize": 2,
"lineWidth": 120
}
}"#;

pub const CONFIG_ISSUE_3175_2: &str = r#"{
"javascript": {
"formatter": {
"quoteStyle": "single"
}
}
}"#;
95 changes: 94 additions & 1 deletion crates/rome_cli/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,9 @@ mod ci {

mod format {
use super::*;
use crate::configs::{CONFIG_DISABLED_FORMATTER, CONFIG_FORMAT};
use crate::configs::{
CONFIG_DISABLED_FORMATTER, CONFIG_FORMAT, CONFIG_ISSUE_3175_1, CONFIG_ISSUE_3175_2,
};
use crate::snap_test::markup_to_string;
use rome_console::markup;
use rome_fs::FileSystemExt;
Expand Down Expand Up @@ -934,6 +936,97 @@ mod format {
);
}

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

let file_path = Path::new("rome.json");
fs.insert(file_path.into(), CONFIG_ISSUE_3175_1.as_bytes());

let file_path = Path::new("file.js");
fs.insert(file_path.into(), "import React from 'react';\n".as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![
OsString::from("format"),
OsString::from("--quote-style"),
OsString::from("single"),
file_path.as_os_str().into(),
]),
);

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

let mut file = fs
.open(file_path)
.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_eq!(content, "import React from 'react';\n");

drop(file);
assert_cli_snapshot(
module_path!(),
"applies_custom_configuration_over_config_file_issue_3175_v1",
fs,
console,
);
}

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

let source = r#"function f() {
return 'hey';
}
"#;

let file_path = Path::new("rome.json");
fs.insert(file_path.into(), CONFIG_ISSUE_3175_2.as_bytes());

let file_path = Path::new("file.js");
fs.insert(file_path.into(), source.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
DynRef::Borrowed(&mut console),
Arguments::from_vec(vec![
OsString::from("format"),
OsString::from("--indent-style"),
OsString::from("space"),
file_path.as_os_str().into(),
]),
);

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

let mut file = fs
.open(file_path)
.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_eq!(content, source);

drop(file);
assert_cli_snapshot(
module_path!(),
"applies_custom_configuration_over_config_file_issue_3175_v2",
fs,
console,
);
}

#[test]
fn applies_custom_quote_style() {
let mut fs = MemoryFileSystem::default();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"formatter": {
"indentStyle": "space",
"indentSize": 2,
"lineWidth": 120
}
}
```

## `file.js`

```js
import React from 'react';

```

# Emitted Messages


Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `rome.json`

```json
{
"javascript": {
"formatter": {
"quoteStyle": "single"
}
}
}
```

## `file.js`

```js
function f() {
return 'hey';
}

```

# Emitted Messages


9 changes: 9 additions & 0 deletions crates/rome_service/src/configuration/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ pub struct JavascriptConfiguration {
pub globals: Option<IndexSet<String>>,
}

impl JavascriptConfiguration {
pub fn with_formatter() -> Self {
Self {
formatter: Some(JavascriptFormatter::default()),
..JavascriptConfiguration::default()
}
}
}

pub(crate) fn deserialize_globals<'de, D>(
deserializer: D,
) -> Result<Option<IndexSet<String>>, D::Error>
Expand Down
39 changes: 0 additions & 39 deletions npm/rome/tests/__snapshots__/wasm.test.mjs.snap

This file was deleted.

Loading