From 8c499c623be73c1cdc3bd2f12928c0bce3b6d803 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Sat, 3 May 2025 00:31:29 +0000 Subject: [PATCH] fix(linter): fix panic when doing code gen on regexp (#10769) fixes the panic in https://github.com/oxc-project/oxc/pull/10765 by providing the source text of the program to the code generator --- crates/oxc_codegen/src/lib.rs | 1 + crates/oxc_linter/src/fixer/mod.rs | 2 +- crates/oxc_linter/src/rules/unicorn/prefer_spread.rs | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 0d1a2a9ddbc04..ca28744ec2676 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -262,6 +262,7 @@ impl<'a> Codegen<'a> { /// NOTE: you must call [`Codegen::with_source_text`] before calling this method #[inline] pub fn print_expression(&mut self, expr: &Expression<'_>) { + debug_assert!(!self.source_text.is_empty()); expr.print_expr(self, Precedence::Lowest, Context::empty()); } } diff --git a/crates/oxc_linter/src/fixer/mod.rs b/crates/oxc_linter/src/fixer/mod.rs index 9c967234360e3..8b58663eec4da 100644 --- a/crates/oxc_linter/src/fixer/mod.rs +++ b/crates/oxc_linter/src/fixer/mod.rs @@ -184,10 +184,10 @@ impl<'c, 'a: 'c> RuleFixer<'c, 'a> { self.new_fix(CompositeFix::Single(fix), message) } - #[expect(clippy::unused_self)] #[must_use] pub fn codegen(self) -> Codegen<'a> { Codegen::new() + .with_source_text(self.source_text()) .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) } diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_spread.rs b/crates/oxc_linter/src/rules/unicorn/prefer_spread.rs index 9645ec774e024..da7748161829e 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_spread.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_spread.rs @@ -553,6 +553,11 @@ fn test() { // `string.split()` (r#""🦄".split("")"#, r#"[..."🦄"]"#, None), (r#""foo bar baz".split("")"#, r#"[..."foo bar baz"]"#, None), + ( + r"Array.from(path.matchAll(/\{([^{}?]+\??)\}/g))", + "[...path.matchAll(/\\{([^{}?]+\\??)\\}/g)]", + None, + ), ]; Tester::new(PreferSpread::NAME, PreferSpread::PLUGIN, pass, fail)