Skip to content

Commit 93b0f7e

Browse files
authored
cargo: Set debug=false to avoid stack overflow (#114)
The same as `oxc` main repo. Stack overflow was occurred with https://www.npmjs.com/package/async?activeTab=code L7.
1 parent 9f4a803 commit 93b0f7e

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ ureq = "3.0.0"
3333
pico-args = "0.5.0"
3434
project-root = "0.2.2"
3535

36+
[profile.dev]
37+
# Disabling debug info reduces stack usage
38+
debug = false
39+
3640
[profile.release]
3741
strip = false # "symbols" # Set to `false` for debug information
3842
debug = true # Set to `true` for debug information

src/formatter.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use oxc::{
22
allocator::Allocator,
3-
parser::{ParseOptions, Parser},
3+
parser::{ParseOptions, Parser, ParserReturn},
44
};
55
use oxc_formatter::{FormatOptions, Formatter};
66

@@ -23,25 +23,47 @@ impl Case for FormatterRunner {
2323

2424
fn idempotency_test(&self, source: &Source) -> Result<String, Vec<Diagnostic>> {
2525
let Source { path, source_type, source_text } = source;
26+
2627
let allocator = Allocator::new();
27-
let options = ParseOptions { preserve_parens: false, ..ParseOptions::default() };
28-
let program = Parser::new(&allocator, source_text, *source_type)
29-
.with_options(options)
30-
.parse()
31-
.program;
32-
let source_text2 = Formatter::new(&allocator, FormatOptions::default()).build(&program);
33-
let program = Parser::new(&allocator, &source_text2, *source_type)
34-
.with_options(options)
35-
.parse()
36-
.program;
37-
let source_text3 = Formatter::new(&allocator, FormatOptions::default()).build(&program);
28+
let options = ParseOptions {
29+
preserve_parens: false,
30+
allow_return_outside_function: true,
31+
allow_v8_intrinsics: true,
32+
parse_regular_expression: false,
33+
};
34+
35+
let ParserReturn { program: program1, errors: errors1, .. } =
36+
Parser::new(&allocator, source_text, *source_type).with_options(options).parse();
37+
if !errors1.is_empty() {
38+
return Err(vec![Diagnostic {
39+
case: self.name(),
40+
path: path.clone(),
41+
message: format!("Parse error in original source: {errors1:?}"),
42+
}]);
43+
}
44+
45+
let source_text2 = Formatter::new(&allocator, FormatOptions::default()).build(&program1);
46+
47+
let ParserReturn { program: program2, errors: errors2, .. } =
48+
Parser::new(&allocator, &source_text2, *source_type).with_options(options).parse();
49+
if !errors2.is_empty() {
50+
return Err(vec![Diagnostic {
51+
case: self.name(),
52+
path: path.clone(),
53+
message: format!("Parse error after formatting: {errors2:?}"),
54+
}]);
55+
}
56+
57+
let source_text3 = Formatter::new(&allocator, FormatOptions::default()).build(&program2);
58+
3859
if source_text2 != source_text3 {
3960
return Err(vec![Diagnostic {
4061
case: self.name(),
4162
path: path.clone(),
4263
message: NodeModulesRunner::get_diff(&source_text2, &source_text3, false),
4364
}]);
4465
}
66+
4567
Ok(source_text3)
4668
}
4769
}

0 commit comments

Comments
 (0)