Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rules): add ion-icon-attribute-is-active-removed rule #30

Merged
merged 1 commit into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
});
});
});
});