Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Added awareness of parent-child links in spawn #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions examples/spawn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createMachine, spawn, assign } from "xstate";

const childMachine = createMachine({});

const parentMachine = createMachine({
entry: [
assign((context, event) => {
return {
wow: spawn(childMachine),
};
}),
],
});
39 changes: 39 additions & 0 deletions src/__tests__/spawn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { parseMachinesFromFile } from "../parseMachinesFromFile";
import { testUtils } from "../testUtils";
import { ParseResultLink } from "../types";

describe("Links between machines", () => {
it("Should grab the identifier names", () => {
const result = parseMachinesFromFile(`
const childMachine = createMachine({});

const parentMachine = createMachine({});
`);

expect(result.machines[0].ast?.machineVariableName).toEqual("childMachine");
expect(result.machines[1].ast?.machineVariableName).toEqual(
"parentMachine",
);
});

it.only("Should grab a parent/child link", () => {
const result = parseMachinesFromFile(`
const childMachine = createMachine({});

const parentMachine = createMachine({
entry: [
assign((context, event) => {
return {
wow: spawn(childMachine),
};
}),
],
});
`);

expect(result.links[0]).toEqual({
sourceIndex: 0,
parentIndex: 1,
} as ParseResultLink);
});
});
8 changes: 5 additions & 3 deletions src/__tests__/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ describe("Validation and failsafes", () => {
parseMachinesFromFile(`
const hello = 2;
`),
).toEqual({
machines: [],
});
).toEqual(
expect.objectContaining({
machines: [],
}),
);
});
});
});
34 changes: 34 additions & 0 deletions src/machineCallExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from "@babel/types";
import { StateNode } from "./stateNode";
import { createParser, GetParserResult } from "./utils";
import { MachineOptions } from "./options";
import traverse from "@babel/traverse";

export type TMachineCallExpression = GetParserResult<
typeof MachineCallExpression
Expand All @@ -20,19 +21,52 @@ export const MachineCallExpression = createParser({
calleeName: node.callee.property.name,
definition: StateNode.parse(node.arguments[0], context),
options: MachineOptions.parse(node.arguments[1], context),
variableDeclarator: undefined,
machineVariableName: undefined,
};
}

if (
t.isIdentifier(node.callee) &&
["createMachine", "Machine"].includes(node.callee.name)
) {
let variableDeclarator: t.VariableDeclarator | undefined = undefined;
let machineVariableName: string | undefined = undefined;

traverse(context.file as any, {
VariableDeclarator: (variableDeclaratorPath) => {
if (
t.isCallExpression(variableDeclaratorPath.node.init) &&
t.isIdentifier(variableDeclaratorPath.node.init.callee) &&
isInSameLoc(variableDeclaratorPath.node.init.callee, node.callee)
) {
variableDeclarator =
variableDeclaratorPath.node as t.VariableDeclarator;

if (t.isIdentifier(variableDeclaratorPath.node.id)) {
machineVariableName = variableDeclaratorPath.node.id.name;
}
}
},
});

return {
callee: node.callee,
calleeName: node.callee.name,
definition: StateNode.parse(node.arguments[0], context),
options: MachineOptions.parse(node.arguments[1], context),
variableDeclarator,
machineVariableName,
};
}
},
});

const isInSameLoc = (node1: t.Node, node2: t.Node) => {
return (
node1.loc?.start.line === node2.loc?.start.line &&
node1.loc?.end.line === node2.loc?.end.line &&
node1.loc?.start.column === node2.loc?.start.column &&
node1.loc?.end.column === node2.loc?.end.column
);
};
49 changes: 49 additions & 0 deletions src/parseMachinesFromFile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as parser from "@babel/parser";
import * as t from "@babel/types";
import traverse from "@babel/traverse";
import { MachineConfig } from "xstate";
import { MachineCallExpression } from "./machineCallExpression";
import { MachineParseResult } from "./MachineParseResult";
import { SpawnAction, SpawnActionParseResult } from "./spawn";
import { toMachineConfig } from "./toMachineConfig";
import { ParseResult } from "./types";

Expand All @@ -13,6 +15,7 @@ export const parseMachinesFromFile = (fileContents: string): ParseResult => {
) {
return {
machines: [],
links: [],
};
}

Expand All @@ -23,8 +26,11 @@ export const parseMachinesFromFile = (fileContents: string): ParseResult => {

let result: ParseResult = {
machines: [],
links: [],
};

const spawnCalls: SpawnActionParseResult[] = [];

traverse(parseResult as any, {
CallExpression(path: any) {
const ast = MachineCallExpression.parse(path.node, {
Expand All @@ -33,8 +39,51 @@ export const parseMachinesFromFile = (fileContents: string): ParseResult => {
if (ast) {
result.machines.push(new MachineParseResult({ ast }));
}

const spawnAst = SpawnAction.parse(path.node, { file: parseResult });

if (spawnAst) {
spawnCalls.push(spawnAst);
}
},
});

spawnCalls.forEach((spawnCall) => {
if (!spawnCall?.machineName) return;

const sourceMachineIndex = result.machines.findIndex(
(machine) => machine.ast?.machineVariableName === spawnCall.machineName,
);

const parentMachineIndex = result.machines.findIndex((machine) =>
isWithinLoc(machine.ast?.definition?.node, spawnCall.node),
);

if (sourceMachineIndex === -1 || parentMachineIndex === -1) {
return;
}

result.links.push({
parentIndex: parentMachineIndex,
sourceIndex: sourceMachineIndex,
});
});

return result;
};

const isWithinLoc = (parent: t.Node | undefined, child: t.Node | undefined) => {
if (!parent?.loc || !child?.loc) return false;

const isBeforeFirst =
child.loc?.start.line < parent.loc?.start.line ||
(child.loc?.start.line === parent.loc?.start.line &&
child.loc?.start.column < parent.loc?.start.column);

const isAfterLast =
child.loc?.end.line > parent.loc?.end.line ||
(child.loc?.end.line === parent.loc?.end.line &&
child.loc?.end.column > parent.loc?.end.column);

return !(isBeforeFirst || isAfterLast);
};
28 changes: 28 additions & 0 deletions src/spawn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as t from "@babel/types";
import {
createParser,
GetParserResult,
namedFunctionCall,
wrapParserResult,
} from "./utils";

export type SpawnActionParseResult = GetParserResult<typeof SpawnAction>;

export const SpawnAction = wrapParserResult(
namedFunctionCall(
"spawn",
createParser({
babelMatcher: t.isIdentifier,
parseNode: (node) => {
return node;
},
}),
),
(result) => {
return {
node: result.node,
machineName: result.argument1Result?.name,
machineIdentifier: result.argument1Result,
};
},
);
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface AnyParser<Result> {
matches: (node: any) => boolean;
}

export interface ParseResultLink {
sourceIndex: number;
parentIndex: number;
}

export interface ParseResult {
machines: MachineParseResult[];
links: ParseResultLink[];
}
23 changes: 13 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,22 @@ export const namedFunctionCall = <Argument1Result, Argument2Result>(
},
});

return {
matches: (node: t.CallExpression) => {
if (!namedFunctionParser.matches(node)) {
return false;
}
const matches = (node: t.CallExpression) => {
if (!namedFunctionParser.matches(node)) {
return false;
}

if (!t.isIdentifier(node.callee)) {
return false;
}
if (!t.isIdentifier(node.callee)) {
return false;
}

return node.callee.name === name;
},
return node.callee.name === name;
};

return {
matches,
parse: (node: t.CallExpression, context) => {
if (!matches(node)) return;
return {
node,
argument1Result: argument1Parser.parse(node.arguments[0], context),
Expand Down