From d6969f3c244bb0d9223ca14fb7b5ed8fe04ce0d8 Mon Sep 17 00:00:00 2001 From: PP_Pro Date: Mon, 9 Oct 2023 15:47:46 +0800 Subject: [PATCH 1/6] fix eslint int `object` module --- cocos/core/data/class.ts | 83 ++++++++++----------- cocos/core/data/object.ts | 22 ++++-- cocos/core/data/utils/attribute-internal.ts | 4 +- cocos/core/data/utils/attribute.ts | 21 ++++-- cocos/core/data/utils/requiring-frame.ts | 30 +++++--- 5 files changed, 88 insertions(+), 72 deletions(-) diff --git a/cocos/core/data/class.ts b/cocos/core/data/class.ts index 6207d12b742..fa40aa387b9 100644 --- a/cocos/core/data/class.ts +++ b/cocos/core/data/class.ts @@ -25,10 +25,10 @@ import { DEV, EDITOR, SUPPORT_JIT, TEST } from 'internal:constants'; import { cclegacy } from '@base/global'; -import { errorID, warnID, error } from '@base/debug'; +import { errorID, warnID, error, _throw, StringSubstitution, getError } from '@base/debug'; import { js } from '@base/utils'; import { BitMask } from '../value-types'; -import { Enum } from '../value-types/enum'; +import { Enum, EnumType } from '../value-types/enum'; import * as attributeUtils from './utils/attribute'; import { IAcceptableAttributes } from './utils/attribute-defines'; import { preprocessAttrs } from './utils/preprocess-class'; @@ -73,7 +73,7 @@ function appendProp (cls, name): void { pushUnique(cls.__props__, name); } -function defineProp (cls, className, propName, val): void { +function defineProp (cls: Constructor, className: string, propName: string, val: PropertyStash): void { if (DEV) { // check base prototype to avoid name collision if (CCClass.getInheritanceChain(cls) @@ -96,12 +96,12 @@ function defineProp (cls, className, propName, val): void { } } -function defineGetSet (cls, name, propName, val): void { +function defineGetSet (cls: Constructor, className: string, propName: string, val: PropertyStash): void { const getter = val.get; const setter = val.set; if (getter) { - parseAttributes(cls, val, name, propName, true); + parseAttributes(cls, val, className, propName, true); if ((EDITOR && !window.Build) || TEST) { onAfterProps_ET.length = 0; } @@ -131,7 +131,7 @@ function getDefault (defaultVal): any { try { return defaultVal(); } catch (e) { - cclegacy._throw(e); + _throw(e); return undefined; } } else { @@ -141,36 +141,36 @@ function getDefault (defaultVal): any { return defaultVal; } -function doDefine (className, baseClass, options): any { - const ctor = options.ctor; +function doDefine (className: string | undefined, baseClass: Constructor | null, options: Parameters[0]): Constructor { + const ctor = options.ctor as Constructor; if (DEV) { // check ctor - if (CCClass._isCCClass(ctor)) { + if (CCClass._isCCClass(ctor) && className) { errorID(3618, className); } } js.value(ctor, CCCLASS_TAG, true, true); - const prototype = ctor.prototype; if (baseClass) { - ctor.$super = baseClass; + (ctor as any).$super = baseClass; } - js.setClassName(className, ctor); + if (className) { + js.setClassName(className, ctor); + } return ctor; } -function define (className, baseClass, options): any { +function define (className: string | undefined, baseClass: Constructor | null, options: Parameters[0]): Constructor { const Component = cclegacy.Component; const frame = RF.peek(); if (frame && js.isChildClassOf(baseClass, Component)) { // project component if (js.isChildClassOf(frame.cls, Component)) { - errorID(3615); - return null; + _throw(getError(3615)); } if (DEV && frame.uuid && className) { // warnID(3616, className); @@ -204,7 +204,9 @@ function define (className, baseClass, options): any { // cc-class is defined by `cc.Class({/* ... */})`. // In such case, `options.ctor` may be `undefined`. // So we can not use `options.ctor`. Instead, we should use `cls` which is the "real" registered cc-class. - EditorExtends.emit('class-registered', cls, frame, className); + if (className) { + EditorExtends.emit('class-registered', cls, frame, className); + } } if (frame) { @@ -258,19 +260,18 @@ function escapeForJS (s): string { // simple test variable name const IDENTIFIER_RE = /^[A-Za-z_$][0-9A-Za-z_$]*$/; -function declareProperties (cls, className, properties, baseClass): void { - cls.__props__ = []; +function declareProperties (cls: Constructor, className: string, properties, baseClass): void { + (cls as any).__props__ = []; if (baseClass && baseClass.__props__) { - cls.__props__ = baseClass.__props__.slice(); + (cls as any).__props__ = baseClass.__props__.slice(); } if (properties) { - // 预处理属性 preprocessAttrs(properties, className, cls); for (const propName in properties) { - const val = properties[propName]; + const val: PropertyStash = properties[propName]; if (!val.get && !val.set) { defineProp(cls, className, propName, val); } else { @@ -280,12 +281,12 @@ function declareProperties (cls, className, properties, baseClass): void { } const attrs = attributeUtils.getClassAttrs(cls); - cls.__values__ = cls.__props__.filter((prop) => attrs[`${prop}${DELIMETER}serializable`] !== false); + (cls as any).__values__ = (cls as any).__props__.filter((prop) => attrs[`${prop}${DELIMETER}serializable`] !== false); } export function CCClass (options: { name?: string; - extends: null | (Function & { __props__?: any; _sealed?: boolean }); + extends: null | (Constructor & { __props__?: any; _sealed?: boolean }); ctor: TFunction; properties?: any; editor?: any; @@ -296,10 +297,10 @@ export function CCClass (options: { // create constructor const cls = define(name, base, options); if (!name) { - name = cclegacy.js.getClassName(cls); + name = js.getClassName(cls); } - cls._sealed = true; + (cls as any)._sealed = true; if (base) { base._sealed = false; } @@ -313,7 +314,7 @@ export function CCClass (options: { if (js.isChildClassOf(base, cclegacy.Component)) { cclegacy.Component._registerEditorProps(cls, editor); } else if (DEV) { - warnID(3623, name!); + warnID(3623, name); } } @@ -346,9 +347,9 @@ CCClass._isCCClass = function isCCClass (constructor): boolean { // @param {Object} serializableFields // @private // -CCClass.fastDefine = function (className, constructor, serializableFields): void { +CCClass.fastDefine = function (className: string, constructor: Constructor, serializableFields: Record): void { js.setClassName(className, constructor); - const props = constructor.__props__ = constructor.__values__ = Object.keys(serializableFields); + const props = (constructor as any).__props__ = (constructor as any).__values__ = Object.keys(serializableFields); const attrs = attributeUtils.getClassAttrs(constructor); for (let i = 0; i < props.length; i++) { const key = props[i]; @@ -378,7 +379,7 @@ CCClass.isCCClassOrFastDefined = isCCClassOrFastDefined; * Return all super classes. * @param constructor The Constructor. */ -function getInheritanceChain (constructor): any[] { +function getInheritanceChain (constructor: Constructor): any[] { const chain: any[] = []; for (; ;) { constructor = getSuper(constructor); @@ -405,11 +406,11 @@ const PrimitiveTypes = { }; interface IParsedAttribute extends IAcceptableAttributes { - ctor?: Function; + ctor?: Constructor; enumList?: readonly any[]; bitmaskList?: any[]; } -type OnAfterProp = (constructor: Function, mainPropertyName: string) => void; +type OnAfterProp = (constructor: Constructor, mainPropertyName: string) => void; const onAfterProps_ET: OnAfterProp[] = []; interface AttributesRecord { @@ -418,7 +419,7 @@ interface AttributesRecord { default?: unknown; } -function parseAttributes (constructor: Function, attributes: PropertyStash, className: string, propertyName: string, usedInGetter): void { +function parseAttributes (constructor: Constructor, attributes: PropertyStash, className: string, propertyName: string, usedInGetter): void { const ERR_Type = DEV ? 'The %s of %s must be type %s' : ''; let attrs: IParsedAttribute | null = null; @@ -440,7 +441,7 @@ function parseAttributes (constructor: Function, attributes: PropertyStash, clas const type = attributes.type; if (type) { - const primitiveType = PrimitiveTypes[type]; + const primitiveType = PrimitiveTypes[type] as string; if (primitiveType) { (attrs || initAttrs())[`${propertyNamePrefix}type`] = type; if (((EDITOR && !window.Build) || TEST) && !attributes._short) { @@ -450,23 +451,18 @@ function parseAttributes (constructor: Function, attributes: PropertyStash, clas if (DEV) { errorID(3644, className, propertyName); } - } - // else if (type === Attr.ScriptUuid) { - // (attrs || initAttrs())[propertyNamePrefix + 'type'] = 'Script'; - // attrs[propertyNamePrefix + 'ctor'] = cc.ScriptAsset; - // } - else if (typeof type === 'object') { + } else if (typeof type === 'object') { if (Enum.isEnum(type)) { setPropertyEnumTypeOnAttrs( - attrs || initAttrs(), + (attrs || initAttrs()) as Record, propertyName, - type, + type as EnumType, ); } else if (BitMask.isBitMask(type)) { (attrs || initAttrs())[`${propertyNamePrefix}type`] = BITMASK_TAG; attrs![`${propertyNamePrefix}bitmaskList`] = BitMask.getList(type); } else if (DEV) { - errorID(3645, className, propertyName, type); + errorID(3645, className, propertyName, type as StringSubstitution); } } else if (typeof type === 'function') { // Do not warn missing-default if the type is object @@ -477,7 +473,7 @@ function parseAttributes (constructor: Function, attributes: PropertyStash, clas onAfterProps_ET.push(attributeUtils.getObjTypeChecker_ET(type)); } } else if (DEV) { - errorID(3646, className, propertyName, type); + errorID(3646, className, propertyName, type as StringSubstitution); } } @@ -492,6 +488,7 @@ function parseAttributes (constructor: Function, attributes: PropertyStash, clas const parseSimpleAttribute = (attributeName: keyof IAcceptableAttributes, expectType: string): void => { if (attributeName in attributes) { const val = attributes[attributeName]; + // eslint-disable-next-line valid-typeof if (typeof val === expectType) { (attrs || initAttrs())[propertyNamePrefix + attributeName] = val; } else if (DEV) { diff --git a/cocos/core/data/object.ts b/cocos/core/data/object.ts index a71f37666b5..0555e20372e 100644 --- a/cocos/core/data/object.ts +++ b/cocos/core/data/object.ts @@ -72,7 +72,7 @@ const AllHideMasks = DontSave | EditorOnly | LockedInEditor | HideInHierarchy; const objectsToDestroy: CCObject[] = []; let deferredDestroyTimer: number | null = null; -function compileDestruct (obj, ctor): Function { +function compileDestruct (obj, ctor): AnyFunction { const shouldSkipId = obj instanceof cclegacy.Node || obj instanceof cclegacy.Component; const idToSkip = shouldSkipId ? '_id' : null; @@ -134,7 +134,7 @@ function compileDestruct (obj, ctor): Function { for (key in propsToReset) { let statement; - if (CCClass.IDENTIFIER_RE.test(key)) { + if (CCClass.IDENTIFIER_RE.test(key as string)) { statement = `o.${key}=`; } else { statement = `o[${CCClass.escapeForJS(key)}]=`; @@ -347,8 +347,8 @@ class CCObject implements EditorExtendableObject { * ``` */ public _destruct (): void { - const ctor: any = this.constructor; - let destruct = ctor.__destruct__; + const ctor = this.constructor as Constructor; + let destruct = (ctor as any).__destruct__; if (!destruct) { destruct = compileDestruct(this, ctor); js.value(ctor, '__destruct__', destruct, true); @@ -395,13 +395,16 @@ if (EDITOR || TEST) { * This method is only available for editors and is not recommended for developers * @zh 在继承 CCObject 对象后,控制是否需要隐藏,锁定,序列化等功能(该方法仅提供给编辑器使用,不建议开发者使用)。 */ - js.getset(prototype, 'objFlags', + js.getset( + prototype, + 'objFlags', function (this: CCObject) { return this._objFlags; }, function (this: CCObject, objFlags: CCObject.Flags) { this._objFlags = objFlags; - }); + }, + ); /* * @en @@ -675,8 +678,11 @@ declare const jsb: any; if (JSB) { copyAllProperties(CCObject, jsb.CCObject, ['prototype', 'length', 'name']); - copyAllProperties(CCObject.prototype, jsb.CCObject.prototype, - ['constructor', 'name', 'hideFlags', 'isValid']); + copyAllProperties( + CCObject.prototype, + jsb.CCObject.prototype, + ['constructor', 'name', 'hideFlags', 'isValid'], + ); (CCObject as unknown as any) = jsb.CCObject; } diff --git a/cocos/core/data/utils/attribute-internal.ts b/cocos/core/data/utils/attribute-internal.ts index 306019d084c..dda597a15de 100644 --- a/cocos/core/data/utils/attribute-internal.ts +++ b/cocos/core/data/utils/attribute-internal.ts @@ -27,10 +27,10 @@ import { getClassAttrs, DELIMETER } from './attribute'; // eslint-disable-next-line @typescript-eslint/ban-types export function setPropertyEnumType (objectOrConstructor: object, propertyName: string, enumType: EnumType): void { - setPropertyEnumTypeOnAttrs(getClassAttrs(objectOrConstructor), propertyName, enumType); + setPropertyEnumTypeOnAttrs(getClassAttrs(objectOrConstructor) as Record, propertyName, enumType); } -export function setPropertyEnumTypeOnAttrs (attrs: Record, propertyName: string, enumType: EnumType): void { +export function setPropertyEnumTypeOnAttrs (attrs: Record, propertyName: string, enumType: EnumType): void { attrs[`${propertyName}${DELIMETER}type`] = 'Enum'; attrs[`${propertyName}${DELIMETER}enumList`] = Enum.getList(enumType); } diff --git a/cocos/core/data/utils/attribute.ts b/cocos/core/data/utils/attribute.ts index eeeeb3dde08..d4a51b87335 100644 --- a/cocos/core/data/utils/attribute.ts +++ b/cocos/core/data/utils/attribute.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-prototype-builtins */ /* Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd. @@ -33,7 +34,7 @@ const { formatStr, get, getClassName, isChildClassOf, value } = js; export const DELIMETER = '$_$'; -export function createAttrsSingle (owner: Object, superAttrs?: any): any { +export function createAttrsSingle (owner: Record, superAttrs?: unknown): any { const attrs = superAttrs ? Object.create(superAttrs) : {}; value(owner, '__attrs__', attrs); return attrs; @@ -46,7 +47,7 @@ export function createAttrs (subclass: any): any { if (typeof subclass !== 'function') { // attributes only in instance const instance = subclass; - return createAttrsSingle(instance, getClassAttrs(instance.constructor)); + return createAttrsSingle(instance as Record, getClassAttrs(instance.constructor)); } let superClass: any; const chains: any[] = cclegacy.Class.getInheritanceChain(subclass); @@ -55,11 +56,11 @@ export function createAttrs (subclass: any): any { const attrs = cls.hasOwnProperty('__attrs__') && cls.__attrs__; if (!attrs) { superClass = chains[i + 1]; - createAttrsSingle(cls, superClass && superClass.__attrs__); + createAttrsSingle(cls as Record, superClass && superClass.__attrs__); } } superClass = chains[0]; - createAttrsSingle(subclass, superClass && superClass.__attrs__); + createAttrsSingle(subclass as Record, superClass && superClass.__attrs__); return subclass.__attrs__; } @@ -207,7 +208,7 @@ cclegacy.CCString = CCString; // Ensures the type matches its default value export function getTypeChecker_ET (type: string, attributeName: string) { - return function (constructor: Function, mainPropertyName: string): void { + return function (constructor: Constructor, mainPropertyName: string): void { const propInfo = `"${getClassName(constructor)}.${mainPropertyName}"`; const mainPropAttrs = attr(constructor, mainPropertyName); let mainPropAttrsType = mainPropAttrs.type; @@ -259,15 +260,19 @@ export function getTypeChecker_ET (type: string, attributeName: string) { // Ensures the type matches its default value export function getObjTypeChecker_ET (typeCtor) { - return function (classCtor, mainPropName): void { + return function (classCtor: Constructor, mainPropName: string): void { getTypeChecker_ET('Object', 'type')(classCtor, mainPropName); // check ValueType const defaultDef = getClassAttrs(classCtor)[`${mainPropName + DELIMETER}default`]; const defaultVal = cclegacy.Class.getDefault(defaultDef); if (!Array.isArray(defaultVal) && isChildClassOf(typeCtor, cclegacy.ValueType)) { const typename = getClassName(typeCtor); - const info = formatStr('No need to specify the "type" of "%s.%s" because %s is a child class of ValueType.', - getClassName(classCtor), mainPropName, typename); + const info = formatStr( + 'No need to specify the "type" of "%s.%s" because %s is a child class of ValueType.', + getClassName(classCtor), + mainPropName, + typename, + ); if (defaultDef) { log(info); } else { diff --git a/cocos/core/data/utils/requiring-frame.ts b/cocos/core/data/utils/requiring-frame.ts index 17eb8511fd2..a013f02af3f 100644 --- a/cocos/core/data/utils/requiring-frame.ts +++ b/cocos/core/data/utils/requiring-frame.ts @@ -26,19 +26,26 @@ import { EDITOR } from 'internal:constants'; import { cclegacy } from '@base/global'; -/** - * - */ -let requiringFrames: any = []; // the requiring frame infos +interface IRequiringFrame { + uuid: string, + script: string, + module: { exports: AnyFunction }, + exports: this['module']['exports'], + beh: unknown, + importMeta?: Record, + cls?: AnyFunction, +} + +let requiringFrames: IRequiringFrame[] = []; // the requiring frame infos -export function push (module, uuid: string, script, importMeta?): void { - if (script === undefined) { - script = uuid; +export function push (module: IRequiringFrame['module'], uuid: string, scriptName: string, importMeta?: Record): void { + if (scriptName === undefined) { + scriptName = uuid; uuid = ''; } requiringFrames.push({ uuid, - script, + script: scriptName, module, exports: module.exports, // original exports beh: null, @@ -47,21 +54,22 @@ export function push (module, uuid: string, script, importMeta?): void { } export function pop (): void { - const frameInfo = requiringFrames.pop(); + const frameInfo = requiringFrames.pop()!; // check exports const module = frameInfo.module; let exports = module.exports; if (exports === frameInfo.exports) { + // eslint-disable-next-line no-unreachable-loop for (const anykey in exports) { // exported return; } // auto export component - module.exports = exports = frameInfo.cls; + module.exports = exports = frameInfo.cls!; } } -export function peek (): any { +export function peek (): IRequiringFrame { return requiringFrames[requiringFrames.length - 1]; } From b1bd49ab2ce0168e57ec7775c34533cbaeaf85a4 Mon Sep 17 00:00:00 2001 From: PP_Pro Date: Mon, 9 Oct 2023 16:05:07 +0800 Subject: [PATCH 2/6] update --- cocos/core/data/class.ts | 18 ++++++++++++------ cocos/core/data/object.ts | 2 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cocos/core/data/class.ts b/cocos/core/data/class.ts index fa40aa387b9..6917848bf55 100644 --- a/cocos/core/data/class.ts +++ b/cocos/core/data/class.ts @@ -154,6 +154,8 @@ function doDefine (className: string | undefined, baseClass: Constructor | null, js.value(ctor, CCCLASS_TAG, true, true); if (baseClass) { + // TODO: this is a dynamic inject method, should be define in class + // issue: https://github.com/cocos/cocos-engine/issues/14643 (ctor as any).$super = baseClass; } @@ -260,18 +262,20 @@ function escapeForJS (s): string { // simple test variable name const IDENTIFIER_RE = /^[A-Za-z_$][0-9A-Za-z_$]*$/; -function declareProperties (cls: Constructor, className: string, properties, baseClass): void { +function declareProperties (cls: Constructor, className: string, properties: Record | undefined, baseClass: Constructor | null): void { + // TODO: this is a dynamic inject method, should be define in class + // issue: https://github.com/cocos/cocos-engine/issues/14643 (cls as any).__props__ = []; - if (baseClass && baseClass.__props__) { - (cls as any).__props__ = baseClass.__props__.slice(); + if (baseClass && (baseClass as any).__props__) { + (cls as any).__props__ = (baseClass as any).__props__.slice(); } if (properties) { preprocessAttrs(properties, className, cls); for (const propName in properties) { - const val: PropertyStash = properties[propName]; + const val = properties[propName]; if (!val.get && !val.set) { defineProp(cls, className, propName, val); } else { @@ -288,7 +292,7 @@ export function CCClass (options: { name?: string; extends: null | (Constructor & { __props__?: any; _sealed?: boolean }); ctor: TFunction; - properties?: any; + properties?: Record; editor?: any; }): any { let name = options.name; @@ -349,6 +353,8 @@ CCClass._isCCClass = function isCCClass (constructor): boolean { // CCClass.fastDefine = function (className: string, constructor: Constructor, serializableFields: Record): void { js.setClassName(className, constructor); + // TODO: this is a dynamic inject method, should be define in class + // issue: https://github.com/cocos/cocos-engine/issues/14643 const props = (constructor as any).__props__ = (constructor as any).__values__ = Object.keys(serializableFields); const attrs = attributeUtils.getClassAttrs(constructor); for (let i = 0; i < props.length; i++) { @@ -454,7 +460,7 @@ function parseAttributes (constructor: Constructor, attributes: PropertyStash, c } else if (typeof type === 'object') { if (Enum.isEnum(type)) { setPropertyEnumTypeOnAttrs( - (attrs || initAttrs()) as Record, + (attrs || initAttrs()) as Record, propertyName, type as EnumType, ); diff --git a/cocos/core/data/object.ts b/cocos/core/data/object.ts index 0555e20372e..e20afd7559b 100644 --- a/cocos/core/data/object.ts +++ b/cocos/core/data/object.ts @@ -348,6 +348,8 @@ class CCObject implements EditorExtendableObject { */ public _destruct (): void { const ctor = this.constructor as Constructor; + // TODO: this is a dynamic inject method, should be define in class + // issue: https://github.com/cocos/cocos-engine/issues/14643 let destruct = (ctor as any).__destruct__; if (!destruct) { destruct = compileDestruct(this, ctor); From 11b3e4fcbe9522a642e2fa31e3ff1d5be41757e2 Mon Sep 17 00:00:00 2001 From: PP_Pro Date: Mon, 9 Oct 2023 16:27:04 +0800 Subject: [PATCH 3/6] revert logic --- cocos/core/data/class.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cocos/core/data/class.ts b/cocos/core/data/class.ts index 6917848bf55..c78f0282ff7 100644 --- a/cocos/core/data/class.ts +++ b/cocos/core/data/class.ts @@ -146,8 +146,8 @@ function doDefine (className: string | undefined, baseClass: Constructor | null, if (DEV) { // check ctor - if (CCClass._isCCClass(ctor) && className) { - errorID(3618, className); + if (CCClass._isCCClass(ctor)) { + errorID(3618, className!); } } @@ -159,9 +159,7 @@ function doDefine (className: string | undefined, baseClass: Constructor | null, (ctor as any).$super = baseClass; } - if (className) { - js.setClassName(className, ctor); - } + js.setClassName(className!, ctor); return ctor; } @@ -206,9 +204,7 @@ function define (className: string | undefined, baseClass: Constructor | null, o // cc-class is defined by `cc.Class({/* ... */})`. // In such case, `options.ctor` may be `undefined`. // So we can not use `options.ctor`. Instead, we should use `cls` which is the "real" registered cc-class. - if (className) { - EditorExtends.emit('class-registered', cls, frame, className); - } + EditorExtends.emit('class-registered', cls, frame, className!); } if (frame) { From 80a2b1a4d85f403d082420e7bd0063f24e31616b Mon Sep 17 00:00:00 2001 From: PP_Pro Date: Mon, 9 Oct 2023 16:37:13 +0800 Subject: [PATCH 4/6] update --- cocos/core/data/utils/requiring-frame.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/core/data/utils/requiring-frame.ts b/cocos/core/data/utils/requiring-frame.ts index a013f02af3f..d9a83ea9782 100644 --- a/cocos/core/data/utils/requiring-frame.ts +++ b/cocos/core/data/utils/requiring-frame.ts @@ -38,7 +38,7 @@ interface IRequiringFrame { let requiringFrames: IRequiringFrame[] = []; // the requiring frame infos -export function push (module: IRequiringFrame['module'], uuid: string, scriptName: string, importMeta?: Record): void { +export function push (module: IRequiringFrame['module'], uuid: IRequiringFrame['uuid'], scriptName: IRequiringFrame['script'], importMeta?: IRequiringFrame['importMeta']): void { if (scriptName === undefined) { scriptName = uuid; uuid = ''; From a1bf041e4082fac58b7221cd7fa462bb23cfc66f Mon Sep 17 00:00:00 2001 From: PP_Pro Date: Mon, 9 Oct 2023 17:01:37 +0800 Subject: [PATCH 5/6] optimize preprocess-class.ts --- cocos/core/data/utils/preprocess-class.ts | 32 ++++++++++------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/cocos/core/data/utils/preprocess-class.ts b/cocos/core/data/utils/preprocess-class.ts index 2c7a34254a4..92192fe0ef5 100644 --- a/cocos/core/data/utils/preprocess-class.ts +++ b/cocos/core/data/utils/preprocess-class.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-prototype-builtins */ /* Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd. @@ -28,6 +29,7 @@ import { error, errorID, warn, warnID } from '@base/debug'; import { cclegacy } from '@base/global'; import { js } from '@base/utils'; import { PrimitiveType } from './attribute'; +import { PropertyStash } from '../class-stash'; // 增加预处理属性这个步骤的目的是降低 CCClass 的实现难度,将比较稳定的通用逻辑和一些需求比较灵活的属性需求分隔开。 @@ -54,6 +56,7 @@ function parseNotify (val, propName, notify, properties): void { const newKey = `_N$${propName}`; val.get = function (): any { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return return this[newKey]; }; val.set = function (value): void { @@ -80,7 +83,7 @@ function parseNotify (val, propName, notify, properties): void { } } -function parseType (val, type, className, propName): void { +function parseType (val: PropertyStash, type: unknown, className: string, propName: string): void { const STATIC_CHECK = (EDITOR && DEV) || TEST; if (Array.isArray(type)) { @@ -159,11 +162,7 @@ function getBaseClassWherePropertyDefined_DEV (propName, cls): any { return null; } -function _wrapOptions (isGetset: boolean, _default, type?: Function | Function[] | PrimitiveType): { - default?: any; - _short?: boolean | undefined; - type?: any; -} { +function _wrapOptions (isGetset: boolean, _default, type?: PropertyStash['type']): PropertyStash { const res: { default?: any, _short?: boolean, @@ -172,21 +171,17 @@ function _wrapOptions (isGetset: boolean, _default, type?: Function | Function[] if (type) { res.type = type; } - return res; + return res as PropertyStash; } -export function getFullFormOfProperty (options, isGetset): { - default?: any; - _short?: boolean | undefined; - type?: any; -} | null { +export function getFullFormOfProperty (options: unknown, isGetset: boolean): PropertyStash | null { const isLiteral = options && options.constructor === Object; if (!isLiteral) { if (Array.isArray(options) && options.length > 0) { return _wrapOptions(isGetset, [], options); } else if (typeof options === 'function') { - const type = options; - return _wrapOptions(isGetset, js.isChildClassOf(type, cclegacy.ValueType) ? new type() : null, type); + const Type = options; + return _wrapOptions(isGetset, js.isChildClassOf(Type, cclegacy.ValueType) ? new (Type as Constructor)() : null, Type); } else if (options instanceof PrimitiveType) { return _wrapOptions(isGetset, undefined, options); } else { @@ -196,7 +191,7 @@ export function getFullFormOfProperty (options, isGetset): { return null; } -export function preprocessAttrs (properties, className, cls): void { +export function preprocessAttrs (properties: Record, className: string, cls): void { for (const propName in properties) { let val = properties[propName]; const fullForm = getFullFormOfProperty(val, false); @@ -221,7 +216,8 @@ export function preprocessAttrs (properties, className, cls): void { const baseClass = js.getClassName(getBaseClassWherePropertyDefined_DEV(propName, cls)); warnID(5517, className, propName, baseClass, propName); } - const notify = val.notify; + // NOTE: not yet support notify attributes + const notify = (val as any).notify; if (notify) { if (DEV) { error('not yet support notify attributes.'); @@ -238,12 +234,12 @@ export function preprocessAttrs (properties, className, cls): void { } const CALL_SUPER_DESTROY_REG_DEV = /\b\._super\b|destroy.*\.call\s*\(\s*\w+\s*[,|)]/; -export function doValidateMethodWithProps_DEV (func, funcName, className, cls, base): false | undefined { +export function doValidateMethodWithProps_DEV (func: string, funcName: string, className: string, cls: any, base: any): void { if (cls.__props__ && cls.__props__.indexOf(funcName) >= 0) { // find class that defines this method as a property const baseClassName = js.getClassName(getBaseClassWherePropertyDefined_DEV(funcName, cls)); errorID(3648, className, funcName, baseClassName); - return false; + return; } if (funcName === 'destroy' && js.isChildClassOf(base, cclegacy.Component) From 2820d3d1b0e48d7d74bfc7dc965b09bb99b81c84 Mon Sep 17 00:00:00 2001 From: PP_Pro Date: Mon, 9 Oct 2023 17:10:10 +0800 Subject: [PATCH 6/6] fix property.ts --- cocos/core/data/decorators/property.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cocos/core/data/decorators/property.ts b/cocos/core/data/decorators/property.ts index 98bfa94ef82..17d8e647658 100644 --- a/cocos/core/data/decorators/property.ts +++ b/cocos/core/data/decorators/property.ts @@ -192,8 +192,8 @@ function mergePropertyOptions ( descriptorOrInitializer: Parameters[2] | undefined, ): void { let fullOptions; - const isGetset = descriptorOrInitializer && typeof descriptorOrInitializer !== 'function' - && (descriptorOrInitializer.get || descriptorOrInitializer.set); + const isGetset = !!(descriptorOrInitializer && typeof descriptorOrInitializer !== 'function' + && (descriptorOrInitializer.get || descriptorOrInitializer.set)); if (options) { fullOptions = getFullFormOfProperty(options, isGetset); } @@ -208,11 +208,11 @@ function mergePropertyOptions ( warnID(3655, propertyKey as string, getClassName(ctor), propertyKey as string, propertyKey as string); } } - if ((descriptorOrInitializer as BabelPropertyDecoratorDescriptor).get) { - propertyRecord.get = (descriptorOrInitializer as BabelPropertyDecoratorDescriptor).get; + if (descriptorOrInitializer.get) { + propertyRecord.get = descriptorOrInitializer.get; } - if ((descriptorOrInitializer as BabelPropertyDecoratorDescriptor).set) { - propertyRecord.set = (descriptorOrInitializer as BabelPropertyDecoratorDescriptor).set; + if (descriptorOrInitializer.set) { + propertyRecord.set = descriptorOrInitializer.set; } } else { // Target property is non-accessor if (DEV && (propertyRecord.get || propertyRecord.set)) {