Skip to content

Commit

Permalink
Extract inline actors (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
farskid authored Jan 3, 2024
1 parent 55fd20a commit 9cef236
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 56 deletions.
16 changes: 11 additions & 5 deletions apps/extension/server/src/getReferences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
forEachAction,
forEachEntity,
getRangeFromSourceLocation,
} from '@xstate/tools-shared';
import { TextDocumentIdentifier } from 'vscode-languageserver';
Expand All @@ -25,8 +25,11 @@ export const getReferences = (
const config = cursorHover.machine.toConfig();
if (!config) return [];

// Actions don't matter here so we stub them out
forEachAction(config, () => {
// Actions and actors don't matter here so we stub them out
forEachEntity(config, (entity) => {
if (entity && 'src' in entity) {
return { src: 'anonymous' };
}
return { type: 'anonymous' };
});

Expand Down Expand Up @@ -66,8 +69,11 @@ export const getReferences = (
const config = cursorHover.machine.toConfig();
if (!config) return [];

// Actions don't matter here so we stub them out
forEachAction(config, () => {
// Actions and actors don't matter here so we stub them out
forEachEntity(config, (entity) => {
if (entity && 'src' in entity) {
return { src: 'anonymous' };
}
return { type: 'anonymous' };
});

Expand Down
52 changes: 52 additions & 0 deletions packages/machine-extractor/src/__tests__/invoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,56 @@ describe('Invoke', () => {

expect(invokes).toHaveLength(1);
});

it('Should extract inline actors correctly', () => {
const result = extractMachinesFromFile(`
createMachine({
invoke: [
{src: 'string source'},
{src: () => {
console.log('inline function')
}},
{src: () => () => {
console.log('callback actor')
}},
{src: fromPromise(() => {
return fetch('https://example.com/...').then((data) => data.json());
})},
{src: identifierActor},
]
})
`);
const invoke = result!.machines[0]!.toConfig()?.invoke;

expect(invoke).toMatchInlineSnapshot(`
[
{
"kind": "named",
"src": "string source",
},
{
"kind": "inline",
"src": "() => {
console.log('inline function')
}",
},
{
"kind": "inline",
"src": "() => () => {
console.log('callback actor')
}",
},
{
"kind": "inline",
"src": "fromPromise(() => {
return fetch('https://example.com/...').then((data) => data.json());
})",
},
{
"kind": "inline",
"src": "identifierActor",
},
]
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('Internal transitions', () => {
"states": {
"a": {
"invoke": {
"kind": "named",
"onDone": {
"internal": false,
},
Expand Down
6 changes: 3 additions & 3 deletions packages/machine-extractor/src/extractAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ export function extractSendToAction(
// Todo: support namespace imports and aliased specifiers
// import * as actions from 'xstate'; actions.sendTo
// import {sendTo as x} from 'xstate'
export const getActionCreatorName = (actionNode: ActionNode) =>
t.isCallExpression(actionNode.node) && t.isIdentifier(actionNode.node.callee)
? actionNode.node.callee.name
export const getCallExpressionName = (node: t.Node) =>
t.isCallExpression(node) && t.isIdentifier(node.callee)
? node.callee.name
: undefined;

export const getObjectPropertyKey = (
Expand Down
25 changes: 22 additions & 3 deletions packages/machine-extractor/src/toMachineConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as t from '@babel/types';
import { getNameOfDeclaration } from 'typescript';
import { MaybeArrayOfActions } from './actions';
import { CondNode } from './conds';
import {
Expand All @@ -8,6 +9,8 @@ import {
extractRaiseAction,
extractSendToAction,
extractStopAction,
getCallExpressionName,
getObjectPropertyKey,
} from './extractAction';
import { TMachineCallExpression } from './machineCallExpression';
import { StateNodeReturn } from './stateNode';
Expand Down Expand Up @@ -133,11 +136,27 @@ const parseStateNode = (
return;
}
// For now, we'll treat "anonymous" as if this is an inline expression
let src: string | undefined =
invoke.src.declarationType === 'named' ? invoke.src.value : undefined;
let invokeDef:
| { src: string; kind: ExtractorInvokeNodeConfig['kind'] }
| undefined = (() => {
if (invoke.src.declarationType === 'named') {
return {
src: invoke.src.value,
kind: 'named',
};
}
return {
src: opts!.fileContent.slice(
invoke.src.node.start!,
invoke.src.node.end!,
),
kind: 'inline',
};
})();

const toPush: ExtractorInvokeNodeConfig = {
src: src || (() => () => {}),
src: invokeDef.src,
kind: invokeDef.kind,
};

if (invoke.id) {
Expand Down
3 changes: 2 additions & 1 deletion packages/machine-extractor/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ export type ExtractrorTransitionNodeConfig = {
};

export type ExtractorInvokeNodeConfig = {
src?: string | Function;
src: string;
id?: string;
autoForward?: boolean;
forward?: boolean;
onDone?: MaybeArray<ExtractrorTransitionNodeConfig>;
onError?: MaybeArray<ExtractrorTransitionNodeConfig>;
kind: 'inline' | 'named';
};

export type ExtractorStateNodeConfig = {
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/forEachEntity/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/xstate-tools-shared-forEachEntity.cjs.js",
"module": "dist/xstate-tools-shared-forEachEntity.esm.js"
}
14 changes: 7 additions & 7 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"import": "./dist/xstate-tools-shared.cjs.mjs",
"default": "./dist/xstate-tools-shared.cjs.js"
},
"./forEachAction": {
"./forEachEntity": {
"types": {
"import": "./forEachAction/dist/xstate-tools-shared-forEachAction.cjs.mjs",
"default": "./forEachAction/dist/xstate-tools-shared-forEachAction.cjs.js"
"import": "./forEachEntity/dist/xstate-tools-shared-forEachEntity.cjs.mjs",
"default": "./forEachEntity/dist/xstate-tools-shared-forEachEntity.cjs.js"
},
"module": "./forEachAction/dist/xstate-tools-shared-forEachAction.esm.js",
"import": "./forEachAction/dist/xstate-tools-shared-forEachAction.cjs.mjs",
"default": "./forEachAction/dist/xstate-tools-shared-forEachAction.cjs.js"
"module": "./forEachEntity/dist/xstate-tools-shared-forEachEntity.esm.js",
"import": "./forEachEntity/dist/xstate-tools-shared-forEachEntity.cjs.mjs",
"default": "./forEachEntity/dist/xstate-tools-shared-forEachEntity.cjs.js"
},
"./package.json": "./package.json"
},
Expand Down Expand Up @@ -48,7 +48,7 @@
"preconstruct": {
"entrypoints": [
"./index.ts",
"./forEachAction.ts"
"./forEachEntity.ts"
]
}
}
Loading

0 comments on commit 9cef236

Please sign in to comment.