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.
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
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
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,44 @@ | ||
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 ts from 'typescript'; | ||
|
||
export const ruleName = 'ion-chip-markup-changed'; | ||
|
||
class IonChipMarkupChangedTemplateVisitor extends BasicTemplateAstVisitor { | ||
visitElement(element: ast.ElementAst, context: any): any { | ||
if (element.name && element.name === 'ion-chip') { | ||
element.children.forEach(child => { | ||
if (child instanceof ast.ElementAst && child.name === 'ion-button') { | ||
const start = child.sourceSpan.start.offset; | ||
this.addFailure( | ||
this.createFailure(start + 1, child.name.length, 'Inside of ion-chip, ion-chip-button must be used instead of ion-button.') | ||
); | ||
} | ||
}); | ||
} | ||
|
||
super.visitElement(element, context); | ||
} | ||
} | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: ruleName, | ||
type: 'functionality', | ||
description: 'Buttons in Ion Chip 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: IonChipMarkupChangedTemplateVisitor | ||
}) | ||
); | ||
} | ||
} |
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,47 @@ | ||
import { ruleName } from '../src/ionChipMarkupChangedRule'; | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe(ruleName, () => { | ||
describe('success', () => { | ||
it('should work when ion-chip-button is used', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-chip> | ||
<ion-label>Default</ion-label> | ||
<ion-chip-button fill="clear" color="light"> | ||
<ion-icon name="close-circle"></ion-icon> | ||
</ion-chip-button> | ||
</ion-chip> | ||
\` | ||
}) | ||
class Bar{} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail when ion-button is used', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-chip> | ||
<ion-label>Default</ion-label> | ||
<ion-button clear color="light"> | ||
~~~~~~~~~~ | ||
<ion-icon name="close-circle"></ion-icon> | ||
</ion-button> | ||
</ion-chip>\` | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'Inside of ion-chip, ion-chip-button must be used instead of ion-button.', | ||
source | ||
}); | ||
}); | ||
}); | ||
}); |