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

Commit

Permalink
refactor(rome_js_analyze): move useIframeTitle from semantic_analyzer…
Browse files Browse the repository at this point in the history
… to analyzer and refactoring (#4438)

* wip

* refactor: move useIframeTitle to analyzers

* fix: codegen
  • Loading branch information
nissy-dev authored May 4, 2023
1 parent ad8dd54 commit ed2473f
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 254 deletions.
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/a11y.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions crates/rome_js_analyze/src/analyzers/a11y/use_iframe_title.rs
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."
}),
)
}
}
3 changes: 1 addition & 2 deletions crates/rome_js_analyze/src/semantic_analyzers/a11y.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 0 additions & 147 deletions crates/rome_js_analyze/src/semantic_analyzers/a11y/use_iframe_title.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<iframe title="" />
<iframe title={""} />
<iframe title={``} />
<iframe title={<span className={"token string"}></span>}></iframe>
<iframe title={undefined} />
<iframe title={false} />
<iframe title={true} />
Expand Down
Loading

0 comments on commit ed2473f

Please # to comment.