Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
chore: further code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Nov 28, 2022
1 parent 0972dec commit a672521
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 35 deletions.
2 changes: 1 addition & 1 deletion crates/rome_analyze/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<L: Language> RegistryVisitor<L> for MetadataRegistry {
/// after the "SemanticModel" is ready, which demands a whole transverse of the parsed tree.
pub struct RuleRegistry<L: Language> {
/// Holds a collection of rules for each phase.
phase_rules: [PhaseRules<L>; 3],
phase_rules: [PhaseRules<L>; 2],
}

impl<L: Language + Default> RuleRegistry<L> {
Expand Down
1 change: 0 additions & 1 deletion crates/rome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ pub struct SuggestionList {
pub(crate) list: Vec<MarkupBuf>,
}

// TODO: this code will be hit once https://github.com/rome/tools/issues/3829 is closed. Make sure it works as expected.
impl Advices for RuleAdvice {
fn record(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
for detail in &self.details {
Expand Down
7 changes: 4 additions & 3 deletions crates/rome_aria/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::constants::{ARIA_PROPERTIES, ARIA_PROPERTY_TYPE};
use std::str::FromStr;

#[rustfmt::skip]
mod generated;
Expand All @@ -7,6 +7,7 @@ pub mod constants;
pub mod properties;
pub mod roles;

use crate::generated::{AriaPropertiesEnum, AriaPropertyTypeEnum};
pub use properties::AriaProperties;
pub use roles::AriaRoles;

Expand All @@ -21,7 +22,7 @@ pub use roles::AriaRoles;
/// assert!(is_aria_property_valid("aria-checked"));
/// ```
pub fn is_aria_property_valid(property: &str) -> bool {
ARIA_PROPERTIES.binary_search(&property).is_ok()
AriaPropertiesEnum::from_str(property).is_ok()
}

/// It checks if an ARIA property type is valid
Expand All @@ -35,7 +36,7 @@ pub fn is_aria_property_valid(property: &str) -> bool {
/// assert!(!is_aria_property_type_valid("bogus"));
/// ```
pub fn is_aria_property_type_valid(property_type: &str) -> bool {
ARIA_PROPERTY_TYPE.binary_search(&property_type).is_ok()
AriaPropertyTypeEnum::from_str(property_type).is_ok()
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::aria_services::Aria;
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_syntax::jsx_ext::JsxAnyElement;
use rome_js_syntax::JsxAttribute;
use rome_js_syntax::{JsxOpeningElement, JsxSelfClosingElement};
use rome_rowan::{declare_node_union, AstNode};
use rome_rowan::AstNode;

declare_rule! {
/// Enforce that elements with ARIA roles must have all required attributes for that role
Expand All @@ -15,23 +15,6 @@ declare_rule! {
}
}

declare_node_union! {
pub(crate) JsxAnyElement = JsxSelfClosingElement| JsxOpeningElement
}

impl JsxAnyElement {
pub(crate) fn find_by_name(&self, attribute: &str) -> Option<JsxAttribute> {
match self {
JsxAnyElement::JsxSelfClosingElement(element) => {
element.attributes().find_by_name(attribute).ok()?
}
JsxAnyElement::JsxOpeningElement(element) => {
element.attributes().find_by_name(attribute).ok()?
}
}
}
}

#[derive(Default, Debug)]
pub(crate) struct UseAriaPropsForRoleState {
missing_aria_props: Vec<String>,
Expand Down Expand Up @@ -91,7 +74,7 @@ impl Rule for UseAriaPropsForRole {
let properties = role.properties();
for (property_name, required) in properties {
if *required {
let attribute = node.find_by_name(property_name);
let attribute = node.find_attribute_by_name(property_name);
if attribute.is_none() {
missing_aria_props.push(property_name.to_string());
}
Expand Down
21 changes: 12 additions & 9 deletions crates/rome_js_analyze/src/aria_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use rome_js_syntax::JsLanguage;
use rome_rowan::AstNode;
use std::sync::Arc;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct AriaServices {
roles: Arc<AriaRoles>,
properties: Arc<AriaProperties>,
pub(crate) roles: Arc<AriaRoles>,
pub(crate) properties: Arc<AriaProperties>,
}

impl AriaServices {
Expand All @@ -25,13 +25,16 @@ impl AriaServices {

impl FromServices for AriaServices {
fn from_services(
_rule_key: &RuleKey,
_services: &ServiceBag,
rule_key: &RuleKey,
services: &ServiceBag,
) -> Result<Self, MissingServicesDiagnostic> {
Ok(Self {
roles: Arc::new(AriaRoles::default()),
properties: Arc::new(AriaProperties::default()),
})
let roles = services
.get_service()
.ok_or_else(|| MissingServicesDiagnostic::new(rule_key.rule_name(), &["AriaRoles"]))?;
let properties = services.get_service().ok_or_else(|| {
MissingServicesDiagnostic::new(rule_key.rule_name(), &["AriaProperties"])
})?;
Ok(Self { roles, properties })
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/rome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use rome_analyze::{
InspectMatcher, LanguageRoot, MatchQueryParams, MetadataRegistry, Phases, RuleAction,
RuleRegistry, ServiceBag, SuppressionCommentEmitterPayload, SuppressionKind, SyntaxVisitor,
};
use rome_aria::{AriaProperties, AriaRoles};
use rome_diagnostics::{category, FileId};
use rome_js_factory::make::{jsx_expression_child, token};
use rome_js_syntax::{
suppression::parse_suppression_comment, JsLanguage, JsSyntaxToken, JsxAnyChild, T,
};
use rome_rowan::{AstNode, TokenAtOffset, TriviaPieceKind};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::{borrow::Cow, error::Error};

mod analyzers;
Expand Down Expand Up @@ -100,11 +102,14 @@ where
analyzer.add_visitor(Phases::Semantic, SemanticModelVisitor);
analyzer.add_visitor(Phases::Semantic, SyntaxVisitor::default());

let mut services = ServiceBag::default();
services.insert_service(Arc::new(AriaRoles::default()));
services.insert_service(Arc::new(AriaProperties::default()));
analyzer.run(AnalyzerContext {
file_id,
root: root.clone(),
range: filter.range,
services: ServiceBag::default(),
services,
options,
})
}
Expand Down

0 comments on commit a672521

Please sign in to comment.