Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #16594: getComponent() can't support abstract class. #16917

Merged
merged 3 commits into from
Apr 22, 2024
Merged
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
11 changes: 8 additions & 3 deletions cocos/core/platform/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
THE SOFTWARE.
*/

/* eslint-disable no-console */
import { EDITOR, JSB, DEV, DEBUG } from 'internal:constants';
import debugInfos from '../../../DebugInfos';
import { legacyCC, ccwindow } from '../global-exports';
Expand Down Expand Up @@ -57,7 +58,7 @@
}

/**
* @en Outputs a log message to the console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

Check warning on line 61 in cocos/core/platform/debug.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 165. Maximum allowed is 150
* @zh 向控制台输出一条日志信息。这条信息可能是单个字符串(包括可选的替代字符串),也可能是一个或多个对象。
*/
export function log (...data: unknown[]): void {
Expand All @@ -66,7 +67,7 @@

/**
* @en
* Outputs a warning message to the console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

Check warning on line 70 in cocos/core/platform/debug.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 165. Maximum allowed is 150
* - In Cocos Creator, warning is yellow.
* - In Chrome, warning have a yellow warning icon with the message text.
* @zh
Expand All @@ -80,7 +81,7 @@

/**
* @en
* Outputs an error message to the console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

Check warning on line 84 in cocos/core/platform/debug.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 164. Maximum allowed is 150
* - In Cocos Creator, error is red.<br/>
* - In Chrome, error have a red icon along with red message text.<br/>
* @zh
Expand Down Expand Up @@ -120,8 +121,8 @@
*/
export function _resetDebugSetting (mode: DebugMode): void {
// reset
ccLog = ccWarn = ccError = ccAssert = ccDebug = (): void => {
};
// eslint-disable-next-line @typescript-eslint/no-empty-function
ccLog = ccWarn = ccError = ccAssert = ccDebug = (): void => {};

if (mode === DebugMode.NONE) {
return;
Expand All @@ -138,7 +139,8 @@
const logDiv = ccdocument.createElement('Div');
logDiv.setAttribute('id', 'logInfoDiv');
logDiv.setAttribute('width', '200');
logDiv.setAttribute('height', legacyCC.game.canvas.height);
const height: number = legacyCC.game.canvas.height;
logDiv.setAttribute('height', `${height}`);
const logDivStyle = logDiv.style;
logDivStyle.zIndex = '99999';
logDivStyle.position = 'absolute';
Expand Down Expand Up @@ -189,10 +191,13 @@
if (!console.error) {
console.error = console.log;
}

if (!console.warn) {
console.warn = console.log;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (EDITOR || console.error.bind) {
// use bind to avoid pollute call stacks
ccError = console.error.bind(console);
Expand Down Expand Up @@ -371,6 +376,6 @@
*/
export function setDisplayStats (displayStats: boolean): void {
if (legacyCC.profiler) {
displayStats ? legacyCC.profiler.showStats() : legacyCC.profiler.hideStats();

Check warning on line 379 in cocos/core/platform/debug.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected an assignment or function call and instead saw an expression
}
}
6 changes: 3 additions & 3 deletions cocos/core/value-types/bitmask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { errorID } from '../platform/debug';
* BitMask(obj);
* ```
*/
export function BitMask<T> (obj: T): T {
export function BitMask<T extends object> (obj: T): T {
if ('__bitmask__' in obj) {
return obj;
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export function BitMask<T> (obj: T): T {
* @returns @en True if it is a BitMask, false else.
* @zh 如果是 BitMask,返回 true;否则返回 false。
*/
BitMask.isBitMask = (BitMaskType): any => BitMaskType && BitMaskType.hasOwnProperty('__bitmask__');
BitMask.isBitMask = (BitMaskType): any => BitMaskType && Object.prototype.hasOwnProperty.call(BitMaskType, '__bitmask__');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change it?

Copy link
Contributor Author

@dumganhar dumganhar Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix an ESLint error:

Do not access Object.prototype method 'hasOwnProperty' from target object.eslintno-prototype-builtins

ESLint doesn't suggest to use obj.hasOwnProperty on target object since target object may contain a same key hasOwnProperty to override the original behavior.


/**
*
Expand Down Expand Up @@ -148,7 +148,7 @@ export function ccbitmask (bitmaskx): void {
if ('__bitmask__' in bitmaskx) {
return;
}
value(bitmaskx, '__bitmask__', null, true);
value(bitmaskx as Record<string | number, any>, '__bitmask__', null, true);
}

legacyCC.BitMask = BitMask;
20 changes: 11 additions & 9 deletions cocos/core/value-types/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import { errorID } from '../platform/debug';
import { assertIsTrue } from '../data/utils/asserts';

const hasOwnPropertyProto = Object.prototype.hasOwnProperty;

export type EnumType = Record<string, string | number>;

/**
Expand All @@ -46,7 +48,7 @@
* @zh 包含枚举名和值的 JavaScript literal 对象,或者是一个 TypeScript enum 类型。
* @return @en The defined enum type. @zh 定义的枚举类型。
*/
export function Enum<T> (obj: T): T {
export function Enum<T extends object> (obj: T): T {
if ('__enums__' in obj) {
return obj;
}
Expand All @@ -61,7 +63,7 @@
* 更新枚举对象的属性列表。
* @param obj @en The enum object to update. @zh 需要更新的枚举对象。
*/
Enum.update = <T> (obj: T): T => {
Enum.update = <T extends object> (obj: T): T => {
let lastIndex = -1;
const keys: string[] = Object.keys(obj);

Expand Down Expand Up @@ -93,7 +95,7 @@
return obj;
};

namespace Enum {

Check warning on line 98 in cocos/core/value-types/enum.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

ES2015 module syntax is preferred over namespaces
export interface Enumerator<EnumT> {
/**
* The name of the enumerator.
Expand All @@ -115,17 +117,17 @@
* Determines if the object is an enum type.
* @param enumType @en The object to judge. @zh 需要判断的对象。
*/
Enum.isEnum = <EnumT extends {}>(enumType: EnumT): boolean => enumType && enumType.hasOwnProperty('__enums__');
Enum.isEnum = <EnumT extends object>(enumType: EnumT): boolean => enumType && hasOwnPropertyProto.call(enumType, '__enums__');

function assertIsEnum <EnumT extends {}> (enumType: EnumT): asserts enumType is EnumT & EnumExtras<EnumT> {
assertIsTrue(enumType.hasOwnProperty('__enums__'));
function assertIsEnum <EnumT extends object> (enumType: EnumT): asserts enumType is EnumT & EnumExtras<EnumT> {
assertIsTrue(hasOwnPropertyProto.call(enumType, '__enums__'));
}

/**
* Get the enumerators from the enum type.
* @param enumType @en An enum type. @zh 枚举类型。
*/
Enum.getList = <EnumT extends {}>(enumType: EnumT): readonly Enum.Enumerator<EnumT>[] => {
Enum.getList = <EnumT extends object>(enumType: EnumT): readonly Enum.Enumerator<EnumT>[] => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix an eslint error:

Don't use {} as a type. {} actually means "any non-nullish value".

  • If you want a type meaning "any object", you probably want object instead.
  • If you want a type meaning "any value", you probably want unknown instead.
  • If you want a type meaning "empty object", you probably want Record<string, never> instead.
  • If you really want a type meaning "any non-nullish value", you probably want NonNullable<unknown> instead.eslint@typescript-eslint/ban-types

assertIsEnum(enumType);

if (enumType.__enums__) {
Expand All @@ -140,7 +142,7 @@
* @param enumType @en The enum type defined from [[Enum]] @zh 从[[Enum]]定义的枚举类型。
* @return {Object[]}
*/
function updateList<EnumT extends {}> (enumType: EnumT): readonly Enum.Enumerator<EnumT>[] {
function updateList<EnumT extends object> (enumType: EnumT): readonly Enum.Enumerator<EnumT>[] {
assertIsEnum(enumType);
const enums: any[] = enumType.__enums__ || [];
enums.length = 0;
Expand Down Expand Up @@ -170,7 +172,7 @@
* @param enumType @en The enum type defined from [[Enum]] @zh 从[[Enum]]定义的枚举类型。
* @param compareFn @en Function used to determine the order of the elements. @zh 用于确定元素顺序的函数。
*/
Enum.sortList = <EnumT extends {}> (enumType: EnumT, compareFn: (a, b) => number): void => {
Enum.sortList = <EnumT extends object> (enumType: EnumT, compareFn: (a, b) => number): void => {
assertIsEnum(enumType);
if (!Array.isArray(enumType.__enums__)) {
return;
Expand Down Expand Up @@ -200,7 +202,7 @@
* @en enumType An enum type, eg, a kind of type with similar semantic defined by TypeScript.
* @zh 枚举类型,例如 TypeScript 中定义的类型。
*/
export function ccenum<EnumT extends {}> (enumType: EnumT): void {
export function ccenum<EnumT extends object> (enumType: EnumT): void {
if (!('__enums__' in enumType)) {
value(enumType, '__enums__', null, true);
}
Expand Down
Loading
Loading