This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rome_js_analyze): move useIframeTitle from semantic_analyzer…
… to analyzer and refactoring (#4438) * wip * refactor: move useIframeTitle to analyzers * fix: codegen
- Loading branch information
Showing
12 changed files
with
181 additions
and
254 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
crates/rome_js_analyze/src/analyzers/a11y/use_iframe_title.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_syntax::jsx_ext::AnyJsxElement; | ||
use rome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Enforces the usage of the attribute `title` for the element `iframe`. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe /> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe></iframe> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe title="" /> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe title={""} /> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe title={undefined} /> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe title={false} /> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe title={true} /> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <iframe title={42} /> | ||
/// ``` | ||
/// | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```jsx | ||
/// <> | ||
/// <iframe title="This is a unique title" /> | ||
/// <iframe title={uniqueTitle} /> | ||
/// <iframe {...props} /> | ||
/// </> | ||
/// ``` | ||
/// | ||
/// ## Accessibility guidelines | ||
/// | ||
/// - [WCAG 2.4.1](https://www.w3.org/WAI/WCAG21/Understanding/bypass-blocks) | ||
/// - [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value) | ||
/// | ||
pub(crate) UseIframeTitle { | ||
version: "12.0.0", | ||
name: "useIframeTitle", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for UseIframeTitle { | ||
type Query = Ast<AnyJsxElement>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let element = ctx.query(); | ||
let name = element.name().ok()?.name_value_token()?; | ||
|
||
if name.text_trimmed() == "iframe" { | ||
if let Some(lang_attribute) = element.find_attribute_by_name("title") { | ||
if !lang_attribute | ||
.as_static_value() | ||
.map_or(true, |attribute| attribute.is_not_string_constant("")) | ||
&& !element.has_trailing_spread_prop(lang_attribute) | ||
{ | ||
return Some(()); | ||
} | ||
} else if !element.has_spread_prop() { | ||
return Some(()); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.syntax().text_trimmed_range(), | ||
markup! { | ||
"Provide a "<Emphasis>"title"</Emphasis>" attribute when using "<Emphasis>"iframe"</Emphasis>" elements." | ||
} | ||
) | ||
.note(markup! { | ||
"Screen readers rely on the title set on an iframe to describe the content being displayed." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
147 changes: 0 additions & 147 deletions
147
crates/rome_js_analyze/src/semantic_analyzers/a11y/use_iframe_title.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.