Skip to content

Commit 48e3231

Browse files
committed
feat(context): add decorator name for @Inject.*, @config.*, @intercept errors
1 parent c466566 commit 48e3231

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

packages/context/src/__tests__/acceptance/binding-config.acceptance.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ describe('Context bindings - injecting configuration for bound artifacts', () =>
5959
expect(server1.configObj).to.eql({port: 3000});
6060
});
6161

62+
it('reports error if @config.* is applied more than once', () => {
63+
expect(() => {
64+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
65+
class TestClass {
66+
constructor() {}
67+
68+
@config('foo') @config('bar') foo: string;
69+
}
70+
}).to.throw(
71+
'@config cannot be applied more than once on TestClass.prototype.foo',
72+
);
73+
});
74+
6275
it('allows propertyPath for injection', async () => {
6376
class RestServerWithPort {
6477
constructor(@config('port') public port: number) {}

packages/context/src/__tests__/unit/inject.unit.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ describe('function argument injection', () => {
131131
const meta = describeInjectedArguments(Test);
132132
expect(meta.map(m => m.bindingSelector)).to.deepEqual(['controller']);
133133
});
134+
135+
it('reports error if @inject is applied more than once', () => {
136+
expect(() => {
137+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
138+
class TestClass {
139+
constructor(@inject('foo') @inject('bar') foo: string) {}
140+
}
141+
}).to.throw(
142+
'@inject cannot be applied more than once on TestClass.constructor[0]',
143+
);
144+
});
134145
});
135146

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

196+
it('reports error if @inject.* is applied more than once', () => {
197+
expect(() => {
198+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
199+
class TestClass {
200+
constructor() {}
201+
202+
@inject.getter('foo') @inject('bar') foo: string;
203+
}
204+
}).to.throw(
205+
'@inject.getter cannot be applied more than once on TestClass.prototype.foo',
206+
);
207+
});
208+
185209
it('supports inheritance without overriding property', () => {
186210
class TestClass {
187211
@inject('foo')

packages/context/src/binding-decorator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export function bind(...specs: BindingSpec[]): ClassDecorator {
8383
const decorator = BindDecoratorFactory.createDecorator(
8484
BINDING_METADATA_KEY,
8585
spec,
86+
{decoratorName: '@bind'},
8687
);
8788
decorator(target);
8889
};

packages/context/src/inject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export function inject(
146146
},
147147
// Do not deep clone the spec as only metadata is mutable and it's
148148
// shallowly cloned
149-
{cloneInputSpec: false},
149+
{cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
150150
);
151151
paramDecorator(target, member!, methodDescriptorOrParameterIndex);
152152
} else if (member) {
@@ -182,7 +182,7 @@ export function inject(
182182
},
183183
// Do not deep clone the spec as only metadata is mutable and it's
184184
// shallowly cloned
185-
{cloneInputSpec: false},
185+
{cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
186186
);
187187
propDecorator(target, member!);
188188
} else {

packages/context/src/interceptor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,15 @@ export function intercept(...interceptorOrKeys: InterceptorOrKey[]) {
263263
return InterceptMethodDecoratorFactory.createDecorator(
264264
INTERCEPT_METHOD_KEY,
265265
interceptorOrKeys,
266+
{decoratorName: '@intercept'},
266267
)(target, method, methodDescriptor!);
267268
}
268269
if (typeof target === 'function' && !method && !methodDescriptor) {
269270
// Class
270271
return InterceptClassDecoratorFactory.createDecorator(
271272
INTERCEPT_CLASS_KEY,
272273
interceptorOrKeys,
274+
{decoratorName: '@intercept'},
273275
)(target);
274276
}
275277
// Not on a class or method

0 commit comments

Comments
 (0)