Skip to content

Commit 0185437

Browse files
mhartingtonimhoffd
authored andcommitted
feat(rules): add ion-tabs-refactored rule (#93)
1 parent 2fb32c2 commit 0185437

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
5858
"ion-range-attributes-renamed": true,
5959
"ion-segment-button-ion-label-required": true,
6060
"ion-spinner-attribute-values-renamed": true,
61+
"ion-tabs-refactored": { "options": [true], "severity": "warning" },
6162
"ion-text-is-now-an-element": true
62-
6363
}
6464
}
6565
```
@@ -530,6 +530,20 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
530530
<a href="https://github.com/dwieeb">@dwieeb</a>
531531
</td>
532532
</tr>
533+
534+
<tr>
535+
<th>
536+
<a href="https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#tabs">Tabs</a>
537+
</th>
538+
<td></td>
539+
<td>:white_check_mark:</td>
540+
<td>
541+
<code>ion-tabs-refactored</code>
542+
</td>
543+
<td>
544+
<a href="https://github.com/mhartington">@mhartington</a>,
545+
</td>
546+
</tr>
533547
<tr>
534548
<th>
535549
<a href="https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#text--typography">Text / Typography</a>

src/ionTabsRefactoredRule.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { NgWalker } from 'codelyzer/angular/ngWalker';
2+
import * as Lint from 'tslint';
3+
import * as ts from 'typescript';
4+
5+
import * as ast from '@angular/compiler';
6+
import { BasicTemplateAstVisitor } from 'codelyzer/angular/templates/basicTemplateAstVisitor';
7+
import { MetadataReader } from 'codelyzer/angular/metadataReader';
8+
9+
export const ruleName = 'ion-tabs-refactored';
10+
11+
const TABS = 'ion-tabs';
12+
13+
const TemplateVisitor = createTabsRefactoredVisitorClass(TABS);
14+
15+
export class Rule extends Lint.Rules.AbstractRule {
16+
public static metadata: Lint.IRuleMetadata = {
17+
ruleName: ruleName,
18+
type: 'functionality',
19+
description: 'Tabs have been refacotred, please see this blog post: ',
20+
options: null,
21+
optionsDescription: 'Not configurable.',
22+
typescriptOnly: false,
23+
hasFix: false
24+
};
25+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
26+
return this.applyWithWalker(
27+
new NgWalker(sourceFile, this.getOptions(), {
28+
templateVisitorCtrl: TemplateVisitor
29+
})
30+
);
31+
}
32+
}
33+
34+
function generateErrorMessage() {
35+
return `Tabs have gone through a significant refactor.
36+
Please see https://github.com/ionic-team/ionic/blob/master/CHANGELOG.md#angular-tabs`;
37+
}
38+
39+
export function createTabsRefactoredVisitorClass(tabElement: string) {
40+
return class extends BasicTemplateAstVisitor {
41+
visitElement(element: ast.ElementAst, context: any): any {
42+
if (element.name === tabElement) this.checkElement(element);
43+
super.visitElement(element, context);
44+
}
45+
46+
private checkElement(element: ast.ElementAst) {
47+
const start = element.sourceSpan.start.offset + 1;
48+
const length = element.name.length;
49+
this.addFailureAt(start, length, generateErrorMessage());
50+
}
51+
};
52+
}

test/ionTabsRefactored.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ruleName } from '../src/ionTabsRefactoredRule';
2+
import { assertAnnotated } from './testHelper';
3+
describe(ruleName, () => {
4+
describe('failure', () => {
5+
it('should fail when ion-tabs are used', () => {
6+
let source = `
7+
@Component({
8+
template: \`
9+
<ion-tabs>
10+
~~~~~~~~
11+
12+
</ion-tabs>
13+
\`
14+
})
15+
class Bar{}
16+
`;
17+
assertAnnotated({
18+
ruleName,
19+
message: `Tabs have gone through a significant refactor.
20+
Please see https://github.com/ionic-team/ionic/blob/master/CHANGELOG.md#angular-tabs`,
21+
source
22+
});
23+
});
24+
});
25+
});

0 commit comments

Comments
 (0)