-
Notifications
You must be signed in to change notification settings - Fork 724
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 26 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
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,19 @@ | ||
import { UNDEFINED_INJECT_ANNOTATION } from "../constants/error_msgs"; | ||
import { Metadata } from "../planning/metadata"; | ||
import { createTaggedDecorator } from "./decorator_utils"; | ||
import { ServiceIdentifierOrFunc } from "./lazy_service_identifier"; | ||
|
||
export function injectBase(metadataKey:string){ | ||
return (serviceIdentifier: ServiceIdentifierOrFunc) => { | ||
return (target: Object, | ||
targetKey:string | symbol | undefined, 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
Record<string | symbol, unknown> | interfaces.Newable<unknown>
is a better type fortarget
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not acceptable by the compiler
I think that I have provided a solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's indeed a valid solution. We want to introduce some linter rules in the future and that's the reason to ban some types. If you want I can create a PR to your branch with equivalent types which respect this rule :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on CI ?
By all means change the typing. I hadn't seen the banning of Function. I look forward to seeing the equivalent types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can also just add this into out
tslint
setup right now and then just try out what works best, sinceban-types
are somehow related to both linters.What was the problem with
Record<string, unknown
anyway ?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When applied to a property typescript will complain
This is why
the js
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They differ. object is allowed in tslint.
https://github.com/typescript-eslint/typescript-eslint/blob/1c1b572c3000d72cfe665b7afbada0ec415e7855/packages/eslint-plugin/src/rules/ban-types.ts#L99
https://github.com/palantir/tslint/blob/285fc1db18d1fd24680d6a2282c6445abf1566ee/src/configs/recommended.ts#L23
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PodaruDragos
tell eslint to ignore the object and the Function types where they are used. The errors is there for lazy typing and errors that arise because of that.
In this instance the type signatures are ok and definitely better than unknown and of couse any.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the class being decorated had
[key:string]:unknown
Then
Record<string | symbol, unknown> | interfaces.Newable<unknown>
will not error