@doeixd/machine 0.0.18 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +235 -3
- package/dist/cjs/development/core.js.map +2 -2
- package/dist/cjs/development/extract.js +500 -0
- package/dist/cjs/development/extract.js.map +7 -0
- package/dist/cjs/development/index.js +135 -477
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/production/extract.js +5 -0
- package/dist/cjs/production/index.js +4 -5
- package/dist/esm/development/core.js.map +2 -2
- package/dist/esm/development/extract.js +486 -0
- package/dist/esm/development/extract.js.map +7 -0
- package/dist/esm/development/index.js +135 -484
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/production/extract.js +5 -0
- package/dist/esm/production/index.js +4 -5
- package/dist/types/index.d.ts +45 -8
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/matcher.d.ts +325 -0
- package/dist/types/matcher.d.ts.map +1 -0
- package/package.json +13 -1
- package/src/index.ts +73 -9
- package/src/matcher.ts +560 -0
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
-
});
|
|
7
|
-
|
|
8
1
|
// src/generators.ts
|
|
9
2
|
function run(flow, initial) {
|
|
10
3
|
const generator = flow(initial);
|
|
@@ -591,478 +584,6 @@ function createParallelMachine(m1, m2) {
|
|
|
591
584
|
};
|
|
592
585
|
}
|
|
593
586
|
|
|
594
|
-
// src/extract.ts
|
|
595
|
-
import { Project, Node } from "ts-morph";
|
|
596
|
-
function _typeToJson(type, verbose = false) {
|
|
597
|
-
const symbol = type.getSymbol();
|
|
598
|
-
if (symbol && symbol.getDeclarations().some(Node.isClassDeclaration)) {
|
|
599
|
-
return symbol.getName();
|
|
600
|
-
}
|
|
601
|
-
if (type.isStringLiteral()) return type.getLiteralValue();
|
|
602
|
-
if (type.isNumberLiteral()) return type.getLiteralValue();
|
|
603
|
-
if (type.isBooleanLiteral()) return type.getLiteralValue();
|
|
604
|
-
if (type.isString()) return "string";
|
|
605
|
-
if (type.isNumber()) return "number";
|
|
606
|
-
if (type.isBoolean()) return "boolean";
|
|
607
|
-
if (type.isArray()) {
|
|
608
|
-
const elementType = type.getArrayElementTypeOrThrow();
|
|
609
|
-
return [_typeToJson(elementType, verbose)];
|
|
610
|
-
}
|
|
611
|
-
if (type.isObject() || type.isIntersection()) {
|
|
612
|
-
const obj = {};
|
|
613
|
-
const properties = type.getProperties();
|
|
614
|
-
for (const prop of properties) {
|
|
615
|
-
const propName = prop.getName();
|
|
616
|
-
if (propName.startsWith("__@")) continue;
|
|
617
|
-
const declaration = prop.getValueDeclaration();
|
|
618
|
-
if (!declaration) continue;
|
|
619
|
-
try {
|
|
620
|
-
obj[propName] = _typeToJson(declaration.getType(), verbose);
|
|
621
|
-
} catch (e) {
|
|
622
|
-
if (verbose) console.error(` Warning: Failed to serialize property ${propName}:`, e);
|
|
623
|
-
obj[propName] = "unknown";
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
return Object.keys(obj).length > 0 ? obj : null;
|
|
627
|
-
}
|
|
628
|
-
if (verbose) {
|
|
629
|
-
console.error(` Unhandled type: ${type.getText()}`);
|
|
630
|
-
}
|
|
631
|
-
return "unknown";
|
|
632
|
-
}
|
|
633
|
-
function resolveClassName(node) {
|
|
634
|
-
if (Node.isIdentifier(node)) {
|
|
635
|
-
return node.getText();
|
|
636
|
-
}
|
|
637
|
-
if (Node.isTypeOfExpression(node)) {
|
|
638
|
-
return node.getExpression().getText();
|
|
639
|
-
}
|
|
640
|
-
return "unknown";
|
|
641
|
-
}
|
|
642
|
-
function parseObjectLiteral(obj) {
|
|
643
|
-
if (!Node.isObjectLiteralExpression(obj)) {
|
|
644
|
-
return {};
|
|
645
|
-
}
|
|
646
|
-
const result = {};
|
|
647
|
-
for (const prop of obj.getProperties()) {
|
|
648
|
-
if (Node.isPropertyAssignment(prop)) {
|
|
649
|
-
const name = prop.getName();
|
|
650
|
-
const init = prop.getInitializer();
|
|
651
|
-
if (init) {
|
|
652
|
-
if (Node.isStringLiteral(init)) {
|
|
653
|
-
result[name] = init.getLiteralValue();
|
|
654
|
-
} else if (Node.isNumericLiteral(init)) {
|
|
655
|
-
result[name] = init.getLiteralValue();
|
|
656
|
-
} else if (init.getText() === "true" || init.getText() === "false") {
|
|
657
|
-
result[name] = init.getText() === "true";
|
|
658
|
-
} else if (Node.isIdentifier(init)) {
|
|
659
|
-
result[name] = init.getText();
|
|
660
|
-
} else if (Node.isObjectLiteralExpression(init)) {
|
|
661
|
-
result[name] = parseObjectLiteral(init);
|
|
662
|
-
} else if (Node.isArrayLiteralExpression(init)) {
|
|
663
|
-
result[name] = init.getElements().map((el) => {
|
|
664
|
-
if (Node.isObjectLiteralExpression(el)) {
|
|
665
|
-
return parseObjectLiteral(el);
|
|
666
|
-
}
|
|
667
|
-
return el.getText();
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
return result;
|
|
674
|
-
}
|
|
675
|
-
function parseInvokeService(obj) {
|
|
676
|
-
if (!Node.isObjectLiteralExpression(obj)) {
|
|
677
|
-
return {};
|
|
678
|
-
}
|
|
679
|
-
const service = {};
|
|
680
|
-
for (const prop of obj.getProperties()) {
|
|
681
|
-
if (Node.isPropertyAssignment(prop)) {
|
|
682
|
-
const name = prop.getName();
|
|
683
|
-
const init = prop.getInitializer();
|
|
684
|
-
if (!init) continue;
|
|
685
|
-
if (name === "onDone" || name === "onError") {
|
|
686
|
-
service[name] = resolveClassName(init);
|
|
687
|
-
} else if (Node.isStringLiteral(init)) {
|
|
688
|
-
service[name] = init.getLiteralValue();
|
|
689
|
-
} else if (Node.isIdentifier(init)) {
|
|
690
|
-
service[name] = init.getText();
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
return service;
|
|
695
|
-
}
|
|
696
|
-
function extractFromCallExpression(call2, verbose = false) {
|
|
697
|
-
if (!Node.isCallExpression(call2)) {
|
|
698
|
-
return null;
|
|
699
|
-
}
|
|
700
|
-
const expression = call2.getExpression();
|
|
701
|
-
const fnName = Node.isIdentifier(expression) ? expression.getText() : null;
|
|
702
|
-
if (!fnName) {
|
|
703
|
-
return null;
|
|
704
|
-
}
|
|
705
|
-
const metadata2 = {};
|
|
706
|
-
const args = call2.getArguments();
|
|
707
|
-
switch (fnName) {
|
|
708
|
-
case "transitionTo":
|
|
709
|
-
if (args[0]) {
|
|
710
|
-
metadata2.target = resolveClassName(args[0]);
|
|
711
|
-
}
|
|
712
|
-
break;
|
|
713
|
-
case "describe":
|
|
714
|
-
if (args[0] && Node.isStringLiteral(args[0])) {
|
|
715
|
-
metadata2.description = args[0].getLiteralValue();
|
|
716
|
-
}
|
|
717
|
-
if (args[1] && Node.isCallExpression(args[1])) {
|
|
718
|
-
const nested = extractFromCallExpression(args[1], verbose);
|
|
719
|
-
if (nested) {
|
|
720
|
-
Object.assign(metadata2, nested);
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
break;
|
|
724
|
-
case "guarded":
|
|
725
|
-
if (args[0]) {
|
|
726
|
-
const guard2 = parseObjectLiteral(args[0]);
|
|
727
|
-
if (Object.keys(guard2).length > 0) {
|
|
728
|
-
metadata2.guards = [guard2];
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
if (args[1] && Node.isCallExpression(args[1])) {
|
|
732
|
-
const nested = extractFromCallExpression(args[1], verbose);
|
|
733
|
-
if (nested) {
|
|
734
|
-
Object.assign(metadata2, nested);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
break;
|
|
738
|
-
case "invoke":
|
|
739
|
-
if (args[0]) {
|
|
740
|
-
const service = parseInvokeService(args[0]);
|
|
741
|
-
if (Object.keys(service).length > 0) {
|
|
742
|
-
metadata2.invoke = service;
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
break;
|
|
746
|
-
case "action":
|
|
747
|
-
if (args[0]) {
|
|
748
|
-
const actionMeta = parseObjectLiteral(args[0]);
|
|
749
|
-
if (Object.keys(actionMeta).length > 0) {
|
|
750
|
-
metadata2.actions = [actionMeta];
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
if (args[1] && Node.isCallExpression(args[1])) {
|
|
754
|
-
const nested = extractFromCallExpression(args[1], verbose);
|
|
755
|
-
if (nested) {
|
|
756
|
-
Object.assign(metadata2, nested);
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
break;
|
|
760
|
-
case "guard":
|
|
761
|
-
if (args[2]) {
|
|
762
|
-
const options = parseObjectLiteral(args[2]);
|
|
763
|
-
if (options.description) {
|
|
764
|
-
metadata2.description = options.description;
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
metadata2.guards = [{ name: "runtime_guard", description: metadata2.description || "Synchronous condition check" }];
|
|
768
|
-
if (args[1] && Node.isCallExpression(args[1])) {
|
|
769
|
-
const nested = extractFromCallExpression(args[1], verbose);
|
|
770
|
-
if (nested) {
|
|
771
|
-
Object.assign(metadata2, nested);
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
break;
|
|
775
|
-
case "guardAsync":
|
|
776
|
-
if (args[2]) {
|
|
777
|
-
const options = parseObjectLiteral(args[2]);
|
|
778
|
-
if (options.description) {
|
|
779
|
-
metadata2.description = options.description;
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
metadata2.guards = [{ name: "runtime_guard_async", description: metadata2.description || "Asynchronous condition check" }];
|
|
783
|
-
if (args[1] && Node.isCallExpression(args[1])) {
|
|
784
|
-
const nested = extractFromCallExpression(args[1], verbose);
|
|
785
|
-
if (nested) {
|
|
786
|
-
Object.assign(metadata2, nested);
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
break;
|
|
790
|
-
default:
|
|
791
|
-
return null;
|
|
792
|
-
}
|
|
793
|
-
return Object.keys(metadata2).length > 0 ? metadata2 : null;
|
|
794
|
-
}
|
|
795
|
-
function extractMetaFromMember(member, verbose = false) {
|
|
796
|
-
if (!Node.isPropertyDeclaration(member)) {
|
|
797
|
-
if (verbose) console.error(` ⚠️ Not a property declaration`);
|
|
798
|
-
return null;
|
|
799
|
-
}
|
|
800
|
-
const initializer = member.getInitializer();
|
|
801
|
-
if (!initializer) {
|
|
802
|
-
if (verbose) console.error(` ⚠️ No initializer`);
|
|
803
|
-
return null;
|
|
804
|
-
}
|
|
805
|
-
if (!Node.isCallExpression(initializer)) {
|
|
806
|
-
if (verbose) console.error(` ⚠️ Initializer is not a call expression`);
|
|
807
|
-
return null;
|
|
808
|
-
}
|
|
809
|
-
const metadata2 = extractFromCallExpression(initializer, verbose);
|
|
810
|
-
if (metadata2 && verbose) {
|
|
811
|
-
console.error(` ✅ Extracted metadata:`, JSON.stringify(metadata2, null, 2));
|
|
812
|
-
}
|
|
813
|
-
return metadata2;
|
|
814
|
-
}
|
|
815
|
-
function analyzeStateNode(classSymbol, verbose = false) {
|
|
816
|
-
const chartNode = { on: {} };
|
|
817
|
-
const classDeclaration = classSymbol.getDeclarations()[0];
|
|
818
|
-
if (!classDeclaration || !Node.isClassDeclaration(classDeclaration)) {
|
|
819
|
-
if (verbose) {
|
|
820
|
-
console.error(`⚠️ Warning: Could not get class declaration for ${classSymbol.getName()}`);
|
|
821
|
-
}
|
|
822
|
-
return chartNode;
|
|
823
|
-
}
|
|
824
|
-
const className = classSymbol.getName();
|
|
825
|
-
if (verbose) {
|
|
826
|
-
console.error(` Analyzing state: ${className}`);
|
|
827
|
-
}
|
|
828
|
-
for (const member of classDeclaration.getInstanceMembers()) {
|
|
829
|
-
const memberName = member.getName();
|
|
830
|
-
if (verbose) {
|
|
831
|
-
console.error(` Checking member: ${memberName}`);
|
|
832
|
-
}
|
|
833
|
-
const meta = extractMetaFromMember(member, verbose);
|
|
834
|
-
if (!meta) continue;
|
|
835
|
-
if (verbose) {
|
|
836
|
-
console.error(` Found transition: ${memberName}`);
|
|
837
|
-
}
|
|
838
|
-
const { invoke: invoke2, actions, guards, ...onEntry } = meta;
|
|
839
|
-
if (invoke2) {
|
|
840
|
-
if (!chartNode.invoke) chartNode.invoke = [];
|
|
841
|
-
chartNode.invoke.push({
|
|
842
|
-
src: invoke2.src,
|
|
843
|
-
onDone: { target: invoke2.onDone },
|
|
844
|
-
onError: { target: invoke2.onError },
|
|
845
|
-
description: invoke2.description
|
|
846
|
-
});
|
|
847
|
-
if (verbose) {
|
|
848
|
-
console.error(` → Invoke: ${invoke2.src}`);
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
if (onEntry.target) {
|
|
852
|
-
const transition = { target: onEntry.target };
|
|
853
|
-
if (onEntry.description) {
|
|
854
|
-
transition.description = onEntry.description;
|
|
855
|
-
}
|
|
856
|
-
if (guards) {
|
|
857
|
-
transition.cond = guards.map((g) => g.name).join(" && ");
|
|
858
|
-
if (verbose) {
|
|
859
|
-
console.error(` → Guard: ${transition.cond}`);
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
if (actions && actions.length > 0) {
|
|
863
|
-
transition.actions = actions.map((a) => a.name);
|
|
864
|
-
if (verbose) {
|
|
865
|
-
console.error(` → Actions: ${transition.actions.join(", ")}`);
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
chartNode.on[memberName] = transition;
|
|
869
|
-
if (verbose) {
|
|
870
|
-
console.error(` → Target: ${onEntry.target}`);
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
return chartNode;
|
|
875
|
-
}
|
|
876
|
-
function analyzeStateNodeWithNesting(className, classSymbol, sourceFile, childConfig, verbose = false) {
|
|
877
|
-
const stateNode = analyzeStateNode(classSymbol, verbose);
|
|
878
|
-
if (childConfig) {
|
|
879
|
-
if (verbose) {
|
|
880
|
-
console.error(` 👪 Analyzing children for state: ${className}`);
|
|
881
|
-
}
|
|
882
|
-
stateNode.initial = childConfig.initialState;
|
|
883
|
-
stateNode.states = {};
|
|
884
|
-
for (const childClassName of childConfig.classes) {
|
|
885
|
-
const childClassDeclaration = sourceFile.getClass(childClassName);
|
|
886
|
-
if (childClassDeclaration) {
|
|
887
|
-
const childSymbol = childClassDeclaration.getSymbolOrThrow();
|
|
888
|
-
stateNode.states[childClassName] = analyzeStateNode(childSymbol, verbose);
|
|
889
|
-
} else {
|
|
890
|
-
console.warn(`⚠️ Warning: Child class '${childClassName}' not found.`);
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
return stateNode;
|
|
895
|
-
}
|
|
896
|
-
function extractMachine(config, project, verbose = false) {
|
|
897
|
-
if (verbose) {
|
|
898
|
-
console.error(`
|
|
899
|
-
🔍 Analyzing machine: ${config.id}`);
|
|
900
|
-
console.error(` Source: ${config.input}`);
|
|
901
|
-
}
|
|
902
|
-
const sourceFile = project.getSourceFile(config.input);
|
|
903
|
-
if (!sourceFile) {
|
|
904
|
-
throw new Error(`Source file not found: ${config.input}`);
|
|
905
|
-
}
|
|
906
|
-
if (config.parallel) {
|
|
907
|
-
if (verbose) {
|
|
908
|
-
console.error(` ⏹️ Parallel machine detected. Analyzing regions.`);
|
|
909
|
-
}
|
|
910
|
-
const parallelChart = {
|
|
911
|
-
id: config.id,
|
|
912
|
-
type: "parallel",
|
|
913
|
-
states: {}
|
|
914
|
-
};
|
|
915
|
-
if (config.description) {
|
|
916
|
-
parallelChart.description = config.description;
|
|
917
|
-
}
|
|
918
|
-
for (const region of config.parallel.regions) {
|
|
919
|
-
if (verbose) {
|
|
920
|
-
console.error(` 📍 Analyzing region: ${region.name}`);
|
|
921
|
-
}
|
|
922
|
-
const regionStates = {};
|
|
923
|
-
for (const className of region.classes) {
|
|
924
|
-
const classDeclaration = sourceFile.getClass(className);
|
|
925
|
-
if (classDeclaration) {
|
|
926
|
-
const classSymbol = classDeclaration.getSymbolOrThrow();
|
|
927
|
-
regionStates[className] = analyzeStateNode(classSymbol, verbose);
|
|
928
|
-
} else {
|
|
929
|
-
console.warn(`⚠️ Warning: Class '${className}' not found for region '${region.name}'.`);
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
parallelChart.states[region.name] = {
|
|
933
|
-
initial: region.initialState,
|
|
934
|
-
states: regionStates
|
|
935
|
-
};
|
|
936
|
-
}
|
|
937
|
-
if (verbose) {
|
|
938
|
-
console.error(` ✅ Extracted ${config.parallel.regions.length} parallel regions`);
|
|
939
|
-
}
|
|
940
|
-
return parallelChart;
|
|
941
|
-
}
|
|
942
|
-
if (!config.initialState || !config.classes) {
|
|
943
|
-
throw new Error(`Machine config for '${config.id}' must have either 'parallel' or 'initialState'/'classes'.`);
|
|
944
|
-
}
|
|
945
|
-
const fullChart = {
|
|
946
|
-
id: config.id,
|
|
947
|
-
initial: config.initialState,
|
|
948
|
-
states: {}
|
|
949
|
-
};
|
|
950
|
-
if (config.description) {
|
|
951
|
-
fullChart.description = config.description;
|
|
952
|
-
}
|
|
953
|
-
for (const className of config.classes) {
|
|
954
|
-
const classDeclaration = sourceFile.getClass(className);
|
|
955
|
-
if (!classDeclaration) {
|
|
956
|
-
console.warn(`⚠️ Warning: Class '${className}' not found in '${config.input}'. Skipping.`);
|
|
957
|
-
continue;
|
|
958
|
-
}
|
|
959
|
-
const classSymbol = classDeclaration.getSymbolOrThrow();
|
|
960
|
-
const hasChildren = className === config.initialState && config.children;
|
|
961
|
-
const stateNode = analyzeStateNodeWithNesting(
|
|
962
|
-
className,
|
|
963
|
-
classSymbol,
|
|
964
|
-
sourceFile,
|
|
965
|
-
hasChildren ? config.children : void 0,
|
|
966
|
-
verbose
|
|
967
|
-
);
|
|
968
|
-
fullChart.states[className] = stateNode;
|
|
969
|
-
}
|
|
970
|
-
if (verbose) {
|
|
971
|
-
console.error(` ✅ Extracted ${config.classes.length} states`);
|
|
972
|
-
}
|
|
973
|
-
return fullChart;
|
|
974
|
-
}
|
|
975
|
-
function extractMachines(config) {
|
|
976
|
-
var _a;
|
|
977
|
-
const verbose = (_a = config.verbose) != null ? _a : false;
|
|
978
|
-
if (verbose) {
|
|
979
|
-
console.error(`
|
|
980
|
-
📊 Starting statechart extraction`);
|
|
981
|
-
console.error(` Machines to extract: ${config.machines.length}`);
|
|
982
|
-
}
|
|
983
|
-
const project = new Project();
|
|
984
|
-
project.addSourceFilesAtPaths("src/**/*.ts");
|
|
985
|
-
project.addSourceFilesAtPaths("examples/**/*.ts");
|
|
986
|
-
const results = [];
|
|
987
|
-
for (const machineConfig of config.machines) {
|
|
988
|
-
try {
|
|
989
|
-
const chart = extractMachine(machineConfig, project, verbose);
|
|
990
|
-
results.push(chart);
|
|
991
|
-
} catch (error) {
|
|
992
|
-
console.error(`❌ Error extracting machine '${machineConfig.id}':`, error);
|
|
993
|
-
if (!verbose) {
|
|
994
|
-
console.error(` Run with --verbose for more details`);
|
|
995
|
-
}
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
if (verbose) {
|
|
999
|
-
console.error(`
|
|
1000
|
-
✅ Extraction complete: ${results.length}/${config.machines.length} machines extracted`);
|
|
1001
|
-
}
|
|
1002
|
-
return results;
|
|
1003
|
-
}
|
|
1004
|
-
function generateChart() {
|
|
1005
|
-
const config = {
|
|
1006
|
-
input: "examples/authMachine.ts",
|
|
1007
|
-
classes: [
|
|
1008
|
-
"LoggedOutMachine",
|
|
1009
|
-
"LoggingInMachine",
|
|
1010
|
-
"LoggedInMachine",
|
|
1011
|
-
"SessionExpiredMachine",
|
|
1012
|
-
"ErrorMachine"
|
|
1013
|
-
],
|
|
1014
|
-
id: "auth",
|
|
1015
|
-
initialState: "LoggedOutMachine",
|
|
1016
|
-
description: "Authentication state machine"
|
|
1017
|
-
};
|
|
1018
|
-
console.error("🔍 Using legacy generateChart function");
|
|
1019
|
-
console.error("⚠️ Consider using extractMachines() with a config file instead\n");
|
|
1020
|
-
const project = new Project();
|
|
1021
|
-
project.addSourceFilesAtPaths("src/**/*.ts");
|
|
1022
|
-
project.addSourceFilesAtPaths("examples/**/*.ts");
|
|
1023
|
-
try {
|
|
1024
|
-
const chart = extractMachine(config, project, true);
|
|
1025
|
-
console.log(JSON.stringify(chart, null, 2));
|
|
1026
|
-
} catch (error) {
|
|
1027
|
-
console.error(`❌ Error:`, error);
|
|
1028
|
-
process.exit(1);
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
var ADVANCED_CONFIG_EXAMPLES = {
|
|
1032
|
-
hierarchical: {
|
|
1033
|
-
input: "examples/dashboardMachine.ts",
|
|
1034
|
-
id: "dashboard",
|
|
1035
|
-
classes: ["DashboardMachine", "LoggedOutMachine"],
|
|
1036
|
-
initialState: "DashboardMachine",
|
|
1037
|
-
children: {
|
|
1038
|
-
contextProperty: "child",
|
|
1039
|
-
initialState: "ViewingChildMachine",
|
|
1040
|
-
classes: ["ViewingChildMachine", "EditingChildMachine"]
|
|
1041
|
-
}
|
|
1042
|
-
},
|
|
1043
|
-
parallel: {
|
|
1044
|
-
input: "examples/editorMachine.ts",
|
|
1045
|
-
id: "editor",
|
|
1046
|
-
parallel: {
|
|
1047
|
-
regions: [
|
|
1048
|
-
{
|
|
1049
|
-
name: "fontWeight",
|
|
1050
|
-
initialState: "NormalWeight",
|
|
1051
|
-
classes: ["NormalWeight", "BoldWeight"]
|
|
1052
|
-
},
|
|
1053
|
-
{
|
|
1054
|
-
name: "textDecoration",
|
|
1055
|
-
initialState: "NoDecoration",
|
|
1056
|
-
classes: ["NoDecoration", "UnderlineState"]
|
|
1057
|
-
}
|
|
1058
|
-
]
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
};
|
|
1062
|
-
if (__require.main === module) {
|
|
1063
|
-
generateChart();
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
587
|
// src/middleware/core.ts
|
|
1067
588
|
var CANCEL = Symbol("CANCEL");
|
|
1068
589
|
function createMiddleware(machine, hooks, options = {}) {
|
|
@@ -2036,6 +1557,132 @@ function state(context, transitions) {
|
|
|
2036
1557
|
return createFunctionalMachine(context);
|
|
2037
1558
|
}
|
|
2038
1559
|
|
|
1560
|
+
// src/matcher.ts
|
|
1561
|
+
function createMatcher(...cases) {
|
|
1562
|
+
const nameToCase = /* @__PURE__ */ new Map();
|
|
1563
|
+
for (const [name, _, predicate] of cases) {
|
|
1564
|
+
if (nameToCase.has(name)) {
|
|
1565
|
+
throw new Error(`Duplicate matcher case name: "${name}"`);
|
|
1566
|
+
}
|
|
1567
|
+
nameToCase.set(name, { predicate });
|
|
1568
|
+
}
|
|
1569
|
+
const isProxy = new Proxy({}, {
|
|
1570
|
+
get(_target, prop) {
|
|
1571
|
+
return function isGuard(machine) {
|
|
1572
|
+
const caseConfig = nameToCase.get(prop);
|
|
1573
|
+
if (!caseConfig) {
|
|
1574
|
+
const available = Array.from(nameToCase.keys()).join(", ");
|
|
1575
|
+
throw new Error(
|
|
1576
|
+
`Unknown matcher case: "${prop}". Available cases: ${available}`
|
|
1577
|
+
);
|
|
1578
|
+
}
|
|
1579
|
+
return caseConfig.predicate(machine);
|
|
1580
|
+
};
|
|
1581
|
+
}
|
|
1582
|
+
});
|
|
1583
|
+
const caseProxy = new Proxy({}, {
|
|
1584
|
+
get(_target, prop) {
|
|
1585
|
+
return function createCaseHandler(handler) {
|
|
1586
|
+
if (!nameToCase.has(prop)) {
|
|
1587
|
+
const available = Array.from(nameToCase.keys()).join(", ");
|
|
1588
|
+
throw new Error(
|
|
1589
|
+
`Unknown matcher case: "${prop}". Available cases: ${available}`
|
|
1590
|
+
);
|
|
1591
|
+
}
|
|
1592
|
+
return {
|
|
1593
|
+
__brand: "CaseHandler",
|
|
1594
|
+
__name: prop,
|
|
1595
|
+
__machine: void 0,
|
|
1596
|
+
__return: void 0,
|
|
1597
|
+
handler
|
|
1598
|
+
};
|
|
1599
|
+
};
|
|
1600
|
+
}
|
|
1601
|
+
});
|
|
1602
|
+
const exhaustive = { __exhaustive: true };
|
|
1603
|
+
function when2(machine) {
|
|
1604
|
+
return {
|
|
1605
|
+
is(...handlers) {
|
|
1606
|
+
if (handlers.length === 0) {
|
|
1607
|
+
throw new Error("Pattern match requires at least one handler and exhaustiveness marker");
|
|
1608
|
+
}
|
|
1609
|
+
const lastHandler = handlers[handlers.length - 1];
|
|
1610
|
+
if (!lastHandler || typeof lastHandler !== "object" || !("__exhaustive" in lastHandler)) {
|
|
1611
|
+
throw new Error(
|
|
1612
|
+
"Pattern match must end with match.exhaustive for compile-time exhaustiveness checking"
|
|
1613
|
+
);
|
|
1614
|
+
}
|
|
1615
|
+
const actualHandlers = handlers.slice(0, -1);
|
|
1616
|
+
for (const caseHandler of actualHandlers) {
|
|
1617
|
+
const caseName = caseHandler.__name;
|
|
1618
|
+
const caseConfig = nameToCase.get(caseName);
|
|
1619
|
+
if (!caseConfig) {
|
|
1620
|
+
throw new Error(`Internal error: Unknown matcher case in handler: ${caseName}`);
|
|
1621
|
+
}
|
|
1622
|
+
if (caseConfig.predicate(machine)) {
|
|
1623
|
+
return caseHandler.handler(machine);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
const handledCases = actualHandlers.map((h) => h.__name).join(", ");
|
|
1627
|
+
const allCases = Array.from(nameToCase.keys()).join(", ");
|
|
1628
|
+
throw new Error(
|
|
1629
|
+
`Non-exhaustive pattern match at runtime: no handler matched the machine.
|
|
1630
|
+
Handled cases: [${handledCases}]
|
|
1631
|
+
All cases: [${allCases}]
|
|
1632
|
+
This may occur if predicates don't cover all runtime possibilities.`
|
|
1633
|
+
);
|
|
1634
|
+
}
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
function simpleMatcher(machine) {
|
|
1638
|
+
for (const [name, _, predicate] of cases) {
|
|
1639
|
+
if (predicate(machine)) {
|
|
1640
|
+
return name;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
return null;
|
|
1644
|
+
}
|
|
1645
|
+
return Object.assign(simpleMatcher, {
|
|
1646
|
+
is: isProxy,
|
|
1647
|
+
when: when2,
|
|
1648
|
+
case: caseProxy,
|
|
1649
|
+
exhaustive
|
|
1650
|
+
});
|
|
1651
|
+
}
|
|
1652
|
+
function classCase(name, machineClass) {
|
|
1653
|
+
return [
|
|
1654
|
+
name,
|
|
1655
|
+
void 0,
|
|
1656
|
+
// Type-only, not used at runtime
|
|
1657
|
+
(m) => m instanceof machineClass
|
|
1658
|
+
];
|
|
1659
|
+
}
|
|
1660
|
+
function discriminantCase(name, key, value) {
|
|
1661
|
+
return [
|
|
1662
|
+
name,
|
|
1663
|
+
void 0,
|
|
1664
|
+
// Type-only, not used at runtime
|
|
1665
|
+
(m) => hasState(m, key, value)
|
|
1666
|
+
];
|
|
1667
|
+
}
|
|
1668
|
+
function customCase(name, predicate) {
|
|
1669
|
+
return [name, void 0, predicate];
|
|
1670
|
+
}
|
|
1671
|
+
function forContext() {
|
|
1672
|
+
return {
|
|
1673
|
+
/**
|
|
1674
|
+
* Creates a discriminated union case with full type inference.
|
|
1675
|
+
*/
|
|
1676
|
+
case(name, key, value) {
|
|
1677
|
+
return [
|
|
1678
|
+
name,
|
|
1679
|
+
void 0,
|
|
1680
|
+
(m) => hasState(m, key, value)
|
|
1681
|
+
];
|
|
1682
|
+
}
|
|
1683
|
+
};
|
|
1684
|
+
}
|
|
1685
|
+
|
|
2039
1686
|
// src/index.ts
|
|
2040
1687
|
function createMachine(context, fnsOrFactory) {
|
|
2041
1688
|
if (typeof fnsOrFactory === "function") {
|
|
@@ -2114,6 +1761,9 @@ function setContext(machine, newContextOrFn) {
|
|
|
2114
1761
|
const newContext = typeof newContextOrFn === "function" ? newContextOrFn(context) : newContextOrFn;
|
|
2115
1762
|
return createMachine(newContext, transitions);
|
|
2116
1763
|
}
|
|
1764
|
+
function createContext(context) {
|
|
1765
|
+
return { context };
|
|
1766
|
+
}
|
|
2117
1767
|
function overrideTransitions(machine, overrides) {
|
|
2118
1768
|
const { context, ...originalTransitions } = machine;
|
|
2119
1769
|
const newTransitions = { ...originalTransitions, ...overrides };
|
|
@@ -2211,24 +1861,24 @@ function next(m, update) {
|
|
|
2211
1861
|
return createMachine(update(context), transitions);
|
|
2212
1862
|
}
|
|
2213
1863
|
export {
|
|
2214
|
-
ADVANCED_CONFIG_EXAMPLES,
|
|
2215
1864
|
BoundMachine,
|
|
2216
1865
|
CANCEL,
|
|
2217
1866
|
META_KEY,
|
|
2218
1867
|
MachineBase,
|
|
2219
1868
|
MiddlewareBuilder,
|
|
2220
1869
|
MultiMachineBase,
|
|
2221
|
-
_typeToJson,
|
|
2222
1870
|
action,
|
|
2223
1871
|
bindTransitions,
|
|
2224
1872
|
branch,
|
|
2225
1873
|
call,
|
|
2226
1874
|
chain,
|
|
1875
|
+
classCase,
|
|
2227
1876
|
combine,
|
|
2228
1877
|
combineFactories,
|
|
2229
1878
|
compose,
|
|
2230
1879
|
composeTyped,
|
|
2231
1880
|
createAsyncMachine,
|
|
1881
|
+
createContext,
|
|
2232
1882
|
createCustomMiddleware,
|
|
2233
1883
|
createEnsemble,
|
|
2234
1884
|
createEnsembleFactory,
|
|
@@ -2239,6 +1889,7 @@ export {
|
|
|
2239
1889
|
createMachine,
|
|
2240
1890
|
createMachineBuilder,
|
|
2241
1891
|
createMachineFactory,
|
|
1892
|
+
createMatcher,
|
|
2242
1893
|
createMiddleware,
|
|
2243
1894
|
createMiddlewareFactory,
|
|
2244
1895
|
createMiddlewareRegistry,
|
|
@@ -2250,12 +1901,12 @@ export {
|
|
|
2250
1901
|
createTransition,
|
|
2251
1902
|
createTransitionExtender,
|
|
2252
1903
|
createTransitionFactory,
|
|
1904
|
+
customCase,
|
|
2253
1905
|
delegateToChild,
|
|
2254
1906
|
describe,
|
|
1907
|
+
discriminantCase,
|
|
2255
1908
|
extendTransitions,
|
|
2256
|
-
|
|
2257
|
-
extractMachines,
|
|
2258
|
-
generateChart,
|
|
1909
|
+
forContext,
|
|
2259
1910
|
guard,
|
|
2260
1911
|
guardAsync,
|
|
2261
1912
|
guarded,
|