-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite deprecation decorators (#3447)
* chore(*): Remove DeprecateClass util method (#2915) * chore(datepicker): Replace deprecated selector (#2915) * chore(*): Rewrite deprecation decorator for properties and methods (#2915) * chore(*): Fix lint warnings (#2915) * chore(*): Invoke original getter/setter (#2915) Invoke original getter/setter for deprecated properties. * feat(*): Improve property deprecation messages (#2915) * chore(*): Fix lint errors (#2915) * chore(datepicker): Revert - Replace deprecated selector (#2915) This reverts commit a2e731b. * chore(datepicker): Restore removed selector (#2915) * chore(datepicker): Show deprecation message when necessary (#2915) * chore(datepicker): Replace deprecated selector (#2915)
- Loading branch information
1 parent
5ad26bf
commit c3ae627
Showing
12 changed files
with
128 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 79 additions & 10 deletions
89
projects/igniteui-angular/src/lib/core/deprecateDecorators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,96 @@ | ||
import { isDevMode } from '@angular/core'; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export function DeprecateClass(message: string): ClassDecorator { | ||
return (constructor: any) => { | ||
console.warn(constructor.name + ': ' + message); | ||
export function DeprecateMethod(message: string): MethodDecorator { | ||
let isMessageShown = false; | ||
|
||
return function (target: any, key: string, descriptor: PropertyDescriptor) { | ||
if (descriptor && descriptor.value) { | ||
const originalMethod = descriptor.value; | ||
|
||
descriptor.value = function () { | ||
const targetName = typeof target === 'function' ? target.name : target.constructor.name; | ||
isMessageShown = showMessage(`${targetName}.${key}: ${message}`, isMessageShown); | ||
|
||
return originalMethod.call(this, arguments); | ||
}; | ||
|
||
return descriptor; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export function DeprecateMethod(message: string): MethodDecorator { | ||
return (constructor: any) => { | ||
console.warn(constructor.constructor.name + ': ' + message); | ||
export function DeprecateProperty(message: string): PropertyDecorator { | ||
return function(target: any, key: string) { | ||
let isMessageShown = false; | ||
const messageToDisplay = `${target.constructor.name}.${key}: ${message}`; | ||
|
||
// if the target already has the property defined | ||
const originalDescriptor = Object.getOwnPropertyDescriptor(target, key); | ||
if (originalDescriptor) { | ||
let getter, setter; | ||
getter = originalDescriptor.get; | ||
setter = originalDescriptor.set; | ||
|
||
if (getter) { | ||
originalDescriptor.get = function() { | ||
isMessageShown = showMessage(messageToDisplay, isMessageShown); | ||
return getter.call(this); | ||
}; | ||
} | ||
|
||
if (setter) { | ||
originalDescriptor.set = function (value) { | ||
isMessageShown = showMessage(messageToDisplay, isMessageShown); | ||
setter.call(this, value); | ||
}; | ||
} | ||
|
||
return originalDescriptor; | ||
} | ||
|
||
// the target doesn't contain a descriptor for that property, so create one | ||
// 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); | ||
return Object.defineProperty(target, key, { | ||
configurable: true, | ||
enumerable: true, | ||
set: function(value) { | ||
isMessageShown = showMessage(messageToDisplay, isMessageShown); | ||
this[newKey] = value; | ||
}, | ||
get: function() { | ||
isMessageShown = showMessage(messageToDisplay, isMessageShown); | ||
return this[newKey]; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export function DeprecateProperty(message: string): PropertyDecorator { | ||
return (constructor: any) => { | ||
console.warn(constructor.constructor.name + ': ' + message); | ||
}; | ||
function generateUniqueKey(target: any, key: string): string { | ||
let newKey = '_' + key; | ||
while (target.hasOwnProperty(newKey)) { | ||
newKey = '_' + newKey; | ||
} | ||
|
||
return newKey; | ||
} | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
function showMessage(message: string, isMessageShown: boolean): boolean { | ||
if (!isMessageShown && isDevMode()) { | ||
console.warn(message); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters