ant-design/components/_util/warning.ts
lijianan ab0e07e25d
Some checks are pending
Publish Any Commit / build (push) Waiting to run
✅ test v6 / lint (push) Waiting to run
✅ test v6 / test-react-legacy (18, 1/2) (push) Waiting to run
✅ test v6 / test-react-legacy (18, 2/2) (push) Waiting to run
✅ test v6 / test-node (push) Waiting to run
✅ test v6 / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test v6 / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test v6 / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test v6 / test-coverage (push) Blocked by required conditions
✅ test v6 / build (push) Waiting to run
✅ test v6 / test lib/es module (es, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (es, 2/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
refactor: [v6] use rc-component (#52337)
* refactor: use @rc-component

* chore: adjust compile

* test: fix logic

* chore: back of reset

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
2025-01-10 14:14:31 +08:00

114 lines
3.4 KiB
TypeScript

import * as React from 'react';
import rcWarning, { resetWarned as rcResetWarned } from '@rc-component/util/lib/warning';
import { resetWarned as deprecatedRcResetWarned } from 'rc-util/lib/warning';
export function noop() {}
let deprecatedWarnList: Record<string, string[]> | null = null;
export function resetWarned() {
deprecatedWarnList = null;
rcResetWarned();
deprecatedRcResetWarned();
}
type Warning = (valid: boolean, component: string, message?: string) => void;
// eslint-disable-next-line import/no-mutable-exports
let warning: Warning = noop;
if (process.env.NODE_ENV !== 'production') {
warning = (valid, component, message) => {
rcWarning(valid, `[antd: ${component}] ${message}`);
// StrictMode will inject console which will not throw warning in React 17.
if (process.env.NODE_ENV === 'test') {
resetWarned();
}
};
}
type BaseTypeWarning = (
valid: boolean,
/**
* - deprecated: Some API will be removed in future but still support now.
* - usage: Some API usage is not correct.
* - breaking: Breaking change like API is removed.
*/
type: 'deprecated' | 'usage' | 'breaking',
message?: string,
) => void;
type TypeWarning = BaseTypeWarning & {
deprecated: (valid: boolean, oldProp: string, newProp: string, message?: string) => void;
};
export interface WarningContextProps {
/**
* @descCN 设置警告等级,设置 `false` 时会将废弃相关信息聚合为单条信息。
* @descEN Set the warning level. When set to `false`, discard related information will be aggregated into a single message.
* @since 5.10.0
*/
strict?: boolean;
}
export const WarningContext = React.createContext<WarningContextProps>({});
/**
* This is a hook but we not named as `useWarning`
* since this is only used in development.
* We should always wrap this in `if (process.env.NODE_ENV !== 'production')` condition
*/
export const devUseWarning: (component: string) => TypeWarning =
process.env.NODE_ENV !== 'production'
? (component) => {
const { strict } = React.useContext(WarningContext);
const typeWarning: TypeWarning = (valid, type, message) => {
if (!valid) {
if (strict === false && type === 'deprecated') {
const existWarning = deprecatedWarnList;
if (!deprecatedWarnList) {
deprecatedWarnList = {};
}
deprecatedWarnList[component] = deprecatedWarnList[component] || [];
if (!deprecatedWarnList[component].includes(message || '')) {
deprecatedWarnList[component].push(message || '');
}
// Warning for the first time
if (!existWarning) {
console.warn(
'[antd] There exists deprecated usage in your code:',
deprecatedWarnList,
);
}
} else {
warning(valid, component, message);
}
}
};
typeWarning.deprecated = (valid, oldProp, newProp, message) => {
typeWarning(
valid,
'deprecated',
`\`${oldProp}\` is deprecated. Please use \`${newProp}\` instead.${
message ? ` ${message}` : ''
}`,
);
};
return typeWarning;
}
: () => {
const noopWarning: TypeWarning = () => {};
noopWarning.deprecated = noop;
return noopWarning;
};
export default warning;