|
| 1 | +import type { BaseNestedOption, INestedOptionContainer } from './nested-option'; |
| 2 | +import { DEPRECATED_CONFIG_COMPONENTS } from './deprecated-config-map'; |
| 3 | +import WARNING_CODES from './warning-codes'; |
| 4 | +import { logWarning } from './warning-helper'; |
| 5 | + |
| 6 | +const warnedUsages = new Set<string>(); |
| 7 | + |
| 8 | +const NESTED_CLASS_NAME_REGEXP = /^(Dx[io][A-Z]\w+)Component$/; |
| 9 | + |
| 10 | +type DeprecatedConfigEntry = Record<string, string>; |
| 11 | + |
| 12 | +function getLegacySelector(nestedOption: BaseNestedOption): string | undefined { |
| 13 | + const className = nestedOption?.constructor?.name; |
| 14 | + if (!className) { |
| 15 | + return undefined; |
| 16 | + } |
| 17 | + |
| 18 | + const match = NESTED_CLASS_NAME_REGEXP.exec(className); |
| 19 | + if (!match) { |
| 20 | + return undefined; |
| 21 | + } |
| 22 | + |
| 23 | + const [, legacyName] = match; |
| 24 | + |
| 25 | + return toKebabCase(legacyName); |
| 26 | +} |
| 27 | + |
| 28 | +function getHostMapping(host: INestedOptionContainer | undefined): DeprecatedConfigEntry | undefined { |
| 29 | + const visited = new Set<INestedOptionContainer>(); |
| 30 | + let current = host; |
| 31 | + |
| 32 | + while (current && !visited.has(current)) { |
| 33 | + visited.add(current); |
| 34 | + |
| 35 | + const ctorName = current.constructor?.name; |
| 36 | + if (ctorName && Object.prototype.hasOwnProperty.call(DEPRECATED_CONFIG_COMPONENTS, ctorName)) { |
| 37 | + return DEPRECATED_CONFIG_COMPONENTS[ctorName] as DeprecatedConfigEntry; |
| 38 | + } |
| 39 | + |
| 40 | + current = (current as { _host?: INestedOptionContainer })._host; |
| 41 | + } |
| 42 | + |
| 43 | + return undefined; |
| 44 | +} |
| 45 | + |
| 46 | +export function warnIfLegacyNestedComponent(nestedOption: BaseNestedOption, host: INestedOptionContainer | undefined): void { |
| 47 | + const legacySelector = getLegacySelector(nestedOption); |
| 48 | + if (!legacySelector) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const mappingEntry = getHostMapping(host); |
| 53 | + if (!mappingEntry) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const replacement = mappingEntry[legacySelector]; |
| 58 | + if (!replacement) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const cacheKey = `${legacySelector}|${replacement}`; |
| 63 | + if (warnedUsages.has(cacheKey)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + warnedUsages.add(cacheKey); |
| 68 | + |
| 69 | + logWarning( |
| 70 | + WARNING_CODES.LEGACY_CONFIG_COMPONENT_USED, |
| 71 | + { legacySelector, replacement }, |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +function toKebabCase(value: string): string { |
| 76 | + return value |
| 77 | + .replace(/([a-z0-9])([A-Z])/g, '$1-$2') |
| 78 | + .replace(/([A-Z])([A-Z][a-z])/g, '$1-$2') |
| 79 | + .toLowerCase(); |
| 80 | +} |
0 commit comments