Skip to content

Commit

Permalink
refactor(module:upload): remove deprecated APIs for v10 (#5774)
Browse files Browse the repository at this point in the history
* 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
hsuanxyz authored Sep 15, 2020
1 parent 49fac72 commit 9f5baae
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 67 deletions.
2 changes: 1 addition & 1 deletion components/skeleton/skeleton.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { NzSkeletonAvatar, NzSkeletonAvatarShape, NzSkeletonAvatarSize, NzSkelet
template: `
<ng-container *ngIf="nzLoading">
<div class="ant-skeleton-header" *ngIf="!!nzAvatar">
<nz-skeleton-element nzType="avatar" [nzSize]="avatar.size || 'default'" [nzShape]="avatar.shape"></nz-skeleton-element>
<nz-skeleton-element nzType="avatar" [nzSize]="avatar.size || 'default'" [nzShape]="avatar.shape || 'circle'"></nz-skeleton-element>
</div>
<div class="ant-skeleton-content">
<h3 *ngIf="!!nzTitle" class="ant-skeleton-title" [style.width]="toCSSUnit(title.width)"></h3>
Expand Down
44 changes: 0 additions & 44 deletions components/upload/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,47 +89,3 @@ export interface NzUploadXHRArgs {
onSuccess?(ret: NzSafeAny, file: NzUploadFile, xhr: NzSafeAny): void;
onError?(err: NzSafeAny, file: NzUploadFile): void;
}

/**
* @deprecated Use {@link NzUploadType} from {@link @ng-zorro-antd/upload} instead.
* @breaking-change 10.0.0
*/
export type UploadType = NzUploadType;

/**
* @deprecated Use {@link NzUploadListType} from {@link @ng-zorro-antd/upload} instead.
* @breaking-change 10.0.0
*/
export type UploadListType = NzUploadListType;

/**
* @deprecated Use {@link NzUploadFile} from {@link @ng-zorro-antd/upload} instead.
* @breaking-change 10.0.0
*/
export type UploadFile = NzUploadFile;

/**
* @deprecated Use {@link NzUploadChangeParam} from {@link @ng-zorro-antd/upload} instead.
* @breaking-change 10.0.0
*/
export type UploadChangeParam = NzUploadChangeParam;

/**
* @deprecated Use {@link NzShowUploadList} from {@link @ng-zorro-antd/upload} instead.
* @breaking-change 10.0.0
*/
export type ShowUploadListInterface = NzShowUploadList;

/**
* @deprecated Use {@link NzUploadTransformFileType} from {@link @ng-zorro-antd/upload} instead.
* @breaking-change 10.0.0
*/
export type UploadTransformFileType = NzUploadTransformFileType;

/**
* @deprecated Use {@link NzUploadXHRArgs} from {@link @ng-zorro-antd/upload} instead.
* @breaking-change 10.0.0
*/
export type UploadXHRArgs = NzUploadXHRArgs;

export class NzShowUploadListInterface {}
5 changes: 5 additions & 0 deletions schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"description": "Updates NG-ZORRO to v9 [https://github.com/NG-ZORRO/ng-zorro-antd/issues/5035]",
"factory": "./ng-update/index#updateToV9"
},
"migration-v10": {
"version": "10",
"description": "Updates NG-ZORRO to v10 [https://github.com/NG-ZORRO/ng-zorro-antd/issues/5775]",
"factory": "./ng-update/index#updateToV10"
},
"ng-post-update": {
"description": "Prints out results after ng-update.",
"factory": "./ng-update/index#postUpdate",
Expand Down
15 changes: 14 additions & 1 deletion schematics/ng-update/data/class-names.ts
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'}
]
}
]
};
74 changes: 74 additions & 0 deletions schematics/ng-update/data/migrations/class-names.ts
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);
}
}
40 changes: 22 additions & 18 deletions schematics/ng-update/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Rule } from '@angular-devkit/schematics';
import { createMigrationSchematicRule, TargetVersion } from '@angular/cdk/schematics';
import { createMigrationSchematicRule, NullableDevkitMigration, TargetVersion } from '@angular/cdk/schematics';
import chalk from 'chalk';
import { ClassNamesMigration } from './data/migrations/class-names';
import { ruleUpgradeData } from './upgrade-data';
import { CalendarTemplateRule } from './upgrade-rules/checks/calendar-input-rule';
import { CarouselTemplateRule } from "./upgrade-rules/checks/carousel-like-template-rule";
Expand All @@ -12,29 +13,32 @@ import { InjectionTokenRule } from "./upgrade-rules/checks/injection-token-rule"
import { SecondaryEntryPointsRule } from './upgrade-rules/checks/secondary-entry-points-rule';
import { TooltipLikeTemplateRule } from './upgrade-rules/checks/tooltip-like-template-rule';

const migrations: NullableDevkitMigration[] = [
TooltipLikeTemplateRule,
DropdownTemplateRule,
DropdownClassRule,
IconTemplateRule,
CalendarTemplateRule,
CarouselTemplateRule,
InjectionTokenRule,
FormTemplateRule,
SecondaryEntryPointsRule,
ClassNamesMigration
];

/** Entry point for the migration schematics with target of NG-ZORRO v7 */
export function updateToV7(): Rule {
return createMigrationSchematicRule(TargetVersion.V7, [TooltipLikeTemplateRule], ruleUpgradeData, postUpdate);
return createMigrationSchematicRule(TargetVersion.V7, migrations, ruleUpgradeData, postUpdate);
}

/** Entry point for the migration schematics with target of NG-ZORRO v9 */
export function updateToV9(): Rule {
return createMigrationSchematicRule(
TargetVersion.V9,
[
TooltipLikeTemplateRule,
DropdownTemplateRule,
DropdownClassRule,
IconTemplateRule,
CalendarTemplateRule,
CarouselTemplateRule,
InjectionTokenRule,
FormTemplateRule,
SecondaryEntryPointsRule
],
ruleUpgradeData,
postUpdate
);
return createMigrationSchematicRule(TargetVersion.V9, migrations, ruleUpgradeData, postUpdate);
}

/** Entry point for the migration schematics with target of NG-ZORRO v10 */
export function updateToV10(): Rule {
return createMigrationSchematicRule(TargetVersion.V10, migrations, ruleUpgradeData, postUpdate);
}

/** Post-update schematic to be called when update is finished. */
Expand Down
84 changes: 84 additions & 0 deletions schematics/ng-update/test-cases/v10/upload-deprecated.spec.ts
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';`);
});

});

});
10 changes: 7 additions & 3 deletions schematics/utils/ng-update/module-specifiers.ts
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
}

0 comments on commit 9f5baae

Please sign in to comment.