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-col-attributes-renamed rule (#33)
- Loading branch information
Showing
3 changed files
with
200 additions
and
4 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 { NgWalker } from 'codelyzer/angular/ngWalker'; | ||
import * as Lint from 'tslint'; | ||
import * as ts from 'typescript'; | ||
|
||
import { createAttributesRenamedTemplateVisitorClass } from './helpers/attributesRenamed'; | ||
|
||
export const ruleName = 'ion-col-attributes-renamed'; | ||
|
||
const formatOldAttr = (prefix: string, breakpoint: string | undefined, value: string) => | ||
`${prefix}${typeof breakpoint === 'undefined' ? '' : `-${breakpoint}`}-${value}`; | ||
const formatNewAttr = (prefix: string, breakpoint: string | undefined, value: string) => | ||
`${prefix}${typeof breakpoint === 'undefined' ? '' : `-${breakpoint}`}="${value}"`; | ||
|
||
const attrPrefixMap = new Map([['col', 'size'], ['offset', 'offset'], ['push', 'push'], ['pull', 'pull']]); | ||
const breakpoints = [undefined, 'xs', 'sm', 'md', 'lg', 'xl']; | ||
const values = Array(12) | ||
.fill(undefined) | ||
.map((v, i) => String(i + 1)); | ||
|
||
const replacementPairs: [string, string][] = [].concat( | ||
...[...attrPrefixMap.entries()].map(([oldPrefix, newPrefix]) => | ||
[].concat(...breakpoints.map(b => values.map(v => [formatOldAttr(oldPrefix, b, v), formatNewAttr(newPrefix, b, v)]))) | ||
) | ||
); | ||
|
||
const replacementMap = new Map(replacementPairs); | ||
|
||
const IonGridAttributesRenamedTemplateVisitor = createAttributesRenamedTemplateVisitorClass('ion-col', replacementMap); | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: ruleName, | ||
type: 'functionality', | ||
description: 'Attributes of ion-col 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: IonGridAttributesRenamedTemplateVisitor | ||
}) | ||
); | ||
} | ||
} |
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,146 @@ | ||
import { expect } from 'chai'; | ||
import { Replacement, Utils } from 'tslint'; | ||
import { ruleName } from '../src/ionColAttributesRenamedRule'; | ||
import { assertAnnotated, assertFailure, assertFailures, assertMultipleAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe(ruleName, () => { | ||
describe('success', () => { | ||
it('should work with proper size attribute', () => { | ||
let source = ` | ||
@Component({ | ||
template: \`<ion-col size="3"></ion-col>\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
|
||
it('should work with proper offset attribute', () => { | ||
let source = ` | ||
@Component({ | ||
template: \`<ion-col offset="3"></ion-col>\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail when col-3 is used', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-col col-3> | ||
~~~~~ | ||
</ion-col> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'The col-3 attribute of ion-col has been renamed. Use size="3" instead.', | ||
source | ||
}); | ||
}); | ||
|
||
it('should fail when col-xs-3 is used', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-col col-xs-3> | ||
~~~~~~~~ | ||
</ion-col> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
assertAnnotated({ | ||
ruleName, | ||
message: 'The col-xs-3 attribute of ion-col has been renamed. Use size-xs="3" instead.', | ||
source | ||
}); | ||
}); | ||
}); | ||
|
||
describe('replacements', () => { | ||
it('should replace col-3 with size="3"', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-col col-3></ion-col> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
const fail = { | ||
message: 'The col-3 attribute of ion-col has been renamed. Use size="3" instead.', | ||
startPosition: { | ||
line: 3, | ||
character: 21 | ||
}, | ||
endPosition: { | ||
line: 3, | ||
character: 26 | ||
} | ||
}; | ||
|
||
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-col size="3"></ion-col> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
expect(res).to.eq(expected); | ||
}); | ||
|
||
it('should replace col-xs-3 with size-xs="3"', () => { | ||
let source = ` | ||
@Component({ | ||
template: \` | ||
<ion-col col-xs-3></ion-col> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
const fail = { | ||
message: 'The col-xs-3 attribute of ion-col has been renamed. Use size-xs="3" instead.', | ||
startPosition: { | ||
line: 3, | ||
character: 21 | ||
}, | ||
endPosition: { | ||
line: 3, | ||
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-col size-xs="3"></ion-col> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
|
||
expect(res).to.eq(expected); | ||
}); | ||
}); | ||
}); |