Skip to content

Commit

Permalink
perf(linter): use CompactStr in prefer-lowercase-title
Browse files Browse the repository at this point in the history
  • Loading branch information
camchenry authored and DonIsaac committed Oct 10, 2024
1 parent 114b8c3 commit ca75d1c
Showing 1 changed file with 8 additions and 11 deletions.
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

0 comments on commit ca75d1c

Please sign in to comment.