Skip to content
Merged
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
30 changes: 29 additions & 1 deletion src/lib/machine.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { detectFormat, parseSketchCode, parseMermaidCode, machineToGraph, getEventCategory } from './machine';
import { detectFormat, parseSketchCode, parseMermaidCode, parseXStateMachineCode, machineToGraph, getEventCategory } from './machine';

describe('detectFormat', () => {
it('detects Sketch DSL before any explicit mode selection', () => {
Expand Down Expand Up @@ -33,6 +33,34 @@ describe('parseSketchCode', () => {
});
});

describe('parseXStateMachineCode', () => {
const machine = `setup({}).createMachine({
id: 'test',
initial: 'idle',
states: { idle: {} },
})`;

it.each([
// xstate imports
[`import { setup } from 'xstate';\n${machine}`],
[`import * as xstate from 'xstate';\n${machine}`],
// export default
[`export default ${machine}`],
// export const/let/var/function/class
[`export const m = ${machine}`],
[`export function getMachine() { return ${machine} }\ngetMachine()`],
// export { ... }
[`const m = ${machine};\nexport { m };`],
// export * from
[`export * from 'some-module';\n${machine}`],
])('strips imports and exports: %s', (code) => {
const result = parseXStateMachineCode(code);
expect(result.error).toBeNull();
expect(result.machines).toHaveLength(1);
expect(result.machines[0]?.id).toBe('test');
});
});

describe('parseMermaidCode', () => {
it('converts <<choice>> nodes to always transitions with guards', () => {
const result = parseMermaidCode(`stateDiagram-v2
Expand Down
19 changes: 15 additions & 4 deletions src/lib/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,23 @@ const XSTATE_PARAM_NAMES = [
] as const;

/**
* Strip import statements for 'xstate' since we inject bindings as fn params.
* Strip xstate imports (we inject bindings as fn params) and ESM export syntax
* so code can be evaluated via `new Function()`.
*/
function stripXStateImports(code: string): string {
function stripImportsAndExports(code: string): string {
return code
// import { ... } from 'xstate'
.replace(/import\s*\{[^}]*\}\s*from\s*['"]xstate['"]\s*;?/g, '')
.replace(/import\s*\*\s*as\s+\w+\s*from\s*['"]xstate['"]\s*;?/g, '');
// import * as x from 'xstate'
.replace(/import\s*\*\s*as\s+\w+\s*from\s*['"]xstate['"]\s*;?/g, '')
// export default ...
.replace(/^\s*export\s+default\s+/gm, '')
// export const/let/var/function/class
.replace(/^\s*export\s+(const|let|var|function|class)\b/gm, '$1')
// export { ... }
.replace(/^\s*export\s*\{[^}]*\}\s*;?\s*$/gm, '')
// export * from '...'
.replace(/^\s*export\s*\*\s*from\s*['"][^'"]+['"]\s*;?\s*$/gm, '');
}

/**
Expand Down Expand Up @@ -369,7 +380,7 @@ export function parseXStateMachineCode(code: string): {
} as any;

const jsCode = tsBlankSpace(code);
const strippedCode = stripXStateImports(jsCode);
const strippedCode = stripImportsAndExports(jsCode);
const fn = new Function(...XSTATE_PARAM_NAMES, strippedCode);

fn(
Expand Down
Loading