Skip to content

Commit

Permalink
chore: to_lowercase -> to_lowercase_cow (#4030)
Browse files Browse the repository at this point in the history
  • Loading branch information
minht11 authored Sep 26, 2024
1 parent d20ad1e commit 75b4387
Show file tree
Hide file tree
Showing 42 changed files with 182 additions and 88 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
allow-dbg-in-tests = true

disallowed-methods = [
{ path = "str::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrExtension::to_ascii_lowercase_cow` instead." },
{ path = "std::ffi::OsStr::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrExtension::to_ascii_lowercase_cow` instead." }
{ path = "str::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrOnlyExtension::to_ascii_lowercase_cow` instead." },
{ path = "std::ffi::OsStr::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrLikeExtension::to_ascii_lowercase_cow` instead." },
{ path = "str::to_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrOnlyExtension::to_lowercase_cow` instead." },
]
1 change: 1 addition & 0 deletions crates/biome_css_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ biome_deserialize = { workspace = true }
biome_deserialize_macros = { workspace = true }
biome_diagnostics = { workspace = true }
biome_rowan = { workspace = true }
biome_string_case = { workspace = true }
biome_suppression = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use biome_css_syntax::{
CssGenericComponentValueList, CssGenericProperty, CssSyntaxKind,
};
use biome_rowan::{AstNode, SyntaxNodeCast, TextRange};
use biome_string_case::StrOnlyExtension;

use crate::utils::{
find_font_family, is_css_variable, is_font_family_keyword, is_system_family_name_keyword,
Expand Down Expand Up @@ -76,7 +77,8 @@ impl Rule for UseGenericFontNames {

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let property_name = node.name().ok()?.text().to_lowercase();
let property_name = node.name().ok()?.text();
let property_name = property_name.to_lowercase_cow();

// Ignore `@font-face`. See more detail: https://drafts.csswg.org/css-fonts/#font-face-rule
if is_in_font_face_at_rule(node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use biome_console::markup;
use biome_css_syntax::{CssFunction, CssParameter};
use biome_rowan::AstNode;
use biome_rowan::AstSeparatedList;
use biome_string_case::StrOnlyExtension;
use regex::Regex;
use std::sync::LazyLock;

Expand Down Expand Up @@ -82,7 +83,7 @@ impl Rule for NoInvalidDirectionInLinearGradient {
"-o-linear-gradient",
"-ms-linear-gradient",
];
if !linear_gradient_property.contains(&node_name.to_lowercase().as_str()) {
if !linear_gradient_property.contains(&node_name.to_lowercase_cow().as_ref()) {
return None;
}
let css_parameter = node.items();
Expand All @@ -101,10 +102,11 @@ impl Rule for NoInvalidDirectionInLinearGradient {
}
}
let direction_property = ["top", "left", "bottom", "right"];
if !direction_property
.iter()
.any(|&keyword| first_css_parameter_text.to_lowercase().contains(keyword))
{
if !direction_property.iter().any(|&keyword| {
first_css_parameter_text
.to_lowercase_cow()
.contains(keyword)
}) {
return None;
}
let has_prefix = vendor_prefixed(&node_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use biome_analyze::{
use biome_console::markup;
use biome_css_syntax::CssGenericProperty;
use biome_rowan::{AstNode, TextRange};
use biome_string_case::StrOnlyExtension;

use crate::utils::{is_known_properties, vendor_prefixed};

Expand Down Expand Up @@ -72,13 +73,14 @@ impl Rule for NoUnknownProperty {

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let property_name = node.name().ok()?.text().to_lowercase();
if !property_name.starts_with("--")
let property_name = node.name().ok()?.text();
let property_name_lower = property_name.to_lowercase_cow();
if !property_name_lower.starts_with("--")
// Ignore `composes` property.
// See https://github.com/css-modules/css-modules/blob/master/docs/composition.md for more details.
&& property_name != "composes"
&& !is_known_properties(&property_name)
&& !vendor_prefixed(&property_name)
&& property_name_lower != "composes"
&& !is_known_properties(&property_name_lower)
&& !vendor_prefixed(&property_name_lower)
{
return Some(node.name().ok()?.range());
}
Expand Down
27 changes: 14 additions & 13 deletions crates/biome_css_analyze/src/lint/correctness/no_unknown_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use biome_css_syntax::{
AnyCssDimension, CssFunction, CssGenericProperty, CssQueryFeaturePlain, CssSyntaxKind,
};
use biome_rowan::{SyntaxNodeCast, TextRange};
use biome_string_case::StrOnlyExtension;

const RESOLUTION_MEDIA_FEATURE_NAMES: [&str; 3] =
["resolution", "min-resolution", "max-resolution"];
Expand Down Expand Up @@ -103,47 +104,47 @@ impl Rule for NoUnknownUnit {
for ancestor in dimension.unit_token().ok()?.ancestors() {
match ancestor.kind() {
CssSyntaxKind::CSS_FUNCTION => {
let function_name = ancestor
let function_name_token = ancestor
.cast::<CssFunction>()?
.name()
.ok()?
.value_token()
.ok()?
.text_trimmed()
.to_lowercase();
.ok()?;
let function_name =
function_name_token.text_trimmed().to_lowercase_cow();

if function_name.ends_with("image-set") {
allow_x = true;
break;
}
}
CssSyntaxKind::CSS_GENERIC_PROPERTY => {
let property_name = ancestor
let property_name_token = ancestor
.cast::<CssGenericProperty>()?
.name()
.ok()?
.as_css_identifier()?
.value_token()
.ok()?
.text_trimmed()
.to_lowercase();
.ok()?;
let property_name =
property_name_token.text_trimmed().to_lowercase_cow();

if property_name == "image-resolution" {
allow_x = true;
break;
}
}
CssSyntaxKind::CSS_QUERY_FEATURE_PLAIN => {
let feature_name = ancestor
let feature_name_token = ancestor
.cast::<CssQueryFeaturePlain>()?
.name()
.ok()?
.value_token()
.ok()?
.text_trimmed()
.to_lowercase();
.ok()?;
let feature_name =
feature_name_token.text_trimmed().to_lowercase_cow();

if RESOLUTION_MEDIA_FEATURE_NAMES.contains(&feature_name.as_str()) {
if RESOLUTION_MEDIA_FEATURE_NAMES.contains(&feature_name.as_ref()) {
allow_x = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use biome_css_syntax::{
CssPseudoClassFunctionValueList, CssPseudoClassIdentifier, CssPseudoElementSelector,
};
use biome_rowan::{declare_node_union, AstNode, TextRange};
use biome_string_case::StrOnlyExtension;

declare_lint_rule! {
/// Disallow unknown pseudo-class selectors.
Expand Down Expand Up @@ -168,17 +169,18 @@ impl Rule for NoUnknownPseudoClass {
}
};

let lower_name = name.to_lowercase();
let lower_name = name.to_lowercase_cow();
let lower_name = lower_name.as_ref();

let is_valid_class = match pseudo_type {
PseudoClassType::PagePseudoClass => is_page_pseudo_class(&lower_name),
PseudoClassType::PagePseudoClass => is_page_pseudo_class(lower_name),
PseudoClassType::WebkitScrollbarPseudoClass => {
WEBKIT_SCROLLBAR_PSEUDO_CLASSES.contains(&lower_name.as_str())
WEBKIT_SCROLLBAR_PSEUDO_CLASSES.contains(&lower_name)
}
PseudoClassType::Other => {
is_custom_selector(&lower_name)
|| vendor_prefixed(&lower_name)
|| is_known_pseudo_class(&lower_name)
is_custom_selector(lower_name)
|| vendor_prefixed(lower_name)
|| is_known_pseudo_class(lower_name)
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use biome_analyze::{
use biome_console::markup;
use biome_css_syntax::{AnyCssPseudoElement, CssPseudoElementSelector};
use biome_rowan::AstNode;
use biome_string_case::StrOnlyExtension;

use crate::utils::{is_pseudo_elements, vender_prefix};

Expand Down Expand Up @@ -79,7 +80,7 @@ impl Rule for NoUnknownPseudoElement {
};

if !vender_prefix(pseudo_element_name.as_str()).is_empty()
|| is_pseudo_elements(pseudo_element_name.to_lowercase().as_str())
|| is_pseudo_elements(pseudo_element_name.to_lowercase_cow().as_ref())
{
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use biome_analyze::{
use biome_console::markup;
use biome_css_syntax::{AnyCssAtRule, AnyCssRule, CssImportAtRule, CssRuleList};
use biome_rowan::AstNode;
use biome_string_case::StrOnlyExtension;

declare_lint_rule! {
/// Disallow duplicate `@import` rules.
Expand Down Expand Up @@ -71,7 +72,7 @@ impl Rule for NoDuplicateAtImportRules {
.url()
.ok()?
.text()
.to_lowercase()
.to_lowercase_cow()
.replace("url(", "")
.replace(')', "")
.replace('"', "'");
Expand All @@ -85,7 +86,9 @@ impl Rule for NoDuplicateAtImportRules {
for media in import_rule.media() {
match media {
Ok(media) => {
if !media_query_set.insert(media.text().to_lowercase()) {
if !media_query_set
.insert(media.text().to_lowercase_cow().into())
{
return Some(import_rule);
}
}
Expand All @@ -97,7 +100,7 @@ impl Rule for NoDuplicateAtImportRules {
for media in import_rule.media() {
match media {
Ok(media) => {
media_set.insert(media.text().to_lowercase());
media_set.insert(media.text().to_lowercase_cow().into());
}
_ => return None,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use biome_analyze::{
use biome_console::markup;
use biome_css_syntax::{AnyCssGenericComponentValue, AnyCssValue, CssGenericProperty};
use biome_rowan::{AstNode, TextRange};
use biome_string_case::StrOnlyExtension;

use crate::utils::{find_font_family, is_font_family_keyword};

Expand Down Expand Up @@ -64,7 +65,8 @@ impl Rule for NoDuplicateFontNames {

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let property_name = node.name().ok()?.text().to_lowercase();
let property_name = node.name().ok()?.text();
let property_name = property_name.to_lowercase_cow();

let is_font_family = property_name == "font-family";
let is_font = property_name == "font";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use biome_analyze::{
use biome_console::markup;
use biome_css_syntax::{AnyCssKeyframesItem, AnyCssKeyframesSelector, CssKeyframesBlock};
use biome_rowan::AstNode;
use biome_string_case::StrOnlyExtension;

declare_lint_rule! {
/// Disallow duplicate selectors within keyframe blocks.
Expand Down Expand Up @@ -53,12 +54,14 @@ impl Rule for NoDuplicateSelectorsKeyframeBlock {

fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let mut selector_list = HashSet::new();
let mut selector_list: HashSet<String> = HashSet::new();
for keyframe_item in node.items() {
match keyframe_item {
AnyCssKeyframesItem::CssKeyframesItem(item) => {
let keyframe_selector = item.selectors().into_iter().next()?.ok()?;
if !selector_list.insert(keyframe_selector.text().to_lowercase()) {
if !selector_list
.insert(keyframe_selector.text().to_lowercase_cow().to_string())
{
return Some(keyframe_selector);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl Visitor for NoDeclarationBlockShorthandPropertyOverridesVisitor {
.and_then(|property_node| property_node.name().ok())
{
let prop = prop_node.text();
#[allow(clippy::disallowed_methods)]
let prop_lowercase = prop.to_lowercase();

let prop_prefix = vender_prefix(&prop_lowercase);
Expand Down
12 changes: 7 additions & 5 deletions crates/biome_css_analyze/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::keywords::{
};
use biome_css_syntax::{AnyCssGenericComponentValue, AnyCssValue, CssGenericComponentValueList};
use biome_rowan::{AstNode, SyntaxNodeCast};
use biome_string_case::StrOnlyExtension;

pub fn is_font_family_keyword(value: &str) -> bool {
BASIC_KEYWORDS.contains(&value) || FONT_FAMILY_KEYWORDS.contains(&value)
Expand All @@ -38,14 +39,15 @@ pub fn is_font_shorthand_keyword(value: &str) -> bool {
}

pub fn is_css_variable(value: &str) -> bool {
value.to_lowercase().starts_with("var(")
value.to_lowercase_cow().starts_with("var(")
}

/// Get the font-families within a `font` shorthand property value.
pub fn find_font_family(value: CssGenericComponentValueList) -> Vec<AnyCssValue> {
let mut font_families: Vec<AnyCssValue> = Vec::new();
for v in value {
let lower_case_value = v.text().to_lowercase();
let value = v.text();
let lower_case_value = value.to_lowercase_cow();

// Ignore CSS variables
if is_css_variable(&lower_case_value) {
Expand Down Expand Up @@ -110,7 +112,7 @@ pub fn find_font_family(value: CssGenericComponentValueList) -> Vec<AnyCssValue>
/// Check if the value is a known CSS value function.
pub fn is_function_keyword(value: &str) -> bool {
FUNCTION_KEYWORDS
.binary_search(&value.to_lowercase().as_str())
.binary_search(&value.to_lowercase_cow().as_ref())
.is_ok()
}

Expand Down Expand Up @@ -178,8 +180,8 @@ pub fn vendor_prefixed(props: &str) -> bool {

/// Check if the input string is a media feature name.
pub fn is_media_feature_name(prop: &str) -> bool {
let input = prop.to_lowercase();
let count = MEDIA_FEATURE_NAMES.binary_search(&input.as_str());
let input = prop.to_lowercase_cow();
let count = MEDIA_FEATURE_NAMES.binary_search(&input.as_ref());
if count.is_ok() {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use biome_formatter::{
};
use biome_formatter::{Formatted, Printed};
use biome_rowan::{AstNode, SyntaxNode, TextRange};
use biome_string_case::StrExtension;
use biome_string_case::StrOnlyExtension;

/// Used to get an object that knows how to format this object.
pub(crate) trait AsFormat<Context> {
Expand Down
Loading

0 comments on commit 75b4387

Please sign in to comment.