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

feat(rome_js_analyzer): rule noAutofocus #3333

Merged
merged 7 commits into from
Oct 6, 2022

Conversation

jk-gan
Copy link
Contributor

@jk-gan jk-gan commented Oct 5, 2022

Summary

Closes #3297

Test Plan

Added test cases to test the situations according to this docs.

Screenshot 2022-10-06 at 10 05 50 PM

Screenshot 2022-10-06 at 10 05 56 PM

Screenshot 2022-10-06 at 10 06 02 PM

@jk-gan jk-gan requested a review from a team October 5, 2022 09:59
@netlify
Copy link

netlify bot commented Oct 5, 2022

Deploy Preview for rometools ready!

Name Link
🔨 Latest commit 1fa5d2c
🔍 Latest deploy log https://app.netlify.com/sites/rometools/deploys/633ee1066fc15a0008795a90
😎 Deploy Preview https://deploy-preview-3333--rometools.netlify.app/docs/lint/rules/noautofocus
📱 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.

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.

Thank you! Overall the PR looks good and it does what intended! I just left few suggestions

@jk-gan
Copy link
Contributor Author

jk-gan commented Oct 5, 2022

alright, let me update this

@jk-gan jk-gan requested review from ematipico and removed request for xunilrj and leops October 5, 2022 10:55
} = node.as_fields();

if name.ok()?.text().trim() == "input" {
let attribute = node.find_attribute_by_name("autoFocus").ok()??;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sry, I do not fully understand this, can you explain more?

@jk-gan
Copy link
Contributor Author

jk-gan commented Oct 5, 2022

I've updated the code based on the feedback. 🙏🏻

fn diagnostic(_ctx: &RuleContext<Self>, attr: &Self::State) -> Option<RuleDiagnostic> {
Some(RuleDiagnostic::new(
rule_category!(),
attr.range(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh, let me change it

@xunilrj
Copy link
Contributor

xunilrj commented Oct 5, 2022

If we only wanna test for autofocus=={undefined} I would go with

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
        let node = ctx.query();
        let name = node.name().ok()?.syntax().text_trimmed();
        if name == "input" {
            let attribute = node.find_attribute_by_name("autoFocus").ok()??;
            is_value_undefined(&attribute).then_some(attribute)
        } else {
            None
        }
    }


fn is_value_undefined(attribute: &JsxAttribute) -> bool {
    attribute
        .initializer().and_then(|x| {
            let name = x.value()
                .ok()?
                .as_jsx_expression_attribute_value()?
                .expression()
                .ok()?
                .as_js_identifier_expression()?
                .name()
                .ok()?
                .syntax().text_trimmed();
            Some(name == "undefined")
        }).unwrap_or(false)        
}

This correctly navigates the AST until finds an undefined, which is a reference because JS quirkiness.

///```
///
/// ```jsx
/// <Input autoFocus={"false"} />
Copy link
Contributor

Choose a reason for hiding this comment

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

If the only difference is the uppercase "I" I would put a comment around it.

@@ -0,0 +1,5 @@
<>
<input />
<input autoFocus={undefined} />
Copy link
Contributor

Choose a reason for hiding this comment

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

<input autoFocus={null} /> is valid or invalid?

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 that null props don't get rendered, but I might be wrong.

<>
<input autoFocus />
<input autoFocus="true" />
<input autoFocus={"false"} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be interesting to also see

<input autoFocus={false} />
<input autoFocus={a} />

Comment on lines 34 to 38
/// <input autoFocus={undefined} />
///```
///
/// ```jsx
/// <Input autoFocus={"false"} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, because I was following this docs. If we want to follow eslint-plugin-jsx-a11y, then it should be any element with autoFocus attribute should be avoided. https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/f6ba03cd5c206e4ca04068253ab79ad32eb929ce/src/rules/no-autofocus.js

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah apologies for that. Probably that rule was buggy to begin with. Yeah it's best to follow the a11y plugin, it's been around longer and they catch more cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries! I will change the implementation to follow a11y plugin.

Copy link
Contributor Author

@jk-gan jk-gan Oct 6, 2022

Choose a reason for hiding this comment

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

Hey, eslint-plugin-jsx-a11y provide a config ignoreNonDOM to ignore user created component autoFocus attribute, e.g. <MyComponent autoFocus={true} />. I wonder how we should do that. Should we include the user-created components or just ignore them or provides an additional option to let the user to config?

Copy link
Contributor Author

@jk-gan jk-gan Oct 6, 2022

Choose a reason for hiding this comment

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

FYI, this is how to config to ignore user-created components in eslint

"rules": {
      "jsx-a11y/no-autofocus": [ 2, {
        "ignoreNonDOM": true
    }]
}

Copy link
Contributor

Choose a reason for hiding this comment

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

As for now, we can't configure rules with options. I think we should always ignore user-created components and just inspect normal elements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

alright

@jk-gan
Copy link
Contributor Author

jk-gan commented Oct 6, 2022

Hi, I've re-implemented the noAutofocus rule. It now will show error on any normal elements, such as <input />, <div />, <button />, with autoFocus attribute (same with eslint-plugin-jsx-a11y plugin).

PS: I'm using element.name().ok()?.as_jsx_name()?; to check if the element is an user-created elements or not because I saw from the Rome Playground user-created element name is JsxReferenceIdentifier. Do let me know if I'm doing this wrong.

@ematipico
Copy link
Contributor

PS: I'm using element.name().ok()?.as_jsx_name()?;

That's the right approach! 💯

@ematipico ematipico merged commit d69bef2 into rome:main Oct 6, 2022
@ematipico ematipico added the hacktoberfest-accepted For PRs that have been accepted for hacktoberfest label Oct 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hacktoberfest-accepted For PRs that have been accepted for hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

noAutoFocus
3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载