From 21c71cf6d0e590ca09e2c9478eb8857910797582 Mon Sep 17 00:00:00 2001 From: Borislav Kulov Date: Tue, 18 Dec 2018 18:45:21 +0200 Subject: [PATCH] chore(*): Invoke original getter/setter (#2915) Invoke original getter/setter for deprecated properties. --- .../src/lib/core/deprecateDecorators.ts | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/projects/igniteui-angular/src/lib/core/deprecateDecorators.ts b/projects/igniteui-angular/src/lib/core/deprecateDecorators.ts index 8bab6112216..aa391cef96b 100644 --- a/projects/igniteui-angular/src/lib/core/deprecateDecorators.ts +++ b/projects/igniteui-angular/src/lib/core/deprecateDecorators.ts @@ -11,14 +11,10 @@ export function DeprecateMethod(message: string): MethodDecorator { const originalMethod = descriptor.value; descriptor.value = function () { - if (!isMessageShown && isDevMode()) { - const targetName = typeof target === 'function' ? target.name : target.constructor.name; - isMessageShown = true; + const targetName = typeof target === 'function' ? target.name : target.constructor.name; + isMessageShown = showMessage(`${targetName}.${key}: ${message}`, isMessageShown); - console.warn(`${targetName}.${key}: ${message}`); - } - - return originalMethod.apply(this, arguments); + return originalMethod.call(this, arguments); }; return descriptor; @@ -36,14 +32,30 @@ export function DeprecateProperty(message: string): PropertyDecorator { // use backing field to set/get the value of the property to ensure there won't be infinite recursive calls const newKey = generateUniqueKey(target, key); - Object.defineProperty(target, key, { + let getter, setter; + const orignalDescriptor = Object.getOwnPropertyDescriptor(target, key); + if (orignalDescriptor) { + getter = orignalDescriptor.get; + setter = orignalDescriptor.set; + + delete target[key]; + } + + return Object.defineProperty(target, key, { set(value) { - this[newKey] = value; + isMessageShown = showMessage(`${target.constructor.name}.${key}: ${message}`, isMessageShown); + + if (setter) { + setter.call(this, value); + } else { + this[newKey] = value; + } }, get() { - if (!isMessageShown && isDevMode()) { - isMessageShown = true; - console.warn(`${target.constructor.name}.${key}: ${message}`); + isMessageShown = showMessage(`${target.constructor.name}.${key}: ${message}`, isMessageShown); + + if (getter) { + return getter.call(this); } return this[newKey]; @@ -63,3 +75,14 @@ function generateUniqueKey(target: any, key: string): string { return newKey; } + +/** + * @hidden + */ +function showMessage(message: string, isMessageShown: boolean): boolean { + if (!isMessageShown && isDevMode()) { + console.warn(message); + } + + return true; +}