Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(core): preserve facade accessor descriptors (#7174)

siamad1d910c03ba
diff --git a/packages/core/src/facade/__tests__/f-base.spec.ts b/packages/core/src/facade/__tests__/f-base.spec.ts
index 9202727b4c..06d0cc555e 100644
--- a/packages/core/src/facade/__tests__/f-base.spec.ts
+++ b/packages/core/src/facade/__tests__/f-base.spec.ts
@@ -106,4 +106,93 @@ describe('FBase', () => {
expect(initLog.get(manual)).toEqual(['a:true']);
expect(manual.steps).toEqual(['mark:manual']);
});
+
+ it('should copy initialable accessor descriptors without invoking getters during extend', () => {
+ const accessLog: string[] = [];
+ const initLog = new WeakMap<object, string[]>();
+
+ class AccessorInitialable extends FBaseInitialable {
+ value = 'instance';
+
+ constructor(@Inject(Injector) injector: IInjector) {
+ super(injector);
+ }
+ }
+
+ class AccessorSourceA {
+ _initialize(injector: IInjector) {
+ const current = initLog.get(this) ?? [];
+ current.push(`a:${Boolean(injector)}`);
+ initLog.set(this, current);
+ }
+
+ get marker() {
+ const instance = this as unknown as AccessorInitialable;
+ accessLog.push(`get:${instance.value}`);
+ return instance.value;
+ }
+
+ set marker(value: string) {
+ const instance = this as unknown as AccessorInitialable;
+ accessLog.push(`set:${value}`);
+ instance.value = value;
+ }
+ }
+
+ class AccessorSourceB {
+ _initialize() {
+ const current = initLog.get(this) ?? [];
+ current.push('b');
+ initLog.set(this, current);
+ }
+ }
+
+ AccessorInitialable.extend(AccessorSourceA);
+ AccessorInitialable.extend(AccessorSourceB);
+
+ expect(accessLog).toEqual([]);
+ expect(typeof Object.getOwnPropertyDescriptor(AccessorInitialable.prototype, 'marker')?.get).toBe('function');
+
+ const injector = new Injector();
+ const instance = injector.createInstance(AccessorInitialable) as AccessorInitialable & { marker: string };
+
+ expect(initLog.get(instance)).toEqual(['a:true', 'b']);
+ expect(instance.marker).toBe('instance');
+ instance.marker = 'updated';
+ expect(instance.marker).toBe('updated');
+ expect(accessLog).toEqual(['get:instance', 'set:updated', 'get:updated']);
+ });
+
+ it('should fall back to assignment when an extension property descriptor is unavailable', () => {
+ class FallbackInitialable extends FBaseInitialable {
+ value = 'fallback';
+
+ constructor(@Inject(Injector) injector: IInjector) {
+ super(injector);
+ }
+ }
+
+ const legacySource = {
+ prototype: new Proxy({}, {
+ ownKeys: () => ['legacy'],
+ getOwnPropertyDescriptor: () => undefined,
+ get: (_target, key) => {
+ if (key !== 'legacy') {
+ return undefined;
+ }
+
+ return function (this: FallbackInitialable) {
+ return `legacy:${this.value}`;
+ };
+ },
+ }),
+ };
+
+ FallbackInitialable.extend(legacySource);
+
+ const injector = new Injector();
+ const instance = injector.createInstance(FallbackInitialable) as FallbackInitialable & { legacy: () => string };
+
+ expect(instance.legacy()).toBe('legacy:fallback');
+ });
});
diff --git a/packages/core/src/facade/f-base.ts b/packages/core/src/facade/f-base.ts
index 01ccceb524..2470fd3bba 100644
--- a/packages/core/src/facade/f-base.ts
+++ b/packages/core/src/facade/f-base.ts
@@ -115,8 +115,13 @@ export class FBaseInitialable extends Disposable {
initializers.push(source.prototype._initialize);
} else if (name !== 'constructor') {
- // @ts-ignore
- this.prototype[name] = source.prototype[name];
+ const descriptor = Object.getOwnPropertyDescriptor(source.prototype, name);
+ if (descriptor) {
+ Object.defineProperty(this.prototype, name, descriptor);
+ } else {
+ // @ts-ignore
+ this.prototype[name] = source.prototype[name];
+ }
}
});