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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add new rule of react-a11y-input-elements * Update README.md and build new rule * Use getJsxAttributesFromJsxElement only for input elements * Revert tslint.json formator changes * Fix missing revert tslint changes * Update rule and tests with latest master changes
- Loading branch information
Showing
6 changed files
with
132 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint'; | ||
|
||
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker'; | ||
import {getJsxAttributesFromJsxElement, isEmpty} from './utils/JsxAttribute'; | ||
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'; | ||
|
||
/** | ||
* Implementation of the react-a11y-input-elements rule. | ||
*/ | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
|
||
public static metadata: ExtendedMetadata = { | ||
ruleName: 'react-a11y-input-elements', | ||
type: 'functionality', | ||
description: 'For accessibility of your website, HTML input boxes and text areas must include default, place-holding characters.', | ||
options: undefined, | ||
optionsDescription: '', | ||
typescriptOnly: true, | ||
issueClass: 'Non-SDL', | ||
issueType: 'Warning', | ||
severity: 'Moderate', | ||
level: 'Opportunity for Excellence', | ||
group: 'Accessibility' | ||
}; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
if (sourceFile.languageVariant === ts.LanguageVariant.JSX) { | ||
return this.applyWithWalker(new ReactA11yInputElementsRuleWalker(sourceFile, this.getOptions())); | ||
} else { | ||
return []; | ||
} | ||
} | ||
} | ||
|
||
class ReactA11yInputElementsRuleWalker extends ErrorTolerantWalker { | ||
|
||
protected visitJsxSelfClosingElement(node: ts.JsxSelfClosingElement): void { | ||
const tagName = node.tagName.getText(); | ||
|
||
if (tagName === 'input') { | ||
const attributes = getJsxAttributesFromJsxElement(node); | ||
if (isEmpty(attributes.value) && isEmpty(attributes.placeholder)) { | ||
this.addFailureAt(node.getStart(), node.getWidth(), MISSING_PLACEHOLDER_INPUT_FAILURE_STRING); | ||
} | ||
} else if (tagName === 'textarea') { | ||
const attributes = getJsxAttributesFromJsxElement(node); | ||
if (isEmpty(attributes.placeholder)) { | ||
this.addFailureAt(node.getStart(), node.getWidth(), MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING); | ||
} | ||
} | ||
super.visitJsxSelfClosingElement(node); | ||
} | ||
|
||
protected visitJsxElement(node: ts.JsxElement): void { | ||
const tagName = node.openingElement.tagName.getText(); | ||
const attributes: { [propName: string]: ts.JsxAttribute } = getJsxAttributesFromJsxElement(node); | ||
|
||
if (tagName === 'textarea') { | ||
if (node.children.length === 0 && isEmpty(attributes.placeholder)) { | ||
this.addFailureAt(node.getStart(), node.getWidth(), MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING); | ||
} | ||
} | ||
super.visitJsxElement(node); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {Utils} from '../utils/Utils'; | ||
import {TestHelper} from './TestHelper'; | ||
import { | ||
MISSING_PLACEHOLDER_INPUT_FAILURE_STRING, | ||
MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING | ||
} from '../reactA11yInputElementsRule'; | ||
|
||
/** | ||
* Unit tests. | ||
*/ | ||
describe('reactA11yInputElementsRule', () : void => { | ||
|
||
const ruleName : string = 'react-a11y-input-elements'; | ||
|
||
it('should pass on input elements with placeholder', () : void => { | ||
const script : string = ` | ||
import React = require('react'); | ||
const a = <input value="someValue" />; | ||
const b = <input placeholder="default placeholder" />; | ||
const c = <textarea placeholder="default placeholder" />; | ||
const d = <textarea>Some text</textarea>; | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should fail on empty input elements without placeholder', () : void => { | ||
const script : string = ` | ||
import React = require('react'); | ||
const a = <input />; | ||
const b = <textarea />; | ||
const c = <textarea></textarea>; | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": MISSING_PLACEHOLDER_INPUT_FAILURE_STRING, | ||
"name": Utils.absolutePath("file.tsx"), | ||
"ruleName": "react-a11y-input-elements", | ||
"startPosition": { "character": 23, "line": 3 } | ||
}, | ||
{ | ||
"failure": MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING, | ||
"name": Utils.absolutePath("file.tsx"), | ||
"ruleName": "react-a11y-input-elements", | ||
"startPosition": { "character": 23, "line": 4 } | ||
}, | ||
{ | ||
"failure": MISSING_PLACEHOLDER_TEXTAREA_FAILURE_STRING, | ||
"name": Utils.absolutePath("file.tsx"), | ||
"ruleName": "react-a11y-input-elements", | ||
"startPosition": { "character": 23, "line": 5 }} | ||
]); | ||
}); | ||
|
||
}); |
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
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