test(formatter): enable spec tests fo JS files
MODIFY
crates/cli/src/lib.rs
crates/cli/src/lib.rs
use clap::{crate_version, App, AppSettings, Arg};
use core::create_app;
use path::RomePath;
-use rome_formatter::{format, FormatOptions, IndentStyle};
+use rome_formatter::{format_file_and_save, FormatOptions, IndentStyle};
use std::{path::PathBuf, str::FromStr};
/// Main function to run Rome CLI
@@ -71,7 +71,7 @@ pub fn run_cli() {
.unwrap_or_default();
let mut file = RomePath::new(input).deduce_handler(&app);
- format(&mut file, FormatOptions::new(options));
+ format_file_and_save(&mut file, FormatOptions::new(options));
}
// Thanks to the settings AppSettings::SubcommandRequiredElseHelp we should not be there
_ => clap::Error::with_description(

MODIFY
crates/core/src/file_handlers/mod.rs
crates/core/src/file_handlers/mod.rs
pub mod javascript;
pub mod json;
pub mod unknown;
+
pub enum Language {
Js,
Json,

MODIFY
crates/formatter/src/lib.rs
crates/formatter/src/lib.rs
@@ -55,6 +55,7 @@ use crate::format_json::tokenize_json;
pub use formatter::Formatter;
use core::file_handlers::Language;
+use core::App;
pub use format_element::{
concat_elements, empty_element, group_elements, hard_line_break, if_group_breaks,
if_group_fits_on_single_line, indent, join_elements, soft_indent, soft_line_break,
@@ -145,7 +146,7 @@ impl FormatResult {
// TODO: implement me + handle errors
/// Main function
-pub fn format(rome_path: &mut RomePath, options: FormatOptions) {
+pub fn format(rome_path: &mut RomePath, options: FormatOptions) -> Option<FormatResult> {
// we assume that file exists
let mut file = rome_path.open();
let mut buffer = String::new();
@@ -167,18 +168,28 @@ pub fn format(rome_path: &mut RomePath, options: FormatOptions) {
Language::Ts | Language::Unknown => None,
};
+ result
+ } else {
+ None
+ }
+ } else {
+ None
+ }
+}
+
+pub fn format_file_and_save(rome_path: &mut RomePath, options: FormatOptions) {
+ let result = format(rome_path, options);
if let Some(result) = result {
rome_path
.save(result.code())
.expect("Could not write the formatted code on file");
}
- };
- }
}
-pub fn format_str(content: &str, options: FormatOptions) -> FormatResult {
- let element = tokenize_json(content);
- format_element(&element, options)
+pub fn format_file(path_to_file: &str, options: FormatOptions, app: &App) -> FormatResult {
+ let mut rome_path = RomePath::new(path_to_file).deduce_handler(app);
+ let element = format(&mut rome_path, options);
+ element.unwrap()
}
pub fn format_element(element: &FormatElement, options: FormatOptions) -> FormatResult {

MODIFY
crates/formatter/src/ts/mod.rs
crates/formatter/src/ts/mod.rs
@@ -22,7 +22,11 @@ mod test {
let tree = parse_text(src, 0);
let child = Script::cast(tree.syntax()).unwrap();
let result = Formatter::default().format_root(child.syntax());
- assert_eq!(result.code(), "let v = (value, second_value) => true;");
+ assert_eq!(
+ result.code(),
+ "let v = (value, second_value) => true;
+"
+ );
}
#[test]
@@ -35,7 +39,8 @@ mod test {
result.code(),
r#"function foo() {
return "something";
-}"#
+}
+"#
);
}
@@ -45,7 +50,11 @@ mod test {
let tree = parse_text(src, 0);
let child = Script::cast(tree.syntax()).unwrap();
let result = Formatter::default().format_root(child.syntax());
- assert_eq!(result.code(), r#"let users = ["john", "chandler", true];"#);
+ assert_eq!(
+ result.code(),
+ r#"let users = ["john", "chandler", true];
+"#
+ );
}
#[test]
@@ -63,7 +72,8 @@ mod test {
let var1 = [true, false];
let broken = [45, 54];
let var2 = (var1, var2) => {};
-}"#
+}
+"#
);
}
}

MODIFY
crates/formatter/src/ts/script.rs
crates/formatter/src/ts/script.rs
-use crate::{concat_elements, hard_line_break, FormatElement, Formatter, ToFormatElement};
+use crate::{format_elements, hard_line_break, FormatElement, Formatter, ToFormatElement};
use rslint_parser::ast::Script;
impl ToFormatElement for Script {
@@ -12,6 +12,6 @@ impl ToFormatElement for Script {
tokens.extend(self.items().map(|item| formatter.format_node(item)));
- concat_elements(tokens)
+ format_elements![concat_elements(tokens), hard_line_break()]
}
}

MODIFY
crates/formatter/tests/spec_test.rs
crates/formatter/tests/spec_test.rs
-use rome_formatter::{format_str, FormatOptions};
+use core::create_app;
+use rome_formatter::{format_file, FormatOptions};
use std::fs;
use std::path::Path;
@@ -14,6 +15,8 @@ use std::path::Path;
/// * `json/null` -> input: `tests/specs/json/null.json`, expected output: `tests/specs/json/null.expected.json`
/// * `null` -> input: `tests/specs/null.json`, expected output: `tests/specs/null.expected.json`
pub fn run(spec_input_file: &str, expected_file: &str) {
+ let app = create_app();
+ let file_path = &spec_input_file;
let spec_input_file = Path::new(spec_input_file);
let expected_file = Path::new(expected_file);
@@ -29,8 +32,7 @@ pub fn run(spec_input_file: &str, expected_file: &str) {
expected_file.display(),
);
- let spec_content = fs::read_to_string(spec_input_file).unwrap();
- let result = format_str(spec_content.as_str(), FormatOptions::default());
+ let result = format_file(file_path, FormatOptions::default(), &app);
let expected_output = fs::read_to_string(expected_file).unwrap();
assert_eq!(&expected_output, result.code());

MODIFY
crates/formatter/tests/spec_tests.rs
crates/formatter/tests/spec_tests.rs
@@ -5,4 +5,9 @@ mod formatter {
use crate::spec_test;
tests_macros::gen_tests! {"tests/specs/json/*.json", spec_test::run}
}
+
+ mod js {
+ use crate::spec_test;
+ tests_macros::gen_tests! {"tests/specs/js/*.js", spec_test::run}
+ }
}

ADD
crates/formatter/tests/specs/js/simple.expected.js
+let v = (value, second_value) => true;

ADD
crates/formatter/tests/specs/js/simple.js
+let v = (value , second_value) => true

test(formatter): enable spec tests fo JS files (#1701)