Skip to content

Commit dd33e57

Browse files
committed
perf(linter): remove unnecessary codegen in eslint/prefer-numeric-literals (#11099)
Related to #11098
1 parent 49d677c commit dd33e57

1 file changed

Lines changed: 25 additions & 38 deletions

File tree

crates/oxc_linter/src/rules/eslint/prefer_numeric_literals.rs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use oxc_macros::declare_oxc_lint;
1010
use oxc_span::Span;
1111
use phf::{Map, phf_map, phf_ordered_set};
1212

13-
use crate::{
14-
AstNode, ast_util::get_symbol_id_of_variable, context::LintContext, fixer::RuleFixer,
15-
rule::Rule,
16-
};
13+
use crate::{AstNode, ast_util::get_symbol_id_of_variable, context::LintContext, rule::Rule};
1714

1815
fn prefer_numeric_literals_diagnostic(span: Span, prefix_name: &str) -> OxcDiagnostic {
1916
OxcDiagnostic::warn(format!("Use {prefix_name} literals instead of parseInt()."))
@@ -147,10 +144,30 @@ fn check_arguments<'a>(call_expr: &CallExpression<'a>, ctx: &LintContext<'a>) {
147144
ctx.diagnostic_with_fix(
148145
prefer_numeric_literals_diagnostic(call_expr.span, name),
149146
|fixer| {
150-
fixer.replace(
151-
call_expr.span,
152-
generate_fix(fixer, ctx, call_expr, &argument, prefix),
153-
)
147+
let code = {
148+
let span = call_expr.span;
149+
let mut code = String::with_capacity(prefix.len() + argument.len() + 2);
150+
151+
if span.start > 1 {
152+
let start = ctx.source_text().as_bytes()[span.start as usize - 1];
153+
if start.is_ascii_alphabetic() || !start.is_ascii() {
154+
code.push(' ');
155+
}
156+
}
157+
158+
code.push_str(prefix);
159+
code.push_str(&argument);
160+
161+
if (span.end as usize) < ctx.source_text().len() {
162+
let end = ctx.source_text().as_bytes()[span.end as usize];
163+
if end.is_ascii_alphabetic() || !end.is_ascii() {
164+
code.push(' ');
165+
}
166+
}
167+
168+
code
169+
};
170+
fixer.replace(call_expr.span, code)
154171
},
155172
);
156173
}
@@ -190,36 +207,6 @@ fn get_string_argument(call_expr: &CallExpression) -> Option<String> {
190207
None
191208
}
192209

193-
fn generate_fix<'a>(
194-
fixer: RuleFixer<'_, 'a>,
195-
ctx: &LintContext<'a>,
196-
call_expr: &CallExpression<'a>,
197-
argument: &str,
198-
prefix: &str,
199-
) -> String {
200-
let mut formatter = fixer.codegen();
201-
let span = call_expr.span;
202-
203-
if span.start > 1 {
204-
let start = ctx.source_text().as_bytes()[span.start as usize - 1];
205-
if start.is_ascii_alphabetic() || !start.is_ascii() {
206-
formatter.print_str(" ");
207-
}
208-
}
209-
210-
formatter.print_str(prefix);
211-
formatter.print_str(argument);
212-
213-
if (span.end as usize) < ctx.source_text().len() {
214-
let end = ctx.source_text().as_bytes()[span.end as usize];
215-
if end.is_ascii_alphabetic() || !end.is_ascii() {
216-
formatter.print_str(" ");
217-
}
218-
}
219-
220-
formatter.into_source_text()
221-
}
222-
223210
#[test]
224211
fn test() {
225212
use crate::tester::Tester;

0 commit comments

Comments
 (0)