Skip to content

Commit 8c499c6

Browse files
committed
fix(linter): fix panic when doing code gen on regexp (#10769)
fixes the panic in #10765 by providing the source text of the program to the code generator
1 parent 2d13b49 commit 8c499c6

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

crates/oxc_codegen/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl<'a> Codegen<'a> {
262262
/// NOTE: you must call [`Codegen::with_source_text`] before calling this method
263263
#[inline]
264264
pub fn print_expression(&mut self, expr: &Expression<'_>) {
265+
debug_assert!(!self.source_text.is_empty());
265266
expr.print_expr(self, Precedence::Lowest, Context::empty());
266267
}
267268
}

crates/oxc_linter/src/fixer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ impl<'c, 'a: 'c> RuleFixer<'c, 'a> {
184184
self.new_fix(CompositeFix::Single(fix), message)
185185
}
186186

187-
#[expect(clippy::unused_self)]
188187
#[must_use]
189188
pub fn codegen(self) -> Codegen<'a> {
190189
Codegen::new()
190+
.with_source_text(self.source_text())
191191
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
192192
}
193193

crates/oxc_linter/src/rules/unicorn/prefer_spread.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ fn test() {
553553
// `string.split()`
554554
(r#""🦄".split("")"#, r#"[..."🦄"]"#, None),
555555
(r#""foo bar baz".split("")"#, r#"[..."foo bar baz"]"#, None),
556+
(
557+
r"Array.from(path.matchAll(/\{([^{}?]+\??)\}/g))",
558+
"[...path.matchAll(/\\{([^{}?]+\\??)\\}/g)]",
559+
None,
560+
),
556561
];
557562

558563
Tester::new(PreferSpread::NAME, PreferSpread::PLUGIN, pass, fail)

0 commit comments

Comments
 (0)