Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(organize_import): utilities for ordering import sources #4313

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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,
@@ -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) {
Original file line number Diff line number Diff line change
@@ -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;

@@ -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();
@@ -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;
Original file line number Diff line number Diff line change
@@ -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};

@@ -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.
Original file line number Diff line number Diff line change
@@ -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"];
@@ -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;
@@ -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;
@@ -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;
Original file line number Diff line number Diff line change
@@ -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.
@@ -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 {
Original file line number Diff line number Diff line change
@@ -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};

@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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};

@@ -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";
Original file line number Diff line number Diff line change
@@ -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.
@@ -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);
}
}
12 changes: 6 additions & 6 deletions crates/biome_css_analyze/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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)
@@ -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) {
@@ -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()
}

@@ -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;
@@ -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()
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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> {
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
@@ -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;
@@ -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"
});
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
@@ -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};

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;

Original file line number Diff line number Diff line change
@@ -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.
Loading