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

feat(rome_js_analyze): noAriaUnsupportedElements #4340

Merged
merged 7 commits into from
Apr 4, 2023
Merged

feat(rome_js_analyze): noAriaUnsupportedElements #4340

merged 7 commits into from
Apr 4, 2023

Conversation

unvalley
Copy link
Contributor

@unvalley unvalley commented Apr 1, 2023

Summary

Closes #3937

This noAriaUnsupportedElementsrule enforces that elements that do not support ARIA roles, states, and properties do not have those attributes.

Diagnostics Example

invalid.jsx:2:2 lint/nursery/noAriaUnsupportedElements ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ! Avoid the role attribute and aria-* attributes when using meta, html, script, and style elements.
  
    1 │ <>
  > 2 │ 	<meta charset="UTF-8" aria-hidden="false" />
      │ 	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    3 │ 	<meta charset="UTF-8" role="meta" />
    4 │ 	<html aria-required="true" />
  
  i Using aria-* on elements that do not support them can cause issues with screen readers.
  

Test Plan

cargo test -p rome_js_analyze -- no_aria_unsupported_elements

test cases: https://github.com/rome/tools/blob/archived-js/internal/compiler/lint/rules/a11y/noAriaUnsupportedElements.test.toml

Changelog

  • The PR requires a changelog line

Documentation

  • The PR requires documentation
  • [ ] I will create a new PR to update the documentation

feat: implemente rule logic

chore: fix doc
docs: website lint rule page
@netlify
Copy link

netlify bot commented Apr 1, 2023

Deploy Preview for docs-rometools ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 3846d40
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/642aed5af20fca00088a2df7
😎 Deploy Preview https://deploy-preview-4340--docs-rometools.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added A-Diagnostic Area: errors and diagnostics A-Linter Area: linter A-Project Area: project configuration and loading labels Apr 1, 2023
Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation looks good! I just added some additional feedback to enhance the code :)


let element_name = node.name().ok()?.as_jsx_name()?.value_token().ok()?;
let element_name = element_name.text_trimmed();
let aria_unsuppurted_elements = ["meta", "html", "script", "style"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this array outside the function and make it a const. Doing so, we can optimize its creation using the compiler:

const ARIA_UNSUPPORTED_ELEMENTS: [&str; 4] = ["meta", "html", "script", "style"];

If you apply this change, the next if will be like this:

if aria_unsuppurted_elements.contains(&&element_name) {}

})
.collect();

if !attributes.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterators have a function called .count() that can be used instead of is_empty(). This means that we don't need to allocate a new vector!

We can remove .collect() and use if attributes.count() > 0

7 │ <script role="script"></script>
8 │ <style aria-labelledby></style>

i Using roles on elements that do not support them can cause issues with screen readers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hint is a bit misleading here. The example applies an ARIA attribute, not a role attribute. Do you think we can customise the message based on the attribute used? aria-* VS role

Copy link
Contributor Author

@unvalley unvalley Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely it is better to guide against role and aria-* respectively, so I will implement it that way!


if ARIA_UNSUPPORTED_ELEMENTS.contains(&element_name) {
// Check if the unsupported element has `role` or `aria-*` attribute
let report = node.attributes().iter().find_map(|attribute| {
Copy link
Contributor Author

@unvalley unvalley Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the logic to use find_map instead of filter_map. Because I think it is enough that the rule needs to be reported when one of the violations (role or aria-*) are found.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

@unvalley
Copy link
Contributor Author

unvalley commented Apr 3, 2023

@ematipico Thanks for the first review, could you check the report again?

@ematipico ematipico merged commit 0fd72a3 into rome:main Apr 4, 2023
@unvalley unvalley deleted the no-aria-unsupported-elements branch April 4, 2023 08:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Diagnostic Area: errors and diagnostics A-Linter Area: linter A-Project Area: project configuration and loading
Projects
None yet
Development

Successfully merging this pull request may close these issues.

noAriaUnsupportedElements
2 participants