Skip to content

Commit

Permalink
internal transition extracting (#369)
Browse files Browse the repository at this point in the history
* Parse internal keyword on transitions

* Add test for parsing internal property on transitions

* changeset

* Only add internal property if transitions is explicitly internal

* Parse -> Extract

Co-authored-by: Mateusz Burzyński <[email protected]>

* Keep explicitly provided `internal` property regardless of its value

Co-authored-by: Mateusz Burzyński <[email protected]>

* Keep explicitly provided `internal` property regardless of its value

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update tests according to new internal extraction

* update snapshot

* update snapshots again

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
  • Loading branch information
farskid and Andarist authored Sep 20, 2023
1 parent d885182 commit 5d47848
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-readers-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xstate/machine-extractor': minor
---

Extract `internal` property on transitions
116 changes: 116 additions & 0 deletions packages/machine-extractor/src/__tests__/transitions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { extractMachinesFromFile } from '..';

describe('Internal transitions', () => {
it('Should keep explicitly provied internal property on transitions', () => {
const result = extractMachinesFromFile(`
createMachine({
initial: 'a',
states: {
a: {
on: {
move: {
target: 'b',
internal: true
}
},
invoke: {
src: 'actor',
onDone: {
internal: false
},
onError: {internal: true}
}
},
b: {
after: {
100: {
target: 'c',
internal: true
}
},
always: {target: 'a', internal: false},
onDone: {
internal: true
}
}
}
})
`);

expect(result?.machines[0]?.toConfig()).toMatchInlineSnapshot(`
{
"initial": "a",
"states": {
"a": {
"invoke": {
"onDone": {
"internal": false,
},
"onError": {
"internal": true,
},
"src": "actor",
},
"on": {
"move": {
"internal": true,
"target": "b",
},
},
},
"b": {
"after": {
"100": {
"internal": true,
"target": "c",
},
},
"always": {
"internal": false,
"target": "a",
},
"onDone": {
"internal": true,
},
},
},
}
`);
});

it('Should leave out internal property if transition is missing internal property', () => {
const result = extractMachinesFromFile(`
createMachine({
initial: 'a',
states: {
a: {
on: {
foo: {
target: 'b'
},
bar: 'c',
}
}
}
})
`);

expect(result?.machines[0]?.toConfig()).toMatchInlineSnapshot(`
{
"initial": "a",
"states": {
"a": {
"on": {
"bar": {
"target": "c",
},
"foo": {
"target": "b",
},
},
},
},
}
`);
});
});
4 changes: 4 additions & 0 deletions packages/machine-extractor/src/toMachineConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ export const getTransitions = (
if (transition?.description) {
toPush.description = transition?.description.value;
}
// Only add `internal` if its present
if (typeof transition.internal?.value === 'boolean') {
toPush.internal = transition.internal.value;
}

transitions.push(toPush);
});
Expand Down
3 changes: 2 additions & 1 deletion packages/machine-extractor/src/transitions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MaybeArrayOfActions } from './actions';
import { Cond } from './conds';
import { StringLiteral, TemplateLiteral } from './scalars';
import { BooleanLiteral, StringLiteral, TemplateLiteral } from './scalars';
import { unionType } from './unionType';
import {
GetParserResult,
Expand All @@ -20,6 +20,7 @@ const TransitionObject = objectTypeWithKnownKeys({
actions: MaybeArrayOfActions,
cond: Cond,
description: unionType([StringLiteral, TemplateLiteral]),
internal: BooleanLiteral,
});

const TransitionConfigOrTargetLiteral = unionType([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export interface Typegen0 {
services: never;
};
eventsCausingActions: {
exitA: "xstate.stop";
exitA: "FOO" | "xstate.stop";
};
eventsCausingDelays: {};
eventsCausingGuards: {};
Expand Down

0 comments on commit 5d47848

Please sign in to comment.