forked from cwoolum/ionicV4-tslint
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add ion-label-attributes-renamed rule (#28)
- Loading branch information
Showing
3 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |