-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use simpler reflect-metadata implementation (#315)
* remove core-js dependency in preset * remove core-js import tests * add small reflect metadata lib * add test for relfect metadata lib * update typescript version in preset to current
- Loading branch information
Showing
10 changed files
with
63 additions
and
122 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import '../reflectMetadata'; | ||
|
||
describe('using minimal reflect metadata', () => { | ||
|
||
// to avoid type conflicts with global.Reflect comfortably | ||
let reflect: any = Reflect; | ||
|
||
it(`should make metadata() and getOwnMetadata() available on global.Reflect`, () => { | ||
expect(typeof reflect.metadata).toBe('function') | ||
expect(typeof reflect.getOwnMetadata).toBe('function') | ||
}); | ||
|
||
it('should set and retrieve metadata using Reflect.metadata and Reflect.getOwnMetadata', () => { | ||
const metadataValue = () => ({ test: 'object'}); | ||
const functionClass = function FunctionClass() {}; | ||
|
||
reflect.metadata('design:paramtypes', metadataValue)(functionClass); | ||
const retrieved = reflect.getOwnMetadata('design:paramtypes', functionClass); | ||
expect(retrieved).toBe(metadataValue); | ||
expect(retrieved().test).toBe('object'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const METADATA_KEY_PARAMTYPES = "design:paramtypes"; | ||
const CTOR_PARAMETERS_JPA = "ctorParametersJPA"; | ||
|
||
// weird workaround to avoid 'ReferenceError: globalThis is not defined' in node version < 11 | ||
(global as any).globalThis = (global as any).globalThis || undefined | ||
|
||
const _global = globalThis || global; // globalThis available since node v12/TS v3.4 | ||
const reflect: any = _global["Reflect"]; // reflect type in global has not these methods | ||
|
||
// let's not blindly override, maybe there is already a reflect lib in use | ||
// but just overriding one of the two functions does not serve any purpose | ||
if (!reflect.metadata && !reflect.getOwnMetadata) { | ||
reflect.metadata = (metadataKey: any, metadataValue: any) => (target: any, key: any) => { | ||
if (metadataKey === METADATA_KEY_PARAMTYPES && key === undefined) { // key undefined is ctor | ||
target[CTOR_PARAMETERS_JPA] = metadataValue; | ||
} | ||
} | ||
reflect.getOwnMetadata = (metadata: any, target: any, key: any) => { | ||
if (metadata === METADATA_KEY_PARAMTYPES && key === undefined) { // key undefined is ctor | ||
return target[CTOR_PARAMETERS_JPA]; | ||
} | ||
} | ||
} |
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