Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniGuardiola committed Jan 12, 2024
1 parent cff3479 commit 508a93f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,20 @@ impl Rule for UseSortedClasses {
);

let value = ctx.query().value()?;
let sorted_value = sort_class_name(value.as_str(), &sort_config);
if value != sorted_value {
let sorted_value = sort_class_name(&value, &sort_config);
if value.text() != sorted_value {
Some(sorted_value)
} else {
None
}
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
Some(
RuleDiagnostic::new(
rule_category!(),
ctx.query().range(),
"These classes should be sorted.",
)
.note(markup! {
"The safe fix will automatically sort them."
}),
)
Some(RuleDiagnostic::new(
rule_category!(),
ctx.query().range(),
"These CSS classes should be sorted.",
))
}

fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<JsRuleAction> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ use super::UseSortedClassesOptions;
// -----

fn get_options_from_analyzer(analyzer_options: &AnalyzerOptions) -> UseSortedClassesOptions {
match analyzer_options
analyzer_options
.configuration
.rules
.get_rule_options::<UseSortedClassesOptions>(&RuleKey::new("nursery", "useSortedClasses"))
{
Some(options) => options.clone(),
None => UseSortedClassesOptions::default(),
}
.cloned()
.unwrap_or_default()
}

fn get_callee_name(call_expression: &JsCallExpression) -> Option<TokenText> {
Expand All @@ -41,14 +39,20 @@ fn is_call_expression_of_target_function(
call_expression: &JsCallExpression,
target_functions: &[String],
) -> bool {
match get_callee_name(call_expression) {
Some(name) => target_functions.contains(&name.to_string()),
None => false,
}
get_callee_name(call_expression)
.is_some_and(|name| target_functions.contains(&name.to_string()))
}

fn get_attribute_name(attribute: &JsxAttribute) -> Option<String> {
Some(attribute.name().ok()?.as_jsx_name()?.to_string())
fn get_attribute_name(attribute: &JsxAttribute) -> Option<TokenText> {
Some(
attribute
.name()
.ok()?
.as_jsx_name()?
.value_token()
.ok()?
.token_text_trimmed(),
)
}

fn is_target_attribute(attribute: &JsxAttribute, target_attributes: &[String]) -> bool {
Expand Down Expand Up @@ -180,16 +184,14 @@ declare_node_union! {

impl AnyClassStringLike {
/// Returns the value of the string literal, JSX string, or template chunk.
pub fn value(&self) -> Option<String> {
pub fn value(&self) -> Option<TokenText> {
match self {
AnyClassStringLike::JsStringLiteralExpression(string_literal) => {
Some(string_literal.inner_string_text().ok()?.to_string())
}
AnyClassStringLike::JsxString(jsx_string) => {
Some(jsx_string.inner_string_text().ok()?.to_string())
Self::JsStringLiteralExpression(string_literal) => {
Some(string_literal.inner_string_text().ok()?)
}
AnyClassStringLike::JsTemplateChunkElement(template_chunk) => {
Some(template_chunk.to_string())
Self::JsxString(jsx_string) => Some(jsx_string.inner_string_text().ok()?),
Self::JsTemplateChunkElement(template_chunk) => {
Some(template_chunk.template_chunk_token().ok()?.token_text())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ impl DeserializationVisitor for UseSortedClassesOptionsVisitor {
diagnostics: &mut Vec<DeserializationDiagnostic>,
) -> Option<Self::Output> {
let mut result = UseSortedClassesOptions::default();
result
.attributes
.extend(CLASS_ATTRIBUTES.iter().map(|&s| s.to_string()));

for (key, value) in members.flatten() {
let Some(key_text) = Text::deserialize(&key, "", diagnostics) else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::cmp::Ordering;

use biome_rowan::TokenText;

use super::{
class_info::{get_class_info, ClassInfo},
sort_config::SortConfig,
Expand Down Expand Up @@ -79,7 +81,7 @@ fn compare_classes(a: &ClassInfo, b: &ClassInfo) -> Ordering {
}

/// Sort the given class string according to the given sort config.
pub fn sort_class_name(class_name: &str, sort_config: &SortConfig) -> String {
pub fn sort_class_name(class_name: &TokenText, sort_config: &SortConfig) -> String {
// Obtain classes by splitting the class string by whitespace.
let classes = class_name.split_whitespace().collect::<Vec<&str>>();

Expand Down

0 comments on commit 508a93f

Please sign in to comment.