-
Notifications
You must be signed in to change notification settings - Fork 723
Improve annotations #1343
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
Merged
tonyhallett
merged 37 commits into
inversify:master
from
tonyhallett:improve-annotations
May 14, 2021
Merged
Improve annotations #1343
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
7b062e2
Merge branch 'master' of https://github.com/inversify/InversifyJS
tonyhallett 2cf47bf
Merge branch 'master' of https://github.com/inversify/InversifyJS
tonyhallett 36310b7
Merge branch 'master' of https://github.com/inversify/InversifyJS
tonyhallett 57ad651
Merge branch 'master' of https://github.com/inversify/InversifyJS
tonyhallett 9ec5940
Merge branch 'master' of https://github.com/inversify/InversifyJS
tonyhallett ce54f28
Merge branch 'master' of https://github.com/inversify/InversifyJS
tonyhallett 2073070
refactor to helper createTaggedDecorator which supports all decorator…
tonyhallett 035d68c
make createTaggedDecorator permit multiple metadata
tonyhallett 2e1c863
refactor tagParameter and tagProperty
tonyhallett dd92dc7
extract _ensureNoMetadataKeyDuplicates
tonyhallett 08e46e6
change parameter name to indexOrPropertyDescriptor
tonyhallett ff16c9a
grammar correct historic test names
tonyhallett 17a0fa7
use sinon sandbox for spying
tonyhallett 3fcb065
review
tonyhallett 2e5a011
wiki
tonyhallett 6da7ac6
improve typing
tonyhallett a0eede0
improve decorator typing
tonyhallett c9487b8
throw error for decorated property missing inject / multiInject decor…
tonyhallett 44c43f0
refactor code climate
tonyhallett 2e4119f
permit injection for symbol property key
tonyhallett ee81df3
move symbol tests to container
tonyhallett 5703cdf
remove callback from createTaggedDecorator. minor type changes.
tonyhallett 9c75702
permit lazyserviceidentifier for multiinject
tonyhallett 7124f67
correct decorator typing
tonyhallett 0f565da
type target to Object
tonyhallett 3137069
replace if condition
tonyhallett 0fc9b09
DecoratorTarget type and throw for annotating static properties
tonyhallett b7b7741
DecoratorTarget for js decorate
tonyhallett 9c39f67
add test for js inject for property
tonyhallett 2756407
extract getSymbolDescription
tonyhallett b325f59
refactor: simplify types
notaphplover 39f3913
_tagParameterOrProperty on constructor function
tonyhallett 5a4462e
DecoratorTarget type
tonyhallett b266fe1
type predicate cast to type testing for
tonyhallett bf99ac1
Prototype type - map properties to include undefined
tonyhallett 03e7bf9
Merge branch 'master' into improve-annotations
tonyhallett a2fac9b
update changelog
tonyhallett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,37 +1,6 @@ | ||
import { UNDEFINED_INJECT_ANNOTATION } from "../constants/error_msgs"; | ||
import * as METADATA_KEY from "../constants/metadata_keys"; | ||
import { interfaces } from "../interfaces/interfaces"; | ||
import { Metadata } from "../planning/metadata"; | ||
import { tagParameter, tagProperty } from "./decorator_utils"; | ||
import { injectBase } from "./inject_base"; | ||
|
||
export type ServiceIdentifierOrFunc = interfaces.ServiceIdentifier<any> | LazyServiceIdentifer; | ||
|
||
export class LazyServiceIdentifer<T = any> { | ||
private _cb: () => interfaces.ServiceIdentifier<T>; | ||
public constructor(cb: () => interfaces.ServiceIdentifier<T>) { | ||
this._cb = cb; | ||
} | ||
|
||
public unwrap() { | ||
return this._cb(); | ||
} | ||
} | ||
|
||
function inject(serviceIdentifier: ServiceIdentifierOrFunc) { | ||
return function(target: any, targetKey: string, index?: number | PropertyDescriptor): void { | ||
if (serviceIdentifier === undefined) { | ||
throw new Error(UNDEFINED_INJECT_ANNOTATION(target.name)); | ||
} | ||
|
||
const metadata = new Metadata(METADATA_KEY.INJECT_TAG, serviceIdentifier); | ||
|
||
if (typeof index === "number") { | ||
tagParameter(target, targetKey, index, metadata); | ||
} else { | ||
tagProperty(target, targetKey, metadata); | ||
} | ||
|
||
}; | ||
} | ||
const inject = injectBase(METADATA_KEY.INJECT_TAG); | ||
|
||
export { inject }; |
This file contains hidden or 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 @@ | ||
import { UNDEFINED_INJECT_ANNOTATION } from "../constants/error_msgs"; | ||
import { Metadata } from "../planning/metadata"; | ||
import { createTaggedDecorator, DecoratorTarget } from "./decorator_utils"; | ||
import { ServiceIdentifierOrFunc } from "./lazy_service_identifier"; | ||
|
||
export function injectBase(metadataKey: string) { | ||
return (serviceIdentifier: ServiceIdentifierOrFunc) => { | ||
return ( | ||
target: DecoratorTarget, | ||
targetKey?: string | symbol, | ||
indexOrPropertyDescriptor?: number | TypedPropertyDescriptor<unknown>, | ||
) => { | ||
if (serviceIdentifier === undefined) { | ||
const className = typeof target === "function" ? target.name : target.constructor.name; | ||
|
||
throw new Error(UNDEFINED_INJECT_ANNOTATION(className)); | ||
} | ||
return createTaggedDecorator( | ||
new Metadata(metadataKey, serviceIdentifier) | ||
)(target, targetKey, indexOrPropertyDescriptor); | ||
}; | ||
} | ||
} |
This file contains hidden or 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,14 @@ | ||
import { interfaces } from "../interfaces/interfaces"; | ||
|
||
export type ServiceIdentifierOrFunc = interfaces.ServiceIdentifier<any> | LazyServiceIdentifer; | ||
|
||
export class LazyServiceIdentifer<T = any> { | ||
private _cb: () => interfaces.ServiceIdentifier<T>; | ||
public constructor(cb: () => interfaces.ServiceIdentifier<T>) { | ||
this._cb = cb; | ||
} | ||
|
||
public unwrap() { | ||
return this._cb(); | ||
} | ||
} |
This file contains hidden or 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,20 +1,6 @@ | ||
import * as METADATA_KEY from "../constants/metadata_keys"; | ||
import { interfaces } from "../interfaces/interfaces"; | ||
import { Metadata } from "../planning/metadata"; | ||
import { tagParameter, tagProperty } from "./decorator_utils"; | ||
import { injectBase } from "./inject_base"; | ||
|
||
function multiInject(serviceIdentifier: interfaces.ServiceIdentifier<any>) { | ||
return function(target: any, targetKey: string, index?: number) { | ||
|
||
const metadata = new Metadata(METADATA_KEY.MULTI_INJECT_TAG, serviceIdentifier); | ||
|
||
if (typeof index === "number") { | ||
tagParameter(target, targetKey, index, metadata); | ||
} else { | ||
tagProperty(target, targetKey, metadata); | ||
} | ||
|
||
}; | ||
} | ||
const multiInject = injectBase(METADATA_KEY.MULTI_INJECT_TAG); | ||
|
||
export { multiInject }; |
This file contains hidden or 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,17 +1,10 @@ | ||
import * as METADATA_KEY from "../constants/metadata_keys"; | ||
import { Metadata } from "../planning/metadata"; | ||
import { tagParameter, tagProperty } from "./decorator_utils"; | ||
import { createTaggedDecorator } from "./decorator_utils"; | ||
|
||
// Used to add named metadata which is used to resolve name-based contextual bindings. | ||
function named(name: string | number | symbol) { | ||
return function(target: any, targetKey: string, index?: number) { | ||
const metadata = new Metadata(METADATA_KEY.NAMED_TAG, name); | ||
if (typeof index === "number") { | ||
tagParameter(target, targetKey, index, metadata); | ||
} else { | ||
tagProperty(target, targetKey, metadata); | ||
} | ||
}; | ||
return createTaggedDecorator(new Metadata(METADATA_KEY.NAMED_TAG, name)); | ||
} | ||
|
||
export { named }; |
This file contains hidden or 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,19 +1,9 @@ | ||
import * as METADATA_KEY from "../constants/metadata_keys"; | ||
import { Metadata } from "../planning/metadata"; | ||
import { tagParameter, tagProperty } from "./decorator_utils"; | ||
import { createTaggedDecorator } from "./decorator_utils"; | ||
|
||
function optional() { | ||
return function(target: any, targetKey: string, index?: number) { | ||
|
||
const metadata = new Metadata(METADATA_KEY.OPTIONAL_TAG, true); | ||
|
||
if (typeof index === "number") { | ||
tagParameter(target, targetKey, index, metadata); | ||
} else { | ||
tagProperty(target, targetKey, metadata); | ||
} | ||
|
||
}; | ||
return createTaggedDecorator(new Metadata(METADATA_KEY.OPTIONAL_TAG, true)); | ||
} | ||
|
||
export { optional }; |
This file contains hidden or 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,16 +1,9 @@ | ||
import { Metadata } from "../planning/metadata"; | ||
import { tagParameter, tagProperty } from "./decorator_utils"; | ||
import { createTaggedDecorator } from "./decorator_utils"; | ||
|
||
// Used to add custom metadata which is used to resolve metadata-based contextual bindings. | ||
function tagged(metadataKey: string | number | symbol, metadataValue: any) { | ||
return function(target: any, targetKey: string, index?: number) { | ||
const metadata = new Metadata(metadataKey, metadataValue); | ||
if (typeof index === "number") { | ||
tagParameter(target, targetKey, index, metadata); | ||
} else { | ||
tagProperty(target, targetKey, metadata); | ||
} | ||
}; | ||
return createTaggedDecorator(new Metadata(metadataKey, metadataValue)); | ||
} | ||
|
||
export { tagged }; |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.