Skip to content

Commit 75ea06e

Browse files
committed
feat(oxfmt): Enable JSX for all JS source type
1 parent e7c2748 commit 75ea06e

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
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+
}

0 commit comments

Comments
 (0)