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-item-is-now-an-element rule (#10)
* feat(rules): add ion-item-is-now-an-element rule * docs(readme): check off ion-item-is-now-an-element rule
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 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,36 @@ | ||
import { NgWalker } from 'codelyzer/angular/ngWalker'; | ||
import * as Lint from 'tslint'; | ||
import { IOptions } from 'tslint'; | ||
import * as ts from 'typescript'; | ||
|
||
import { createDirectiveToElementTemplateVisitorClass, generateDescription } from './helpers/directiveToElement'; | ||
|
||
export const ruleName = 'ion-item-is-now-an-element'; | ||
const directive = 'ion-item'; | ||
|
||
const IonItemIsNowAnElementTemplateVisitor = createDirectiveToElementTemplateVisitorClass(directive); | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: ruleName, | ||
type: 'functionality', | ||
description: generateDescription(directive), | ||
options: null, | ||
optionsDescription: 'Not configurable.', | ||
typescriptOnly: false, | ||
hasFix: true | ||
}; | ||
|
||
constructor(options: IOptions) { | ||
options.ruleSeverity = 'error'; | ||
super(options); | ||
} | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker( | ||
new NgWalker(sourceFile, this.getOptions(), { | ||
templateVisitorCtrl: IonItemIsNowAnElementTemplateVisitor | ||
}) | ||
); | ||
} | ||
} |
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,71 @@ | ||
import { ruleName } from '../src/ionItemIsNowAnElementRule'; | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe(ruleName, () => { | ||
describe('success', () => { | ||
it('should work with proper style', () => { | ||
let source = ` | ||
@Component({ | ||
template: \`<ion-item><ion-item>\` | ||
}) | ||
class Bar{} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail when ion-item attribute is used on button', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<button ion-item></button>\` | ||
~~~~~~~~ | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'ion-item is now an Element instead of an Angular Directive.', | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail when ion-item attribute is used on anchor', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<a ion-item (click)="doSomething()"></a>\` | ||
~~~~~~~~ | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'ion-item is now an Element instead of an Angular Directive.', | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail when ion-item attribute is used with multiline', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<a | ||
ion-item | ||
~~~~~~~~ | ||
(click)="doSomething()">Click Me</a>\` | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'ion-item is now an Element instead of an Angular Directive.', | ||
source | ||
}); | ||
}); | ||
}); | ||
}); |