Skip to content

Commit

Permalink
feat(rule): add new ion-chip rule
Browse files Browse the repository at this point in the history
  • Loading branch information
cwoolum committed May 31, 2018
1 parent 83e8491 commit 6fc7d7d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Rules without an **author** and without green checkmarks need some help! See [#c
| `ion-back-button-is-no-longer-added-to-navigation-bar` | [#back-button](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#back-button) | :black_square_button: tested<br> :black_square_button: fixable | |
| `ion-button-is-now-an-element` | [#button](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#button) | :white_check_mark: tested<br> :black_square_button: fixable | [@cwoolum](https://github.com/cwoolum/) |
| `ion-button-attributes-are-renamed` | [#attributes-renamed](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#attributes-renamed) | :white_check_mark: tested<br> :black_square_button: fixable | [@cwoolum](https://github.com/cwoolum/) |
| `ion-chip-markup-has-changed` | [#chip](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#chip) | :black_square_button: tested<br> :black_square_button: fixable | |
| `ion-chip-markup-has-changed` | [#chip](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#chip) | :white_check_mark: tested<br> :black_square_button: fixable | [@cwoolum](https://github.com/cwoolum/) |
| `ion-datetime-class-has-been-renamed` | [#datetime](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#datetime) | :black_square_button: tested<br> :black_square_button: fixable | |
| `ion-fab-markup-has-changed` | [#fab-markup-changed](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#markup-changed-2) | :black_square_button: tested<br> :black_square_button: fixable | |
| `ion-fab-attributes-have-changed` | [#fab-attributes-renamed](https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#attributes-renamed-1) | :black_square_button: tested<br> :black_square_button: fixable | |
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { Rule as ActionSheetTitleAndSubtitleAreNowHeaderAndSubheader } from './i
export { Rule as AlertTitleAndSubtitleAreNowHeaderAndSubheader } from './ionAlertTitleAndSubtitleAreNowHeaderAndSubHeaderRule';
export { Rule as IonButtonIsNowAnElementRule } from './ionButtonIsNowAnElementRule';
export { Rule as IonButtonAttributesAreRenamedRule } from './ionButtonAttributesAreRenamedRule';
export { Rule as IonChipMarkupChangedRule } from './ionChipMarkupChangedRule';

export * from 'codelyzer/angular/config';

Expand Down
44 changes: 44 additions & 0 deletions src/ionChipMarkupChangedRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 ts from 'typescript';

export const ruleName = 'ion-chip-markup-changed';

class IonChipMarkupChangedTemplateVisitor extends BasicTemplateAstVisitor {
visitElement(element: ast.ElementAst, context: any): any {
if (element.name && element.name === 'ion-chip') {
element.children.forEach(child => {
if (child instanceof ast.ElementAst && child.name === 'ion-button') {
const start = child.sourceSpan.start.offset;
this.addFailure(
this.createFailure(start + 1, child.name.length, 'Inside of ion-chip, ion-chip-button must be used instead of ion-button.')
);
}
});
}

super.visitElement(element, context);
}
}

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: 'Buttons in Ion Chip have been renamed.',
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: IonChipMarkupChangedTemplateVisitor
})
);
}
}
47 changes: 47 additions & 0 deletions test/IonChipMarkupChanged.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ruleName } from '../src/ionChipMarkupChangedRule';
import { assertAnnotated, assertSuccess } from './testHelper';

describe(ruleName, () => {
describe('success', () => {
it('should work when ion-chip-button is used', () => {
let source = `
@Component({
template: \`
<ion-chip>
<ion-label>Default</ion-label>
<ion-chip-button fill="clear" color="light">
<ion-icon name="close-circle"></ion-icon>
</ion-chip-button>
</ion-chip>
\`
})
class Bar{}
`;
assertSuccess(ruleName, source);
});
});

describe('failure', () => {
it('should fail when ion-button is used', () => {
let source = `
@Component({
template: \`
<ion-chip>
<ion-label>Default</ion-label>
<ion-button clear color="light">
~~~~~~~~~~
<ion-icon name="close-circle"></ion-icon>
</ion-button>
</ion-chip>\`
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'Inside of ion-chip, ion-chip-button must be used instead of ion-button.',
source
});
});
});
});

0 comments on commit 6fc7d7d

Please sign in to comment.