Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, ops::Deref};
use std::ops::Deref;

use itertools::Itertools;
use oxc_ast::{
Expand All @@ -13,10 +13,9 @@ use oxc_span::{CompactStr, GetSpan, Span};

use crate::{LintContext, rule::Rule};

fn class_methods_use_this_diagnostic(span: Span, name: Option<Cow<'_, str>>) -> OxcDiagnostic {
let method_name_str = name.map_or(String::new(), |name| format!(" `{name}`"));
OxcDiagnostic::warn(format!("Expected method{method_name_str} to have this."))
.with_help(format!("Consider converting method{method_name_str} to a static method."))
fn class_methods_use_this_diagnostic(span: Span, name: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Expected method `{name}` to have this."))
.with_help(format!("Consider converting method `{name}` to a static method."))
.with_label(span)
}

Expand Down Expand Up @@ -219,8 +218,10 @@ impl Rule for ClassMethodsUseThis {
}
let mut finder = ThisFinder::new();
finder.visit_function_body(function_body);
if !finder.has_this {
ctx.diagnostic(class_methods_use_this_diagnostic(name.span(), name.name()));
if !finder.has_this
&& let Some(name_str) = name.name()
{
ctx.diagnostic(class_methods_use_this_diagnostic(name.span(), &name_str));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub fn to_precision(mut num: f64, precision: usize) -> String {
let precision_i32 = precision as i32;

// Handle sign
let mut prefix = String::new();
let mut prefix = String::with_capacity(8);
if num < 0.0 {
prefix.push('-');
num = -num;
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/sort_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ impl SortImports {
// add a empty string for zip with specifiers
paddings.push("");

let specifiers_len = specifiers.len();
let specifiers = specifiers.iter().sorted_by(|a, b| {
let a = a.local.name.as_str();
let b = b.local.name.as_str();
Expand All @@ -334,7 +335,7 @@ impl SortImports {
});

let sorted_text = specifiers.zip(paddings).fold(
String::new(),
String::with_capacity(specifiers_len * 8),
|mut acc, (specifier, padding)| {
let _ = acc.write_str(ctx.source_range(specifier.span));
let _ = acc.write_str(padding);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/sort_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl Rule for SortKeys {
// trailing comma) to the end, we must remove their trailing comma so
// the resulting object doesn't end up with an extra comma before `}`.
// Also normalize separators between properties to `, ` for clarity.
let mut sorted_text = String::new();
let mut sorted_text = String::with_capacity(8 * indices.len()); // initialize with avg 8 chars per prop
let trim_end_commas = |s: &str| -> String {
let s = s.trim_end();
let mut trimmed = s.to_string();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Rule for ConsistentTypeSpecifierStyle {
ctx.diagnostic_with_fix(
consistent_type_specifier_style_diagnostic(item.span(), &self.mode),
|fixer| {
let mut import_source = String::new();
let mut import_source = String::with_capacity(128);

if !value_specifiers.is_empty() {
let value_import_declaration =
Expand Down
14 changes: 12 additions & 2 deletions crates/oxc_linter/src/rules/react/jsx_fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ impl Rule for JsxFragments {
closing_element.span().end,
jsx_elem.span().end,
));
let mut replacement = String::new();
let mut replacement = String::with_capacity(
before_opening_tag.len()
+ between_opening_tag_and_closing_tag.len()
+ after_closing_tag.len()
+ "<></>".len(),
);
replacement.push_str(before_opening_tag);
replacement.push_str("<>");
replacement.push_str(between_opening_tag_and_closing_tag);
Expand All @@ -156,7 +161,12 @@ impl Rule for JsxFragments {
jsx_frag.closing_fragment.span().end,
jsx_frag.span().end,
));
let mut replacement = String::new();
let mut replacement = String::with_capacity(
before_opening_tag.len()
+ between_opening_tag_and_closing_tag.len()
+ after_closing_tag.len()
+ "<React.Fragment></React.Fragment>".len(),
);
replacement.push_str(before_opening_tag);
replacement.push_str("<React.Fragment>");
replacement.push_str(between_opening_tag_and_closing_tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,18 @@ impl Rule for ConsistentTypeDefinitions {
let body_span = &decl.body.span;
let body = &ctx.source_text()[body_span.start as usize..body_span.end as usize];

let mut extends = String::new();
for exp in &decl.extends {
write!(extends, " & {}", exp.span.source_text(ctx.source_text())).unwrap();
}

ctx.diagnostic_with_fix(
consistent_type_definitions_diagnostic(
"type",
"interface",
Span::sized(decl.span.start, 9),
),
|fixer| {
let mut extends = String::with_capacity(8 * decl.extends.len());
for exp in &decl.extends {
write!(extends, " & {}", exp.span.source_text(ctx.source_text()))
.unwrap();
}
fixer.replace(
exp.span,
format!("type {name} = {body}{extends}\nexport default {name}"),
Expand Down Expand Up @@ -220,18 +220,18 @@ impl Rule for ConsistentTypeDefinitions {
let body_span = &decl.body.span;
let body = &ctx.source_text()[body_span.start as usize..body_span.end as usize];

let mut extends = String::new();
for exp in &decl.extends {
write!(extends, " & {}", exp.span.source_text(ctx.source_text())).unwrap();
}

ctx.diagnostic_with_fix(
consistent_type_definitions_diagnostic(
"type",
"interface",
Span::sized(start, 9),
),
|fixer| {
let mut extends = String::with_capacity(8 * decl.extends.len());
for exp in &decl.extends {
write!(extends, " & {}", exp.span.source_text(ctx.source_text()))
.unwrap();
}
fixer.replace(
Span::new(start, decl.span.end),
format!("type {name} = {body}{extends}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn is_pow_2_expression(expression: &Expression, ctx: &LintContext<'_>) -> bool {
/// removes any newlines from the string
/// removes any duplicate spaces
fn clean_string(input: &str) -> String {
let mut result = String::new();
let mut result = String::with_capacity(input.len());
let mut prev_char = ' ';

for c in input.chars() {
Expand Down
Loading