Skip to content
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

Debugging design:paramtypes #566

Draft
wants to merge 1 commit into
base: feat/tsyringe
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/di/tsyringe/__test__/tsyringe-adapter.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DependencySix,
DependencyThree,
DependencyTwo,
SymbolToken,
} from './assets/integration.assets';

describe('TSyringe Automock Adapter Integration Test', () => {
Expand All @@ -21,51 +22,55 @@ describe('TSyringe Automock Adapter Integration Test', () => {
expect(injectablesContainer.list()).toStrictEqual<ClassInjectable[]>([
{
identifier: DependencyOne,
metadata: undefined,
value: DependencyOne,
type: 'PARAM',
},
{
identifier: DependencyTwo,
metadata: undefined,
value: DependencyTwo,
type: 'PARAM',
},
{
identifier: DependencyThree,
metadata: undefined,
value: DependencyThree,
type: 'PARAM',
},
{
identifier: 'SOME_TOKEN_FROM_REF',
value: DependencyFive,
type: 'PARAM',
},
{
identifier: DependencySix,
value: UndefinedDependency,
metadata: undefined,
value: DependencySix,
type: 'PARAM',
},
{
identifier: 'CUSTOM_TOKEN',
value: Object,
identifier: SymbolToken,
metadata: undefined,
value: UndefinedDependency,
type: 'PARAM',
},
{
identifier: 'CUSTOM_TOKEN_SECOND',
metadata: undefined,
value: UndefinedDependency,
type: 'PARAM',
},
{
identifier: 'ANOTHER_CUSTOM_TOKEN',
metadata: undefined,
value: String,
type: 'PARAM',
},
{
identifier: 'LITERAL_VALUE_ARR',
metadata: undefined,
value: Array,
type: 'PARAM',
},
{
identifier: 'LITERAL_VALUE_STR',
metadata: undefined,
value: String,
type: 'PARAM',
},
Expand Down
41 changes: 34 additions & 7 deletions packages/di/tsyringe/src/class-ctor-reflector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InjectionToken } from 'tsyringe';
import type { Type } from '@suites/types.common';
import type { ClassInjectable } from '@suites/types.di';
import {
Expand All @@ -22,6 +23,22 @@ export function ClassCtorReflector(reflector: MetadataReflector) {
): (ClassInjectable | ClassInjectable<IdentifierMetadata>)[] {
const paramTypes = reflectParamTypes(targetClass);

// function DummyDecorator(target: Function) {}
// @DummyDecorator
// class Hmm {
// constructor(count: number) {}
// }

// console.log('Hmm', reflectParamTypes(Hmm));
// -> Hmm [ [ Function: Number ] ]

/**
* Line 14 in https://github.com/microsoft/tsyringe/blob/2cd2e00a5fd25308bcf911ca250ded3fe9083af5/src/reflection-helpers.ts#L14
* is called by `injectable()` decorator tsyring, and looks like tsyringe is overwriting the design:paramtypes with it's own token format
*
* And that token format doesn't have the usual primitive constructors like String|Boolean etc :thinking_face:
*/

return paramTypes.map((injectable, paramIndex) => {
const error = `Automock encountered an error while attempting to detect a token or type for the
dependency at index [${paramIndex}] in the class '${targetClass.name}'.
Expand All @@ -33,12 +50,23 @@ please ensure that the circular dependency is resolved, or consider using 'delay
throw new UndefinedDependencyError(error);
}

console.log('----', injectable);

if (isNormalToken(injectable)) {
return {
identifier: injectable,
metadata: undefined,
type: 'PARAM',
value: UndefinedDependency,
};
}

if (isConstructorToken(injectable)) {
return {
identifier: injectable,
metadata: undefined,
value: NonExistingDependency,
type: 'PARAM',
value: injectable,
};
}

Expand All @@ -51,8 +79,8 @@ please ensure that the circular dependency is resolved, or consider using 'delay

return {
identifier: value,
value: value,
metadata: undefined,
value: value,
type: 'PARAM',
};
}
Expand All @@ -61,23 +89,22 @@ please ensure that the circular dependency is resolved, or consider using 'delay
return {
identifier: injectable.token,
type: 'PARAM',
value: NonExistingDependency,
value: injectable.token,
metadata: { args: injectable.transformArgs },
} as ClassInjectable<IdentifierMetadata>;
}

return {
identifier: injectable,
metadata: undefined,
type: 'PARAM',
value: UndefinedDependency,
};
});
}

function reflectParamTypes(
targetClass: Type
): (TSyringeReflectedInjectableIdentifier | undefined)[] {
return reflector.getMetadata(PARAMTYPES_METADATA, targetClass) || [];
function reflectParamTypes(targetClass: Type): TSyringeReflectedInjectableIdentifier[] {
return Reflect.getMetadata(PARAMTYPES_METADATA, targetClass) || [];
}

function isNormalToken(token: TSyringeReflectedInjectableIdentifier): token is string | symbol {
Expand Down
1 change: 1 addition & 0 deletions packages/di/tsyringe/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type TSyringeReflectedInjectableIdentifier =
| Type
| string
| symbol
// @TODO ???
| { wrap: () => Type }
| TransformDescriptor
| TokenDescriptor;
Expand Down
Loading