Skip to content

Commit 1b58521

Browse files
committed
feat(oxfmt,language_server): Enable JSX for all JS source type (#14605)
Closes #14468
1 parent 14686a4 commit 1b58521

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

apps/oxfmt/src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rayon::prelude::*;
55

66
use oxc_allocator::Allocator;
77
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, OxcDiagnostic};
8-
use oxc_formatter::{FormatOptions, Formatter};
8+
use oxc_formatter::{FormatOptions, Formatter, enable_jsx_source_type};
99
use oxc_parser::{ParseOptions, Parser};
1010

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

4747
let path = Path::new(&entry.path);
48-
let source_type = entry.source_type;
48+
let source_type = enable_jsx_source_type(entry.source_type);
4949

5050
// TODO: Use `read_to_arena_str()` like `oxlint`?
5151
let source_text = fs::read_to_string(path).expect("Failed to read file");

crates/oxc_formatter/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ use rustc_hash::{FxHashMap, FxHashSet};
3535
use write::FormatWrite;
3636

3737
pub use crate::options::*;
38-
pub use crate::service::{oxfmtrc::Oxfmtrc, source_type::get_supported_source_type};
38+
pub use crate::service::{
39+
oxfmtrc::Oxfmtrc,
40+
source_type::{enable_jsx_source_type, get_supported_source_type},
41+
};
3942
use crate::{
4043
formatter::{FormatContext, Formatted, format_element::document::Document},
4144
generated::ast_nodes::{AstNode, AstNodes},

crates/oxc_formatter/src/service/source_type.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,23 @@ pub fn get_supported_source_type(path: &std::path::Path) -> Option<SourceType> {
4848

4949
None
5050
}
51+
52+
#[must_use]
53+
pub fn enable_jsx_source_type(source_type: SourceType) -> SourceType {
54+
if source_type.is_jsx() {
55+
return source_type;
56+
}
57+
58+
// Always enable JSX for JavaScript files, no syntax conflict
59+
if source_type.is_javascript() {
60+
return source_type.with_jsx(true);
61+
}
62+
63+
// Prettier uses `regexp.test(source_text)` to detect JSX in TypeScript files.
64+
// But we don't follow it for now, since it hurts the performance.
65+
// if source_type.is_typescript() {
66+
// // See https://github.com/prettier/prettier/blob/0d1e7abd5037a1fe8fbcf88a4d8cd13ec4d13a78/src/language-js/parse/utils/jsx-regexp.evaluate.js
67+
// }
68+
69+
source_type
70+
}

crates/oxc_language_server/src/formatter/server_formatter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::path::Path;
33
use log::warn;
44
use oxc_allocator::Allocator;
55
use oxc_data_structures::rope::{Rope, get_line_column};
6-
use oxc_formatter::{FormatOptions, Formatter, Oxfmtrc, get_supported_source_type};
6+
use oxc_formatter::{
7+
FormatOptions, Formatter, Oxfmtrc, enable_jsx_source_type, get_supported_source_type,
8+
};
79
use oxc_parser::{ParseOptions, Parser};
810
use tower_lsp_server::{
911
UriExt,
@@ -25,7 +27,7 @@ impl ServerFormatter {
2527

2628
pub fn run_single(&self, uri: &Uri, content: Option<String>) -> Option<Vec<TextEdit>> {
2729
let path = uri.to_file_path()?;
28-
let source_type = get_supported_source_type(&path)?;
30+
let source_type = get_supported_source_type(&path).map(enable_jsx_source_type)?;
2931
let source_text = if let Some(content) = content {
3032
content
3133
} else {

0 commit comments

Comments
 (0)