Skip to content

Commit

Permalink
feat(organize_import): utilities for ordering import sources (#4313)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Oct 21, 2024
1 parent 43e759d commit 5579f9f
Show file tree
Hide file tree
Showing 26 changed files with 693 additions and 68 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use biome_css_syntax::{
CssGenericComponentValueList, CssGenericProperty, CssSyntaxKind,
};
use biome_rowan::{AstNode, SyntaxNodeCast, TextRange};
use biome_string_case::StrOnlyExtension;
use biome_string_case::StrLikeExtension;

use crate::utils::{
find_font_family, is_css_variable, is_font_family_keyword, is_system_family_name_keyword,
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Rule for UseGenericFontNames {
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let property_name = node.name().ok()?.text();
let property_name = property_name.to_lowercase_cow();
let property_name = property_name.to_ascii_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,7 +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 biome_string_case::StrLikeExtension;
use regex::Regex;
use std::sync::LazyLock;

Expand Down Expand Up @@ -83,7 +83,7 @@ impl Rule for NoInvalidDirectionInLinearGradient {
"-o-linear-gradient",
"-ms-linear-gradient",
];
if !linear_gradient_property.contains(&node_name.to_lowercase_cow().as_ref()) {
if !linear_gradient_property.contains(&node_name.to_ascii_lowercase_cow().as_ref()) {
return None;
}
let css_parameter = node.items();
Expand All @@ -104,7 +104,7 @@ impl Rule for NoInvalidDirectionInLinearGradient {
let direction_property = ["top", "left", "bottom", "right"];
if !direction_property.iter().any(|&keyword| {
first_css_parameter_text
.to_lowercase_cow()
.to_ascii_lowercase_cow()
.contains(keyword)
}) {
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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 biome_string_case::StrLikeExtension;

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

Expand Down Expand Up @@ -74,7 +74,7 @@ impl Rule for NoUnknownProperty {
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let property_name = node.name().ok()?.text();
let property_name_lower = property_name.to_lowercase_cow();
let property_name_lower = property_name.to_ascii_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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use biome_css_syntax::{
AnyCssDimension, CssFunction, CssGenericProperty, CssQueryFeaturePlain, CssSyntaxKind,
};
use biome_rowan::{SyntaxNodeCast, TextRange};
use biome_string_case::StrOnlyExtension;
use biome_string_case::StrLikeExtension;

const RESOLUTION_MEDIA_FEATURE_NAMES: [&str; 3] =
["resolution", "min-resolution", "max-resolution"];
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Rule for NoUnknownUnit {
.value_token()
.ok()?;
let function_name =
function_name_token.text_trimmed().to_lowercase_cow();
function_name_token.text_trimmed().to_ascii_lowercase_cow();

if function_name.ends_with("image-set") {
allow_x = true;
Expand All @@ -127,7 +127,7 @@ impl Rule for NoUnknownUnit {
.value_token()
.ok()?;
let property_name =
property_name_token.text_trimmed().to_lowercase_cow();
property_name_token.text_trimmed().to_ascii_lowercase_cow();

if property_name == "image-resolution" {
allow_x = true;
Expand All @@ -142,7 +142,7 @@ impl Rule for NoUnknownUnit {
.value_token()
.ok()?;
let feature_name =
feature_name_token.text_trimmed().to_lowercase_cow();
feature_name_token.text_trimmed().to_ascii_lowercase_cow();

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

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

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

let is_valid_class = match pseudo_type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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 biome_string_case::StrLikeExtension;

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

Expand Down Expand Up @@ -80,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_cow().as_ref())
|| is_pseudo_elements(pseudo_element_name.to_ascii_lowercase_cow().as_ref())
{
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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 biome_string_case::StrLikeExtension;

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

Expand Down Expand Up @@ -66,7 +66,7 @@ impl Rule for NoDuplicateFontNames {
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let property_name = node.name().ok()?.text();
let property_name = property_name.to_lowercase_cow();
let property_name = property_name.to_ascii_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,7 +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;
use biome_string_case::StrLikeExtension;

declare_lint_rule! {
/// Disallow duplicate selectors within keyframe blocks.
Expand Down Expand Up @@ -59,9 +59,12 @@ impl Rule for NoDuplicateSelectorsKeyframeBlock {
match keyframe_item {
AnyCssKeyframesItem::CssKeyframesItem(item) => {
let keyframe_selector = item.selectors().into_iter().next()?.ok()?;
if !selector_list
.insert(keyframe_selector.text().to_lowercase_cow().to_string())
{
if !selector_list.insert(
keyframe_selector
.text()
.to_ascii_lowercase_cow()
.to_string(),
) {
return Some(keyframe_selector);
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/biome_css_analyze/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::keywords::{
};
use biome_css_syntax::{AnyCssGenericComponentValue, AnyCssValue, CssGenericComponentValueList};
use biome_rowan::{AstNode, SyntaxNodeCast};
use biome_string_case::StrOnlyExtension;
use biome_string_case::{StrLikeExtension, StrOnlyExtension};

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

pub fn is_css_variable(value: &str) -> bool {
value.to_lowercase_cow().starts_with("var(")
value.to_ascii_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 value = v.text();
let lower_case_value = value.to_lowercase_cow();
let lower_case_value = value.to_ascii_lowercase_cow();

// Ignore CSS variables
if is_css_variable(&lower_case_value) {
Expand Down Expand Up @@ -112,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_cow().as_ref())
.binary_search(&value.to_ascii_lowercase_cow().as_ref())
.is_ok()
}

Expand Down Expand Up @@ -180,7 +180,7 @@ 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_cow();
let input = prop.to_ascii_lowercase_cow();
let count = MEDIA_FEATURE_NAMES.binary_search(&input.as_ref());
if count.is_ok() {
return true;
Expand Down Expand Up @@ -224,7 +224,7 @@ fn is_custom_element(prop: &str) -> bool {

/// Check if the input string is a known type selector.
pub fn is_known_type_selector(prop: &str) -> bool {
let input = prop.to_lowercase_cow();
let input = prop.to_ascii_lowercase_cow();
HTML_TAGS.binary_search(&input.as_ref()).is_ok()
|| SVG_TAGS.binary_search(&prop).is_ok()
|| MATH_ML_TAGS.binary_search(&input.as_ref()).is_ok()
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::StrOnlyExtension;
use biome_string_case::StrLikeExtension;

/// Used to get an object that knows how to format this object.
pub(crate) trait AsFormat<Context> {
Expand Down
4 changes: 2 additions & 2 deletions crates/biome_css_formatter/src/utils/component_value_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::comments::CssComments;
use biome_css_syntax::{CssGenericDelimiter, CssGenericProperty, CssLanguage, CssSyntaxKind};
use biome_formatter::{write, CstFormatContext};
use biome_formatter::{FormatOptions, FormatResult};
use biome_string_case::StrOnlyExtension;
use biome_string_case::StrLikeExtension;

use crate::prelude::*;
use crate::CssFormatter;
Expand Down Expand Up @@ -180,7 +180,7 @@ where
.and_then(|parent| parent.name().ok())
.and_then(|name| name.as_css_identifier().map(|name| name.text()))
.map_or(false, |name| {
let name = name.to_lowercase_cow();
let name = name.to_ascii_lowercase_cow();

name.starts_with("grid-template") || name == "grid"
});
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/utils/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use biome_formatter::{
Format, FormatResult,
};
use biome_rowan::SyntaxToken;
use biome_string_case::StrOnlyExtension;
use biome_string_case::StrLikeExtension;

use crate::{prelude::CssFormatContext, AsFormat, CssFormatter};

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_formatter/src/token/number.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_rowan::{Language, SyntaxToken};
use biome_string_case::StrOnlyExtension;
use biome_string_case::StrLikeExtension;
use std::borrow::Cow;
use std::num::NonZeroUsize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use biome_rowan::{

use crate::JsRuleAction;

pub mod util;

declare_source_rule! {
/// Provides a whole-source code action to sort the imports in the file
/// using import groups and natural ordering.
Expand Down
Loading

0 comments on commit 5579f9f

Please sign in to comment.