-
Notifications
You must be signed in to change notification settings - Fork 654
Angular: Configuration Components Deprecation Warning #31511
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
base: 25_2
Are you sure you want to change the base?
Changes from 7 commits
12f5284
4d83b6c
c8eb424
57617ef
daeed9c
944b9f8
04f909c
5e5e9ab
0c5de27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,5 @@ export const knownWarnings = [ | |
| 'W0019 -', | ||
| 'W0022 -', | ||
| 'W2108 -', | ||
| 'W3001 -', | ||
| ]; | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import type { BaseNestedOption, INestedOptionContainer } from './nested-option'; | ||
| import { DEPRECATED_CONFIG_COMPONENTS } from './deprecated-config-map'; | ||
| import WARNING_CODES from './warning-codes'; | ||
| import { logWarning } from './warning-helper'; | ||
|
|
||
| const warnedUsages = new Set<string>(); | ||
|
|
||
| const LEGACY_CLASS_NAME_REGEXP = /^Dx([io])([A-Za-z0-9]+)Component$/; | ||
|
|
||
| type DeprecatedConfigEntry = Record<string, string>; | ||
|
|
||
| function getLegacySelector(nestedOption: BaseNestedOption): string | undefined { | ||
| const className = nestedOption?.constructor?.name; | ||
| if (!className) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const match = LEGACY_CLASS_NAME_REGEXP.exec(className); | ||
| if (!match) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const [, type, rest] = match; | ||
| const prefix = type === 'o' ? 'dxo-' : 'dxi-'; | ||
|
|
||
| return `${prefix}${toKebabCase(rest)}`; | ||
| } | ||
|
|
||
| function getHostMapping(host: INestedOptionContainer | undefined): DeprecatedConfigEntry | undefined { | ||
| const visited = new Set<INestedOptionContainer>(); | ||
| let current = host; | ||
|
|
||
| while (current && !visited.has(current)) { | ||
| visited.add(current); | ||
|
|
||
| const ctorName = current.constructor?.name; | ||
| if (ctorName && Object.prototype.hasOwnProperty.call(DEPRECATED_CONFIG_COMPONENTS, ctorName)) { | ||
| return DEPRECATED_CONFIG_COMPONENTS[ctorName] as DeprecatedConfigEntry; | ||
| } | ||
|
|
||
| current = (current as { _host?: INestedOptionContainer })._host; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| export function warnIfLegacyNestedComponent(nestedOption: BaseNestedOption, host: INestedOptionContainer | undefined): void { | ||
| const legacySelector = getLegacySelector(nestedOption); | ||
| if (!legacySelector) { | ||
| return; | ||
| } | ||
|
|
||
| const mappingEntry = getHostMapping(host); | ||
| if (!mappingEntry) { | ||
| return; | ||
| } | ||
|
|
||
| const replacement = mappingEntry[legacySelector]; | ||
| if (!replacement) { | ||
| return; | ||
| } | ||
|
|
||
| const cacheKey = `${legacySelector}|${replacement}`; | ||
| if (warnedUsages.has(cacheKey)) { | ||
| return; | ||
| } | ||
|
|
||
| warnedUsages.add(cacheKey); | ||
|
|
||
| logWarning( | ||
| WARNING_CODES.LEGACY_CONFIG_COMPONENT_USED, | ||
| { legacySelector, replacement }, | ||
| ); | ||
| } | ||
|
|
||
| function toKebabCase(value: string): string { | ||
| return value | ||
| .replace(/([a-z0-9])([A-Z])/g, '$1-$2') | ||
| .replace(/([A-Z])([A-Z][a-z])/g, '$1-$2') | ||
| .toLowerCase(); | ||
| } | ||
ajivanyandev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,3 +8,5 @@ export * from './template'; | |||||
| export * from './transfer-state'; | ||||||
| export * from './utils'; | ||||||
| export * from './watcher-helper'; | ||||||
| export * from './warning-helper'; | ||||||
| export * from './warning-codes'; | ||||||
|
Comment on lines
+11
to
+12
|
||||||
| export * from './warning-helper'; | |
| export * from './warning-codes'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const WARNING_CODES = { | ||
| LEGACY_CONFIG_COMPONENT_USED: { | ||
| code: 'W3001', | ||
| template: 'You are using the legacy {legacySelector} configuration component. We recommend ' | ||
| + 'upgrading to the new {replacement} configuration component.', | ||
| // TODO: link to docs article, when available | ||
| }, | ||
| } as const; | ||
|
|
||
| export type WarningId = keyof typeof WARNING_CODES; | ||
| export type WarningDefinition = typeof WARNING_CODES[WarningId]; | ||
|
|
||
| export default WARNING_CODES; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { WarningDefinition } from './warning-codes'; | ||
|
|
||
| type TemplatePrimitive = string | number | boolean; | ||
| export type TemplateArgs = TemplatePrimitive[] | Record<string, TemplatePrimitive>; | ||
|
|
||
| function formatWarningMessage(template: string, args?: TemplateArgs): string { | ||
| if (!args) { | ||
| return template; | ||
| } | ||
|
|
||
| if (Array.isArray(args)) { | ||
| return args.reduce<string>( | ||
| (message, value, index) => replacePlaceholder(message, `{${index}}`, String(value)), | ||
| template, | ||
| ); | ||
| } | ||
|
|
||
| return Object.entries(args).reduce<string>( | ||
| (message, [key, value]) => replacePlaceholder(message, `{${key}}`, String(value)), | ||
| template, | ||
| ); | ||
| } | ||
|
|
||
| export function logWarning(warning: WarningDefinition, args?: TemplateArgs): void { | ||
| if (typeof console === 'undefined' || typeof console.warn !== 'function') { | ||
| return; | ||
| } | ||
|
|
||
| const message = formatWarningMessage(warning.template, args); | ||
| console.warn(`${warning.code} - ${message}`); | ||
| } | ||
|
|
||
| function replacePlaceholder(message: string, placeholder: string, value: string): string { | ||
| return message.split(placeholder).join(value); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
hasOwn(available in ES2022) or check the property directly on the object instead of usingObject.prototype.hasOwnProperty.call. The current approach is overly verbose. Consider:if (ctorName && ctorName in DEPRECATED_CONFIG_COMPONENTS)orif (ctorName && DEPRECATED_CONFIG_COMPONENTS[ctorName])since DEPRECATED_CONFIG_COMPONENTS is a const object with known properties.