Skip to content

Commit

Permalink
Handle guard edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Mar 11, 2024
1 parent b277797 commit c7ae8f9
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 5 deletions.
96 changes: 96 additions & 0 deletions new-packages/ts-project/__tests__/source-edits/edit-guard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { expect, test } from 'vitest';
import { createTestProject, testdir, ts } from '../utils';

test('should be possible to update a guard name', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";
createMachine({
on: {
CALL_HIM_MAYBE: {
guard: "isItTooLate",
actions: "callDavid",
},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'edit_guard',
path: [],
transitionPath: ['on', 'CALL_HIM_MAYBE', 0],
name: 'isHeAwake',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";
createMachine({
on: {
CALL_HIM_MAYBE: {
guard: "isHeAwake",
actions: "callDavid",
},
},
});",
}
`);
});

test('should be possible to update a parametrized guard name', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";
createMachine({
on: {
CALL_HIM_MAYBE: {
guard: { type: "isItTooLate", time: "9am" },
actions: "callDavid",
},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'edit_guard',
path: [],
transitionPath: ['on', 'CALL_HIM_MAYBE', 0],
name: 'isHeAwake',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";
createMachine({
on: {
CALL_HIM_MAYBE: {
guard: { type: "isHeAwake", time: "9am" },
actions: "callDavid",
},
},
});",
}
`);
});
28 changes: 25 additions & 3 deletions new-packages/ts-project/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ function produceNewDigraphUsingEdit(
: undefined;
break;
}
case 'add_transition':
case 'add_transition': {
const sourceNode = findNodeByStatePath(digraphDraft, edit.sourcePath);
const targetNode =
edit.targetPath && findNodeByStatePath(digraphDraft, edit.targetPath);
Expand Down Expand Up @@ -455,6 +455,7 @@ function produceNewDigraphUsingEdit(
}

break;
}
case 'remove_transition':
case 'reanchor_transition':
case 'change_transition_path':
Expand Down Expand Up @@ -553,8 +554,28 @@ function produceNewDigraphUsingEdit(
break;
}
case 'remove_guard':
case 'edit_guard':
throw new Error(`Not implemented: ${edit.type}`);
case 'edit_guard': {
const eventTypeData = getEventTypeData(digraphDraft, {
sourcePath: edit.path,
transitionPath: edit.transitionPath,
});
const edge =
digraphDraft.edges[
getEdgeGroup(digraphDraft, eventTypeData)[
last(edit.transitionPath) as number
]
];
const blockId = edge.data.guard;
assert(typeof blockId === 'string');
digraphDraft.blocks[blockId].sourceId = edit.name;
digraphDraft.implementations.guards[edit.name] ??= {
type: 'guard',
id: edit.name,
name: edit.name,
};
break;
}
case 'add_invoke': {
const node = findNodeByStatePath(digraphDraft, edit.path);
const block = createActorBlock({
Expand All @@ -573,13 +594,14 @@ function produceNewDigraphUsingEdit(
case 'remove_invoke':
case 'edit_invoke':
throw new Error(`Not implemented: ${edit.type}`);
case 'set_description':
case 'set_description': {
const node = findNodeByStatePath(digraphDraft, edit.statePath);
if (edit.transitionPath) {
throw new Error(`Not implemented`);
}
node.data.description = edit.description;
break;
}
}
}

Expand Down
31 changes: 29 additions & 2 deletions new-packages/ts-project/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,35 @@ function createProjectMachine({
);
break;
}
case 'actor':
case 'guard':
case 'actor': {
break;
}
case 'guard': {
const edge = currentState.digraph!.edges[block.parentId];
const transitionNode = findNodeByAstPath(
host.ts,
createMachineCall,
currentState.astPaths.edges[edge.uniqueId],
);
assert(host.ts.isObjectLiteralExpression(transitionNode));

const guardProperty = findProperty(
undefined,
host.ts,
transitionNode,
'guard',
);

assert(!!guardProperty);

updateParameterizedObjectLocation(
host.ts,
codeChanges,
guardProperty.initializer,
block.sourceId,
);
break;
}
}
}
}
Expand Down

0 comments on commit c7ae8f9

Please sign in to comment.