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

feat(rome_js_analyze): useHeadingContent #4218

Closed
wants to merge 8 commits into from

Conversation

nissy-dev
Copy link
Contributor

Summary

Fix #3943

Test Plan

I added snapshot tests

Documentation

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

@netlify
Copy link

netlify bot commented Feb 17, 2023

Deploy Preview for docs-rometools canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit e183c15
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/63f761e5e353900007ae2b96

@denbezrukov
Copy link
Contributor

denbezrukov commented Feb 18, 2023

I was wondering about these test cases:
https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/__tests__/src/rules/heading-has-content-test.js

valid:
<h1 children={children} />

invalid:
<h1>{undefined}</h1>

<h1>
    <></>
</h1>
<h1>
    <Fragment/>
</h1>
<h1>
    <React.Fragment/>
</h1>

We have jsx_member_name_is_react_fragment and jsx_reference_identifier_is_fragment helpers.
What do you think?

@nissy-dev
Copy link
Contributor Author

@denbezrukov

I was wondering about these test cases:
https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/__tests__/src/rules/heading-has-content-test.js

Thank you for your review! I try to modify codes to catch more invalid cases which you or eslint-plugin-jsx-a11y mentions 👍

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.

I think it's worth adding some other test cases.

})
.count()
== 0
&& !node.has_dangerously_set_inner_html_attribute()?
Copy link
Contributor

Choose a reason for hiding this comment

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

We can move the check has_dangerously_set_inner_html_attribute up, next to if node.is_heading_element()?

Comment on lines 79 to 83
.filter(|child_node| {
is_accessible_to_screen_reader(child_node) == Some(true)
})
.count()
== 0
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's more readable to use the function any instead of filter().count().

== 0
&& !node.has_dangerously_set_inner_html_attribute()?
{
return Some(element.syntax().text_range());
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need to return anything here. We already have element, which is the Query. The rule can just return Some(())

None
}

fn diagnostic(_: &RuleContext<Self>, reference: &Self::State) -> Option<RuleDiagnostic> {
Copy link
Contributor

Choose a reason for hiding this comment

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

As per the previous comment, you can use the context to retrieve the range:

let node = ctx.query();

let range = node.syntax().text_trimmed_range();

You have to use text_trimmed_range

@@ -0,0 +1,116 @@
use rome_js_syntax::{
Copy link
Contributor

@ematipico ematipico Feb 20, 2023

Choose a reason for hiding this comment

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

Nit: we already have a utils.rs file with a utils/ folder. Having another utils* file seems a bit redundant. My suggestions:

  1. having just a aria.rs file
  2. adding these two functions inside the utils.rs file

}

/// Check if the `aria-hidden` attribute is present or the value is true.
pub(crate) fn is_aria_hidden_truthy(aria_hidden_attribute: &JsxAttribute) -> Option<bool> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this function doesn't need to be pub(crate)

<h1>
<div aria-hidden />
</h1>
<h1></h1>
Copy link
Contributor

Choose a reason for hiding this comment

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

Some invalid case we can add:

<>
	<h1 aria-hidden="true">content</h1>
	<h1 aria-hidden="true" dangerouslySetInnerHTML={{ __html: "heading" }} />	
</>

@nissy-dev
Copy link
Contributor Author

Thank you for your reviews! I refactor codes and pass more tests.
I appreciate you will review it when you have time.

@denbezrukov
Copy link
Contributor

@nissy-dev Thank you!
I was wondering if our new test cases can be also important for UseAnchorContentNode rule.
I mean that our new logic (for children/undefined/null/empty string) can be a part of is_accessible_to_screen_reader function.

What do you think?
We can do it in another MR, if you want =)

@nissy-dev
Copy link
Contributor Author

nissy-dev commented Feb 23, 2023

@denbezrukov
I want to do it in another PR! And then, I think this is related to #4073. We should prepare for utils functions that checks value of the attributes before writing more a11y rules.

@nissy-dev
Copy link
Contributor Author

I merged #4342, so I will try to reimplement in a few days.

@ematipico
Copy link
Contributor

@nissy-dev I think we can rebase the PR, fix the conflicts and we can merge it!

@nissy-dev
Copy link
Contributor Author

I recreate PR in #4423

@nissy-dev nissy-dev closed this Apr 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useHeadingContent
3 participants