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-fab-fixed-content rule (#29)
- Loading branch information
Showing
3 changed files
with
183 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,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'; | ||
|
||
export const ruleName = 'ion-fab-fixed-content'; | ||
|
||
class TemplateVisitor extends BasicTemplateAstVisitor { | ||
visitElement(element: ast.ElementAst, context: any): any { | ||
if (element.name && element.name === 'ion-fab') { | ||
const attributeFound = element.attrs.find(attr => attr.name === 'slot'); | ||
|
||
if (!attributeFound || attributeFound.value !== 'fixed') { | ||
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-fab container is no longer fixed by default. Use slot="fixed".', [ | ||
Lint.Replacement.replaceFromTo(position, position, ' slot="fixed"') | ||
]); | ||
} | ||
} | ||
|
||
super.visitElement(element, context); | ||
} | ||
} | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: ruleName, | ||
type: 'functionality', | ||
description: 'The ion-fab container is no longer fixed by default. Use slot="fixed".', | ||
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,130 @@ | ||
import { expect } from 'chai'; | ||
import { Replacement, Utils } from 'tslint'; | ||
import { ruleName } from '../src/ionFabFixedContentRule'; | ||
import { assertAnnotated, assertFailure, assertFailures, assertMultipleAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe(ruleName, () => { | ||
describe('success', () => { | ||
it('should work with proper style', () => { | ||
let source = ` | ||
@Component({ | ||
template: \`<ion-fab slot="fixed"></ion-fab>\` | ||
}) | ||
class Bar{} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail with no attributes', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-fab></ion-fab>\` | ||
~~~~~~~ | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".', | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail without slot attribute', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-fab vertical="top" horizontal="end"></ion-fab>\` | ||
~~~~~~~ | ||
}) | ||
class Bar{} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".', | ||
source | ||
}); | ||
}); | ||
}); | ||
|
||
describe('replacements', () => { | ||
it('should add slot="fixed" to ion-fab without attributes', () => { | ||
let source = ` | ||
@Component({ | ||
template: \`<ion-fab></ion-fab> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
const fail = { | ||
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".', | ||
startPosition: { | ||
line: 2, | ||
character: 22 | ||
}, | ||
endPosition: { | ||
line: 2, | ||
character: 29 | ||
} | ||
}; | ||
|
||
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-fab slot="fixed"></ion-fab> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
expect(res).to.eq(expected); | ||
}); | ||
|
||
it('should add slot="fixed" to ion-radio without slot', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-fab vertical="top" horizontal="end"></ion-fab> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
const fail = { | ||
message: 'The ion-fab container is no longer fixed by default. Use slot="fixed".', | ||
startPosition: { | ||
line: 3, | ||
character: 13 | ||
}, | ||
endPosition: { | ||
line: 3, | ||
character: 20 | ||
} | ||
}; | ||
|
||
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-fab slot="fixed" vertical="top" horizontal="end"></ion-fab> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
expect(res).to.eq(expected); | ||
}); | ||
}); | ||
}); |