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

Commit

Permalink
chore: code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Dec 16, 2022
1 parent ef58adf commit 973f7b4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 94 deletions.
1 change: 0 additions & 1 deletion crates/rome_aria/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod macros;
pub mod properties;
pub mod roles;

pub use iso::AriaIso;
pub use properties::AriaProperties;
pub(crate) use roles::AriaRoleDefinition;
pub use roles::AriaRoles;
Expand Down
48 changes: 25 additions & 23 deletions crates/rome_js_analyze/src/aria_analyzers/nursery/use_valid_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ declare_rule! {
/// <html lang="en-babab" />
/// ```
///
/// ```jsx,expect_diagnostic
/// <html lang="en-GB-typo" />
/// ```
///
/// ### Valid
///
/// ```jsx
Expand All @@ -31,14 +35,14 @@ declare_rule! {
}
}

enum ErrorKind {
InvalidLanguage,
InvalidCountry,
InvalidValue,
enum InvalidKind {
Language,
Country,
Value,
}

pub(crate) struct UseValidLangState {
error_kind: ErrorKind,
invalid_kind: InvalidKind,
attribute_range: TextRange,
}

Expand All @@ -58,35 +62,33 @@ impl Rule for UseValidLang {
let mut split_value = attribute_text.text().split('-');
match (split_value.next(), split_value.next()) {
(Some(language), Some(country)) => {
if !ctx.is_valid_language(language) {
if !ctx.is_valid_iso_language(language) {
return Some(UseValidLangState {
attribute_range: attribute_value.range(),
error_kind: ErrorKind::InvalidLanguage,
invalid_kind: InvalidKind::Language,
});
} else if !ctx.is_valid_country(country) {
} else if !ctx.is_valid_iso_country(country) {
return Some(UseValidLangState {
attribute_range: attribute_value.range(),
error_kind: ErrorKind::InvalidCountry,
invalid_kind: InvalidKind::Country,
});
}
}

(Some(language), None) => {
if !ctx.is_valid_language(language) {
} else if split_value.next().is_some() {
return Some(UseValidLangState {
attribute_range: attribute_value.range(),
error_kind: ErrorKind::InvalidLanguage,
invalid_kind: InvalidKind::Value,
});
}
}
_ => {
if split_value.next().is_some() {

(Some(language), None) => {
if !ctx.is_valid_iso_language(language) {
return Some(UseValidLangState {
attribute_range: attribute_value.range(),
error_kind: ErrorKind::InvalidValue,
invalid_kind: InvalidKind::Language,
});
}
}
_ => {}
}
}

Expand All @@ -101,8 +103,8 @@ impl Rule for UseValidLang {
"Provide a valid value for the "<Emphasis>"lang"</Emphasis>" attribute."
},
);
diagnostic = match state.error_kind {
ErrorKind::InvalidLanguage => {
diagnostic = match state.invalid_kind {
InvalidKind::Language => {
let languages = ctx.iso_language_list();
let languages = if languages.len() > 15 {
&languages[..15]
Expand All @@ -112,7 +114,7 @@ impl Rule for UseValidLang {

diagnostic.footer_list("Some of valid languages:", languages)
}
ErrorKind::InvalidCountry => {
InvalidKind::Country => {
let countries = ctx.iso_country_list();
let countries = if countries.len() > 15 {
&countries[..15]
Expand All @@ -122,8 +124,8 @@ impl Rule for UseValidLang {

diagnostic.footer_list("Some of valid countries:", countries)
}
ErrorKind::InvalidValue => diagnostic,
};y
InvalidKind::Value => diagnostic,
};
Some(diagnostic)
}
}
6 changes: 1 addition & 5 deletions crates/rome_js_analyze/src/aria_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rome_analyze::{
RuleKey, ServiceBag,
};
use rome_aria::iso::{countries, is_valid_country, is_valid_language, languages};
use rome_aria::{AriaIso, AriaProperties, AriaRoles};
use rome_aria::{AriaProperties, AriaRoles};
use rome_js_syntax::JsLanguage;
use rome_rowan::AstNode;
use std::sync::Arc;
Expand Down Expand Up @@ -51,13 +51,9 @@ impl FromServices for AriaServices {
let properties: &Arc<AriaProperties> = services.get_service().ok_or_else(|| {
MissingServicesDiagnostic::new(rule_key.rule_name(), &["AriaProperties"])
})?;
let iso: &Arc<AriaIso> = services
.get_service()
.ok_or_else(|| MissingServicesDiagnostic::new(rule_key.rule_name(), &["AriaIso"]))?;
Ok(Self {
roles: roles.clone(),
properties: properties.clone(),
iso: iso.clone(),
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/rome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rome_analyze::{
DeserializableRuleOptions, InspectMatcher, LanguageRoot, MatchQueryParams, MetadataRegistry,
Phases, RuleAction, RuleRegistry, ServiceBag, SuppressionKind, SyntaxVisitor,
};
use rome_aria::{AriaIso, AriaProperties, AriaRoles};
use rome_aria::{AriaProperties, AriaRoles};
use rome_diagnostics::{category, Diagnostic, FileId};
use rome_js_syntax::suppression::SuppressionDiagnostic;
use rome_js_syntax::{suppression::parse_suppression_comment, JsLanguage};
Expand Down Expand Up @@ -174,7 +174,6 @@ where

services.insert_service(Arc::new(AriaRoles::default()));
services.insert_service(Arc::new(AriaProperties::default()));
services.insert_service(Arc::new(AriaIso::default()));
analyzer.run(AnalyzerContext {
file_id,
root: root.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// invalid
let a = <html lang="lorem" />;
let a = <html lang="en-babab" />;
let a = <html lang="en-GB-something" />;

// valid
let a = <Html lang="en-babab" />;
Expand Down
22 changes: 19 additions & 3 deletions crates/rome_js_analyze/tests/specs/nursery/useValidLang.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expression: useValidLang.jsx
// invalid
let a = <html lang="lorem" />;
let a = <html lang="en-babab" />;
let a = <html lang="en-GB-something" />;

// valid
let a = <Html lang="en-babab" />;
Expand All @@ -26,7 +27,7 @@ useValidLang.jsx:2:20 lint/nursery/useValidLang ━━━━━━━━━━
> 2 │ let a = <html lang="lorem" />;
│ ^^^^^^^
3 │ let a = <html lang="en-babab" />;
4 │
4 │ let a = <html lang="en-GB-something" />;
i Some of valid languages:
Expand Down Expand Up @@ -58,8 +59,8 @@ useValidLang.jsx:3:20 lint/nursery/useValidLang ━━━━━━━━━━
2 │ let a = <html lang="lorem" />;
> 3 │ let a = <html lang="en-babab" />;
│ ^^^^^^^^^^
4 │
5 │ // valid
4 │ let a = <html lang="en-GB-something" />;
5 │
i Some of valid countries:
Expand All @@ -82,4 +83,19 @@ useValidLang.jsx:3:20 lint/nursery/useValidLang ━━━━━━━━━━
```

```
useValidLang.jsx:4:20 lint/nursery/useValidLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Provide a valid value for the lang attribute.
2 │ let a = <html lang="lorem" />;
3 │ let a = <html lang="en-babab" />;
> 4 │ let a = <html lang="en-GB-something" />;
│ ^^^^^^^^^^^^^^^^^
5 │
6 │ // valid
```


Loading

0 comments on commit 973f7b4

Please sign in to comment.