warning.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as React from 'react';
  2. import rcWarning, { resetWarned as rcResetWarned } from "rc-util/es/warning";
  3. export function noop() {}
  4. let deprecatedWarnList = null;
  5. export function resetWarned() {
  6. deprecatedWarnList = null;
  7. rcResetWarned();
  8. }
  9. let _warning = noop;
  10. if (process.env.NODE_ENV !== 'production') {
  11. _warning = (valid, component, message) => {
  12. rcWarning(valid, `[antd: ${component}] ${message}`);
  13. // StrictMode will inject console which will not throw warning in React 17.
  14. if (process.env.NODE_ENV === 'test') {
  15. resetWarned();
  16. }
  17. };
  18. }
  19. const warning = _warning;
  20. export const WarningContext = /*#__PURE__*/React.createContext({});
  21. /**
  22. * This is a hook but we not named as `useWarning`
  23. * since this is only used in development.
  24. * We should always wrap this in `if (process.env.NODE_ENV !== 'production')` condition
  25. */
  26. export const devUseWarning = process.env.NODE_ENV !== 'production' ? component => {
  27. const {
  28. strict
  29. } = React.useContext(WarningContext);
  30. const typeWarning = (valid, type, message) => {
  31. if (!valid) {
  32. if (strict === false && type === 'deprecated') {
  33. const existWarning = deprecatedWarnList;
  34. if (!deprecatedWarnList) {
  35. deprecatedWarnList = {};
  36. }
  37. deprecatedWarnList[component] = deprecatedWarnList[component] || [];
  38. if (!deprecatedWarnList[component].includes(message || '')) {
  39. deprecatedWarnList[component].push(message || '');
  40. }
  41. // Warning for the first time
  42. if (!existWarning) {
  43. console.warn('[antd] There exists deprecated usage in your code:', deprecatedWarnList);
  44. }
  45. } else {
  46. process.env.NODE_ENV !== "production" ? warning(valid, component, message) : void 0;
  47. }
  48. }
  49. };
  50. typeWarning.deprecated = (valid, oldProp, newProp, message) => {
  51. typeWarning(valid, 'deprecated', `\`${oldProp}\` is deprecated. Please use \`${newProp}\` instead.${message ? ` ${message}` : ''}`);
  52. };
  53. return typeWarning;
  54. } : () => {
  55. const noopWarning = () => {};
  56. noopWarning.deprecated = noop;
  57. return noopWarning;
  58. };
  59. export default warning;