-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics): add v9 migration rules for icon and calendar (#4467)
- Loading branch information
Showing
7 changed files
with
324 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
85 changes: 85 additions & 0 deletions
85
schematics/ng-update/test-cases/v9/input-names-calendar.spec.ts
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,85 @@ | ||
import { getSystemPath, normalize, virtualFs } from '@angular-devkit/core'; | ||
import { TempScopedNodeJsSyncHost } from '@angular-devkit/core/node/testing'; | ||
import { HostTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import * as shx from 'shelljs'; | ||
|
||
describe('calendar migration', () => { | ||
let runner: SchematicTestRunner; | ||
let host: TempScopedNodeJsSyncHost; | ||
let tree: UnitTestTree; | ||
let tmpDirPath: string; | ||
let previousWorkingDir: string; | ||
let warnOutput: string[]; | ||
let errorOutput: string[]; | ||
|
||
beforeEach(() => { | ||
runner = new SchematicTestRunner('test', require.resolve('../../../migration.json')); | ||
host = new TempScopedNodeJsSyncHost(); | ||
tree = new UnitTestTree(new HostTree(host)); | ||
|
||
writeFile('/tsconfig.json', JSON.stringify({ | ||
compilerOptions: { | ||
experimentalDecorators: true, | ||
lib: ['es2015'] | ||
} | ||
})); | ||
writeFile('/angular.json', JSON.stringify({ | ||
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}} | ||
})); | ||
|
||
warnOutput = []; | ||
errorOutput = []; | ||
runner.logger.subscribe(logEntry => { | ||
if (logEntry.level === 'warn') { | ||
warnOutput.push(logEntry.message); | ||
} else if (logEntry.level === 'error') { | ||
errorOutput.push(logEntry.message); | ||
} | ||
}); | ||
|
||
previousWorkingDir = shx.pwd(); | ||
tmpDirPath = getSystemPath(host.root); | ||
|
||
shx.cd(tmpDirPath); | ||
|
||
writeFakeAngular(); | ||
}); | ||
|
||
afterEach(() => { | ||
shx.cd(previousWorkingDir); | ||
shx.rm('-r', tmpDirPath); | ||
}); | ||
|
||
function writeFakeAngular(): void { writeFile('/node_modules/@angular/core/index.d.ts', ``); } | ||
|
||
function writeFile(filePath: string, contents: string): void { | ||
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents)); | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
async function runMigration(): Promise<any> { | ||
await runner.runSchematicAsync('migration-v9', {}, tree).toPromise(); | ||
} | ||
|
||
describe('Calendar', () => { | ||
|
||
it('should properly report invalid deprecated input', async() => { | ||
writeFile('/index.ts', `; | ||
import {Component} from '@angular/core' | ||
@Component({ | ||
template: \` | ||
<nz-calendar nzCard></nz-calendar> | ||
\` | ||
}) | ||
export class MyComp { | ||
}`); | ||
await runMigration(); | ||
|
||
expect(warnOutput).toContain( 'index.ts@5:24 - Found deprecated input "nzCard" component. ' + | ||
'Use "nzFullscreen" to instead please.'); | ||
}); | ||
|
||
}); | ||
|
||
}); |
103 changes: 103 additions & 0 deletions
103
schematics/ng-update/test-cases/v9/input-names-icon.spec.ts
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,103 @@ | ||
import { getSystemPath, normalize, virtualFs } from '@angular-devkit/core'; | ||
import { TempScopedNodeJsSyncHost } from '@angular-devkit/core/node/testing'; | ||
import { HostTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import * as shx from 'shelljs'; | ||
|
||
describe('icon migration', () => { | ||
let runner: SchematicTestRunner; | ||
let host: TempScopedNodeJsSyncHost; | ||
let tree: UnitTestTree; | ||
let tmpDirPath: string; | ||
let previousWorkingDir: string; | ||
let warnOutput: string[]; | ||
let errorOutput: string[]; | ||
|
||
beforeEach(() => { | ||
runner = new SchematicTestRunner('test', require.resolve('../../../migration.json')); | ||
host = new TempScopedNodeJsSyncHost(); | ||
tree = new UnitTestTree(new HostTree(host)); | ||
|
||
writeFile('/tsconfig.json', JSON.stringify({ | ||
compilerOptions: { | ||
experimentalDecorators: true, | ||
lib: ['es2015'] | ||
} | ||
})); | ||
writeFile('/angular.json', JSON.stringify({ | ||
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}} | ||
})); | ||
|
||
warnOutput = []; | ||
errorOutput = []; | ||
runner.logger.subscribe(logEntry => { | ||
if (logEntry.level === 'warn') { | ||
warnOutput.push(logEntry.message); | ||
} else if (logEntry.level === 'error') { | ||
errorOutput.push(logEntry.message); | ||
} | ||
}); | ||
|
||
previousWorkingDir = shx.pwd(); | ||
tmpDirPath = getSystemPath(host.root); | ||
|
||
shx.cd(tmpDirPath); | ||
|
||
writeFakeAngular(); | ||
}); | ||
|
||
afterEach(() => { | ||
shx.cd(previousWorkingDir); | ||
shx.rm('-r', tmpDirPath); | ||
}); | ||
|
||
function writeFakeAngular(): void { writeFile('/node_modules/@angular/core/index.d.ts', ``); } | ||
|
||
function writeFile(filePath: string, contents: string): void { | ||
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents)); | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
async function runMigration(): Promise<any> { | ||
await runner.runSchematicAsync('migration-v9', {}, tree).toPromise(); | ||
} | ||
|
||
describe('Icon', () => { | ||
|
||
it('should rename deprecated input names', async() => { | ||
writeFile('/index.ts', ` | ||
import {Component} from '@angular/core'; | ||
@Component({template: \` | ||
<i nz-icon [iconfont]="'value'" [spin]="true" [type]="'play'" theme="o"></i> | ||
\`}) | ||
export class MyComp { | ||
} | ||
`); | ||
|
||
await runMigration(); | ||
const content = tree.readContent('/index.ts'); | ||
expect(content).toContain(`[nzIconfont]="'value'"`); | ||
expect(content).toContain(`[nzSpin]="true"`); | ||
expect(content).toContain(`[nzType]="'play'"`); | ||
expect(content).toContain(`nzTheme="o"`); | ||
}); | ||
|
||
it('should properly report invalid deprecated css selector', async() => { | ||
writeFile('/index.ts', `; | ||
import {Component} from '@angular/core' | ||
@Component({ | ||
template: \` | ||
<i class="anticon play"></i> | ||
\` | ||
}) | ||
export class MyComp { | ||
}`); | ||
await runMigration(); | ||
|
||
expect(warnOutput).toContain( 'index.ts@5:14 - Found deprecated css selector "i.anticon" component. ' + | ||
'Use "i[nz-icon]" to instead please.'); | ||
}); | ||
|
||
}); | ||
|
||
}); |
20 changes: 20 additions & 0 deletions
20
schematics/ng-update/upgrade-rules/checks/calendar-input-rule.ts
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,20 @@ | ||
import { findInputsOnElementWithTag, MigrationRule, ResolvedResource, TargetVersion } from '@angular/cdk/schematics'; | ||
import { findElementWithClassName } from '../../../utils/ng-update/elements'; | ||
|
||
export class CalendarTemplateRule extends MigrationRule<null> { | ||
|
||
ruleEnabled = this.targetVersion === TargetVersion.V9; | ||
|
||
visitTemplate(template: ResolvedResource): void { | ||
|
||
findInputsOnElementWithTag(template.content, 'nzCard', ['nz-calendar']) | ||
.forEach(offset => { | ||
this.failures.push({ | ||
filePath: template.filePath, | ||
position: template.getCharacterAndLineOfPosition(offset), | ||
message: `Found deprecated input "nzCard" component. Use "nzFullscreen" to instead please.` | ||
}); | ||
}) | ||
|
||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
schematics/ng-update/upgrade-rules/checks/icon-template-rule.ts
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,20 @@ | ||
import { MigrationRule, ResolvedResource, TargetVersion } from '@angular/cdk/schematics'; | ||
import { findElementWithClassName } from '../../../utils/ng-update/elements'; | ||
|
||
export class IconTemplateRule extends MigrationRule<null> { | ||
|
||
ruleEnabled = this.targetVersion === TargetVersion.V9; | ||
|
||
visitTemplate(template: ResolvedResource): void { | ||
|
||
findElementWithClassName(template.content, 'anticon', 'i') | ||
.forEach(offset => { | ||
this.failures.push({ | ||
filePath: template.filePath, | ||
position: template.getCharacterAndLineOfPosition(offset), | ||
message: `Found deprecated css selector "i.anticon" component. Use "i[nz-icon]" to instead please.` | ||
}); | ||
}) | ||
|
||
} | ||
} |
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