Skip to content

Commit

Permalink
feat(rules): add ion-icon-attribute-is-active-removed rule (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored and cwoolum committed Jul 5, 2018
1 parent 01cade5 commit 0679c7b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,13 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
<a href="https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#icon">Icon</a>
</th>
<td></td>
<td>:white_large_square:</td>
<td>:white_check_mark:</td>
<td>
<code>ion-icon-property-is-active-is-removed</code>
<code>ion-icon-attribute-is-active-removed</code>
</td>
<td>
<a href="https://github.com/dwieeb">@dwieeb</a>
</td>
<td></td>
</tr>
<tr>
<th rowspan="3">
Expand Down
45 changes: 45 additions & 0 deletions src/ionIconAttributeIsActiveRemovedRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 * as tsutils from 'tsutils';
import * as ts from 'typescript';

export const ruleName = 'ion-icon-attribute-is-active-removed';

class TemplateVisitor extends BasicTemplateAstVisitor {
visitElement(element: ast.ElementAst, context: any): any {
if (element.name && element.name === 'ion-icon') {
const attributeFound = element.attrs.find(attr => attr.name === 'isActive');

if (attributeFound) {
const start = attributeFound.sourceSpan.start.offset;
const end = attributeFound.sourceSpan.end.offset;

this.addFailureAt(start, end - start, 'The isActive attribute of ion-icon has been removed.');
}
}

super.visitElement(element, context);
}
}

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: 'The isActive attribute of ion-icon has been removed.',
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: TemplateVisitor
})
);
}
}
37 changes: 37 additions & 0 deletions test/ionIconAttributeIsActiveRemoved.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect } from 'chai';
import { Replacement, Utils } from 'tslint';
import { ruleName } from '../src/ionIconAttributeIsActiveRemovedRule';
import { assertAnnotated, assertFailure, assertFailures, assertMultipleAnnotated, assertSuccess } from './testHelper';

describe(ruleName, () => {
describe('success', () => {
it('should work with proper style', () => {
let source = `
@Component({
template: \`<ion-icon></ion-icon>\`
})
class Bar{}
`;
assertSuccess(ruleName, source);
});
});

describe('failure', () => {
it('should fail with isActive attribute', () => {
let source = `
@Component({
template: \`
<ion-icon isActive="true"></ion-icon>\`
~~~~~~~~~~~~~~~
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'The isActive attribute of ion-icon has been removed.',
source
});
});
});
});

0 comments on commit 0679c7b

Please sign in to comment.