@rotorsoft/act-diagram 0.1.1 → 0.1.3

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/dist/index.js CHANGED
@@ -207,11 +207,40 @@ function buildModel(result, files, expectedSlices) {
207
207
  model.reactions.push(r);
208
208
  }
209
209
  const entryPath = a._sourceFile ?? "app.ts";
210
+ const actSliceNames = new Set(
211
+ (a.slices ?? []).filter((s) => s != null).map((s) => s._varName ?? "").filter(Boolean)
212
+ );
213
+ for (const sl of model.slices) {
214
+ if (sl.error && !actSliceNames.has(sl.name)) {
215
+ const actSrc = files.find((f) => f.path === entryPath)?.content ?? "";
216
+ if (actSrc.includes(sl.name)) {
217
+ actSliceNames.add(sl.name);
218
+ }
219
+ }
220
+ }
221
+ const entrySlices = model.slices.filter((s) => actSliceNames.has(s.name));
222
+ const sliceStateKeys = new Set(entrySlices.flatMap((s) => s.states));
223
+ const actStateKeys = /* @__PURE__ */ new Set();
224
+ for (const s of a.states ?? []) {
225
+ try {
226
+ if (s && s._tag === "State") {
227
+ actStateKeys.add(s._modelKey ?? s.name);
228
+ }
229
+ } catch {
230
+ }
231
+ }
232
+ const allStateKeys = /* @__PURE__ */ new Set([...actStateKeys, ...sliceStateKeys]);
233
+ const actProjNames = new Set(
234
+ (a.projections ?? []).filter((p) => p && p._tag === "Projection").map((p) => p.target)
235
+ );
236
+ for (const sl of entrySlices) {
237
+ for (const pn of sl.projections) actProjNames.add(pn);
238
+ }
210
239
  model.entries.push({
211
240
  path: entryPath,
212
- states: model.states,
213
- slices: model.slices,
214
- projections: model.projections,
241
+ states: model.states.filter((s) => allStateKeys.has(s.varName)),
242
+ slices: entrySlices,
243
+ projections: model.projections.filter((p) => actProjNames.has(p.name)),
215
244
  reactions: a.reactions ?? []
216
245
  });
217
246
  }
@@ -641,6 +670,15 @@ function resolve(imp, fromPath, byKey) {
641
670
  }
642
671
 
643
672
  // src/client/lib/evaluate.ts
673
+ var stripNonCode = (src, level = "full") => {
674
+ const blank = (m) => m.replace(/[^\n]/g, " ");
675
+ let result = src.replace(/\/\/[^\n]*/g, blank).replace(/\/\*[\s\S]*?\*\//g, blank).replace(/`(?:\\[\s\S]|[^`])*`/g, blank);
676
+ if (level === "full") {
677
+ result = result.replace(/"(?:\\[\s\S]|[^"])*"/g, blank).replace(/'(?:\\[\s\S]|[^'])*'/g, blank);
678
+ }
679
+ return result;
680
+ };
681
+ var isSourceFile = (f) => f.path.endsWith(".ts") && !f.path.endsWith(".d.ts") && !f.path.endsWith(".tsx") && !f.path.endsWith(".spec.ts") && !f.path.endsWith(".test.ts") && !f.path.includes("node_modules/") && !f.path.includes("__tests__/") && !f.path.includes("/test/") && !f.path.startsWith("test/");
644
682
  function transpile(code) {
645
683
  try {
646
684
  const safe = code.replace(/\bimport\.meta\.\w+/g, "'__import_meta__'");
@@ -662,7 +700,8 @@ function execute(files) {
662
700
  projections: [],
663
701
  acts: [],
664
702
  error: void 0,
665
- fileErrors: /* @__PURE__ */ new Map()
703
+ fileErrors: /* @__PURE__ */ new Map(),
704
+ expectedSlices: /* @__PURE__ */ new Map()
666
705
  };
667
706
  try {
668
707
  const __built__ = {
@@ -714,11 +753,7 @@ function execute(files) {
714
753
  }
715
754
  return unknownModuleProxy();
716
755
  };
717
- const sorted = topoSort(
718
- files.filter(
719
- (f) => f.path.endsWith(".ts") && !f.path.endsWith(".d.ts") && !f.path.endsWith(".tsx") && !f.path.endsWith(".spec.ts") && !f.path.endsWith(".test.ts") && !f.path.includes("node_modules/") && !f.path.includes("__tests__/") && !f.path.includes("/test/") && !f.path.startsWith("test/")
720
- )
721
- );
756
+ const sorted = topoSort(files.filter(isSourceFile));
722
757
  for (const file of sorted) {
723
758
  _currentFile = file.path;
724
759
  const js = transpile(file.content);
@@ -728,10 +763,19 @@ function execute(files) {
728
763
  const sliceNamesInAct = [];
729
764
  const wsRe = /\.withSlice\(\s*(?:\w+\.)*(\w+)\s*\)/g;
730
765
  let wsm;
731
- while ((wsm = wsRe.exec(js)) !== null) {
766
+ const codeOnly = stripNonCode(js);
767
+ while ((wsm = wsRe.exec(codeOnly)) !== null) {
732
768
  if (!sliceNamesInAct.includes(wsm[1])) sliceNamesInAct.push(wsm[1]);
733
769
  }
734
- const fileRequire = (mod) => resolveModule(mod, key);
770
+ for (const m of codeOnly.matchAll(
771
+ /(?:exports\.)?(\w+)\s*=\s*(?:\w+\.)?slice(?:\s*\(\s*\)|\.call\(void 0,\s*\))/g
772
+ )) {
773
+ result.expectedSlices.set(m[1], file.path);
774
+ }
775
+ const fileRequire = Object.assign(
776
+ (mod) => resolveModule(mod, key),
777
+ { resolve: (mod) => mod, cache: {}, main: void 0 }
778
+ );
735
779
  try {
736
780
  const cleanJs = js.replace(/\bconst\s+__dirname\b/g, "var __dirname").replace(/\bconst\s+__filename\b/g, "var __filename");
737
781
  const fn = new Function(
@@ -777,48 +821,10 @@ function execute(files) {
777
821
  }
778
822
  function extractModel(files) {
779
823
  const result = execute(files);
780
- const expectedSlices = /* @__PURE__ */ new Map();
781
- for (const file of files) {
782
- if (!file.path.endsWith(".ts") || file.path.endsWith(".d.ts") || file.path.endsWith(".spec.ts") || file.path.endsWith(".test.ts") || file.path.includes("node_modules/"))
783
- continue;
784
- for (const m of file.content.matchAll(
785
- /(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*slice\s*\(\s*\)/g
786
- )) {
787
- expectedSlices.set(m[1], file.path);
788
- }
789
- }
790
- return buildModel(result, files, expectedSlices);
824
+ return buildModel(result, files, result.expectedSlices);
791
825
  }
792
826
 
793
827
  // src/client/lib/navigate.ts
794
- function isInsideComment(content, matchIndex) {
795
- const before = content.slice(0, matchIndex);
796
- const lastNl = before.lastIndexOf("\n");
797
- const lineStart = lastNl >= 0 ? lastNl + 1 : 0;
798
- const lineEnd = content.indexOf("\n", matchIndex);
799
- const lineText = content.slice(lineStart, lineEnd >= 0 ? lineEnd : content.length).trimStart();
800
- if (lineText.startsWith("//") || lineText.startsWith("*") || lineText.startsWith("/*")) {
801
- return true;
802
- }
803
- const lastBlockOpen = before.lastIndexOf("/*");
804
- if (lastBlockOpen >= 0) {
805
- const lastBlockClose = before.lastIndexOf("*/");
806
- if (lastBlockClose < lastBlockOpen) return true;
807
- }
808
- return false;
809
- }
810
- function findNonCommentMatch(content, re, startFrom = 0) {
811
- const globalRe = new RegExp(
812
- re.source,
813
- re.flags.includes("g") ? re.flags : re.flags + "g"
814
- );
815
- globalRe.lastIndex = startFrom;
816
- let match;
817
- while ((match = globalRe.exec(content)) !== null) {
818
- if (!isInsideComment(content, match.index)) return match.index;
819
- }
820
- return -1;
821
- }
822
828
  function positionAt(content, index) {
823
829
  const before = content.slice(0, index);
824
830
  const line = before.split("\n").length;
@@ -894,10 +900,19 @@ function buildPatterns(esc, type) {
894
900
  }
895
901
  }
896
902
  function navigateToCode(files, name, type, targetFile) {
903
+ const stripped = /* @__PURE__ */ new Map();
904
+ const code = (f) => {
905
+ let c = stripped.get(f.path);
906
+ if (c === void 0) {
907
+ c = stripNonCode(f.content, "nav");
908
+ stripped.set(f.path, c);
909
+ }
910
+ return c;
911
+ };
897
912
  if (type === "file") {
898
913
  const file = files.find((f) => f.path === name || f.path.endsWith(name));
899
914
  if (file) {
900
- const actMatch = /\bact\s*\(\s*\)/.exec(file.content);
915
+ const actMatch = /\bact\s*\(\s*\)/.exec(code(file));
901
916
  if (actMatch) {
902
917
  const { line, col } = positionAt(file.content, actMatch.index);
903
918
  return { file: file.path, line, col };
@@ -909,31 +924,35 @@ function navigateToCode(files, name, type, targetFile) {
909
924
  if (targetFile) {
910
925
  const file = files.find((f) => f.path === targetFile);
911
926
  if (file) {
927
+ const c = code(file);
912
928
  const esc2 = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
913
929
  if (type === "event") {
914
930
  const emitsBlockRe = new RegExp(
915
931
  `\\.emits\\([\\s\\S]*?(${esc2})\\s*[,:}]`
916
932
  );
917
- const blockMatch = emitsBlockRe.exec(file.content);
933
+ const blockMatch = emitsBlockRe.exec(c);
918
934
  if (blockMatch) {
919
935
  const nameIdx = blockMatch.index + blockMatch[0].lastIndexOf(name);
920
936
  const { line, col } = positionAt(file.content, nameIdx);
921
937
  return { file: file.path, line, col };
922
938
  }
923
939
  const nameRe2 = new RegExp(`\\b${esc2}\\b`, "g");
924
- if (findNonCommentMatch(file.content, nameRe2) >= 0) {
925
- const emitsIdx = findNonCommentMatch(file.content, /\.emits\s*\(/);
926
- if (emitsIdx >= 0) {
927
- const { line, col } = positionAt(file.content, emitsIdx + 1);
940
+ if (nameRe2.exec(c)) {
941
+ const emitsMatch = /\.emits\s*\(/.exec(c);
942
+ if (emitsMatch) {
943
+ const { line, col } = positionAt(
944
+ file.content,
945
+ emitsMatch.index + 1
946
+ );
928
947
  return { file: file.path, line, col };
929
948
  }
930
949
  }
931
950
  }
932
951
  if (type === "guard") {
933
952
  const descRe = new RegExp(`description:\\s*["'\`]${esc2}["'\`]`);
934
- const descMatch = descRe.exec(file.content);
953
+ const descMatch = descRe.exec(c);
935
954
  if (descMatch) {
936
- const before = file.content.slice(0, descMatch.index);
955
+ const before = c.slice(0, descMatch.index);
937
956
  const declRe = /(?:export\s+)?(?:const|let|var)\s+(\w+)\s*(?::\s*Invariant[^=]*)?\s*=/g;
938
957
  let lastDecl = null;
939
958
  let m;
@@ -951,19 +970,20 @@ function navigateToCode(files, name, type, targetFile) {
951
970
  }
952
971
  if (type === "action") {
953
972
  const nameRe2 = new RegExp(`\\b${esc2}\\b`, "g");
954
- const blockStart = file.content.indexOf(".on(");
973
+ const blockStart = c.indexOf(".on(");
955
974
  if (blockStart >= 0) {
956
- const idx2 = findNonCommentMatch(file.content, nameRe2, blockStart);
957
- if (idx2 >= 0) {
958
- const { line, col } = positionAt(file.content, idx2);
975
+ nameRe2.lastIndex = blockStart;
976
+ const match2 = nameRe2.exec(c);
977
+ if (match2) {
978
+ const { line, col } = positionAt(file.content, match2.index);
959
979
  return { file: file.path, line, col };
960
980
  }
961
981
  }
962
982
  }
963
- const nameRe = new RegExp(`\\b${esc2}\\b`, "g");
964
- const idx = findNonCommentMatch(file.content, nameRe);
965
- if (idx >= 0) {
966
- const { line, col } = positionAt(file.content, idx);
983
+ const nameRe = new RegExp(`\\b${esc2}\\b`);
984
+ const match = nameRe.exec(c);
985
+ if (match) {
986
+ const { line, col } = positionAt(file.content, match.index);
967
987
  return { file: file.path, line, col };
968
988
  }
969
989
  }
@@ -978,9 +998,10 @@ function navigateToCode(files, name, type, targetFile) {
978
998
  const descRe = new RegExp(`description:\\s*["'\`]${esc}["'\`]`);
979
999
  for (const f of sortedFiles) {
980
1000
  if (!/\.tsx?$/.test(f.path)) continue;
981
- const descMatch = descRe.exec(f.content);
982
- if (descMatch && !isInsideComment(f.content, descMatch.index)) {
983
- const before = f.content.slice(0, descMatch.index);
1001
+ const c = code(f);
1002
+ const descMatch = descRe.exec(c);
1003
+ if (descMatch) {
1004
+ const before = c.slice(0, descMatch.index);
984
1005
  const declRe = /(?:export\s+)?(?:const|let|var)\s+(\w+)\s*(?::\s*Invariant[^=]*)?\s*=/g;
985
1006
  let lastDecl = null;
986
1007
  let m;
@@ -997,18 +1018,17 @@ function navigateToCode(files, name, type, targetFile) {
997
1018
  }
998
1019
  const patterns = buildPatterns(esc, type);
999
1020
  for (const re of patterns) {
1000
- for (let i = 0; i < sortedFiles.length; i++) {
1001
- if (!/\.tsx?$/.test(sortedFiles[i].path)) continue;
1002
- const content = sortedFiles[i].content;
1021
+ for (const f of sortedFiles) {
1022
+ if (!/\.tsx?$/.test(f.path)) continue;
1023
+ const c = code(f);
1003
1024
  const globalRe = new RegExp(re.source, re.flags + "g");
1004
1025
  let match;
1005
- while ((match = globalRe.exec(content)) !== null) {
1006
- if (isInsideComment(content, match.index)) continue;
1026
+ while ((match = globalRe.exec(c)) !== null) {
1007
1027
  const matchText = match[0];
1008
1028
  const nameOffsetInMatch = matchText.lastIndexOf(name);
1009
1029
  const nameStart = match.index + Math.max(0, nameOffsetInMatch);
1010
- const { line, col } = positionAt(content, nameStart);
1011
- return { file: sortedFiles[i].path, line, col };
1030
+ const { line, col } = positionAt(f.content, nameStart);
1031
+ return { file: f.path, line, col };
1012
1032
  }
1013
1033
  }
1014
1034
  }