Skip to content

Commit 83fc28b

Browse files
committedDec 13, 2023
Add fallback to no-conflict version
1 parent 359e1f6 commit 83fc28b

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed
 

‎ReflectNoConflict.ts

+27
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ export function decorate(decorators: (ClassDecorator | MemberDecorator)[], targe
222222
*
223223
*/
224224
export function metadata(metadataKey: any, metadataValue: any): { (target: Function): void; (target: any, propertyKey: string | symbol): void; } {
225+
if (typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata !== metadata) {
226+
return Reflect.metadata(metadataKey, metadataValue);
227+
}
225228
function decorator(target: Function): void;
226229
function decorator(target: any, propertyKey: string | symbol): void;
227230
function decorator(target: any, propertyKey?: string | symbol): void {
@@ -333,6 +336,9 @@ export function defineMetadata(metadataKey: any, metadataValue: any, target: any
333336
*
334337
*/
335338
export function defineMetadata(metadataKey: any, metadataValue: any, target: any, propertyKey?: string | symbol): void {
339+
if (typeof Reflect !== "undefined" && typeof Reflect.defineMetadata === "function" && Reflect.defineMetadata !== defineMetadata) {
340+
return Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey!);
341+
}
336342
if (!IsObject(target)) throw new TypeError();
337343
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
338344
return OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
@@ -424,6 +430,9 @@ export function hasMetadata(metadataKey: any, target: any, propertyKey: string |
424430
*
425431
*/
426432
export function hasMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean {
433+
if (typeof Reflect !== "undefined" && typeof Reflect.hasMetadata === "function" && Reflect.hasMetadata !== hasMetadata) {
434+
return Reflect.hasMetadata(metadataKey, target, propertyKey!);
435+
}
427436
if (!IsObject(target)) throw new TypeError();
428437
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
429438
return OrdinaryHasMetadata(metadataKey, target, propertyKey);
@@ -515,6 +524,9 @@ export function hasOwnMetadata(metadataKey: any, target: any, propertyKey: strin
515524
*
516525
*/
517526
export function hasOwnMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean {
527+
if (typeof Reflect !== "undefined" && typeof Reflect.hasOwnMetadata === "function" && Reflect.hasOwnMetadata !== hasOwnMetadata) {
528+
return Reflect.hasOwnMetadata(metadataKey, target, propertyKey!);
529+
}
518530
if (!IsObject(target)) throw new TypeError();
519531
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
520532
return OrdinaryHasOwnMetadata(metadataKey, target, propertyKey);
@@ -606,6 +618,9 @@ export function getMetadata(metadataKey: any, target: any, propertyKey: string |
606618
*
607619
*/
608620
export function getMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): any {
621+
if (typeof Reflect !== "undefined" && typeof Reflect.getMetadata === "function" && Reflect.getMetadata !== getMetadata) {
622+
return Reflect.getMetadata(metadataKey, target, propertyKey!);
623+
}
609624
if (!IsObject(target)) throw new TypeError();
610625
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
611626
return OrdinaryGetMetadata(metadataKey, target, propertyKey);
@@ -697,6 +712,9 @@ export function getOwnMetadata(metadataKey: any, target: any, propertyKey: strin
697712
*
698713
*/
699714
export function getOwnMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): any {
715+
if (typeof Reflect !== "undefined" && typeof Reflect.getOwnMetadata === "function" && Reflect.getOwnMetadata !== getOwnMetadata) {
716+
return Reflect.getOwnMetadata(metadataKey, target, propertyKey!);
717+
}
700718
if (!IsObject(target)) throw new TypeError();
701719
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
702720
return OrdinaryGetOwnMetadata(metadataKey, target, propertyKey);
@@ -785,6 +803,9 @@ export function getMetadataKeys(target: any, propertyKey: string | symbol): any[
785803
*
786804
*/
787805
export function getMetadataKeys(target: any, propertyKey?: string | symbol): any[] {
806+
if (typeof Reflect !== "undefined" && typeof Reflect.getMetadataKeys === "function" && Reflect.getMetadataKeys !== getMetadataKeys) {
807+
return Reflect.getMetadataKeys(target, propertyKey!);
808+
}
788809
if (!IsObject(target)) throw new TypeError();
789810
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
790811
return OrdinaryMetadataKeys(target, propertyKey);
@@ -873,6 +894,9 @@ export function getOwnMetadataKeys(target: any, propertyKey: string | symbol): a
873894
*
874895
*/
875896
export function getOwnMetadataKeys(target: any, propertyKey?: string | symbol): any[] {
897+
if (typeof Reflect !== "undefined" && typeof Reflect.getOwnMetadataKeys === "function" && Reflect.getOwnMetadataKeys !== getOwnMetadataKeys) {
898+
return Reflect.getOwnMetadataKeys(target, propertyKey!);
899+
}
876900
if (!IsObject(target)) throw new TypeError();
877901
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
878902
return OrdinaryOwnMetadataKeys(target, propertyKey);
@@ -964,6 +988,9 @@ export function deleteMetadata(metadataKey: any, target: any, propertyKey: strin
964988
*
965989
*/
966990
export function deleteMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean {
991+
if (typeof Reflect !== "undefined" && typeof Reflect.deleteMetadata === "function" && Reflect.deleteMetadata !== deleteMetadata) {
992+
return Reflect.deleteMetadata(metadataKey, target, propertyKey!);
993+
}
967994
if (!IsObject(target)) throw new TypeError();
968995
if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey);
969996
const metadataMap = GetOrCreateMetadataMap(target, propertyKey, /*Create*/ false);

‎test/full/reflect-metadata.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("Reflect.metadata", () => {
1313

1414
it("DecoratorThrowsWithInvalidTargetWithTargetKey", () => {
1515
let decorator = Reflect.metadata("key", "value");
16-
assert.throws(() => decorator(undefined, "name"), TypeError);
16+
assert.throws(() => decorator(undefined!, "name"), TypeError);
1717
});
1818

1919
it("DecoratorThrowsWithInvalidTargetKey", () => {

‎test/lite/reflect-metadata.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("Reflect.metadata", () => {
1313

1414
it("DecoratorThrowsWithInvalidTargetWithTargetKey", () => {
1515
let decorator = Reflect.metadata("key", "value");
16-
assert.throws(() => decorator(undefined, "name"), TypeError);
16+
assert.throws(() => decorator(undefined!, "name"), TypeError);
1717
});
1818

1919
it("DecoratorThrowsWithInvalidTargetKey", () => {

‎test/no-conflict/reflect-metadata.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("Reflect.metadata", () => {
1313

1414
it("DecoratorThrowsWithInvalidTargetWithTargetKey", () => {
1515
let decorator = Reflect_.metadata("key", "value");
16-
assert.throws(() => decorator(undefined, "name"), TypeError);
16+
assert.throws(() => decorator(undefined!, "name"), TypeError);
1717
});
1818

1919
it("DecoratorThrowsWithInvalidTargetKey", () => {

0 commit comments

Comments
 (0)
Please sign in to comment.