Skip to content

Commit 75b4387

Browse files
authored
chore: to_lowercase -> to_lowercase_cow (biomejs#4030)
1 parent d20ad1e commit 75b4387

File tree

42 files changed

+182
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+182
-88
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clippy.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
allow-dbg-in-tests = true
22

33
disallowed-methods = [
4-
{ path = "str::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrExtension::to_ascii_lowercase_cow` instead." },
5-
{ path = "std::ffi::OsStr::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrExtension::to_ascii_lowercase_cow` instead." }
4+
{ path = "str::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrOnlyExtension::to_ascii_lowercase_cow` instead." },
5+
{ path = "std::ffi::OsStr::to_ascii_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrLikeExtension::to_ascii_lowercase_cow` instead." },
6+
{ path = "str::to_lowercase", reason = "Avoid memory allocation. Use `biome_string_case::StrOnlyExtension::to_lowercase_cow` instead." },
67
]

crates/biome_css_analyze/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ biome_deserialize = { workspace = true }
2121
biome_deserialize_macros = { workspace = true }
2222
biome_diagnostics = { workspace = true }
2323
biome_rowan = { workspace = true }
24+
biome_string_case = { workspace = true }
2425
biome_suppression = { workspace = true }
2526
regex = { workspace = true }
2627
rustc-hash = { workspace = true }

crates/biome_css_analyze/src/lint/a11y/use_generic_font_names.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use biome_css_syntax::{
77
CssGenericComponentValueList, CssGenericProperty, CssSyntaxKind,
88
};
99
use biome_rowan::{AstNode, SyntaxNodeCast, TextRange};
10+
use biome_string_case::StrOnlyExtension;
1011

1112
use crate::utils::{
1213
find_font_family, is_css_variable, is_font_family_keyword, is_system_family_name_keyword,
@@ -76,7 +77,8 @@ impl Rule for UseGenericFontNames {
7677

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

8183
// Ignore `@font-face`. See more detail: https://drafts.csswg.org/css-fonts/#font-face-rule
8284
if is_in_font_face_at_rule(node) {

crates/biome_css_analyze/src/lint/correctness/no_invalid_direction_in_linear_gradient.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use biome_console::markup;
55
use biome_css_syntax::{CssFunction, CssParameter};
66
use biome_rowan::AstNode;
77
use biome_rowan::AstSeparatedList;
8+
use biome_string_case::StrOnlyExtension;
89
use regex::Regex;
910
use std::sync::LazyLock;
1011

@@ -82,7 +83,7 @@ impl Rule for NoInvalidDirectionInLinearGradient {
8283
"-o-linear-gradient",
8384
"-ms-linear-gradient",
8485
];
85-
if !linear_gradient_property.contains(&node_name.to_lowercase().as_str()) {
86+
if !linear_gradient_property.contains(&node_name.to_lowercase_cow().as_ref()) {
8687
return None;
8788
}
8889
let css_parameter = node.items();
@@ -101,10 +102,11 @@ impl Rule for NoInvalidDirectionInLinearGradient {
101102
}
102103
}
103104
let direction_property = ["top", "left", "bottom", "right"];
104-
if !direction_property
105-
.iter()
106-
.any(|&keyword| first_css_parameter_text.to_lowercase().contains(keyword))
107-
{
105+
if !direction_property.iter().any(|&keyword| {
106+
first_css_parameter_text
107+
.to_lowercase_cow()
108+
.contains(keyword)
109+
}) {
108110
return None;
109111
}
110112
let has_prefix = vendor_prefixed(&node_name);

crates/biome_css_analyze/src/lint/correctness/no_unknown_property.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use biome_analyze::{
44
use biome_console::markup;
55
use biome_css_syntax::CssGenericProperty;
66
use biome_rowan::{AstNode, TextRange};
7+
use biome_string_case::StrOnlyExtension;
78

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

@@ -72,13 +73,14 @@ impl Rule for NoUnknownProperty {
7273

7374
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
7475
let node = ctx.query();
75-
let property_name = node.name().ok()?.text().to_lowercase();
76-
if !property_name.starts_with("--")
76+
let property_name = node.name().ok()?.text();
77+
let property_name_lower = property_name.to_lowercase_cow();
78+
if !property_name_lower.starts_with("--")
7779
// Ignore `composes` property.
7880
// See https://github.com/css-modules/css-modules/blob/master/docs/composition.md for more details.
79-
&& property_name != "composes"
80-
&& !is_known_properties(&property_name)
81-
&& !vendor_prefixed(&property_name)
81+
&& property_name_lower != "composes"
82+
&& !is_known_properties(&property_name_lower)
83+
&& !vendor_prefixed(&property_name_lower)
8284
{
8385
return Some(node.name().ok()?.range());
8486
}

crates/biome_css_analyze/src/lint/correctness/no_unknown_unit.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use biome_css_syntax::{
66
AnyCssDimension, CssFunction, CssGenericProperty, CssQueryFeaturePlain, CssSyntaxKind,
77
};
88
use biome_rowan::{SyntaxNodeCast, TextRange};
9+
use biome_string_case::StrOnlyExtension;
910

1011
const RESOLUTION_MEDIA_FEATURE_NAMES: [&str; 3] =
1112
["resolution", "min-resolution", "max-resolution"];
@@ -103,47 +104,47 @@ impl Rule for NoUnknownUnit {
103104
for ancestor in dimension.unit_token().ok()?.ancestors() {
104105
match ancestor.kind() {
105106
CssSyntaxKind::CSS_FUNCTION => {
106-
let function_name = ancestor
107+
let function_name_token = ancestor
107108
.cast::<CssFunction>()?
108109
.name()
109110
.ok()?
110111
.value_token()
111-
.ok()?
112-
.text_trimmed()
113-
.to_lowercase();
112+
.ok()?;
113+
let function_name =
114+
function_name_token.text_trimmed().to_lowercase_cow();
114115

115116
if function_name.ends_with("image-set") {
116117
allow_x = true;
117118
break;
118119
}
119120
}
120121
CssSyntaxKind::CSS_GENERIC_PROPERTY => {
121-
let property_name = ancestor
122+
let property_name_token = ancestor
122123
.cast::<CssGenericProperty>()?
123124
.name()
124125
.ok()?
125126
.as_css_identifier()?
126127
.value_token()
127-
.ok()?
128-
.text_trimmed()
129-
.to_lowercase();
128+
.ok()?;
129+
let property_name =
130+
property_name_token.text_trimmed().to_lowercase_cow();
130131

131132
if property_name == "image-resolution" {
132133
allow_x = true;
133134
break;
134135
}
135136
}
136137
CssSyntaxKind::CSS_QUERY_FEATURE_PLAIN => {
137-
let feature_name = ancestor
138+
let feature_name_token = ancestor
138139
.cast::<CssQueryFeaturePlain>()?
139140
.name()
140141
.ok()?
141142
.value_token()
142-
.ok()?
143-
.text_trimmed()
144-
.to_lowercase();
143+
.ok()?;
144+
let feature_name =
145+
feature_name_token.text_trimmed().to_lowercase_cow();
145146

146-
if RESOLUTION_MEDIA_FEATURE_NAMES.contains(&feature_name.as_str()) {
147+
if RESOLUTION_MEDIA_FEATURE_NAMES.contains(&feature_name.as_ref()) {
147148
allow_x = true;
148149
break;
149150
}

crates/biome_css_analyze/src/lint/nursery/no_unknown_pseudo_class.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use biome_css_syntax::{
1414
CssPseudoClassFunctionValueList, CssPseudoClassIdentifier, CssPseudoElementSelector,
1515
};
1616
use biome_rowan::{declare_node_union, AstNode, TextRange};
17+
use biome_string_case::StrOnlyExtension;
1718

1819
declare_lint_rule! {
1920
/// Disallow unknown pseudo-class selectors.
@@ -168,17 +169,18 @@ impl Rule for NoUnknownPseudoClass {
168169
}
169170
};
170171

171-
let lower_name = name.to_lowercase();
172+
let lower_name = name.to_lowercase_cow();
173+
let lower_name = lower_name.as_ref();
172174

173175
let is_valid_class = match pseudo_type {
174-
PseudoClassType::PagePseudoClass => is_page_pseudo_class(&lower_name),
176+
PseudoClassType::PagePseudoClass => is_page_pseudo_class(lower_name),
175177
PseudoClassType::WebkitScrollbarPseudoClass => {
176-
WEBKIT_SCROLLBAR_PSEUDO_CLASSES.contains(&lower_name.as_str())
178+
WEBKIT_SCROLLBAR_PSEUDO_CLASSES.contains(&lower_name)
177179
}
178180
PseudoClassType::Other => {
179-
is_custom_selector(&lower_name)
180-
|| vendor_prefixed(&lower_name)
181-
|| is_known_pseudo_class(&lower_name)
181+
is_custom_selector(lower_name)
182+
|| vendor_prefixed(lower_name)
183+
|| is_known_pseudo_class(lower_name)
182184
}
183185
};
184186

crates/biome_css_analyze/src/lint/nursery/no_unknown_pseudo_element.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use biome_analyze::{
44
use biome_console::markup;
55
use biome_css_syntax::{AnyCssPseudoElement, CssPseudoElementSelector};
66
use biome_rowan::AstNode;
7+
use biome_string_case::StrOnlyExtension;
78

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

@@ -79,7 +80,7 @@ impl Rule for NoUnknownPseudoElement {
7980
};
8081

8182
if !vender_prefix(pseudo_element_name.as_str()).is_empty()
82-
|| is_pseudo_elements(pseudo_element_name.to_lowercase().as_str())
83+
|| is_pseudo_elements(pseudo_element_name.to_lowercase_cow().as_ref())
8384
{
8485
return None;
8586
}

crates/biome_css_analyze/src/lint/suspicious/no_duplicate_at_import_rules.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use biome_analyze::{
66
use biome_console::markup;
77
use biome_css_syntax::{AnyCssAtRule, AnyCssRule, CssImportAtRule, CssRuleList};
88
use biome_rowan::AstNode;
9+
use biome_string_case::StrOnlyExtension;
910

1011
declare_lint_rule! {
1112
/// Disallow duplicate `@import` rules.
@@ -71,7 +72,7 @@ impl Rule for NoDuplicateAtImportRules {
7172
.url()
7273
.ok()?
7374
.text()
74-
.to_lowercase()
75+
.to_lowercase_cow()
7576
.replace("url(", "")
7677
.replace(')', "")
7778
.replace('"', "'");
@@ -85,7 +86,9 @@ impl Rule for NoDuplicateAtImportRules {
8586
for media in import_rule.media() {
8687
match media {
8788
Ok(media) => {
88-
if !media_query_set.insert(media.text().to_lowercase()) {
89+
if !media_query_set
90+
.insert(media.text().to_lowercase_cow().into())
91+
{
8992
return Some(import_rule);
9093
}
9194
}
@@ -97,7 +100,7 @@ impl Rule for NoDuplicateAtImportRules {
97100
for media in import_rule.media() {
98101
match media {
99102
Ok(media) => {
100-
media_set.insert(media.text().to_lowercase());
103+
media_set.insert(media.text().to_lowercase_cow().into());
101104
}
102105
_ => return None,
103106
}

crates/biome_css_analyze/src/lint/suspicious/no_duplicate_font_names.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use biome_analyze::{
66
use biome_console::markup;
77
use biome_css_syntax::{AnyCssGenericComponentValue, AnyCssValue, CssGenericProperty};
88
use biome_rowan::{AstNode, TextRange};
9+
use biome_string_case::StrOnlyExtension;
910

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

@@ -64,7 +65,8 @@ impl Rule for NoDuplicateFontNames {
6465

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

6971
let is_font_family = property_name == "font-family";
7072
let is_font = property_name == "font";

crates/biome_css_analyze/src/lint/suspicious/no_duplicate_selectors_keyframe_block.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use biome_analyze::{
66
use biome_console::markup;
77
use biome_css_syntax::{AnyCssKeyframesItem, AnyCssKeyframesSelector, CssKeyframesBlock};
88
use biome_rowan::AstNode;
9+
use biome_string_case::StrOnlyExtension;
910

1011
declare_lint_rule! {
1112
/// Disallow duplicate selectors within keyframe blocks.
@@ -53,12 +54,14 @@ impl Rule for NoDuplicateSelectorsKeyframeBlock {
5354

5455
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
5556
let node = ctx.query();
56-
let mut selector_list = HashSet::new();
57+
let mut selector_list: HashSet<String> = HashSet::new();
5758
for keyframe_item in node.items() {
5859
match keyframe_item {
5960
AnyCssKeyframesItem::CssKeyframesItem(item) => {
6061
let keyframe_selector = item.selectors().into_iter().next()?.ok()?;
61-
if !selector_list.insert(keyframe_selector.text().to_lowercase()) {
62+
if !selector_list
63+
.insert(keyframe_selector.text().to_lowercase_cow().to_string())
64+
{
6265
return Some(keyframe_selector);
6366
}
6467
}

crates/biome_css_analyze/src/lint/suspicious/no_shorthand_property_overrides.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl Visitor for NoDeclarationBlockShorthandPropertyOverridesVisitor {
105105
.and_then(|property_node| property_node.name().ok())
106106
{
107107
let prop = prop_node.text();
108+
#[allow(clippy::disallowed_methods)]
108109
let prop_lowercase = prop.to_lowercase();
109110

110111
let prop_prefix = vender_prefix(&prop_lowercase);

crates/biome_css_analyze/src/utils.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::keywords::{
1515
};
1616
use biome_css_syntax::{AnyCssGenericComponentValue, AnyCssValue, CssGenericComponentValueList};
1717
use biome_rowan::{AstNode, SyntaxNodeCast};
18+
use biome_string_case::StrOnlyExtension;
1819

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

4041
pub fn is_css_variable(value: &str) -> bool {
41-
value.to_lowercase().starts_with("var(")
42+
value.to_lowercase_cow().starts_with("var(")
4243
}
4344

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

5052
// Ignore CSS variables
5153
if is_css_variable(&lower_case_value) {
@@ -110,7 +112,7 @@ pub fn find_font_family(value: CssGenericComponentValueList) -> Vec<AnyCssValue>
110112
/// Check if the value is a known CSS value function.
111113
pub fn is_function_keyword(value: &str) -> bool {
112114
FUNCTION_KEYWORDS
113-
.binary_search(&value.to_lowercase().as_str())
115+
.binary_search(&value.to_lowercase_cow().as_ref())
114116
.is_ok()
115117
}
116118

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

179181
/// Check if the input string is a media feature name.
180182
pub fn is_media_feature_name(prop: &str) -> bool {
181-
let input = prop.to_lowercase();
182-
let count = MEDIA_FEATURE_NAMES.binary_search(&input.as_str());
183+
let input = prop.to_lowercase_cow();
184+
let count = MEDIA_FEATURE_NAMES.binary_search(&input.as_ref());
183185
if count.is_ok() {
184186
return true;
185187
}

crates/biome_css_formatter/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use biome_formatter::{
2626
};
2727
use biome_formatter::{Formatted, Printed};
2828
use biome_rowan::{AstNode, SyntaxNode, TextRange};
29-
use biome_string_case::StrExtension;
29+
use biome_string_case::StrOnlyExtension;
3030

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

0 commit comments

Comments
 (0)