Skip to content

Commit a6fa354

Browse files
committed
refactor(formatter): Export unified way to get_parse_options
1 parent d65fd52 commit a6fa354

File tree

14 files changed

+48
-108
lines changed

14 files changed

+48
-108
lines changed

apps/oxfmt/src/service.rs

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

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

1111
use crate::{command::OutputOptions, walk::WalkEntry};
1212

@@ -53,14 +53,7 @@ impl FormatService {
5353
let allocator = Allocator::new();
5454

5555
let ret = Parser::new(&allocator, &source_text, source_type)
56-
.with_options(ParseOptions {
57-
parse_regular_expression: false,
58-
// Enable all syntax features
59-
allow_v8_intrinsics: true,
60-
allow_return_outside_function: true,
61-
// `oxc_formatter` expects this to be false
62-
preserve_parens: false,
63-
})
56+
.with_options(get_parse_options())
6457
.parse();
6558
if !ret.errors.is_empty() {
6659
let diagnostics = DiagnosticService::wrap_diagnostics(

crates/oxc_formatter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ doctest = false
2323
oxc_allocator = { workspace = true }
2424
oxc_ast = { workspace = true }
2525
oxc_data_structures = { workspace = true, features = ["stack"] }
26+
oxc_parser = { workspace = true }
2627
oxc_span = { workspace = true }
2728
oxc_syntax = { workspace = true }
2829

@@ -36,7 +37,6 @@ unicode-width = "0.2"
3637

3738
[dev-dependencies]
3839
insta = { workspace = true }
39-
oxc_parser = { workspace = true }
4040
pico-args = { workspace = true }
4141
project-root = { workspace = true }
4242
serde_json = { workspace = true }

crates/oxc_formatter/examples/formatter.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use std::{fs, path::Path};
1515

1616
use oxc_allocator::Allocator;
17-
use oxc_formatter::{BracketSameLine, FormatOptions, Formatter, Semicolons};
18-
use oxc_parser::{ParseOptions, Parser};
17+
use oxc_formatter::{BracketSameLine, FormatOptions, Formatter, Semicolons, get_parse_options};
18+
use oxc_parser::Parser;
1919
use oxc_span::SourceType;
2020
use pico_args::Arguments;
2121

@@ -34,14 +34,7 @@ fn main() -> Result<(), String> {
3434

3535
// Parse the source code
3636
let ret = Parser::new(&allocator, &source_text, source_type)
37-
.with_options(ParseOptions {
38-
parse_regular_expression: false,
39-
// Enable all syntax features
40-
allow_v8_intrinsics: true,
41-
allow_return_outside_function: true,
42-
// `oxc_formatter` expects this to be false
43-
preserve_parens: false,
44-
})
37+
.with_options(get_parse_options())
4538
.parse();
4639

4740
// Report any parsing errors

crates/oxc_formatter/examples/sort_imports.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use std::{fs, path::Path};
44

55
use oxc_allocator::Allocator;
6-
use oxc_formatter::{FormatOptions, Formatter, SortImports, SortOrder};
7-
use oxc_parser::{ParseOptions, Parser};
6+
use oxc_formatter::{FormatOptions, Formatter, SortImports, SortOrder, get_parse_options};
7+
use oxc_parser::Parser;
88
use oxc_span::SourceType;
99
use pico_args::Arguments;
1010

@@ -36,14 +36,7 @@ fn main() -> Result<(), String> {
3636

3737
// Parse the source code
3838
let ret = Parser::new(&allocator, &source_text, source_type)
39-
.with_options(ParseOptions {
40-
parse_regular_expression: false,
41-
// Enable all syntax features
42-
allow_v8_intrinsics: true,
43-
allow_return_outside_function: true,
44-
// `oxc_formatter` expects this to be false
45-
preserve_parens: false,
46-
})
39+
.with_options(get_parse_options())
4740
.parse();
4841

4942
// Report any parsing errors

crates/oxc_formatter/src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
3232
use write::FormatWrite;
3333

3434
pub use crate::options::*;
35-
pub use crate::service::{
36-
oxfmtrc::Oxfmtrc,
37-
source_type::{enable_jsx_source_type, get_supported_source_type},
38-
};
35+
pub use crate::service::{oxfmtrc::Oxfmtrc, parse_utils::*};
3936
use crate::{
4037
ast_nodes::{AstNode, AstNodes},
4138
formatter::{FormatContext, Formatted, format_element::document::Document},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub mod oxfmtrc;
2-
pub mod source_type;
2+
pub mod parse_utils;

crates/oxc_formatter/src/service/source_type.rs renamed to crates/oxc_formatter/src/service/parse_utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
use std::ffi::OsStr;
22

3+
use oxc_parser::ParseOptions;
34
use oxc_span::SourceType;
45

6+
pub fn get_parse_options() -> ParseOptions {
7+
ParseOptions {
8+
// Do not need to parse regexp
9+
parse_regular_expression: false,
10+
// Enable all syntax features
11+
allow_return_outside_function: true,
12+
allow_v8_intrinsics: true,
13+
// `oxc_formatter` expects this to be `false`, otherwise panics
14+
preserve_parens: false,
15+
}
16+
}
17+
518
// Additional extensions from linguist-languages, which Prettier also supports
619
// - https://github.com/ikatyang-collab/linguist-languages/blob/d1dc347c7ced0f5b42dd66c7d1c4274f64a3eb6b/data/JavaScript.js
720
// No special extensions for TypeScript

crates/oxc_formatter/tests/fixtures/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::{env::current_dir, fs, path::Path, str::FromStr};
33
use oxc_allocator::Allocator;
44
use oxc_formatter::{
55
ArrowParentheses, BracketSameLine, BracketSpacing, FormatOptions, Formatter, IndentStyle,
6-
IndentWidth, LineWidth, QuoteStyle, Semicolons, TrailingCommas,
6+
IndentWidth, LineWidth, QuoteStyle, Semicolons, TrailingCommas, get_parse_options,
77
};
8-
use oxc_parser::{ParseOptions, Parser};
8+
use oxc_parser::Parser;
99
use oxc_span::SourceType;
1010

1111
type OptionSet = serde_json::Map<String, serde_json::Value>;
@@ -141,14 +141,8 @@ fn format_options_display(json: &OptionSet) -> String {
141141
/// Format a source file with given options
142142
fn format_source(source_text: &str, source_type: SourceType, options: FormatOptions) -> String {
143143
let allocator = Allocator::default();
144-
let ret = Parser::new(&allocator, source_text, source_type)
145-
.with_options(ParseOptions {
146-
parse_regular_expression: false,
147-
allow_v8_intrinsics: true,
148-
allow_return_outside_function: true,
149-
preserve_parens: false,
150-
})
151-
.parse();
144+
let ret =
145+
Parser::new(&allocator, source_text, source_type).with_options(get_parse_options()).parse();
152146

153147
let formatter = Formatter::new(&allocator, options);
154148
formatter.build(&ret.program)

crates/oxc_formatter/tests/ir_transform/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,14 @@ pub fn assert_format(code: &str, options: &FormatOptions, expected: &str) {
3939

4040
fn format_code(code: &str, options: &FormatOptions) -> String {
4141
use oxc_allocator::Allocator;
42-
use oxc_formatter::Formatter;
43-
use oxc_parser::{ParseOptions, Parser};
42+
use oxc_formatter::{Formatter, get_parse_options};
43+
use oxc_parser::Parser;
4444
use oxc_span::SourceType;
4545

4646
let allocator = Allocator::new();
4747
let source_type = SourceType::from_path("dummy.tsx").unwrap();
4848

49-
let ret = Parser::new(&allocator, code, source_type)
50-
.with_options(ParseOptions {
51-
parse_regular_expression: false,
52-
// Enable all syntax features
53-
allow_v8_intrinsics: true,
54-
allow_return_outside_function: true,
55-
// `oxc_formatter` expects this to be false
56-
preserve_parens: false,
57-
})
58-
.parse();
49+
let ret = Parser::new(&allocator, code, source_type).with_options(get_parse_options()).parse();
5950

6051
if let Some(error) = ret.errors.first() {
6152
panic!("💥 Parser error: {}", error.message);

crates/oxc_language_server/src/formatter/server_formatter.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use log::warn;
44
use oxc_allocator::Allocator;
55
use oxc_data_structures::rope::{Rope, get_line_column};
66
use oxc_formatter::{
7-
FormatOptions, Formatter, Oxfmtrc, enable_jsx_source_type, get_supported_source_type,
7+
FormatOptions, Formatter, Oxfmtrc, enable_jsx_source_type, get_parse_options,
8+
get_supported_source_type,
89
};
9-
use oxc_parser::{ParseOptions, Parser};
10+
use oxc_parser::Parser;
1011
use tower_lsp_server::{
1112
UriExt,
1213
lsp_types::{Pattern, Position, Range, TextEdit, Uri},
@@ -42,14 +43,7 @@ impl ServerFormatter {
4243

4344
let allocator = Allocator::new();
4445
let ret = Parser::new(&allocator, &source_text, source_type)
45-
.with_options(ParseOptions {
46-
parse_regular_expression: false,
47-
// Enable all syntax features
48-
allow_v8_intrinsics: true,
49-
allow_return_outside_function: true,
50-
// `oxc_formatter` expects this to be false
51-
preserve_parens: false,
52-
})
46+
.with_options(get_parse_options())
5347
.parse();
5448

5549
if !ret.errors.is_empty() {

0 commit comments

Comments
 (0)