Skip to content

Commit

Permalink
feat(context): add decorator name for @Inject.*, @config.*, @intercept
Browse files Browse the repository at this point in the history
…errors
  • Loading branch information
raymondfeng committed Sep 3, 2019
1 parent c466566 commit 48e3231
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ describe('Context bindings - injecting configuration for bound artifacts', () =>
expect(server1.configObj).to.eql({port: 3000});
});

it('reports error if @config.* is applied more than once', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class TestClass {
constructor() {}

@config('foo') @config('bar') foo: string;
}
}).to.throw(
'@config cannot be applied more than once on TestClass.prototype.foo',
);
});

it('allows propertyPath for injection', async () => {
class RestServerWithPort {
constructor(@config('port') public port: number) {}
Expand Down
24 changes: 24 additions & 0 deletions packages/context/src/__tests__/unit/inject.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ describe('function argument injection', () => {
const meta = describeInjectedArguments(Test);
expect(meta.map(m => m.bindingSelector)).to.deepEqual(['controller']);
});

it('reports error if @inject is applied more than once', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class TestClass {
constructor(@inject('foo') @inject('bar') foo: string) {}
}
}).to.throw(
'@inject cannot be applied more than once on TestClass.constructor[0]',
);
});
});

describe('property injection', () => {
Expand Down Expand Up @@ -182,6 +193,19 @@ describe('property injection', () => {
}).to.throw(/@inject cannot be used on a method/);
});

it('reports error if @inject.* is applied more than once', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class TestClass {
constructor() {}

@inject.getter('foo') @inject('bar') foo: string;
}
}).to.throw(
'@inject.getter cannot be applied more than once on TestClass.prototype.foo',
);
});

it('supports inheritance without overriding property', () => {
class TestClass {
@inject('foo')
Expand Down
1 change: 1 addition & 0 deletions packages/context/src/binding-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function bind(...specs: BindingSpec[]): ClassDecorator {
const decorator = BindDecoratorFactory.createDecorator(
BINDING_METADATA_KEY,
spec,
{decoratorName: '@bind'},
);
decorator(target);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/context/src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function inject(
},
// Do not deep clone the spec as only metadata is mutable and it's
// shallowly cloned
{cloneInputSpec: false},
{cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
);
paramDecorator(target, member!, methodDescriptorOrParameterIndex);
} else if (member) {
Expand Down Expand Up @@ -182,7 +182,7 @@ export function inject(
},
// Do not deep clone the spec as only metadata is mutable and it's
// shallowly cloned
{cloneInputSpec: false},
{cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
);
propDecorator(target, member!);
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/context/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,15 @@ export function intercept(...interceptorOrKeys: InterceptorOrKey[]) {
return InterceptMethodDecoratorFactory.createDecorator(
INTERCEPT_METHOD_KEY,
interceptorOrKeys,
{decoratorName: '@intercept'},
)(target, method, methodDescriptor!);
}
if (typeof target === 'function' && !method && !methodDescriptor) {
// Class
return InterceptClassDecoratorFactory.createDecorator(
INTERCEPT_CLASS_KEY,
interceptorOrKeys,
{decoratorName: '@intercept'},
)(target);
}
// Not on a class or method
Expand Down

0 comments on commit 48e3231

Please sign in to comment.