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

feat(label-has-associated-control): add option for enforcing label's htmlFor matches control's id #1042

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

michaelfaith
Copy link
Contributor

This change adds an option to the label-has-associated-control rule, enforcing that the label's htmlFor attribute matches the associated control's id attribute. Previously, the only validation done on htmlFor was that it was on the label component and had text. There was no attempt to cross-check that value against any attribute on the associated control. Not, when the option is enabled, cases where they don't match will report.

I also took the opportunity to update the error messages so that each assert type gets an error message with verbiage specific to the assertion. (not sure if this should be called out as a separate feature in the changelog?).

Note: the current implementation only checks the first instance it finds of a child component that matches each control component type. It assumes that there won't be any acceptable cases where a label would have multiple inputs nested beneath it. Let me know if that assumption doesn't hold and i can refine that logic more.

Closes: #956

@michaelfaith michaelfaith changed the title feat: add option for enforcing label's htmlFor matches control's id feat(label-has-associated-control): add option for enforcing label's htmlFor matches control's id Dec 24, 2024
@michaelfaith michaelfaith force-pushed the feat/label-has-associated-control-option branch 7 times, most recently from c5c7649 to 789d368 Compare December 25, 2024 17:40
…htmlFor matches control's id

This change adds an option to the `label-has-associated-control` rule, enforcing that the label's htmlFor attribute matches the associated control's id attribute.  Previously, the only validation done on htmlFor was that it was on the label component and had text.  There was no attempt to cross-check that value against any attribute on the associated control.  Not, when the option is enabled, cases where they don't match will report.

I also took the opportunity to update the error messages so that each assert type gets an error message with verbiage specific to the assertion. (not sure if this should be called out as a separate feature in the changelog?).

Note: the current implementation only checks the first instance it finds of child component that matches each control component type.  It assumes that there won't be any acceptable cases where a label would have multiple inputs nested beneath it.  Let me know if that assumption doesn't hold.

Closes:
@michaelfaith michaelfaith force-pushed the feat/label-has-associated-control-option branch from 789d368 to 0f1845a Compare December 25, 2024 17:42
type: 'JSXClosingFragment',
};

export type JSXFragment = JSXElement & {
Copy link
Contributor Author

@michaelfaith michaelfaith Dec 25, 2024

Choose a reason for hiding this comment

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

It would have been better to use Omit here to remove openingElement and closingElement from JSXElement, but this version of flow doesn't have Omit (that was introduced in 0.211.0). I'd be up for upgrading the version of flow in a separate change if you'd like?

type: 'JSXClosingFragment',
};

export type JSXFragment = JSXElement & {
Copy link
Contributor Author

@michaelfaith michaelfaith Dec 25, 2024

Choose a reason for hiding this comment

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

The ast-types-flow package doesn't include a type for JSXFragment

@michaelfaith michaelfaith changed the title feat(label-has-associated-control): add option for enforcing label's htmlFor matches control's id feat(label-has-associated-control): add option for enforcing label's htmlFor matches control's id Dec 25, 2024
@michaelfaith michaelfaith changed the title feat(label-has-associated-control): add option for enforcing label's htmlFor matches control's id feat(label-has-associated-control): add option for enforcing label's htmlFor matches control's id Dec 25, 2024
@michaelfaith michaelfaith marked this pull request as ready for review December 25, 2024 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

label-has-associated-control not checking an actual valid configuration
1 participant