Skip to content

Commit

Permalink
feat(rules): add ion-item-is-now-an-element rule (#10)
Browse files Browse the repository at this point in the history
* feat(rules): add ion-item-is-now-an-element rule

* docs(readme): check off ion-item-is-now-an-element rule
  • Loading branch information
imhoffd committed Jun 27, 2018
1 parent ec428a0 commit 846a37e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ We are looking for contributors to help build these rules out! We have not relea
</tr>
<tr>
<th rowspan="4"><a href="https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#item">Item</a></th>
<td>:black_square_button: <code>ion-item-is-now-an-element</code></td>
<td></td>
<td>:white_check_mark: <code>ion-item-is-now-an-element</code></td>
<td><a href="https://github.com/dwieeb">@dwieeb</a></td>
</tr>
<tr>
<td>:black_square_button: <code>ion-item-ion-label-is-now-required</code></td>
Expand Down
36 changes: 36 additions & 0 deletions src/ionItemIsNowAnElementRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NgWalker } from 'codelyzer/angular/ngWalker';
import * as Lint from 'tslint';
import { IOptions } from 'tslint';
import * as ts from 'typescript';

import { createDirectiveToElementTemplateVisitorClass, generateDescription } from './helpers/directiveToElement';

export const ruleName = 'ion-item-is-now-an-element';
const directive = 'ion-item';

const IonItemIsNowAnElementTemplateVisitor = createDirectiveToElementTemplateVisitorClass(directive);

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: generateDescription(directive),
options: null,
optionsDescription: 'Not configurable.',
typescriptOnly: false,
hasFix: true
};

constructor(options: IOptions) {
options.ruleSeverity = 'error';
super(options);
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new NgWalker(sourceFile, this.getOptions(), {
templateVisitorCtrl: IonItemIsNowAnElementTemplateVisitor
})
);
}
}
71 changes: 71 additions & 0 deletions test/ionItemIsNowAnElement.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ruleName } from '../src/ionItemIsNowAnElementRule';
import { assertAnnotated, assertSuccess } from './testHelper';

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

describe('failure', () => {
it('should fail when ion-item attribute is used on button', () => {
let source = `
@Component({
template: \`
<button ion-item></button>\`
~~~~~~~~
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'ion-item is now an Element instead of an Angular Directive.',
source
});
});

it('should fail when ion-item attribute is used on anchor', () => {
let source = `
@Component({
template: \`
<a ion-item (click)="doSomething()"></a>\`
~~~~~~~~
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'ion-item is now an Element instead of an Angular Directive.',
source
});
});

it('should fail when ion-item attribute is used with multiline', () => {
let source = `
@Component({
template: \`
<a
ion-item
~~~~~~~~
(click)="doSomething()">Click Me</a>\`
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'ion-item is now an Element instead of an Angular Directive.',
source
});
});
});
});

0 comments on commit 846a37e

Please sign in to comment.