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

feat(rome_cli): Enable JSON formatting #4133

Merged
merged 8 commits into from
Jan 10, 2023
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
3 changes: 3 additions & 0 deletions Cargo.lock

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

32 changes: 17 additions & 15 deletions crates/rome_cli/tests/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ pub const CONFIG_FORMAT: &str = r#"{
"#;

pub const CONFIG_INIT_DEFAULT: &str = r#"{
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}"#;
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}
"#;

pub const CONFIG_INIT_DEFAULT_WHEN_INSTALLED: &str = r#"{
"$schema": "./node_modules/rome/configuration_schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}"#;
"$schema": "./node_modules/rome/configuration_schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}
"#;

pub const CONFIG_DISABLED_FORMATTER: &str = r#"{
"formatter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ expression: content

```json
{
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}

```

# Emitted Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ expression: content

```json
{
"$schema": "./node_modules/rome/configuration_schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
"$schema": "./node_modules/rome/configuration_schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}

```

## `./node_modules/rome/configuration_schema.json`
Expand Down
7 changes: 7 additions & 0 deletions crates/rome_fs/src/fs/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ fn handle_dir_entry<'scope>(
}

if file_type.is_file() {
if matches!(
path.file_name().and_then(OsStr::to_str),
Some("package.json" | "package-lock.json" | "tsconfig.json")
) {
return;
}

// Performing this check here let's us skip skip unsupported
// files entirely, as well as silently ignore unsupported files when
// doing a directory traversal, but printing an error message if the
Expand Down
10 changes: 9 additions & 1 deletion crates/rome_service/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub use javascript::{JavascriptConfiguration, JavascriptFormatter};
pub use linter::{LinterConfiguration, RuleConfiguration, Rules};
use rome_analyze::{AnalyzerConfiguration, AnalyzerRules, MetadataRegistry};
use rome_js_analyze::metadata;
use rome_json_formatter::context::JsonFormatOptions;
use rome_json_parser::parse_json;
use rome_json_syntax::JsonRoot;

/// The configuration that is contained inside the file `rome.json`
Expand Down Expand Up @@ -209,8 +211,14 @@ pub fn create_config(
WorkspaceError::Configuration(ConfigurationDiagnostic::new_serialization_error())
})?;

let parsed = parse_json(&contents);
let formatted =
rome_json_formatter::format_node(JsonFormatOptions::default(), &parsed.syntax())?
.print()
.expect("valid format document");

config_file
.set_content(contents.as_bytes())
.set_content(formatted.as_code().as_bytes())
.map_err(|_| WorkspaceError::cant_read_file(format!("{}", path.display())))?;

Ok(())
Expand Down
25 changes: 5 additions & 20 deletions crates/rome_service/src/file_handlers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ use crate::settings::{
};
use crate::workspace::GetSyntaxTreeResult;
use crate::WorkspaceError;
#[cfg(any(debug_assertions, target_family = "wasm"))]
use rome_formatter::{FormatError, Printed};
use rome_fs::RomePath;
use rome_json_formatter::context::JsonFormatOptions;
use rome_json_formatter::format_node;
use rome_json_syntax::{JsonLanguage, JsonRoot, JsonSyntaxNode};
use rome_parser::AnyParse;
#[cfg(any(debug_assertions, target_family = "wasm"))]
use rome_rowan::{TextRange, TextSize, TokenAtOffset};

impl Language for JsonLanguage {
Expand Down Expand Up @@ -61,25 +59,15 @@ impl ExtensionHandler for JsonFileHandler {
debug_formatter_ir: Some(debug_formatter_ir),
},
analyzer: Default::default(),
formatter: formatter_capabilities(),
formatter: FormatterCapabilities {
format: Some(format),
format_range: Some(format_range),
format_on_type: Some(format_on_type),
},
}
}
}

#[cfg(any(debug_assertions, target_family = "wasm"))]
fn formatter_capabilities() -> FormatterCapabilities {
FormatterCapabilities {
format: Some(format),
format_range: Some(format_range),
format_on_type: Some(format_on_type),
}
}

#[cfg(all(not(debug_assertions), not(target_family = "wasm")))]
fn formatter_capabilities() -> FormatterCapabilities {
FormatterCapabilities::default()
}

fn parse(_: &RomePath, _: LanguageId, text: &str) -> AnyParse {
let parse = rome_json_parser::parse_json(text);
AnyParse::from(parse)
Expand Down Expand Up @@ -108,7 +96,6 @@ fn debug_formatter_ir(
Ok(root_element.to_string())
}

#[cfg(any(debug_assertions, target_family = "wasm"))]
#[tracing::instrument(level = "debug", skip(parse))]
fn format(
rome_path: &RomePath,
Expand All @@ -128,7 +115,6 @@ fn format(
}
}

#[cfg(any(debug_assertions, target_family = "wasm"))]
fn format_range(
rome_path: &RomePath,
parse: AnyParse,
Expand All @@ -142,7 +128,6 @@ fn format_range(
Ok(printed)
}

#[cfg(any(debug_assertions, target_family = "wasm"))]
fn format_on_type(
rome_path: &RomePath,
parse: AnyParse,
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载