Skip to content

Commit

Permalink
deprecate exported x-deprecated interface (cocos#11802)
Browse files Browse the repository at this point in the history
* deprecate exported x-deprecated interface

* update

* update docs
  • Loading branch information
PPpro authored Jul 1, 2022
1 parent 0c29698 commit 151eeb3
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 23 deletions.
20 changes: 20 additions & 0 deletions cocos/core/utils/deprecated-3.6.0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { deprecateModuleExportedName } from './x-deprecated';

deprecateModuleExportedName({
replaceProperty: {
since: '3.6.0',
removed: false,
},
removeProperty: {
since: '3.6.0',
removed: false,
},
markAsWarning: {
since: '3.6.0',
removed: false,
},
setDefaultLogTimes: {
since: '3.6.0',
removed: false,
},
});
1 change: 1 addition & 0 deletions cocos/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
THE SOFTWARE.
*/

import './deprecated-3.6.0';
import * as js from './js';

/**
Expand Down
26 changes: 19 additions & 7 deletions cocos/core/utils/x-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { error, errorID, warn, warnID } from '../platform/debug';

let defaultLogTimes = 10;

/**
* @deprecated since v3.6.0, this is an engine private interface that will be removed in the future.
*/
export function setDefaultLogTimes (times: number): void {
if (times > 0) {
defaultLogTimes = times;
Expand Down Expand Up @@ -78,10 +81,19 @@ interface IMarkItem {
suggest?: string;
}

/**
* @deprecated since v3.6.0, this is an engine private interface that will be removed in the future.
*/
export let replaceProperty: (owner: object, ownerName: string, properties: IReplacement[]) => void;

/**
* @deprecated since v3.6.0, this is an engine private interface that will be removed in the future.
*/
export let removeProperty: (owner: object, ownerName: string, properties: IRemoveItem[]) => void;

/**
* @deprecated since v3.6.0, this is an engine private interface that will be removed in the future.
*/
export let markAsWarning: (owner: object, ownerName: string, properties: IMarkItem[]) => void;

let replacePropertyLog: (n: string, dp: string, n2: string, newp: string, f: Function, id: number, s: string) => void;
Expand Down Expand Up @@ -288,7 +300,7 @@ markAsWarning = (owner: object, ownerName: string, properties: IMarkItem[]) => {
* @engineInternal
*/
interface IDeprecateInfo {
newTypeName?: string;
newName?: string;
since: string;
removed: boolean,
}
Expand All @@ -311,7 +323,7 @@ const topLevelDeprecateList: TopLevelDeprecateList = {
* ```ts
* deprecateModuleExportedName({
* ButtonComponent: {
* newTypeName: 'Button',
* newName: 'Button',
* since: '1.2.0',
* removed: false,
* },
Expand All @@ -331,15 +343,15 @@ function _checkObsoleteByName (checkName: string) {
if (!deprecateInfo) {
return;
}
const { newTypeName, since, removed } = deprecateInfo;
const { newName, since, removed } = deprecateInfo;
if (removed) {
if (newTypeName) {
errorID(16003, checkName, since, newTypeName);
if (newName) {
errorID(16003, checkName, since, newName);
} else {
errorID(16002, checkName, since);
}
} else if (newTypeName) {
warnID(16001, checkName, since, newTypeName);
} else if (newName) {
warnID(16001, checkName, since, newName);
} else {
warnID(16000, checkName, since);
}
Expand Down
2 changes: 1 addition & 1 deletion cocos/input/deprecated-3.3.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { deprecateModuleExportedName } from '../core/utils/x-deprecated';

deprecateModuleExportedName({
SystemEventType: {
newTypeName: 'SystemEvent.EventType',
newName: 'SystemEvent.EventType',
since: '3.3.0',
removed: false,
},
Expand Down
30 changes: 15 additions & 15 deletions cocos/ui/deprecated-1.2.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,77 @@ import { deprecateModuleExportedName } from '../core/utils/x-deprecated';

deprecateModuleExportedName({
ButtonComponent: {
newTypeName: 'Button',
newName: 'Button',
since: '1.2.0',
removed: false,
},
EditBoxComponent: {
newTypeName: 'EditBox',
newName: 'EditBox',
since: '1.2.0',
removed: false,
},
LayoutComponent: {
newTypeName: 'Layout',
newName: 'Layout',
since: '1.2.0',
removed: false,
},
ProgressBarComponent: {
newTypeName: 'ProgressBar',
newName: 'ProgressBar',
since: '1.2.0',
removed: false,
},
ScrollViewComponent: {
newTypeName: 'ScrollView',
newName: 'ScrollView',
since: '1.2.0',
removed: false,
},
ScrollBarComponent: {
newTypeName: 'ScrollBar',
newName: 'ScrollBar',
since: '1.2.0',
removed: false,
},
SliderComponent: {
newTypeName: 'Slider',
newName: 'Slider',
since: '1.2.0',
removed: false,
},
ToggleComponent: {
newTypeName: 'Toggle',
newName: 'Toggle',
since: '1.2.0',
removed: false,
},
ToggleContainerComponent: {
newTypeName: 'ToggleContainer',
newName: 'ToggleContainer',
since: '1.2.0',
removed: false,
},
WidgetComponent: {
newTypeName: 'Widget',
newName: 'Widget',
since: '1.2.0',
removed: false,
},
PageViewComponent: {
newTypeName: 'PageView',
newName: 'PageView',
since: '1.2.0',
removed: false,
},
PageViewIndicatorComponent: {
newTypeName: 'PageViewIndicator',
newName: 'PageViewIndicator',
since: '1.2.0',
removed: false,
},
SafeAreaComponent: {
newTypeName: 'SafeArea',
newName: 'SafeArea',
since: '1.2.0',
removed: false,
},
UICoordinateTrackerComponent: {
newTypeName: 'UICoordinateTracker',
newName: 'UICoordinateTracker',
since: '1.2.0',
removed: false,
},
BlockInputEventsComponent: {
newTypeName: 'BlockInputEvents',
newName: 'BlockInputEvents',
since: '1.2.0',
removed: false,
},
Expand Down
151 changes: 151 additions & 0 deletions docs/contribution/deprecated-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Deprecated API

## Frame description

In order to maintain the abandoned `API` while making it more friendly and convenient, it will be implemented through three functions:

- `markAsWarning` embeds a warning in the property on the given object, and the property needs to exist on the given object.

- `removeProperty` redefines the removed property on the given object, and embeds an error message. The property should not exist on the given object.

- `replaceProperty` redefines the removed property on the given object, and embeds a warning and calls the new property. Incompatible parameters need to be adapted. The given object should not have the property.

## Function signature

```typescript
interface IRemoveItem {
/** The name of the obsolete property */
name: string;
/** Number of warnings */
logTimes?: number;
/** Additional suggestions */
suggest?: string;
}

interface IMarkItem {
/** The name of the obsolete property */
name: string;
/** Number of warnings */
logTimes?: number;
/** Additional suggestions */
suggest?: string;
}

interface IReplacement {
/** The name of the obsolete property */
name: string;
/** Number of warnings */
logTimes?: number;
/** Additional suggestions */
suggest?: string;
/** The object of the discarded property */
target?: object;
/** The name of the object of the discarded property */
targetName?: string;
/** Custom replacement property (function) */
customFunction?: Function;
/** Setter of custom replacement properties */
customSetter?: (v: any) => void;
/** Getter for custom replacement propertys */
customGetter?: () => any;
}

export let removeProperty: (owner: object, ownerName: string, properties: IRemoveItem[]) => void;

export let markAsWarning: (owner: object, ownerName: string, properties: IMarkItem[]) => void;

export let replaceProperty: (owner: object, ownerName: string, properties: IReplacement[]) => void;

/** This function is used to set the global default information output times */
export function setDefaultLogTimes (times: number): void;
```

## Terms of Use

According to the module division, each module maintains an obsolete file. In order to facilitate maintenance, the name is unified as deprecated.ts and placed in the directory of the corresponding module, and the file needs to be `import` in the `index.ts` file of the corresponding module, such as `import'./deprecated'`.

> **Note**: the `deprecated.ts` file in the `cocos\utils` directory is a declaration and implementation file.
## Usage example

```typescript
// For APIs that are not compatible with replacement parameters, adapt them through appropriate custom functions
replaceProperty(AnimationComponent.prototype, 'AnimationComponent.prototype', [
{
name: 'removeClip',
newName: 'removeState',
customFunction: function (...args: any) {
const arg0 = args[0] as AnimationClip;
return AnimationComponent.prototype.removeState.call(this, arg0.name);
}
}
]);

replaceProperty(vmath, 'vmath', [
{
name: 'vec2',
newName: 'Vec2',
target: math,
targetName: 'math',
'logTimes': 1
},
{
name: 'EPSILON',
target: math,
targetName: 'math',
'logTimes': 2
}
]);

removeProperty(vmath, 'vmath', [
{
'name': 'random',
'suggest': 'use Math.random.'
}
]);

markAsWarning(math, 'math', [
{
'name': 'toRadian'
}
]);
```

## Usage Notes

- The operation targets are all objects. If you want to modify the member functions of the class, please pass in **target.prototype**.

- `replaceProperty` does not pass in `newName` or `newTarget`, which means it is consistent with `name` or `target`.

- If you want to control the number of times, it is best to call `setDefaultLogTimes` before use, because other modules may change the default times.

## Deprecate Module Exported Name

The engine supports deprecation of exported name since `3.6.0`, the above mentioned are object property deprecation operations. If you need to deprecate the object or type itself, you need to use the `deprecateModuleExportedName` interface. The usage example is as follows:

```ts
deprecateModuleExportedName({
ButtonComponent: {
newName: 'Button',
since: '1.2.0',
removed: false,
},
});

deprecateModuleExportedName({
replaceProperty: {
since: '3.6.0',
removed: false,
},
});
```

The project script will report a warning if the following import operations are performed:
```ts
import { ButtonComponent } from 'cc';
```
or
```ts
import * as cc from 'cc';
console.log(cc.ButtonComponent);
```
2 changes: 2 additions & 0 deletions docs/contribution/deprecated-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ and the importing module:
export * from './select-deprecated-1.2.0';
```

### More Reference
[How to deprecate an API](./deprecated-api.md)

0 comments on commit 151eeb3

Please sign in to comment.