Skip to content

Commit

Permalink
feat(rules): add ion-fab-fixed-content rule (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored and cwoolum committed Jul 5, 2018
1 parent 0601a0c commit 7fbff99
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 3 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
</td>
</tr>
<tr>
<td></td>
<td>:white_large_square:</td>
<td>:wrench:</td>
<td>:white_check_mark:</td>
<td>
<code>ion-fab-fixed-content</code>
</td>
<td></td>
<td>
<a href="https://github.com/dwieeb">@dwieeb</a>
</td>
</tr>
<tr>
<th>
Expand Down
48 changes: 48 additions & 0 deletions src/ionFabFixedContentRule.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';

export const ruleName = 'ion-fab-fixed-content';

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

if (!attributeFound || attributeFound.value !== 'fixed') {
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-fab container is no longer fixed by default. Use slot="fixed".', [
Lint.Replacement.replaceFromTo(position, position, ' slot="fixed"')
]);
}
}

super.visitElement(element, context);
}
}

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: 'The ion-fab container is no longer fixed by default. Use slot="fixed".',
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
})
);
}
}
130 changes: 130 additions & 0 deletions test/ionFabFixedContent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { expect } from 'chai';
import { Replacement, Utils } from 'tslint';
import { ruleName } from '../src/ionFabFixedContentRule';
import { assertAnnotated, assertFailure, assertFailures, assertMultipleAnnotated, assertSuccess } from './testHelper';

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

describe('failure', () => {
it('should fail with no attributes', () => {
let source = `
@Component({
template: \`
<ion-fab></ion-fab>\`
~~~~~~~
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".',
source
});
});

it('should fail without slot attribute', () => {
let source = `
@Component({
template: \`
<ion-fab vertical="top" horizontal="end"></ion-fab>\`
~~~~~~~
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".',
source
});
});
});

describe('replacements', () => {
it('should add slot="fixed" to ion-fab without attributes', () => {
let source = `
@Component({
template: \`<ion-fab></ion-fab>
\`
})
class Bar {}
`;

const fail = {
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".',
startPosition: {
line: 2,
character: 22
},
endPosition: {
line: 2,
character: 29
}
};

const failures = assertFailure(ruleName, source, fail);
const fixes = failures.map(f => f.getFix());
const res = Replacement.applyAll(source, Utils.flatMap(fixes, Utils.arrayify));

let expected = `
@Component({
template: \`<ion-fab slot="fixed"></ion-fab>
\`
})
class Bar {}
`;

expect(res).to.eq(expected);
});

it('should add slot="fixed" to ion-radio without slot', () => {
let source = `
@Component({
template: \`
<ion-fab vertical="top" horizontal="end"></ion-fab>
\`
})
class Bar {}
`;

const fail = {
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".',
startPosition: {
line: 3,
character: 13
},
endPosition: {
line: 3,
character: 20
}
};

const failures = assertFailure(ruleName, source, fail);
const fixes = failures.map(f => f.getFix());
const res = Replacement.applyAll(source, Utils.flatMap(fixes, Utils.arrayify));

let expected = `
@Component({
template: \`
<ion-fab slot="fixed" vertical="top" horizontal="end"></ion-fab>
\`
})
class Bar {}
`;

expect(res).to.eq(expected);
});
});
});

0 comments on commit 7fbff99

Please sign in to comment.