Skip to content

Commit

Permalink
feat(rules): add ion-label-attributes-renamed rule (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored and cwoolum committed Jul 5, 2018
1 parent 0679c7b commit 0601a0c
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 3 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,14 @@ We are looking for contributors to help build these rules out! See [`CONTRIBUTIN
<th>
<a href="https://github.com/ionic-team/ionic/blob/master/angular/BREAKING.md#label">Label</a>
</th>
<td></td>
<td>:white_large_square:</td>
<td>:wrench:</td>
<td>:white_check_mark:</td>
<td>
<code>ion-label-attributes-renamed</code>
</td>
<td></td>
<td>
<a href="https://github.com/dwieeb">@dwieeb</a>
</td>
</tr>
<tr>
<th>
Expand Down
31 changes: 31 additions & 0 deletions src/ionLabelAttributesRenamedRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NgWalker } from 'codelyzer/angular/ngWalker';
import * as Lint from 'tslint';
import * as ts from 'typescript';

import { createAttributesRenamedTemplateVisitorClass } from './helpers/attributesRenamed';

export const ruleName = 'ion-label-attributes-renamed';

const replacementMap = new Map([['fixed', 'position="fixed"'], ['floating', 'position="floating"'], ['stacked', 'position="stacked"']]);

const IonButtonAttributesAreRenamedTemplateVisitor = createAttributesRenamedTemplateVisitorClass('ion-label', replacementMap);

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: 'Attributes of ion-label 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: IonButtonAttributesAreRenamedTemplateVisitor
})
);
}
}
181 changes: 181 additions & 0 deletions test/ionLabelAttributesRenamed.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { expect } from 'chai';
import { Replacement, Utils } from 'tslint';
import { ruleName } from '../src/ionLabelAttributesRenamedRule';
import { assertAnnotated, assertFailure, assertFailures, assertMultipleAnnotated, assertSuccess } from './testHelper';

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

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

assertAnnotated({
ruleName,
message: 'The fixed attribute of ion-label has been renamed. Use position="fixed" instead.',
source
});
});

it('should fail when floating is used', () => {
let source = `
@Component({
template: \`
<ion-label floating></ion-label>\`
~~~~~~~~
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'The floating attribute of ion-label has been renamed. Use position="floating" instead.',
source
});
});

it('should fail when stacked is used', () => {
let source = `
@Component({
template: \`
<ion-label stacked></ion-label>\`
~~~~~~~
})
class Bar{}
`;

assertAnnotated({
ruleName,
message: 'The stacked attribute of ion-label has been renamed. Use position="stacked" instead.',
source
});
});
});

describe('replacements', () => {
it('should replace fixed with position="fixed"', () => {
let source = `
@Component({
template: \`<ion-label fixed></ion-label>
\`
})
class Bar {}
`;

const fail = {
message: 'The fixed attribute of ion-label has been renamed. Use position="fixed" instead.',
startPosition: {
line: 2,
character: 32
},
endPosition: {
line: 2,
character: 37
}
};

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-label position="fixed"></ion-label>
\`
})
class Bar {}
`;

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

it('should replace floating with position="floating"', () => {
let source = `
@Component({
template: \`<ion-label floating></ion-label>
\`
})
class Bar {}
`;

const fail = {
message: 'The floating attribute of ion-label has been renamed. Use position="floating" instead.',
startPosition: {
line: 2,
character: 32
},
endPosition: {
line: 2,
character: 40
}
};

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-label position="floating"></ion-label>
\`
})
class Bar {}
`;

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

it('should replace stacked with position="stacked"', () => {
let source = `
@Component({
template: \`<ion-label stacked></ion-label>
\`
})
class Bar {}
`;

const fail = {
message: 'The stacked attribute of ion-label has been renamed. Use position="stacked" instead.',
startPosition: {
line: 2,
character: 32
},
endPosition: {
line: 2,
character: 39
}
};

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-label position="stacked"></ion-label>
\`
})
class Bar {}
`;

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

0 comments on commit 0601a0c

Please sign in to comment.