|
| 1 | +import * as ast from '@angular/compiler'; |
| 2 | +import { NgWalker } from 'codelyzer/angular/ngWalker'; |
| 3 | +import { BasicTemplateAstVisitor } from 'codelyzer/angular/templates/basicTemplateAstVisitor'; |
| 4 | +import * as Lint from 'tslint'; |
| 5 | +import { IOptions } from 'tslint'; |
| 6 | +import * as ts from 'typescript'; |
| 7 | + |
| 8 | +export const ruleName = 'ion-fab-attributes-renamed'; |
| 9 | + |
| 10 | +function generateErrorMessage(attrName: string, replacement: string) { |
| 11 | + return `The ${attrName} attribute of ion-fab has been renamed. Use ${replacement} instead.`; |
| 12 | +} |
| 13 | + |
| 14 | +const attrMap = new Map([ |
| 15 | + ['center', 'horizontal="center"'], |
| 16 | + ['start', 'horizontal="start"'], |
| 17 | + ['end', 'horizontal="end"'], |
| 18 | + ['top', 'vertical="top"'], |
| 19 | + ['bottom', 'vertical="bottom"'], |
| 20 | + ['middle', 'vertical="center"'] |
| 21 | +]); |
| 22 | + |
| 23 | +class IonFabAttributesRenamedTemplateVisitor extends BasicTemplateAstVisitor { |
| 24 | + visitElement(element: ast.ElementAst, context: any): any { |
| 25 | + if (element.name === 'ion-fab') { |
| 26 | + for (const attr of element.attrs) { |
| 27 | + const replacement = attrMap.get(attr.name); |
| 28 | + |
| 29 | + if (replacement) { |
| 30 | + const start = attr.sourceSpan.start.offset; |
| 31 | + const length = attr.name.length; |
| 32 | + const position = this.getSourcePosition(start); |
| 33 | + |
| 34 | + this.addFailureAt(start, length, generateErrorMessage(attr.name, replacement), [ |
| 35 | + Lint.Replacement.replaceFromTo(position, position + length, replacement) |
| 36 | + ]); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + super.visitElement(element, context); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export class Rule extends Lint.Rules.AbstractRule { |
| 46 | + public static metadata: Lint.IRuleMetadata = { |
| 47 | + ruleName: ruleName, |
| 48 | + type: 'functionality', |
| 49 | + description: 'Attributes of ion-fab have been renamed.', |
| 50 | + options: null, |
| 51 | + optionsDescription: 'Not configurable.', |
| 52 | + typescriptOnly: false, |
| 53 | + hasFix: true |
| 54 | + }; |
| 55 | + |
| 56 | + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { |
| 57 | + return this.applyWithWalker( |
| 58 | + new NgWalker(sourceFile, this.getOptions(), { |
| 59 | + templateVisitorCtrl: IonFabAttributesRenamedTemplateVisitor |
| 60 | + }) |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
0 commit comments