-
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 carousel and inject toke…
…ns (#4469)
- Loading branch information
Showing
6 changed files
with
275 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
116 changes: 116 additions & 0 deletions
116
schematics/ng-update/test-cases/v9/deprecated-injection-tokens.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,116 @@ | ||
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'; | ||
import { SchematicsTestNGConfig, SchematicsTestTsConfig } from '../config'; | ||
|
||
describe('injection tokens migration', () => { | ||
let runner: SchematicTestRunner; | ||
let host: TempScopedNodeJsSyncHost; | ||
let tree: UnitTestTree; | ||
let tmpDirPath: string; | ||
let previousWorkingDir: string; | ||
let warnOutput: string[]; | ||
|
||
beforeEach(() => { | ||
runner = new SchematicTestRunner('test', require.resolve('../../../migration.json')); | ||
host = new TempScopedNodeJsSyncHost(); | ||
tree = new UnitTestTree(new HostTree(host)); | ||
|
||
writeFile('/tsconfig.json', JSON.stringify(SchematicsTestTsConfig)); | ||
writeFile('/angular.json', JSON.stringify(SchematicsTestNGConfig)); | ||
|
||
warnOutput = []; | ||
runner.logger.subscribe(logEntry => { | ||
if (logEntry.level === 'warn') { | ||
warnOutput.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('Injection Tokens', () => { | ||
|
||
it('should properly report invalid deprecated injection tokens', async() => { | ||
writeFile('/index.ts', ` | ||
import { NZ_NOTIFICATION_CONFIG, NZ_MESSAGE_CONFIG, NZ_DEFAULT_EMPTY_CONTENT } from 'ng-zorro-antd'; | ||
`); | ||
await runMigration(); | ||
|
||
const tokensWarn = [ | ||
'index.ts@2:16 - Found deprecated symbol "NZ_NOTIFICATION_CONFIG" which has been removed. Use "NZ_CONFIG" to ' + | ||
'instead please.', | ||
'index.ts@2:40 - Found deprecated symbol "NZ_MESSAGE_CONFIG" which has been removed. Use "NZ_CONFIG" to ' + | ||
'instead please.', | ||
'index.ts@2:59 - Found deprecated symbol "NZ_DEFAULT_EMPTY_CONTENT" which has been removed. Use "NZ_CONFIG" ' + | ||
'to instead please.' | ||
]; | ||
|
||
tokensWarn.forEach(warn => { | ||
expect(warnOutput).toContain(warn); | ||
}); | ||
}); | ||
|
||
it('should properly report invalid deprecated injection tokens whit secondary entry', async() => { | ||
writeFile('/index.ts', ` | ||
import { NZ_NOTIFICATION_CONFIG} from 'ng-zorro-antd/notification'; | ||
import { NZ_DEFAULT_EMPTY_CONTENT } from 'ng-zorro-antd/empty'; | ||
import { NZ_MESSAGE_CONFIG } from 'ng-zorro-antd/message'; | ||
`); | ||
await runMigration(); | ||
|
||
const tokensWarn = [ | ||
'index.ts@2:16 - Found deprecated symbol "NZ_NOTIFICATION_CONFIG" which has been removed. Use "NZ_CONFIG" ' + | ||
'to instead please.', | ||
'index.ts@3:16 - Found deprecated symbol "NZ_DEFAULT_EMPTY_CONTENT" which has been removed. Use "NZ_CONFIG" ' + | ||
'to instead please.', | ||
'index.ts@4:16 - Found deprecated symbol "NZ_MESSAGE_CONFIG" which has been removed. Use "NZ_CONFIG" to ' + | ||
'instead please.' | ||
]; | ||
|
||
tokensWarn.forEach(warn => { | ||
expect(warnOutput).toContain(warn); | ||
}); | ||
}); | ||
|
||
it('should not report invalid deprecated injection tokens in other package', async() => { | ||
writeFile('/index.ts', ` | ||
import { NZ_NOTIFICATION_CONFIG} from 'other/notification'; | ||
import { NZ_DEFAULT_EMPTY_CONTENT } from 'other/empty'; | ||
import { NZ_MESSAGE_CONFIG } from 'other/message'; | ||
import { NZ_NOTIFICATION_CONFIG, NZ_MESSAGE_CONFIG, NZ_DEFAULT_EMPTY_CONTENT } from 'other'; | ||
`); | ||
await runMigration(); | ||
|
||
expect(warnOutput.length).toBe(0); | ||
}); | ||
|
||
}); | ||
|
||
}); |
78 changes: 78 additions & 0 deletions
78
schematics/ng-update/test-cases/v9/deprecated-inputs-carousel.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,78 @@ | ||
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'; | ||
import { SchematicsTestNGConfig, SchematicsTestTsConfig } from '../config'; | ||
|
||
describe('carousel migration', () => { | ||
let runner: SchematicTestRunner; | ||
let host: TempScopedNodeJsSyncHost; | ||
let tree: UnitTestTree; | ||
let tmpDirPath: string; | ||
let previousWorkingDir: string; | ||
let warnOutput: string[]; | ||
|
||
beforeEach(() => { | ||
runner = new SchematicTestRunner('test', require.resolve('../../../migration.json')); | ||
host = new TempScopedNodeJsSyncHost(); | ||
tree = new UnitTestTree(new HostTree(host)); | ||
|
||
writeFile('/tsconfig.json', JSON.stringify(SchematicsTestTsConfig)); | ||
writeFile('/angular.json', JSON.stringify(SchematicsTestNGConfig)); | ||
|
||
warnOutput = []; | ||
runner.logger.subscribe(logEntry => { | ||
if (logEntry.level === 'warn') { | ||
warnOutput.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('Carousel', () => { | ||
|
||
it('should properly report invalid deprecated input', async() => { | ||
writeFile('/index.ts', `; | ||
import {Component} from '@angular/core' | ||
@Component({ | ||
template: \` | ||
<nz-carousel [nzVertical]="true"></nz-carousel> | ||
<nz-carousel nzVertical></nz-carousel> | ||
\` | ||
}) | ||
export class MyComp { | ||
}`); | ||
await runMigration(); | ||
|
||
expect(warnOutput).toContain('index.ts@6:24 - Found deprecated "[nzVertical]" input. Use "[nzDotPosition]" to ' + | ||
'instead please.'); | ||
expect(warnOutput).toContain('index.ts@5:25 - Found deprecated "[nzVertical]" input. Use "[nzDotPosition]" to ' + | ||
'instead please.'); | ||
}); | ||
|
||
}); | ||
|
||
}); |
19 changes: 19 additions & 0 deletions
19
schematics/ng-update/upgrade-rules/checks/carousel-like-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,19 @@ | ||
import { findInputsOnElementWithTag, MigrationRule, ResolvedResource, TargetVersion } from '@angular/cdk/schematics'; | ||
|
||
export class CarouselTemplateRule extends MigrationRule<null> { | ||
|
||
ruleEnabled = this.targetVersion === TargetVersion.V9; | ||
|
||
visitTemplate(template: ResolvedResource): void { | ||
|
||
findInputsOnElementWithTag(template.content, 'nzVertical', ['nz-carousel']) | ||
.forEach(offset => { | ||
this.failures.push({ | ||
filePath: template.filePath, | ||
position: template.getCharacterAndLineOfPosition(offset), | ||
message: `Found deprecated "[nzVertical]" input. Use "[nzDotPosition]" to instead please.` | ||
}); | ||
}); | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
schematics/ng-update/upgrade-rules/checks/injection-token-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,40 @@ | ||
import { MigrationRule, TargetVersion } from '@angular/cdk/schematics'; | ||
import * as ts from 'typescript'; | ||
import { isNgZorroImportDeclaration } from "../../../utils/ng-update/module-specifiers"; | ||
|
||
export class InjectionTokenRule extends MigrationRule<null> { | ||
|
||
ruleEnabled = this.targetVersion === TargetVersion.V9; | ||
|
||
visitNode(node: ts.Node): void { | ||
if (ts.isImportDeclaration(node)) { | ||
this._visitImportDeclaration(node); | ||
} | ||
} | ||
|
||
private _visitImportDeclaration(node: ts.ImportDeclaration): void { | ||
if (!isNgZorroImportDeclaration(node) || !node.importClause || | ||
!node.importClause.namedBindings) { | ||
return; | ||
} | ||
|
||
const namedBindings = node.importClause.namedBindings; | ||
|
||
if (ts.isNamedImports(namedBindings)) { | ||
this._checkInjectionToken(namedBindings); | ||
} | ||
} | ||
|
||
private _checkInjectionToken(namedImports: ts.NamedImports): void { | ||
namedImports.elements.filter(element => ts.isIdentifier(element.name)).forEach(element => { | ||
const importName = element.name.text; | ||
|
||
const deprecatedTokens = ['NZ_MESSAGE_CONFIG', 'NZ_NOTIFICATION_CONFIG', 'NZ_DEFAULT_EMPTY_CONTENT']; | ||
|
||
if (deprecatedTokens.indexOf(importName) !== -1) { | ||
this.createFailureAtNode( | ||
element, `Found deprecated symbol "${importName}" which has been removed. Use "NZ_CONFIG" 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getImportDeclaration } from '@angular/cdk/schematics'; | ||
import * as ts from 'typescript'; | ||
|
||
export const materialModuleSpecifier = 'ng-zorro-antd'; | ||
|
||
export function isNgZorroImportDeclaration(node: ts.Node): boolean { | ||
return isNgZorroDeclaration(getImportDeclaration(node)); | ||
} | ||
|
||
function isNgZorroDeclaration(declaration: ts.ImportDeclaration|ts.ExportDeclaration): boolean { | ||
if (!declaration.moduleSpecifier) { | ||
return false; | ||
} | ||
|
||
const moduleSpecifier = declaration.moduleSpecifier.getText(); | ||
return moduleSpecifier.indexOf(materialModuleSpecifier) !== -1 | ||
} |