-
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.
refactor(module:upload): remove deprecated APIs for v10 (#5774)
* refactor(module:upload): remove deprecated APIs for v10 BREAKING CHANGES: - `UploadType` has been removed, use `NzUploadType` instead. - `UploadListType` has been removed, use `NzUploadListType` instead. - `UploadFile` has been removed, use `NzUploadFile` instead. - `UploadChangeParam` has been removed, use `NzUploadChangeParam` instead. - `ShowUploadListInterface` has been removed, use `NzShowUploadList` instead. - `UploadTransformFileType` has been removed, use `NzUploadTransformFileType` instead. - `UploadXHRArgs` has been removed, use `NzUploadXHRArgs` instead.
- Loading branch information
Showing
8 changed files
with
207 additions
and
67 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
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 |
---|---|---|
@@ -1,5 +1,18 @@ | ||
import { ClassNameUpgradeData, TargetVersion, VersionChanges } from '@angular/cdk/schematics'; | ||
|
||
export const classNames: VersionChanges<ClassNameUpgradeData> = { | ||
[ TargetVersion.V7 ]: [ ] | ||
[TargetVersion.V10]: [ | ||
{ | ||
pr: 'https://github.com/NG-ZORRO/ng-zorro-antd/pull/5774', | ||
changes: [ | ||
{replace: 'UploadType', replaceWith: 'NzUploadType'}, | ||
{replace: 'UploadListType', replaceWith: 'NzUploadListType'}, | ||
{replace: 'UploadFile', replaceWith: 'NzUploadFile'}, | ||
{replace: 'UploadChangeParam', replaceWith: 'NzUploadChangeParam'}, | ||
{replace: 'ShowUploadListInterface', replaceWith: 'NzShowUploadList'}, | ||
{replace: 'UploadTransformFileType', replaceWith: 'NzUploadTransformFileType'}, | ||
{replace: 'UploadXHRArgs', replaceWith: 'NzUploadXHRArgs'} | ||
] | ||
} | ||
] | ||
}; |
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,74 @@ | ||
import { | ||
ClassNameUpgradeData, | ||
getVersionUpgradeData, isExportSpecifierNode, isImportSpecifierNode, | ||
isNamespaceImportNode, | ||
Migration, UpgradeData | ||
} from '@angular/cdk/schematics'; | ||
import * as ts from 'typescript'; | ||
import { isNgZorroExportDeclaration, isNgZorroImportDeclaration } from '../../../utils/ng-update/module-specifiers'; | ||
|
||
export class ClassNamesMigration extends Migration<UpgradeData> { | ||
/** Change data that upgrades to the specified target version. */ | ||
data: ClassNameUpgradeData[] = getVersionUpgradeData(this, 'classNames'); | ||
|
||
/** | ||
* List of identifier names that have been imported from `@ng-zorro-antd` | ||
* in the current source file and therefore can be considered trusted. | ||
*/ | ||
trustedIdentifiers: Set<string> = new Set(); | ||
|
||
/** List of namespaces that have been imported from `@ng-zorro-antd`. */ | ||
trustedNamespaces: Set<string> = new Set(); | ||
|
||
// Only enable the migration rule if there is upgrade data. | ||
enabled = this.data.length !== 0; | ||
|
||
visitNode(node: ts.Node): void { | ||
if (ts.isIdentifier(node)) { | ||
this.visitIdentifier(node); | ||
} | ||
} | ||
|
||
/** Method that is called for every identifier inside of the specified project. */ | ||
private visitIdentifier(identifier: ts.Identifier): void { | ||
if (!this.data.some(data => data.replace === identifier.text)) { | ||
return; | ||
} | ||
|
||
if (isNamespaceImportNode(identifier) && isNgZorroImportDeclaration(identifier)) { | ||
this.trustedNamespaces.add(identifier.text); | ||
|
||
return this._createFailureWithReplacement(identifier); | ||
} | ||
|
||
if (isExportSpecifierNode(identifier) && isNgZorroExportDeclaration(identifier)) { | ||
return this._createFailureWithReplacement(identifier); | ||
} | ||
|
||
if (isImportSpecifierNode(identifier) && isNgZorroImportDeclaration(identifier)) { | ||
this.trustedIdentifiers.add(identifier.text); | ||
|
||
return this._createFailureWithReplacement(identifier); | ||
} | ||
|
||
if (ts.isPropertyAccessExpression(identifier.parent)) { | ||
const expression = identifier.parent.expression; | ||
|
||
if (ts.isIdentifier(expression) && this.trustedNamespaces.has(expression.text)) { | ||
return this._createFailureWithReplacement(identifier); | ||
} | ||
} else if (this.trustedIdentifiers.has(identifier.text)) { | ||
return this._createFailureWithReplacement(identifier); | ||
} | ||
} | ||
|
||
/** Creates a failure and replacement for the specified identifier. */ | ||
private _createFailureWithReplacement(identifier: ts.Identifier): void { | ||
const classData = this.data.find(data => data.replace === identifier.text)!; | ||
const filePath = this.fileSystem.resolve(identifier.getSourceFile().fileName); | ||
|
||
this.fileSystem.edit(filePath) | ||
.remove(identifier.getStart(), identifier.getWidth()) | ||
.insertRight(identifier.getStart(), classData.replaceWith); | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
schematics/ng-update/test-cases/v10/upload-deprecated.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,84 @@ | ||
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('upload 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-v10', {}, tree).toPromise(); | ||
} | ||
|
||
describe('Interfaces', () => { | ||
|
||
it('should replace deprecated interfaces', async() => { | ||
writeFile('/index.ts', `import { | ||
UploadType, | ||
UploadListType, | ||
UploadFile, | ||
UploadChangeParam, | ||
ShowUploadListInterface, | ||
UploadTransformFileType, | ||
UploadXHRArgs | ||
} from 'ng-zorro-antd/upload';`); | ||
|
||
await runMigration(); | ||
const content = tree.readContent('/index.ts'); | ||
|
||
expect(content).toEqual(`import { | ||
NzUploadType, | ||
NzUploadListType, | ||
NzUploadFile, | ||
NzUploadChangeParam, | ||
NzShowUploadList, | ||
NzUploadTransformFileType, | ||
NzUploadXHRArgs | ||
} from 'ng-zorro-antd/upload';`); | ||
}); | ||
|
||
}); | ||
|
||
}); |
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 |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import { getImportDeclaration } from '@angular/cdk/schematics'; | ||
import { getExportDeclaration, getImportDeclaration } from '@angular/cdk/schematics'; | ||
import * as ts from 'typescript'; | ||
|
||
export const materialModuleSpecifier = 'ng-zorro-antd'; | ||
export const ngZorroModuleSpecifier = 'ng-zorro-antd'; | ||
|
||
export function isNgZorroImportDeclaration(node: ts.Node): boolean { | ||
return isNgZorroDeclaration(getImportDeclaration(node)); | ||
} | ||
|
||
export function isNgZorroExportDeclaration(node: ts.Node): boolean { | ||
return isNgZorroDeclaration(getExportDeclaration(node)); | ||
} | ||
|
||
function isNgZorroDeclaration(declaration: ts.ImportDeclaration|ts.ExportDeclaration): boolean { | ||
if (!declaration.moduleSpecifier) { | ||
return false; | ||
} | ||
|
||
const moduleSpecifier = declaration.moduleSpecifier.getText(); | ||
return moduleSpecifier.indexOf(materialModuleSpecifier) !== -1 | ||
return moduleSpecifier.indexOf(ngZorroModuleSpecifier) !== -1 | ||
} |