Skip to content

Commit 2cead8b

Browse files
committed
feat(formatter): keep parser options consistent for all formatter usages (#13884)
Copy from oxfmt https://github.com/oxc-project/oxc/blob/adda5f56de542ce178f0c68f5732d60166c278b2/apps/oxfmt/src/service.rs#L45-L54
1 parent 7d45b2f commit 2cead8b

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

crates/oxc_formatter/examples/formatter.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ fn main() -> Result<(), String> {
3434
// Parse the source code
3535
let ret = Parser::new(&allocator, &source_text, source_type)
3636
.with_options(ParseOptions {
37-
preserve_parens: false,
37+
parse_regular_expression: false,
38+
// Enable all syntax features
3839
allow_v8_intrinsics: true,
39-
..ParseOptions::default()
40+
allow_return_outside_function: true,
41+
// `oxc_formatter` expects this to be false
42+
preserve_parens: false,
4043
})
4144
.parse();
4245

tasks/benchmark/benches/formatter.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ fn bench_formatter(criterion: &mut Criterion) {
1515
group.bench_function(id, |b| {
1616
b.iter_with_setup_wrapper(|runner| {
1717
allocator.reset();
18-
let parse_options = ParseOptions { preserve_parens: false, ..Default::default() };
18+
let parse_options = ParseOptions {
19+
parse_regular_expression: false,
20+
// Enable all syntax features
21+
allow_v8_intrinsics: true,
22+
allow_return_outside_function: true,
23+
// `oxc_formatter` expects this to be false
24+
preserve_parens: false,
25+
};
1926
let program = Parser::new(&allocator, source_text, source_type)
2027
.with_options(parse_options)
2128
.parse()

tasks/coverage/snapshots/formatter_babel.snap

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ commit: 41d96516
22

33
formatter_babel Summary:
44
AST Parsed : 2423/2423 (100.00%)
5-
Positive Passed: 2417/2423 (99.75%)
5+
Positive Passed: 2419/2423 (99.83%)
66
Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js
77

88
Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/comments/regression/13750/input.js
99

10-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/sourcetype-commonjs/top-level-return/input.js
11-
A 'return' statement can only be used within a function body.
12-
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/sourcetype-commonjs/top-level-return-asi/input.js
13-
A 'return' statement can only be used within a function body.
1410
Mismatch: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2015/class/division/input.js
1511

1612
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/es2022/top-level-await-unambiguous/module/input.js

tasks/coverage/src/tools/formatter.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ fn get_result(source_text: &str, source_type: SourceType) -> TestResult {
2020
let options = FormatOptions::default();
2121

2222
let allocator = Allocator::default();
23-
let parse_options = ParseOptions { preserve_parens: false, ..ParseOptions::default() };
23+
let parse_options = ParseOptions {
24+
parse_regular_expression: false,
25+
// Enable all syntax features
26+
allow_v8_intrinsics: true,
27+
allow_return_outside_function: true,
28+
// `oxc_formatter` expects this to be false
29+
preserve_parens: false,
30+
};
2431
let ParserReturn { program, .. } =
2532
Parser::new(&allocator, source_text, source_type).with_options(parse_options).parse();
2633
let source_text1 = Formatter::new(&allocator, options.clone()).build(&program);

tasks/prettier_conformance/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,12 @@ impl TestRunner {
422422
let source_type = source_type.with_jsx(source_type.is_javascript());
423423
let ret = Parser::new(&allocator, source_text, source_type)
424424
.with_options(ParseOptions {
425-
preserve_parens: false,
425+
parse_regular_expression: false,
426+
// Enable all syntax features
426427
allow_v8_intrinsics: true,
427-
..ParseOptions::default()
428+
allow_return_outside_function: true,
429+
// `oxc_formatter` expects this to be false
430+
preserve_parens: false,
428431
})
429432
.parse();
430433
Formatter::new(&allocator, formatter_options).build(&ret.program)

0 commit comments

Comments
 (0)