|
import * as React from 'react'; |
|
import rcWarning, { resetWarned as rcResetWarned } from 'rc-util/lib/warning'; |
|
|
|
export function noop() {} |
|
|
|
let deprecatedWarnList: Record<string, string[]> | null = null; |
|
|
|
export function resetWarned() { |
|
deprecatedWarnList = null; |
|
rcResetWarned(); |
|
} |
|
|
|
type Warning = (valid: boolean, component: string, message?: string) => void; |
|
|
|
let _warning: Warning = noop; |
|
if (process.env.NODE_ENV !== 'production') { |
|
_warning = (valid, component, message) => { |
|
rcWarning(valid, `[antd: ${component}] ${message}`); |
|
|
|
|
|
if (process.env.NODE_ENV === 'test') { |
|
resetWarned(); |
|
} |
|
}; |
|
} |
|
const warning = _warning; |
|
|
|
type BaseTypeWarning = ( |
|
valid: boolean, |
|
|
|
|
|
|
|
|
|
|
|
type: 'deprecated' | 'usage' | 'breaking', |
|
message?: string, |
|
) => void; |
|
|
|
type TypeWarning = BaseTypeWarning & { |
|
deprecated: (valid: boolean, oldProp: string, newProp: string, message?: string) => void; |
|
}; |
|
|
|
export interface WarningContextProps { |
|
|
|
|
|
|
|
|
|
|
|
strict?: boolean; |
|
} |
|
|
|
export const WarningContext = React.createContext<WarningContextProps>({}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
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 || ''); |
|
} |
|
|
|
|
|
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; |
|
|