|
| 1 | +import { ElementAst } from '@angular/compiler'; |
| 2 | +import { sprintf } from 'sprintf-js'; |
| 3 | +import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib'; |
| 4 | +import { SourceFile } from 'typescript/lib/typescript'; |
| 5 | +import { NgWalker } from './angular/ngWalker'; |
| 6 | +import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor'; |
| 7 | + |
| 8 | +export class Rule extends Rules.AbstractRule { |
| 9 | + static readonly metadata: IRuleMetadata = { |
| 10 | + description: 'Enforces that no distracting elements are used', |
| 11 | + options: null, |
| 12 | + optionsDescription: 'Not configurable.', |
| 13 | + rationale: 'Elements that can be visually distracting can cause accessibility issues with visually impaired users.', |
| 14 | + ruleName: 'template-no-distracting-elements', |
| 15 | + type: 'functionality', |
| 16 | + typescriptOnly: true |
| 17 | + }; |
| 18 | + |
| 19 | + static readonly FAILURE_STRING = 'Avoid using <%s/> elements as they create visual accessibility issues.'; |
| 20 | + |
| 21 | + apply(sourceFile: SourceFile): RuleFailure[] { |
| 22 | + return this.applyWithWalker( |
| 23 | + new NgWalker(sourceFile, this.getOptions(), { |
| 24 | + templateVisitorCtrl: TemplateNoDistractingElementsVisitor |
| 25 | + }) |
| 26 | + ); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +export function getFailureMessage(element: string) { |
| 31 | + return sprintf(Rule.FAILURE_STRING, element); |
| 32 | +} |
| 33 | + |
| 34 | +class TemplateNoDistractingElementsVisitor extends BasicTemplateAstVisitor { |
| 35 | + visitElement(prop: ElementAst, context: any): any { |
| 36 | + this.validateElement(prop); |
| 37 | + super.visitElement(prop, context); |
| 38 | + } |
| 39 | + |
| 40 | + private validateElement(el: ElementAst): void { |
| 41 | + if (el.name === 'marquee' || el.name === 'blink') { |
| 42 | + const { |
| 43 | + sourceSpan: { |
| 44 | + end: { offset: endOffset }, |
| 45 | + start: { offset: startOffset } |
| 46 | + } |
| 47 | + } = el; |
| 48 | + this.addFailureFromStartToEnd(startOffset, endOffset, getFailureMessage(el.name)); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments