Skip to content
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
4 changes: 2 additions & 2 deletions apps/oxfmt/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rayon::prelude::*;

use oxc_allocator::Allocator;
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, OxcDiagnostic};
use oxc_formatter::{FormatOptions, Formatter};
use oxc_formatter::{FormatOptions, Formatter, enable_jsx_source_type};
use oxc_parser::{ParseOptions, Parser};

use crate::{command::OutputOptions, walk::WalkEntry};
Expand Down Expand Up @@ -45,7 +45,7 @@ impl FormatService {
let start_time = Instant::now();

let path = Path::new(&entry.path);
let source_type = entry.source_type;
let source_type = enable_jsx_source_type(entry.source_type);

// TODO: Use `read_to_arena_str()` like `oxlint`?
let source_text = fs::read_to_string(path).expect("Failed to read file");
Expand Down
5 changes: 4 additions & 1 deletion crates/oxc_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ use rustc_hash::{FxHashMap, FxHashSet};
use write::FormatWrite;

pub use crate::options::*;
pub use crate::service::{oxfmtrc::Oxfmtrc, source_type::get_supported_source_type};
pub use crate::service::{
oxfmtrc::Oxfmtrc,
source_type::{enable_jsx_source_type, get_supported_source_type},
};
use crate::{
formatter::{FormatContext, Formatted, format_element::document::Document},
generated::ast_nodes::{AstNode, AstNodes},
Expand Down
20 changes: 20 additions & 0 deletions crates/oxc_formatter/src/service/source_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,23 @@ pub fn get_supported_source_type(path: &std::path::Path) -> Option<SourceType> {

None
}

#[must_use]
pub fn enable_jsx_source_type(source_type: SourceType) -> SourceType {
if source_type.is_jsx() {
return source_type;
}

// Always enable JSX for JavaScript files, no syntax conflict
if source_type.is_javascript() {
return source_type.with_jsx(true);
}

// Prettier uses `regexp.test(source_text)` to detect JSX in TypeScript files.
// But we don't follow it for now, since it hurts the performance.
// if source_type.is_typescript() {
// // See https://github.com/prettier/prettier/blob/0d1e7abd5037a1fe8fbcf88a4d8cd13ec4d13a78/src/language-js/parse/utils/jsx-regexp.evaluate.js
// }

source_type
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::path::Path;
use log::warn;
use oxc_allocator::Allocator;
use oxc_data_structures::rope::{Rope, get_line_column};
use oxc_formatter::{FormatOptions, Formatter, Oxfmtrc, get_supported_source_type};
use oxc_formatter::{
FormatOptions, Formatter, Oxfmtrc, enable_jsx_source_type, get_supported_source_type,
};
use oxc_parser::{ParseOptions, Parser};
use tower_lsp_server::{
UriExt,
Expand All @@ -25,7 +27,7 @@ impl ServerFormatter {

pub fn run_single(&self, uri: &Uri, content: Option<String>) -> Option<Vec<TextEdit>> {
let path = uri.to_file_path()?;
let source_type = get_supported_source_type(&path)?;
let source_type = get_supported_source_type(&path).map(enable_jsx_source_type)?;
let source_text = if let Some(content) = content {
content
} else {
Expand Down
Loading