Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture any string in the meta field of a machine or state #225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-guests-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xstate/machine-extractor': minor
---

Capture any string field from meta instead of just description
8 changes: 8 additions & 0 deletions packages/machine-extractor/examples/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createMachine } from 'xstate';

export const machine = createMachine({
meta: {
description: 'description',
someOtherField: 'hello there',
},
});
8 changes: 3 additions & 5 deletions packages/machine-extractor/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { StringLiteral, TemplateLiteral } from './scalars';
import { unionType } from './unionType';
import { objectTypeWithKnownKeys } from './utils';
import { objectOf } from './utils';

export const MetaDescription = unionType([StringLiteral, TemplateLiteral]);
export const MetaValues = unionType([StringLiteral, TemplateLiteral]);

export const StateMeta = objectTypeWithKnownKeys({
description: MetaDescription,
});
export const StateMeta = objectOf(MetaValues);
12 changes: 8 additions & 4 deletions packages/machine-extractor/src/toMachineConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ const parseStateNode = (
config.always = getTransitions(astResult.always, opts);
}

if (astResult.meta?.description) {
config.meta = {
description: astResult.meta.description.value,
};
if (astResult.meta) {
config.meta = astResult.meta.properties.reduce(
(meta, props) => ({
...meta,
[props.key]: props.result.value,
}),
{},
);
}

if (astResult.onDone) {
Expand Down