Skip to content

Commit

Permalink
preserve getter while resolving plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardoperra committed Nov 5, 2024
1 parent 953f7da commit c48a715
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/state/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export function resolve<
// full control of the property for future Plugin updates
for (const p in resolvedContext) {
if (p === 'set' && typeof resolvedContext[p] !== 'function') continue;
resolvedStore[p as keyof typeof resolvedStore] = resolvedContext[p];

const descriptor = Object.getOwnPropertyDescriptor(resolvedContext, p);
Object.defineProperty(resolvedStore, p, descriptor ?? resolvedContext[p]);
}

resolvedPlugins.push(extensionCreator.name);
Expand Down
17 changes: 17 additions & 0 deletions packages/state/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ describe('resolve', () => {
expect(setFn).toHaveBeenCalledWith(10);
});

it('will preserve getters', () => {
const state = defineSignal(() => 0).extend((api) => {
return {
get fooGetter() {
return 'test';
},
};
});

const store = resolve(state);
expect(store).toHaveProperty('fooGetter');
expect(
Object.getOwnPropertyDescriptor(store, 'fooGetter')?.get,
).toBeDefined();
expect(Reflect.get(store, 'fooGetter')).toEqual('test');
});

it('will throw exception when dependency is missing', () => {
const plugin1 = makePlugin(() => ({}), {
name: 'plugin1',
Expand Down
7 changes: 4 additions & 3 deletions packages/state/vite/babel/astAutoNaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export interface BabelAstAddAutoNamingOptions {
filterStores: (callee: string) => boolean;
}

export function babelAstAddAutoNaming({
filterStores,
}: BabelAstAddAutoNamingOptions): babel.PluginObj<any> {
export function babelAstAddAutoNaming(
_: unknown,
{ filterStores }: BabelAstAddAutoNamingOptions,
): babel.PluginObj<any> {
return {
name: 'statebuilder:stateprovider-addAutoName',
visitor: {
Expand Down
11 changes: 10 additions & 1 deletion packages/state/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@ export default defineConfig({
// threads: false,
// isolate: false,
},
plugins: [solidPlugin(), tsconfigPaths()],
plugins: [
solidPlugin(),
tsconfigPaths(),
statebuilder({
autoKey: false,
experimental: {
transformStateProviderDirective: true,
},
}),
],
});

0 comments on commit c48a715

Please sign in to comment.