Skip to content

Commit a71d8bc

Browse files
authored
feat(rules): add ion-fab-attributes-renamed rule (#16)
1 parent 84a4fdd commit a71d8bc

File tree

4 files changed

+395
-5
lines changed

4 files changed

+395
-5
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,14 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
209209
</td>
210210
</tr>
211211
<tr>
212-
<td></td>
213-
<td>:white_large_square:</td>
212+
<td>:wrench:</td>
213+
<td>:white_check_mark:</td>
214214
<td>
215-
<code>ion-fab-attributes-are-renamed</code>
215+
<code>ion-fab-attributes-renamed</code>
216+
</td>
217+
<td>
218+
<a href="https://github.com/dwieeb">@dwieeb</a>
216219
</td>
217-
<td></td>
218220
</tr>
219221
<tr>
220222
<td></td>

src/ionFabAttributesRenamedRule.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)