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

Commit

Permalink
refactor: move useIframeTitle to analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
nissy-dev committed May 4, 2023
1 parent 4bc91db commit 097d3c8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 67 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.

35 changes: 16 additions & 19 deletions crates/rome_js_analyze/src/analyzers/a11y/use_iframe_title.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_syntax::{jsx_ext::AnyJsxElement, AnyJsExpression, AnyJsxAttributeValue};
use rome_rowan::{AstNode, AstNodeList};
use rome_js_syntax::jsx_ext::AnyJsxElement;
use rome_rowan::AstNode;

declare_rule! {
/// Enforces the usage of the attribute `title` for the element `iframe`.
Expand Down Expand Up @@ -72,10 +72,21 @@ impl Rule for UseIframeTitle {
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let element = ctx.query();
let name = element.name().ok()?.name_value_token()?;

if node.name_value_token()?.text_trimmed() == "iframe" && has_valid_title(element) {
return Some(());
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
Expand All @@ -97,17 +108,3 @@ impl Rule for UseIframeTitle {
)
}
}

fn has_valid_title(element: &AnyJsxElement) -> bool {
element
.find_attribute_by_name("title")
.map_or(false, |attribute| {
if attribute.initializer().is_none() {
return false;
}

attribute.as_static_value().map_or(true, |value| {
!value.is_null_or_undefined() && value.is_not_string_constant("")
}) && !element.has_trailing_spread_prop(attribute)
})
}
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 96
expression: invalid.jsx
---
# Input
Expand All @@ -12,7 +11,6 @@ expression: invalid.jsx
<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 Expand Up @@ -83,7 +81,7 @@ invalid.jsx:6:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━
> 6 │ <iframe title={""} />
│ ^^^^^^^^^^^^^^^^^^^^^
7 │ <iframe title={``} />
8 │ <iframe title={<span className={"token string"}></span>}></iframe>
8 │ <iframe title={undefined} />
i Screen readers rely on the title set on an iframe to describe the content being displayed.
Expand All @@ -99,8 +97,8 @@ invalid.jsx:7:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━
6 │ <iframe title={""} />
> 7 │ <iframe title={``} />
│ ^^^^^^^^^^^^^^^^^^^^^
8 │ <iframe title={<span className={"token string"}></span>}></iframe>
9 │ <iframe title={undefined} />
8 │ <iframe title={undefined} />
9 │ <iframe title={false} />
i Screen readers rely on the title set on an iframe to describe the content being displayed.
Expand All @@ -114,10 +112,10 @@ invalid.jsx:8:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━
6 │ <iframe title={""} />
7 │ <iframe title={``} />
> 8 │ <iframe title={<span className={"token string"}></span>}></iframe>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9 │ <iframe title={undefined} />
10 │ <iframe title={false} />
> 8 │ <iframe title={undefined} />
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9 │ <iframe title={false} />
10 │ <iframe title={true} />
i Screen readers rely on the title set on an iframe to describe the content being displayed.
Expand All @@ -130,62 +128,45 @@ invalid.jsx:9:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━
! Provide a title attribute when using iframe elements.
7 │ <iframe title={``} />
8 │ <iframe title={<span className={"token string"}></span>}></iframe>
> 9 │ <iframe title={undefined} />
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 │ <iframe title={false} />
11 │ <iframe title={true} />
i Screen readers rely on the title set on an iframe to describe the content being displayed.
```

```
invalid.jsx:10:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Provide a title attribute when using iframe elements.
8 │ <iframe title={<span className={"token string"}></span>}></iframe>
9 │ <iframe title={undefined} />
> 10 │ <iframe title={false} />
8 │ <iframe title={undefined} />
> 9 │ <iframe title={false} />
│ ^^^^^^^^^^^^^^^^^^^^^^^^
11 │ <iframe title={true} />
12 │ <iframe title={42} />
10 │ <iframe title={true} />
11 │ <iframe title={42} />
i Screen readers rely on the title set on an iframe to describe the content being displayed.
```

```
invalid.jsx:11:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.jsx:10:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Provide a title attribute when using iframe elements.
9 │ <iframe title={undefined} />
10 │ <iframe title={false} />
> 11 │ <iframe title={true} />
8 │ <iframe title={undefined} />
9 │ <iframe title={false} />
> 10 │ <iframe title={true} />
│ ^^^^^^^^^^^^^^^^^^^^^^^
12 │ <iframe title={42} />
13 │ </>;
11 │ <iframe title={42} />
12 │ </>;
i Screen readers rely on the title set on an iframe to describe the content being displayed.
```

```
invalid.jsx:12:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid.jsx:11:2 lint/a11y/useIframeTitle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Provide a title attribute when using iframe elements.
10 │ <iframe title={false} />
11 │ <iframe title={true} />
> 12 │ <iframe title={42} />
9 │ <iframe title={false} />
10 │ <iframe title={true} />
> 11 │ <iframe title={42} />
│ ^^^^^^^^^^^^^^^^^^^^^
13 │ </>;
14
12 │ </>;
13
i Screen readers rely on the title set on an iframe to describe the content being displayed.
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_service/src/configuration/linter/rules.rs

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

2 changes: 1 addition & 1 deletion editors/vscode/configuration_schema.json

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

2 changes: 1 addition & 1 deletion npm/backend-jsonrpc/src/workspace.ts

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

2 changes: 1 addition & 1 deletion npm/rome/configuration_schema.json

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

0 comments on commit 097d3c8

Please sign in to comment.