From 47ac2a6774d428b1a022fb0320270c222e5ecda9 Mon Sep 17 00:00:00 2001 From: Adam Berger Date: Thu, 29 Sep 2022 21:56:19 +0000 Subject: [PATCH] Allow any string value in meta --- .changeset/tasty-guests-cry.md | 5 +++++ packages/machine-extractor/examples/meta.ts | 8 ++++++++ packages/machine-extractor/src/meta.ts | 8 +++----- packages/machine-extractor/src/toMachineConfig.ts | 12 ++++++++---- 4 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 .changeset/tasty-guests-cry.md create mode 100644 packages/machine-extractor/examples/meta.ts diff --git a/.changeset/tasty-guests-cry.md b/.changeset/tasty-guests-cry.md new file mode 100644 index 00000000..d7388a3b --- /dev/null +++ b/.changeset/tasty-guests-cry.md @@ -0,0 +1,5 @@ +--- +'@xstate/machine-extractor': minor +--- + +Capture any string field from meta instead of just description diff --git a/packages/machine-extractor/examples/meta.ts b/packages/machine-extractor/examples/meta.ts new file mode 100644 index 00000000..81bb72c8 --- /dev/null +++ b/packages/machine-extractor/examples/meta.ts @@ -0,0 +1,8 @@ +import { createMachine } from 'xstate'; + +export const machine = createMachine({ + meta: { + description: 'description', + someOtherField: 'hello there', + }, +}); diff --git a/packages/machine-extractor/src/meta.ts b/packages/machine-extractor/src/meta.ts index 9ae92895..b247d9ae 100644 --- a/packages/machine-extractor/src/meta.ts +++ b/packages/machine-extractor/src/meta.ts @@ -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); diff --git a/packages/machine-extractor/src/toMachineConfig.ts b/packages/machine-extractor/src/toMachineConfig.ts index 345d7fb6..4567e986 100644 --- a/packages/machine-extractor/src/toMachineConfig.ts +++ b/packages/machine-extractor/src/toMachineConfig.ts @@ -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) {