Skip to content

Commit

Permalink
perf(linter): remove unneeded memory allocation for str (#4142)
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 authored Oct 1, 2024
1 parent 7e29f06 commit f959987
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use biome_console::markup;
use biome_css_syntax::{AnyCssDeclarationName, CssGenericProperty, CssLanguage, CssSyntaxKind};
use biome_rowan::{AstNode, Language, SyntaxNode, TextRange, WalkEvent};

fn remove_vendor_prefix(prop: &str, prefix: &str) -> String {
fn remove_vendor_prefix<'a>(prop: &'a str, prefix: &'a str) -> &'a str {
if let Some(prop) = prop.strip_prefix(prefix) {
return prop.to_string();
return prop;
}

prop.to_string()
prop
}

fn get_override_props(property: &str) -> Vec<&str> {
Expand Down Expand Up @@ -109,18 +109,16 @@ impl Visitor for NoDeclarationBlockShorthandPropertyOverridesVisitor {
let prop_lowercase = prop.to_lowercase();

let prop_prefix = vender_prefix(&prop_lowercase);
let unprefixed_prop = remove_vendor_prefix(&prop_lowercase, &prop_prefix);
let override_props = get_override_props(&unprefixed_prop);
let unprefixed_prop = remove_vendor_prefix(&prop_lowercase, prop_prefix);
let override_props = get_override_props(unprefixed_prop);

self.prior_props_in_block.iter().for_each(|prior_prop| {
let prior_prop_prefix = vender_prefix(&prior_prop.lowercase);
let unprefixed_prior_prop =
remove_vendor_prefix(&prior_prop.lowercase, &prior_prop_prefix);
remove_vendor_prefix(&prior_prop.lowercase, prior_prop_prefix);

if prop_prefix == prior_prop_prefix
&& override_props
.binary_search(&unprefixed_prior_prop.as_str())
.is_ok()
&& override_props.binary_search(&unprefixed_prior_prop).is_ok()
{
ctx.match_query(
NoDeclarationBlockShorthandPropertyOverridesQuery {
Expand Down
8 changes: 4 additions & 4 deletions crates/biome_css_analyze/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ pub fn is_custom_function(value: &str) -> bool {
}

// Returns the vendor prefix extracted from an input string.
pub fn vender_prefix(prop: &str) -> String {
pub fn vender_prefix(prop: &str) -> &str {
for prefix in VENDOR_PREFIXES.iter() {
if prop.starts_with(prefix) {
return (*prefix).to_string();
return prefix;
}
}
String::new()
""
}

pub fn is_pseudo_elements(prop: &str) -> bool {
Expand All @@ -138,7 +138,7 @@ pub fn is_pseudo_elements(prop: &str) -> bool {
|| OTHER_PSEUDO_ELEMENTS.contains(&prop)
}

/// Check if the input string is custom selector
/// Check if the input string is custom selector
/// See https://drafts.csswg.org/css-extensions/#custom-selectors for more details
pub fn is_custom_selector(prop: &str) -> bool {
prop.starts_with("--")
Expand Down

0 comments on commit f959987

Please sign in to comment.