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 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #196] new rule: react-a11y-meta
- Loading branch information
Showing
5 changed files
with
152 additions
and
2 deletions.
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,74 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint/lib/lint'; | ||
|
||
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker'; | ||
import {ExtendedMetadata} from './utils/ExtendedMetadata'; | ||
import {SyntaxKind} from './utils/SyntaxKind'; | ||
|
||
const FAILURE_STRING: string = 'Do not use http-equiv="refresh"'; | ||
|
||
/** | ||
* Implementation of the react-a11y-meta rule. | ||
*/ | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
|
||
public static metadata: ExtendedMetadata = { | ||
ruleName: 'react-a11y-meta', | ||
type: 'functionality', | ||
description: '... add a meaningful one line description', | ||
options: null, | ||
issueClass: 'Ignored', | ||
issueType: 'Warning', | ||
severity: 'Low', | ||
level: 'Opportunity for Excellence', | ||
group: 'Accessibility' | ||
}; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new ReactA11yMetaRuleWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class ReactA11yMetaRuleWalker extends ErrorTolerantWalker { | ||
|
||
|
||
protected visitJsxElement(node: ts.JsxElement): void { | ||
this.validateOpeningElement(node, node.openingElement); | ||
super.visitJsxElement(node); | ||
} | ||
|
||
protected visitJsxSelfClosingElement(node: ts.JsxSelfClosingElement): void { | ||
this.validateOpeningElement(node, node); | ||
} | ||
|
||
private validateOpeningElement(parent: ts.Node, openElement: ts.JsxOpeningElement): void { | ||
if (openElement.tagName.getText() === 'meta') { | ||
const attributes: ts.NodeArray<ts.JsxAttribute | ts.JsxSpreadAttribute> = openElement.attributes; | ||
attributes.forEach((parameter: ts.JsxAttribute | ts.JsxSpreadAttribute): void => { | ||
if (parameter.kind === SyntaxKind.current().JsxAttribute) { | ||
const attribute: ts.JsxAttribute = <ts.JsxAttribute>parameter; | ||
if (attribute.name.getText() === 'http-equiv') { | ||
if (attribute.initializer != null) { | ||
if (attribute.initializer.kind === SyntaxKind.current().StringLiteral) { | ||
const value: string = (<ts.StringLiteral>attribute.initializer).text; | ||
if (value === 'refresh') { | ||
this.addFailure(this.createFailure(parent.getStart(), openElement.getWidth(), FAILURE_STRING)); | ||
} | ||
} else if (attribute.initializer.kind === SyntaxKind.current().JsxExpression) { | ||
const exp: ts.JsxExpression = <ts.JsxExpression>attribute.initializer; | ||
if (exp.expression.kind === SyntaxKind.current().StringLiteral) { | ||
const value: string = (<ts.StringLiteral>exp.expression).text; | ||
if (value === 'refresh') { | ||
this.addFailure( | ||
this.createFailure(openElement.getStart(), openElement.getWidth(), FAILURE_STRING) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |
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,74 @@ | ||
/// <reference path="../typings/mocha.d.ts" /> | ||
/// <reference path="../typings/chai.d.ts" /> | ||
|
||
import {TestHelper} from './TestHelper'; | ||
|
||
/** | ||
* Unit tests. | ||
*/ | ||
describe('reactA11yMetaRule', () : void => { | ||
|
||
const ruleName : string = 'react-a11y-meta'; | ||
|
||
it('should pass on meta tags without refresh', () : void => { | ||
const script : string = ` | ||
import React = require('react'); | ||
const x = <meta http-equiv="not_refresh" /> | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should fail on meta tags with refresh - self closing', () : void => { | ||
const script : string = ` | ||
import React = require('react'); | ||
const x = <meta http-equiv="refresh" /> | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Do not use http-equiv=\"refresh\"", | ||
"name": "file.tsx", | ||
"ruleName": "react-a11y-meta", | ||
"startPosition": { "character": 23, "line": 4 } | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on meta tags with refresh', () : void => { | ||
const script : string = ` | ||
import React = require('react'); | ||
const x = <meta http-equiv="refresh" ></meta> | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Do not use http-equiv=\"refresh\"", | ||
"name": "file.tsx", | ||
"ruleName": "react-a11y-meta", | ||
"startPosition": { "character": 23, "line": 4 } | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on meta tags with refresh - self-closing', () : void => { | ||
const script : string = ` | ||
import React = require('react'); | ||
const x = <meta http-equiv={"refresh"} /> | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Do not use http-equiv=\"refresh\"", | ||
"name": "file.tsx", | ||
"ruleName": "react-a11y-meta", | ||
"startPosition": { "character": 23, "line": 4 } | ||
} | ||
]); | ||
}); | ||
|
||
}); |
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