Skip to content

Commit

Permalink
feat(rules): add ion-item-ion-label-required rule (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored and cwoolum committed Jul 5, 2018
1 parent c4db1f8 commit 098bd7f
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 8 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,13 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
</tr>
<tr>
<td></td>
<td>:white_large_square:</td>
<td>:white_check_mark:</td>
<td>
<code>ion-item-ion-label-is-now-required</code>
<code>ion-item-ion-label-required</code>
</td>
<td>
<a href="https://github.com/dwieeb">@dwieeb</a>
</td>
<td></td>
</tr>
<tr>
<td></td>
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as ast from '@angular/compiler';

export function isElementAst(node: ast.TemplateAst): node is ast.ElementAst {
const n = node as ast.ElementAst;
return n && typeof n.children === 'object' && typeof n.name === 'string' && typeof n.attrs === 'object';
}
7 changes: 2 additions & 5 deletions src/ionBackButtonNotAddedByDefaultRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import * as Lint from 'tslint';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';

export const ruleName = 'ion-back-button-not-added-by-default';
import { isElementAst } from './helpers/utils';

function isElementAst(node: ast.TemplateAst): node is ast.ElementAst {
const n = node as ast.ElementAst;
return n && typeof n.children === 'object' && typeof n.name === 'string' && typeof n.attrs === 'object';
}
export const ruleName = 'ion-back-button-not-added-by-default';

class TemplateVisitor extends BasicTemplateAstVisitor {
visitElement(element: ast.ElementAst, context: any): any {
Expand Down
48 changes: 48 additions & 0 deletions src/ionItemIonLabelRequiredRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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';

import { isElementAst } from './helpers/utils';

export const ruleName = 'ion-item-ion-label-required';

class TemplateVisitor extends BasicTemplateAstVisitor {
visitElement(element: ast.ElementAst, context: any): any {
if (element.name && element.name === 'ion-item') {
const ionLabelElement = element.children.find((e): e is ast.ElementAst => isElementAst(e) && e.name === 'ion-label');

if (!ionLabelElement) {
const start = element.sourceSpan.start.offset;
const length = element.name.length;
const position = this.getSourcePosition(start) + length + 1;

this.addFailureAt(start + 1, length, 'The ion-item requires an ion-label component. It is no longer automatically added.');
}
}

super.visitElement(element, context);
}
}

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: 'The ion-item requires an ion-label component. It is no longer automatically added.',
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
})
);
}
}
80 changes: 80 additions & 0 deletions test/ionItemIonLabelRequired.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ruleName } from '../src/ionItemIonLabelRequiredRule';
import { assertAnnotated, assertSuccess } from './testHelper';

describe(ruleName, () => {
describe('success', () => {
it('should work with ion-label child', () => {
let source = `
@Component({
template: \`
<ion-item>
<ion-thumbnail slot="start">
<img src="http://pngimg.com/upload/dog_png2402.png">
</ion-thumbnail>
<ion-label>Dog</ion-label>
</ion-item>
\`
})
class Bar{}
`;
assertSuccess(ruleName, source);
});

it('should work with single ion-label child', () => {
let source = `
@Component({
template: \`
<ion-item>
<ion-label>Dog</ion-label>
</ion-item>
\`
})
class Bar{}
`;
assertSuccess(ruleName, source);
});
});

describe('failure', () => {
it('should fail when ion-label missing', () => {
let source = `
@Component({
template: \`
<ion-item>
~~~~~~~~
<ion-thumbnail slot="start">
<img src="http://pngimg.com/upload/dog_png2402.png">
</ion-thumbnail>
Dog
</ion-toolbar>
\`
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'The ion-item requires an ion-label component. It is no longer automatically added.',
source
});
});

it('should fail with only text', () => {
let source = `
@Component({
template: \`
<ion-item>Dog</ion-item>
~~~~~~~~
\`
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'The ion-item requires an ion-label component. It is no longer automatically added.',
source
});
});
});
});

0 comments on commit 098bd7f

Please sign in to comment.