-
-
Notifications
You must be signed in to change notification settings - Fork 33
controlComponents option for label-has-for #40
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ const { | |
|
|
||
| const controlTypes = ["input", "meter", "progress", "select", "textarea"]; | ||
|
|
||
| const validateNesting = (node, allowChildren) => | ||
| const validateNesting = (node, allowChildren, controlComponents) => | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (optional) What do you think about having the controlComponents and the allowChildren both on the same second parameter as an object? This way it would be easier to do the recursion and it would be more convinient to pass the arguments on the call stack on the rest of the file, meaning we would only need to write both, the allowChildren, and the controlComponents on the first call in the stack, the |
||
| node.children.some((child) => { | ||
| if (child.rawName === "slot") { | ||
| return allowChildren; | ||
|
|
@@ -17,35 +17,41 @@ const validateNesting = (node, allowChildren) => | |
| if (child.type === "VElement") { | ||
| return ( | ||
| !isHiddenFromScreenReader(child) && | ||
| (controlTypes.includes(getElementType(child)) || | ||
| (controlTypes | ||
| .concat(controlComponents) | ||
| .includes(getElementType(child)) || | ||
| validateNesting(child, allowChildren)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (important) don't we need to pass the controlComponents to this calling of the funciton also? |
||
| ); | ||
| } | ||
|
|
||
| return false; | ||
| }); | ||
|
|
||
| const validate = (node, rule, allowChildren) => { | ||
| const validate = (node, rule, allowChildren, controlComponents) => { | ||
| switch (rule) { | ||
| case "nesting": | ||
| return validateNesting(node, allowChildren); | ||
| return validateNesting(node, allowChildren, controlComponents); | ||
| case "id": | ||
| return getElementAttributeValue(node, "for"); | ||
| default: | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| const isValidLabel = (node, required, allowChildren) => { | ||
| const isValidLabel = (node, required, allowChildren, controlComponents) => { | ||
| if (Array.isArray(required.some)) { | ||
| return required.some.some((rule) => validate(node, rule, allowChildren)); | ||
| return required.some.some((rule) => | ||
| validate(node, rule, allowChildren, controlComponents) | ||
| ); | ||
| } | ||
|
|
||
| if (Array.isArray(required.every)) { | ||
| return required.every.every((rule) => validate(node, rule, allowChildren)); | ||
| return required.every.every((rule) => | ||
| validate(node, rule, allowChildren, controlComponents) | ||
| ); | ||
| } | ||
|
|
||
| return validate(node, required, allowChildren); | ||
| return validate(node, required, allowChildren, controlComponents); | ||
| }; | ||
|
|
||
| module.exports = { | ||
|
|
@@ -67,6 +73,13 @@ module.exports = { | |
| }, | ||
| uniqueItems: true | ||
| }, | ||
| controlComponents: { | ||
| type: "array", | ||
| items: { | ||
| type: "string" | ||
| }, | ||
| uniqueItems: true | ||
| }, | ||
| required: { | ||
| oneOf: [ | ||
| { | ||
|
|
@@ -116,12 +129,13 @@ module.exports = { | |
| const { | ||
| allowChildren = false, | ||
| components = [], | ||
| controlComponents = [], | ||
| required = { every: ["nesting", "id"] } | ||
| } = context.options[0] || {}; | ||
|
|
||
| if ( | ||
| ["label"].concat(components).includes(getElementType(node)) && | ||
| !isValidLabel(node, required, allowChildren) | ||
| !isValidLabel(node, required, allowChildren, controlComponents) | ||
| ) { | ||
| context.report({ node, messageId: "default" }); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be nice if we added a negative test for the custom input components?