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-ion-label-required rule (#32)
- Loading branch information
Showing
5 changed files
with
141 additions
and
8 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,6 @@ | ||
import * as ast from '@angular/compiler'; | ||
|
||
export function isElementAst(node: ast.TemplateAst): node is ast.ElementAst { | ||
const n = node as ast.ElementAst; | ||
return n && typeof n.children === 'object' && typeof n.name === 'string' && typeof n.attrs === 'object'; | ||
} |
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,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'; | ||
|
||
import { isElementAst } from './helpers/utils'; | ||
|
||
export const ruleName = 'ion-item-ion-label-required'; | ||
|
||
class TemplateVisitor extends BasicTemplateAstVisitor { | ||
visitElement(element: ast.ElementAst, context: any): any { | ||
if (element.name && element.name === 'ion-item') { | ||
const ionLabelElement = element.children.find((e): e is ast.ElementAst => isElementAst(e) && e.name === 'ion-label'); | ||
|
||
if (!ionLabelElement) { | ||
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-item requires an ion-label component. It is no longer automatically added.'); | ||
} | ||
} | ||
|
||
super.visitElement(element, context); | ||
} | ||
} | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: ruleName, | ||
type: 'functionality', | ||
description: 'The ion-item requires an ion-label component. It is no longer automatically added.', | ||
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 | ||
}) | ||
); | ||
} | ||
} |
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,80 @@ | ||
import { ruleName } from '../src/ionItemIonLabelRequiredRule'; | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe(ruleName, () => { | ||
describe('success', () => { | ||
it('should work with ion-label child', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-item> | ||
<ion-thumbnail slot="start"> | ||
<img src="http://pngimg.com/upload/dog_png2402.png"> | ||
</ion-thumbnail> | ||
<ion-label>Dog</ion-label> | ||
</ion-item> | ||
\` | ||
}) | ||
class Bar{} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
|
||
it('should work with single ion-label child', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-item> | ||
<ion-label>Dog</ion-label> | ||
</ion-item> | ||
\` | ||
}) | ||
class Bar{} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail when ion-label missing', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-item> | ||
~~~~~~~~ | ||
<ion-thumbnail slot="start"> | ||
<img src="http://pngimg.com/upload/dog_png2402.png"> | ||
</ion-thumbnail> | ||
Dog | ||
</ion-toolbar> | ||
\` | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'The ion-item requires an ion-label component. It is no longer automatically added.', | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail with only text', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-item>Dog</ion-item> | ||
~~~~~~~~ | ||
\` | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'The ion-item requires an ion-label component. It is no longer automatically added.', | ||
source | ||
}); | ||
}); | ||
}); | ||
}); |