Skip to content

Commit

Permalink
feat(rules): add ion-fab-attributes-renamed rule (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Jun 29, 2018
1 parent 84a4fdd commit a71d8bc
Show file tree
Hide file tree
Showing 4 changed files with 395 additions and 5 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
</td>
</tr>
<tr>
<td></td>
<td>:white_large_square:</td>
<td>:wrench:</td>
<td>:white_check_mark:</td>
<td>
<code>ion-fab-attributes-are-renamed</code>
<code>ion-fab-attributes-renamed</code>
</td>
<td>
<a href="https://github.com/dwieeb">@dwieeb</a>
</td>
<td></td>
</tr>
<tr>
<td></td>
Expand Down
63 changes: 63 additions & 0 deletions src/ionFabAttributesRenamedRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as ast from '@angular/compiler';
import { NgWalker } from 'codelyzer/angular/ngWalker';
import { BasicTemplateAstVisitor } from 'codelyzer/angular/templates/basicTemplateAstVisitor';
import * as Lint from 'tslint';
import { IOptions } from 'tslint';
import * as ts from 'typescript';

export const ruleName = 'ion-fab-attributes-renamed';

function generateErrorMessage(attrName: string, replacement: string) {
return `The ${attrName} attribute of ion-fab has been renamed. Use ${replacement} instead.`;
}

const attrMap = new Map([
['center', 'horizontal="center"'],
['start', 'horizontal="start"'],
['end', 'horizontal="end"'],
['top', 'vertical="top"'],
['bottom', 'vertical="bottom"'],
['middle', 'vertical="center"']
]);

class IonFabAttributesRenamedTemplateVisitor extends BasicTemplateAstVisitor {
visitElement(element: ast.ElementAst, context: any): any {
if (element.name === 'ion-fab') {
for (const attr of element.attrs) {
const replacement = attrMap.get(attr.name);

if (replacement) {
const start = attr.sourceSpan.start.offset;
const length = attr.name.length;
const position = this.getSourcePosition(start);

this.addFailureAt(start, length, generateErrorMessage(attr.name, replacement), [
Lint.Replacement.replaceFromTo(position, position + length, replacement)
]);
}
}
}

super.visitElement(element, context);
}
}

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: 'Attributes of ion-fab have been renamed.',
options: null,
optionsDescription: 'Not configurable.',
typescriptOnly: false,
hasFix: true
};

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new NgWalker(sourceFile, this.getOptions(), {
templateVisitorCtrl: IonFabAttributesRenamedTemplateVisitor
})
);
}
}
Loading

0 comments on commit a71d8bc

Please sign in to comment.