Skip to content

Commit 5c42a6d

Browse files
committed
fix(tegg): address review follow-ups
1 parent c356ead commit 5c42a6d

44 files changed

Lines changed: 545 additions & 86 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tegg/core/aop-runtime/src/LoadUnitAopHook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class LoadUnitAopHook implements LifecycleHook<LoadUnitLifecycleContext,
1717
private readonly crosscutAdviceFactory: CrosscutAdviceFactory;
1818

1919
@Inject()
20-
private readonly aopContextAdviceRegistry: AopContextAdviceRegistry = new AopContextAdviceRegistry();
20+
private readonly aopContextAdviceRegistry: AopContextAdviceRegistry;
2121

2222
async postCreate(_: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise<void> {
2323
for (const proto of loadUnit.iterateEggPrototype()) {

tegg/core/aop-runtime/test/aop-runtime.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { describe, beforeEach, afterEach, it } from 'vitest';
1313
import { Hello } from './fixtures/modules/hello_succeed/Hello.js';
1414

1515
import { crossCutGraphHook } from '../src/CrossCutGraphHook.js';
16+
import { AopContextAdviceRegistry } from '../src/AopContextAdviceRegistry.js';
1617
import { EggObjectAopHook } from '../src/EggObjectAopHook.js';
1718
import { EggPrototypeCrossCutHook } from '../src/EggPrototypeCrossCutHook.js';
1819
import { LoadUnitAopHook } from '../src/LoadUnitAopHook.js';
@@ -22,6 +23,13 @@ import { CallTrace } from './fixtures/modules/hello_cross_cut/CallTrace.js';
2223
import { crosscutAdviceParams } from './fixtures/modules/hello_cross_cut/HelloCrossCut.js';
2324
import { pointcutAdviceParams } from './fixtures/modules/hello_point_cut/HelloPointCut.js';
2425

26+
function createLoadUnitAopHook(crosscutAdviceFactory: CrosscutAdviceFactory): LoadUnitAopHook {
27+
const loadUnitAopHook = new LoadUnitAopHook();
28+
Reflect.set(loadUnitAopHook, 'crosscutAdviceFactory', crosscutAdviceFactory);
29+
Reflect.set(loadUnitAopHook, 'aopContextAdviceRegistry', new AopContextAdviceRegistry());
30+
return loadUnitAopHook;
31+
}
32+
2533
describe('test/aop-runtime.test.ts', () => {
2634
afterEach(() => {
2735
mock.reset();
@@ -37,8 +45,7 @@ describe('test/aop-runtime.test.ts', () => {
3745
beforeEach(async () => {
3846
crosscutAdviceFactory = new CrosscutAdviceFactory();
3947
eggObjectAopHook = new EggObjectAopHook();
40-
loadUnitAopHook = new LoadUnitAopHook();
41-
Reflect.set(loadUnitAopHook, 'crosscutAdviceFactory', crosscutAdviceFactory);
48+
loadUnitAopHook = createLoadUnitAopHook(crosscutAdviceFactory);
4249
eggPrototypeCrossCutHook = new EggPrototypeCrossCutHook();
4350
Reflect.set(eggPrototypeCrossCutHook, 'crosscutAdviceFactory', crosscutAdviceFactory);
4451
EggPrototypeLifecycleUtil.registerLifecycle(eggPrototypeCrossCutHook);
@@ -167,8 +174,7 @@ describe('test/aop-runtime.test.ts', () => {
167174
beforeEach(async () => {
168175
crosscutAdviceFactory = new CrosscutAdviceFactory();
169176
eggObjectAopHook = new EggObjectAopHook();
170-
loadUnitAopHook = new LoadUnitAopHook();
171-
Reflect.set(loadUnitAopHook, 'crosscutAdviceFactory', crosscutAdviceFactory);
177+
loadUnitAopHook = createLoadUnitAopHook(crosscutAdviceFactory);
172178
eggPrototypeCrossCutHook = new EggPrototypeCrossCutHook();
173179
Reflect.set(eggPrototypeCrossCutHook, 'crosscutAdviceFactory', crosscutAdviceFactory);
174180
EggPrototypeLifecycleUtil.registerLifecycle(eggPrototypeCrossCutHook);
@@ -195,8 +201,7 @@ describe('test/aop-runtime.test.ts', () => {
195201
beforeEach(async () => {
196202
crosscutAdviceFactory = new CrosscutAdviceFactory();
197203
eggObjectAopHook = new EggObjectAopHook();
198-
loadUnitAopHook = new LoadUnitAopHook();
199-
Reflect.set(loadUnitAopHook, 'crosscutAdviceFactory', crosscutAdviceFactory);
204+
loadUnitAopHook = createLoadUnitAopHook(crosscutAdviceFactory);
200205
eggPrototypeCrossCutHook = new EggPrototypeCrossCutHook();
201206
Reflect.set(eggPrototypeCrossCutHook, 'crosscutAdviceFactory', crosscutAdviceFactory);
202207
EggPrototypeLifecycleUtil.registerLifecycle(eggPrototypeCrossCutHook);

tegg/core/common-util/src/ModuleConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export class ModuleConfigUtil {
9191
path: modulePath,
9292
name: ModuleConfigUtil.getModuleName(pkg),
9393
package: ModuleConfigUtil.getPackageName(pkg),
94+
...(moduleReferenceConfig.optional === undefined ? {} : { optional: moduleReferenceConfig.optional }),
9495
};
9596
} else if (ModuleReferenceConfigHelp.isInlineModuleReference(moduleReferenceConfig)) {
9697
const modulePath = path.join(configDir, moduleReferenceConfig.path);
@@ -99,6 +100,7 @@ export class ModuleConfigUtil {
99100
path: modulePath,
100101
name: ModuleConfigUtil.getModuleName(pkg),
101102
package: ModuleConfigUtil.getPackageName(pkg),
103+
...(moduleReferenceConfig.optional === undefined ? {} : { optional: moduleReferenceConfig.optional }),
102104
};
103105
} else {
104106
throw new Error('unknown type of module reference config: ' + JSON.stringify(moduleReferenceConfig));

tegg/core/common-util/test/ModuleConfig.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('test/ModuleConfig.test.ts', () => {
137137
const ref = ModuleConfigUtil.readModuleReference(fixturesPath);
138138
assert.deepStrictEqual(ref, [
139139
{ path: path.join(fixturesPath, 'app/module-a'), name: 'moduleA', package: 'module-a' },
140-
{ path: path.join(fixturesPath, 'app/module-b'), name: 'moduleB', package: 'module-b' },
140+
{ path: path.join(fixturesPath, 'app/module-b'), name: 'moduleB', package: 'module-b', optional: true },
141141
]);
142142
});
143143
});
@@ -153,6 +153,7 @@ describe('test/ModuleConfig.test.ts', () => {
153153
path: path.join(fixturesPath, 'node_modules/module-a'),
154154
name: 'moduleA',
155155
package: 'module-a',
156+
optional: true,
156157
},
157158
]);
158159
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{ "path": "../app/module-a" }, { "path": "../app/module-b" }]
1+
[{ "path": "../app/module-a" }, { "path": "../app/module-b", "optional": true }]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{ "package": "module-a" }]
1+
[{ "package": "module-a", "optional": true }]

tegg/core/loader/test/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ exports[`should export stable 1`] = `
66
"LoaderUtil": [Function],
77
"ModuleLoader": [Function],
88
"TEGG_MANIFEST_KEY": "tegg",
9+
"buildTeggManifestData": [Function],
910
}
1011
`;

tegg/core/metadata/src/errors.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { FrameworkBaseError } from '@eggjs/errors';
22
import { ErrorCodes } from '@eggjs/tegg-types';
33
import type { EggPrototypeName, QualifierInfo } from '@eggjs/tegg-types';
44

5+
function formatQualifiers(qualifiers: readonly QualifierInfo[]): string {
6+
return `[${qualifiers.map((qualifier) => `${String(qualifier.attribute)}=${String(qualifier.value)}`).join(',')}]`;
7+
}
8+
59
export class TeggError extends FrameworkBaseError {
610
get module() {
711
return 'TEGG';
@@ -19,7 +23,7 @@ export class EggPrototypeNotFound extends TeggError {
1923

2024
export class MultiPrototypeFound extends TeggError {
2125
constructor(name: EggPrototypeName, qualifier: QualifierInfo[], result?: string) {
22-
const msg = `multi proto found for name:${String(name)} and qualifiers ${JSON.stringify(qualifier)}${
26+
const msg = `multi proto found for name:${String(name)} and qualifiers ${formatQualifiers(qualifier)}${
2327
result ? `, result is ${result}` : ''
2428
}`;
2529
super(msg, ErrorCodes.MULTI_PROTO_FOUND);

tegg/core/metadata/src/factory/EggPrototypeFactory.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PrototypeUtil } from '@eggjs/core-decorator';
22
import { FrameworkErrorFormatter } from '@eggjs/errors';
33
import { MapUtil } from '@eggjs/tegg-common-util';
4-
import { AccessLevel, TeggScope } from '@eggjs/tegg-types';
4+
import { AccessLevel, DefineModuleQualifierAttribute, TeggScope } from '@eggjs/tegg-types';
55
import type {
66
EggProtoImplClass,
77
EggPrototypeName,
@@ -98,7 +98,9 @@ export class EggPrototypeFactory {
9898
if (protos.length === 1) {
9999
return protos[0];
100100
}
101-
throw FrameworkErrorFormatter.formatError(new MultiPrototypeFound(name, qualifiers));
101+
throw FrameworkErrorFormatter.formatError(
102+
new MultiPrototypeFound(name, qualifiers, JSON.stringify(protos.map(EggPrototypeFactory.formatPrototype))),
103+
);
102104
}
103105

104106
private doGetPrototype(name: EggPrototypeName, qualifiers: QualifierInfo[], loadUnit?: LoadUnit): EggPrototype[] {
@@ -114,4 +116,13 @@ export class EggPrototypeFactory {
114116
const protos = this.publicProtoMap.get(name);
115117
return protos?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
116118
}
119+
120+
private static formatPrototype(proto: EggPrototype): string {
121+
return (
122+
`${String(proto.name)}@${proto.loadUnitId}` +
123+
` define:${String(proto.defineModuleName ?? proto.getQualifier(DefineModuleQualifierAttribute))}` +
124+
`@${String(proto.defineUnitPath ?? proto.loadUnitId)}` +
125+
` qualifiers:[${String(DefineModuleQualifierAttribute)}=${String(proto.getQualifier(DefineModuleQualifierAttribute))}]`
126+
);
127+
}
117128
}

tegg/core/metadata/src/factory/LoadUnitFactory.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ export class LoadUnitFactory {
4545
return await creator(ctx);
4646
}
4747

48-
static async createLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise<LoadUnit> {
48+
static async createLoadUnit(
49+
unitPath: string,
50+
type: EggLoadUnitTypeLike,
51+
loader: Loader,
52+
unitName?: string,
53+
): Promise<LoadUnit> {
4954
const loadUnitMap = LoadUnitFactory.loadUnitMap;
5055
if (loadUnitMap.has(unitPath)) {
5156
return loadUnitMap.get(unitPath)!.loadUnit;
5257
}
5358
const ctx: LoadUnitLifecycleContext = {
5459
unitPath,
60+
unitName,
5561
loader,
5662
};
5763
const loadUnit = await LoadUnitFactory.getLoanUnit(ctx, type);
@@ -65,9 +71,15 @@ export class LoadUnitFactory {
6571
return loadUnit;
6672
}
6773

68-
static async createPreloadLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise<LoadUnit> {
74+
static async createPreloadLoadUnit(
75+
unitPath: string,
76+
type: EggLoadUnitTypeLike,
77+
loader: Loader,
78+
unitName?: string,
79+
): Promise<LoadUnit> {
6980
const ctx: LoadUnitLifecycleContext = {
7081
unitPath,
82+
unitName,
7183
loader,
7284
};
7385
return await LoadUnitFactory.getLoanUnit(ctx, type);

0 commit comments

Comments
 (0)