Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(linter): use CompactStr in prefer-lowercase-title #6405

Merged
Merged
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
19 changes: 8 additions & 11 deletions crates/oxc_linter/src/rules/jest/prefer_lowercase_title.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::{ast::Argument, AstKind};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_span::{CompactStr, Span};

use crate::{
context::LintContext,
Expand All @@ -20,8 +20,8 @@ fn unexpected_lowercase(x0: &str, span1: Span) -> OxcDiagnostic {

#[derive(Debug, Default, Clone)]
pub struct PreferLowercaseTitleConfig {
allowed_prefixes: Vec<String>,
ignore: Vec<String>,
allowed_prefixes: Vec<CompactStr>,
ignore: Vec<CompactStr>,
ignore_top_level_describe: bool,
}

Expand Down Expand Up @@ -142,16 +142,12 @@ impl Rule for PreferLowercaseTitle {
let ignore = obj
.and_then(|config| config.get("ignore"))
.and_then(serde_json::Value::as_array)
.map(|v| {
v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect()
})
.map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect())
.unwrap_or_default();
let allowed_prefixes = obj
.and_then(|config| config.get("allowedPrefixes"))
.and_then(serde_json::Value::as_array)
.map(|v| {
v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect()
})
.map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect())
.unwrap_or_default();

Self(Box::new(PreferLowercaseTitleConfig {
Expand Down Expand Up @@ -209,7 +205,7 @@ impl PreferLowercaseTitle {
}
}

fn populate_ignores(ignore: &[String]) -> Vec<&str> {
fn populate_ignores(ignore: &[CompactStr]) -> Vec<&str> {
let mut ignores: Vec<&str> = vec![];
let test_case_name = ["fit", "it", "xit", "test", "xtest"];
let describe_alias = ["describe", "fdescribe", "xdescribe"];
Expand All @@ -232,7 +228,8 @@ impl PreferLowercaseTitle {
}

fn lint_string<'a>(&self, ctx: &LintContext<'a>, literal: &'a str, span: Span) {
if literal.is_empty() || self.allowed_prefixes.iter().any(|name| literal.starts_with(name))
if literal.is_empty()
|| self.allowed_prefixes.iter().any(|name| literal.starts_with(name.as_str()))
{
return;
}
Expand Down