Skip to content

Commit f210ebc

Browse files
fix(core): invalid scopes on module re-export #2341
1 parent 1167450 commit f210ebc

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import { ExceptionHandler } from './exception-handler';
2-
import { UNHANDLED_RUNTIME_EXCEPTION } from './messages';
2+
3+
const DEFAULT_TEARDOWN = () => process.exit(1);
34

45
export class ExceptionsZone {
56
private static readonly exceptionHandler = new ExceptionHandler();
67

7-
public static run(fn: () => void) {
8+
public static run(
9+
callback: () => void,
10+
teardown: (err: any) => void = DEFAULT_TEARDOWN,
11+
) {
812
try {
9-
fn();
13+
callback();
1014
} catch (e) {
1115
this.exceptionHandler.handle(e);
12-
throw UNHANDLED_RUNTIME_EXCEPTION;
16+
teardown(e);
1317
}
1418
}
1519

16-
public static async asyncRun(fn: () => Promise<void>) {
20+
public static async asyncRun(
21+
callback: () => Promise<void>,
22+
teardown: (err: any) => void = DEFAULT_TEARDOWN,
23+
) {
1724
try {
18-
await fn();
25+
await callback();
1926
} catch (e) {
2027
this.exceptionHandler.handle(e);
21-
throw UNHANDLED_RUNTIME_EXCEPTION;
28+
teardown(e);
2229
}
2330
}
2431
}

packages/core/injector/injector.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,20 @@ export class Injector {
444444
moduleRegistry: any[] = [],
445445
contextId = STATIC_CONTEXT,
446446
inquirer?: InstanceWrapper,
447+
isTraversing?: boolean,
447448
): Promise<any> {
448449
let instanceWrapperRef: InstanceWrapper = null;
449450

450451
const imports = module.imports || new Set<Module>();
451-
const children = [...imports.values()].filter(item => item);
452+
const identity = (item: any) => item;
452453

454+
let children = [...imports.values()].filter(identity);
455+
if (isTraversing) {
456+
const contextModuleExports = module.exports;
457+
children = children.filter(child =>
458+
contextModuleExports.has(child.metatype && child.metatype.name),
459+
);
460+
}
453461
for (const relatedModule of children) {
454462
if (moduleRegistry.includes(relatedModule.id)) {
455463
continue;
@@ -464,6 +472,7 @@ export class Injector {
464472
moduleRegistry,
465473
contextId,
466474
inquirer,
475+
true,
467476
);
468477
if (instanceRef) {
469478
return instanceRef;

packages/core/test/errors/test/exceptions-zone.spec.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
import * as sinon from 'sinon';
21
import { expect } from 'chai';
2+
import * as sinon from 'sinon';
33
import { ExceptionsZone } from '../../../errors/exceptions-zone';
4-
import { UNHANDLED_RUNTIME_EXCEPTION } from '../../../errors/messages';
54

65
describe('ExceptionsZone', () => {
6+
const rethrow = err => {
7+
throw err;
8+
};
9+
710
describe('run', () => {
811
let callback: sinon.SinonSpy;
912
beforeEach(() => {
1013
callback = sinon.spy();
1114
});
1215
it('should call callback', () => {
13-
ExceptionsZone.run(callback as any);
16+
ExceptionsZone.run(callback as any, rethrow);
1417
expect(callback.called).to.be.true;
1518
});
1619
describe('when callback throws exception', () => {
1720
const exceptionHandler = {
1821
handle: () => {},
1922
};
2023
let handleSpy: sinon.SinonSpy;
21-
beforeEach(() => {
24+
before(() => {
2225
(ExceptionsZone as any).exceptionHandler = exceptionHandler;
2326
handleSpy = sinon.spy(exceptionHandler, 'handle');
2427
});
25-
it('should call "handle" method of exceptionHandler and throws UNHANDLED_RUNTIME_EXCEPTION', () => {
28+
it('should call "handle" method of exceptionHandler and rethrows', () => {
2629
const throwsCallback = () => {
27-
throw 3;
30+
throw new Error('');
2831
};
29-
expect(() => ExceptionsZone.run(throwsCallback)).to.throws(
30-
UNHANDLED_RUNTIME_EXCEPTION,
31-
);
32+
expect(() => ExceptionsZone.run(throwsCallback, rethrow)).to.throws();
3233
expect(handleSpy.called).to.be.true;
3334
});
3435
});
@@ -39,24 +40,24 @@ describe('ExceptionsZone', () => {
3940
callback = sinon.spy();
4041
});
4142
it('should call callback', async () => {
42-
await ExceptionsZone.asyncRun(callback as any);
43+
await ExceptionsZone.asyncRun(callback as any, rethrow);
4344
expect(callback.called).to.be.true;
4445
});
4546
describe('when callback throws exception', () => {
4647
const exceptionHandler = {
4748
handle: () => {},
4849
};
4950
let handleSpy: sinon.SinonSpy;
50-
beforeEach(() => {
51+
before(() => {
5152
(ExceptionsZone as any).exceptionHandler = exceptionHandler;
5253
handleSpy = sinon.spy(exceptionHandler, 'handle');
5354
});
54-
it('should call "handle" method of exceptionHandler and throws UNHANDLED_RUNTIME_EXCEPTION', async () => {
55+
it('should call "handle" method of exceptionHandler and rethrows error', async () => {
5556
const throwsCallback = () => {
56-
throw 3;
57+
throw new Error('');
5758
};
58-
expect(ExceptionsZone.asyncRun(throwsCallback)).to.eventually.be
59-
.rejected;
59+
expect(ExceptionsZone.asyncRun(throwsCallback, rethrow)).to.eventually
60+
.be.rejected;
6061
});
6162
});
6263
});

0 commit comments

Comments
 (0)