Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EngineErrorMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -3639,7 +3639,7 @@ This device does not support WebGL.

### 16334

A Class already exists with the same %s : %s. %s
Could not register class ID '%s' for '%s' since the ID has been registered for '%s'.

### 16335

Expand Down
10 changes: 2 additions & 8 deletions cocos/core/utils/js-typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
* @zh 如果不是空对象或者不是一个对象,返回 `true`;否则返回 `false`。
*/
export function isEmptyObject (obj: any): boolean {
for (const key in obj) {

Check failure on line 88 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Invalid loop. Its body allows only one iteration
return false;
}
return true;
Expand All @@ -102,7 +102,7 @@
* @param writable @en If the property is writable. @zh 属性是否可写。
* @param enumerable @en If the property is enumerable. @zh 属性是否可枚举。
*/
export const value = ((): (object: Record<string | number, any>, propertyName: string, value_: any, writable?: boolean, enumerable?: boolean) => void => {

Check warning on line 105 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 154. Maximum allowed is 150
const descriptor: PropertyDescriptor = {
value: undefined,
enumerable: false,
Expand All @@ -128,13 +128,13 @@
* @param enumerable @en If the property is enumerable. @zh 属性是否可列举。
* @param configurable @en If the property is configurable. @zh 属性是否可配置。
*/
export const getset = ((): (object: Record<string | number, any>, propertyName: string, getter: Getter, setter?: Setter | boolean, enumerable?: boolean, configurable?: boolean) => void => {

Check warning on line 131 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 189. Maximum allowed is 150
const descriptor: PropertyDescriptor = {
get: undefined,
set: undefined,
enumerable: false,
};
return (object: Record<string | number, any>, propertyName: string, getter: Getter, setter?: Setter | boolean, enumerable = false, configurable = false): void => {

Check warning on line 137 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 167. Maximum allowed is 150
if (typeof setter === 'boolean') {
logID(1031);
enumerable = setter;
Expand All @@ -160,7 +160,7 @@
* @param enumerable @en If the property is enumerable. @zh 属性是否可列举。
* @param configurable @en If the property is configurable. @zh 属性是否可配置。
*/
export const get = ((): (object: Record<string | number, any>, propertyName: string, getter: Getter, enumerable?: boolean, configurable?: boolean) => void => {

Check warning on line 163 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 159. Maximum allowed is 150
const descriptor: PropertyDescriptor = {
get: undefined,
enumerable: false,
Expand All @@ -184,7 +184,7 @@
* @param enumerable @en If the property is enumerable. @zh 属性是否可列举。
* @param configurable @en If the property is configurable. @zh 属性是否可配置。
*/
export const set = ((): (object: Record<string | number, any>, propertyName: string, setter: Setter, enumerable?: boolean, configurable?: boolean) => void => {

Check warning on line 187 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 159. Maximum allowed is 150
const descriptor: PropertyDescriptor = {
set: undefined,
enumerable: false,
Expand Down Expand Up @@ -256,11 +256,11 @@
if (str.charAt(0) === '[') {
// str is "[object objectClass]"
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
arr = /\[\w+\s*(\w+)\]/.exec(str);

Check failure on line 259 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`
} else {
// str is function objectClass () {} for IE Firefox
// eslint-disable-next-line @typescript-eslint/prefer-regexp-exec
arr = /^function\s*(\w+)/.exec(str);

Check failure on line 263 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`
}
if (arr && arr.length === 2) {
ret = arr[1];
Expand Down Expand Up @@ -303,9 +303,9 @@
}

if (writable) {
getset(object, oldProp, getter, setter);

Check failure on line 306 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `Record<string | number, any>`
} else {
get(object, oldProp, getter);

Check failure on line 308 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `Record<string | number, any>`
}
}

Expand All @@ -323,7 +323,7 @@
export function obsoletes (obj, objName, props, writable): void {
for (const obsoleted in props) {
const newName = props[obsoleted];
obsolete(obj, `${objName}.${obsoleted}`, newName, writable);

Check failure on line 326 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `boolean | undefined`

Check failure on line 326 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`
}
}

Expand Down Expand Up @@ -366,7 +366,7 @@
return '';
}
if (subst.length === 0) {
return `${msg}`;

Check warning on line 369 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Invalid type "unknown" of template literal expression
}

const hasSubstitution = typeof msg === 'string' && REGEXP_NUM_OR_STR.test(msg);
Expand All @@ -374,15 +374,15 @@
for (const arg of subst) {
const regExpToTest = typeof arg === 'number' ? REGEXP_NUM_OR_STR : REGEXP_STR;
if (regExpToTest.test(msg as string)) {
const notReplaceFunction = `${arg}`;

Check warning on line 377 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Invalid type "unknown" of template literal expression
msg = (msg as string).replace(regExpToTest, notReplaceFunction);
} else {
msg += ` ${arg}`;

Check warning on line 380 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Invalid type "unknown" of template literal expression

Check failure on line 380 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Invalid operand for a '+' operation. Operands must each be a number or string, allowing a string + any of: `any`, `boolean`, `null`, `RegExp`, `undefined`. Got `unknown`
}
}
} else {
for (const arg of subst) {
msg += ` ${arg}`;

Check warning on line 385 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Invalid type "unknown" of template literal expression

Check failure on line 385 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Invalid operand for a '+' operation. Operands must each be a number or string, allowing a string + any of: `any`, `boolean`, `null`, `RegExp`, `undefined`. Got `unknown`
}
}
return msg as string;
Expand Down Expand Up @@ -522,7 +522,7 @@
errorID(5405);
return;
}
if (Object.keys(cls.prototype).length > 0) {

Check failure on line 525 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `{}`
errorID(5406);
}
}
Expand Down Expand Up @@ -620,7 +620,7 @@
export const _nameToClass: Record<string, Constructor> = createMap(true);

function setup (tag: string, table: Record<string | number, any>, allowExist: boolean): (id: string, constructor: Constructor) => void {
return function (id: string, constructor: Constructor): void {

Check warning on line 623 in cocos/core/utils/js-typed.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected unnamed function
// deregister old
// eslint-disable-next-line no-prototype-builtins
if (constructor.prototype.hasOwnProperty(tag)) {
Expand All @@ -631,14 +631,8 @@
if (id) {
const registered = table[id];
if (!allowExist && registered && registered !== constructor) {
let detail = '';
if (TEST) {
// eslint-disable-next-line no-multi-str
detail += ' (This may be caused by error of unit test.) \
If you dont need serialization, you can set class id to "". You can also call \
js.unregisterClass to remove the id of unused class';
}
errorID(16334, tag, id, detail);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
errorID(16334, id, constructor.name, registered.name);
} else {
table[id] = constructor;
}
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/core/builtin/DebugInfos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ ccstd::unordered_map<uint32_t, ccstd::string> debugInfos = {
{ 16331, "InputAssemblerInfo.vertexBuffers is null." },
{ 16332, "Illegal index buffer stride." },
{ 16333, "This device does not support WebGL." },
{ 16334, "A Class already exists with the same %s : %s. %s" },
{ 16334, "Could not register class ID '%s' for '%s' since the ID has been registered for '%s'." },
{ 16335, "'%s' has already been set as name or alias of another class." },
{ 16336, "'%s' has already been set as id or alias of another class." },
{ 16337, "can not support canvas rendering in 3D" },
Expand Down
17 changes: 15 additions & 2 deletions tests/core/ccclass.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ccclass } from 'cc.decorator';
import { warnID } from '../../cocos/core';
import { warnID, errorID } from '../../cocos/core';
import { float, property } from '../../cocos/core/data/class-decorator';
import * as requiringFrame from '../../cocos/core/data/utils/requiring-frame';
import { getClassName, unregisterClass, getClassId } from '../../cocos/core/utils/js-typed';
Expand Down Expand Up @@ -62,6 +62,19 @@ describe('ccclass warnings', () => {
});
});

test('Error on duplicated class id', () => {
// @ts-expect-error
errorID.mockClear();

const id = 'test-duplicated-class-id';

{@ccclass(id) class A {}}
{@ccclass(id) class B {}}

expect(errorID).toHaveBeenCalledTimes(1);
expect(errorID).toHaveBeenCalledWith(16334, id, 'B', 'A');
});

describe('Class id & class name', () => {
type ExpectedNameComeFrom = 'self-name' | 'explicit-name' | 'frame-name';
type ExpectedIdComeFrom = 'self-name' | 'explicit-name' | 'frame-uuid' | 'temp-id';
Expand Down Expand Up @@ -136,4 +149,4 @@ describe('Class id & class name', () => {
unregisterClass(Cls_);
}
});
});
});
Loading