This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
exclude specific input types from react-a11y-input-elements rule #754
Merged
JoshuaKGoldberg
merged 4 commits into
microsoft:master
from
lizzzp1:a11y-input-placeholder-fix
Jan 2, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { ExtendedMetadata } from './utils/ExtendedMetadata'; | |
export const MISSING_PLACEHOLDER_INPUT_FAILURE_STRING: string = 'Input elements must include default, place-holding characters if empty'; | ||
export const MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING: string = | ||
'Textarea elements must include default, place-holding characters if empty'; | ||
const EXCLUDED_INPUT_TYPES = ['checkbox', 'radio', 'file']; | ||
|
||
/** | ||
* Implementation of the react-a11y-input-elements rule. | ||
|
@@ -45,14 +46,32 @@ export class Rule extends Lint.Rules.AbstractRule { | |
} | ||
} | ||
|
||
function isExcludedInputType(node: ts.JsxSelfClosingElement, attributes: { [propName: string]: ts.JsxAttribute }): boolean { | ||
for (const attribute of node.attributes.properties) { | ||
if (tsutils.isJsxAttribute(attribute)) { | ||
const isInputAttributeType = attributes.type; | ||
if (attribute.initializer !== undefined && tsutils.isStringLiteral(attribute.initializer)) { | ||
const attributeText = attribute.initializer.text; | ||
if (isInputAttributeType !== undefined && EXCLUDED_INPUT_TYPES.indexOf(attributeText) !== -1) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>) { | ||
function cb(node: ts.Node): void { | ||
if (tsutils.isJsxSelfClosingElement(node)) { | ||
const tagName = node.tagName.getText(); | ||
|
||
if (tagName === 'input') { | ||
const attributes = getJsxAttributesFromJsxElement(node); | ||
if (isEmpty(attributes.value) && isEmpty(attributes.placeholder)) { | ||
const isExcludedInput = isExcludedInputType(node, attributes); | ||
const isExcludedInputTypeValueEmpty = isEmpty(attributes.value) && isExcludedInput; | ||
const isPlaceholderEmpty = isEmpty(attributes.placeholder) && !isExcludedInput; | ||
if ((isEmpty(attributes.value) && isPlaceholderEmpty) || isExcludedInputTypeValueEmpty) { | ||
ctx.addFailureAt(node.getStart(), node.getWidth(), MISSING_PLACEHOLDER_INPUT_FAILURE_STRING); | ||
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. 😬 might this be simplified? 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. A couple things to try:
...but the test cases look good, build passes, and this is still a pretty simple rule; I'm fine with merging this in as is & filing a followup issue to look into cleaning it up. I'll wait for direction from you if you want to tackle it now - totally up to you! 😊 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. Good call! Dried it up ⬇️ |
||
} | ||
} else if (tagName === 'textarea') { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
got this type from below. Let me know if this looks right to you.