Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Idea: Enforce boolean literals for "booleanish" HTML attributes such as aria-hidden #986

Open
jdufresne opened this issue May 9, 2024 · 4 comments
Assignees

Comments

@jdufresne
Copy link

The following forms are both correct and equivalent:

<i className="fa fa-camera-retro fa-lg" aria-hidden="true" />
<i className="fa fa-camera-retro fa-lg" aria-hidden={true} />
// OR
<i className="fa fa-camera-retro fa-lg" aria-hidden />

This proposal is that a rule would prefer the 2nd form. That is, use the more specific literal type for these attributes. This would only apply when using a literal. There are several aria-* that are booleanish. This rule could apply to all of them.

The benefit is a more consistent codebase that avoids flip-flopping on style. As well, using a boolean in a true/false context feels more natural and explicit in intent.

The types defined by @types/react at:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5b5ec4e409bcd8d422efc505db5b0223c0c5af56/types/react/index.d.ts#L2591-L2812

@ljharb
Copy link
Member

ljharb commented May 9, 2024

Things that deal with aria belong in eslint-plugin-jsx-a11y (and aren't react-specific), so I'll transfer this issue there.

@ljharb ljharb transferred this issue from jsx-eslint/eslint-plugin-react May 9, 2024
@ljharb
Copy link
Member

ljharb commented May 9, 2024

However, I wonder if there's existing rules already that enforce the style of boolean jsx properties - have you tried https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md ?

@jdufresne
Copy link
Author

Thanks for the link and moving the ticket.

The docs on that rule read:

When using a boolean attribute in JSX, you can set the attribute value to true or omit the value.

However, this proposal is about when an attribute is the literal string "true" which of course is not a boolean. So my understanding is the rule doesn't apply to the proposal.

@ljharb
Copy link
Member

ljharb commented May 9, 2024

aha, good point - in that case, we'd want an aria-specific rule in this project to force true over "true", at which point the other rule can kick in :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants