@eagleoutice/flowr 2.17.11 → 2.17.12

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.
@@ -30,6 +30,8 @@ const convert_values_1 = require("../../../r-bridge/lang-4.x/convert-values");
30
30
  const model_1 = require("../../../r-bridge/lang-4.x/ast/model/model");
31
31
  const r_function_definition_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
32
32
  const r_function_call_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
33
+ const r_for_loop_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-for-loop");
34
+ const r_access_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-access");
33
35
  const r_argument_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
34
36
  const range_1 = require("../../../util/range");
35
37
  const type_1 = require("../../../r-bridge/lang-4.x/ast/model/type");
@@ -57,6 +59,9 @@ class ClassificationAccumulator {
57
59
  this.push(input_types_1.InputType.Unknown);
58
60
  return;
59
61
  }
62
+ else if (c.types.length === 0) {
63
+ return;
64
+ }
60
65
  this.types.push(...c.types);
61
66
  this.values.push(c.value);
62
67
  this.cds.push(...c.cds ?? []);
@@ -87,6 +92,10 @@ class InputClassifier {
87
92
  packages;
88
93
  environment;
89
94
  cache = new Map();
95
+ /** the vertices being classified with their depth, the lowest depth a cycle returned to, and what was classified within an open cycle */
96
+ open = new Map();
97
+ provisional = new Map();
98
+ lowestOpen = Infinity;
90
99
  fullClassifier;
91
100
  unsanitized;
92
101
  declarationIndex;
@@ -306,23 +315,36 @@ class InputClassifier {
306
315
  if (cached) {
307
316
  return cached;
308
317
  }
309
- /* a temporary unknown breaks cycles */
310
- this.cache.set(vertex.id, sourceOf(vertex.id, input_types_1.InputType.Unknown));
311
- switch (vertex.tag) {
312
- case vertex_1.VertexType.Value: {
313
- const value = this.extractConstantValue(vertex.id);
314
- return this.classifyCdsAndReturn(vertex, (0, objects_1.compactRecord)({ ...sourceOf(vertex.id, input_types_1.InputType.Constant), value }));
318
+ /* a cycle (`i <- i + 1` in a loop) brings in nothing the other definitions do not already */
319
+ const open = this.open.get(vertex.id);
320
+ const provisional = this.provisional.get(vertex.id);
321
+ if (open !== undefined || provisional !== undefined) {
322
+ this.lowestOpen = Math.min(this.lowestOpen, open ?? provisional?.[1]);
323
+ return provisional?.[0] ?? { id: vertex.id, types: [], trace: input_types_1.InputTraceType.Pure };
324
+ }
325
+ const [depth, outer] = [this.open.size, this.lowestOpen];
326
+ this.open.set(vertex.id, depth);
327
+ this.lowestOpen = Infinity;
328
+ /* inline, as long chains of definitions come close to the stack limit; a function definition is a constant, whatever it computes when called */
329
+ const result = vertex_1.DfgVertex.isFunctionCall(vertex) ? this.classifyFunctionCall(vertex)
330
+ : vertex_1.DfgVertex.isVariableDefinition(vertex) ? this.classifyVariableDefinition(vertex)
331
+ : vertex_1.DfgVertex.isUse(vertex) ? this.classifyVariable(vertex)
332
+ : this.classifyCdsAndReturn(vertex, (0, objects_1.compactRecord)({ ...sourceOf(vertex.id, input_types_1.InputType.Constant), value: vertex_1.DfgVertex.isValue(vertex) ? this.extractConstantValue(vertex.id) : undefined }));
333
+ this.open.delete(vertex.id);
334
+ if (this.lowestOpen < depth) {
335
+ /* it lacks what the open vertex of its cycle brings in, which is all it lacks until that one is done */
336
+ this.provisional.set(vertex.id, [result, this.lowestOpen]);
337
+ }
338
+ else {
339
+ if (result.types.length === 0) {
340
+ result.types.push(input_types_1.InputType.Unknown);
315
341
  }
316
- case vertex_1.VertexType.FunctionCall:
317
- return this.classifyFunctionCall(vertex);
318
- case vertex_1.VertexType.VariableDefinition:
319
- return this.classifyVariableDefinition(vertex);
320
- case vertex_1.VertexType.Use:
321
- return this.classifyVariable(vertex);
322
- /* a function definition is a constant: its code is fixed, whatever it computes when called */
323
- case vertex_1.VertexType.FunctionDefinition:
324
- return this.classifyCdsAndReturn(vertex, sourceOf(vertex.id, input_types_1.InputType.Constant));
342
+ this.cache.set(vertex.id, result);
343
+ this.provisional.forEach(([, low], id) => low >= depth && this.provisional.delete(id));
344
+ this.lowestOpen = Infinity;
325
345
  }
346
+ this.lowestOpen = Math.min(outer, this.lowestOpen);
347
+ return result;
326
348
  }
327
349
  /**
328
350
  * Whether `call` quotes only the code written in its arguments: no name in them resolves outside of them
@@ -380,16 +402,15 @@ class InputClassifier {
380
402
  declaredAt: field !== undefined && linked.declaredBy ? this.declarations().get(declarationKey(linked.name, field)) : undefined
381
403
  });
382
404
  }
405
+ /** the field `input$n` names, or the one `input[[n]]` reads by the value of `n` */
383
406
  accessedField(call) {
384
407
  const ref = call.args[1] === undefined ? undefined : graph_1.FunctionArgument.getReference(call.args[1]);
385
408
  const node = ref === undefined ? undefined : this.dfg.idMap?.get(ref);
386
- if (r_string_1.RString.is(node)) {
387
- return node.content.str;
388
- }
389
- else if (r_symbol_1.RSymbol.is(node)) {
409
+ if (r_symbol_1.RSymbol.is(node) && r_access_1.RAccess.isNamed(this.dfg.idMap?.get(call.id))) {
390
410
  return identifier_1.Identifier.getName(node.content);
391
411
  }
392
- return undefined;
412
+ const value = r_string_1.RString.is(node) ? node.content.str : this.classifyAt(ref)?.value;
413
+ return typeof value === 'string' ? value : undefined;
393
414
  }
394
415
  /** the argument a built-in hands on: the value of a block or `get` (then `renamed`), the condition of a loop */
395
416
  passedOn(call) {
@@ -450,7 +471,7 @@ class InputClassifier {
450
471
  templateArguments(call) {
451
472
  return (this.config.templates ?? []).filter(t => this.matches(call, [t.call])).flatMap(t => t.argName === '...'
452
473
  ? call.args.filter(a => !graph_1.FunctionArgument.isNamed(a)).map(graph_1.FunctionArgument.getReference)
453
- : [this.argumentReference(call, t.argIdx, t.argName)]);
474
+ : [this.argumentReference(call, t.argIdx, t.argName)]).filter(assert_1.isNotUndefined);
454
475
  }
455
476
  /** the types and control dependencies of what `ref` yields, `unknown` when it cannot be classified */
456
477
  dependencies(ref, unknown = [input_types_1.InputType.Unknown]) {
@@ -494,7 +515,7 @@ class InputClassifier {
494
515
  if (!this.matches(call, [narrow.call])) {
495
516
  continue;
496
517
  }
497
- const bound = this.argumentReference(call, narrow.argIdx, narrow.argName);
518
+ const bound = this.argumentReference(call, narrow.argIdx, narrow.argName) ?? this.formalDefault(call);
498
519
  const narrowed = this.classifyAt(bound);
499
520
  if (narrowed) {
500
521
  /* which of the values it is depends on the other arguments */
@@ -511,6 +532,10 @@ class InputClassifier {
511
532
  types.splice(types.indexOf(input_types_1.InputType.File), 1);
512
533
  types.push(input_types_1.InputType.TempFile);
513
534
  }
535
+ else if (readsFile && call.args.some(arg => this.isStdin(graph_1.FunctionArgument.getReference(arg)))) {
536
+ /* what is read from the standard input is what the user types */
537
+ types.splice(types.indexOf(input_types_1.InputType.File), 1, ...types.includes(input_types_1.InputType.User) ? [] : [input_types_1.InputType.User]);
538
+ }
514
539
  /* `quote` yields written code, only capturing (`substitute(x)`) or splicing (`bquote(.(y))`) brings in other code */
515
540
  if (types.includes(input_types_1.InputType.Lang) && this.quotesOnlyWrittenCode(call)) {
516
541
  types.splice(types.indexOf(input_types_1.InputType.Lang), 1, input_types_1.InputType.DerivedConstant);
@@ -526,8 +551,9 @@ class InputClassifier {
526
551
  acc.merge(this.classifyAt(block));
527
552
  /* and whether they answer depends on everything the block does */
528
553
  const statements = block === undefined ? undefined : this.whole.getVertex(block);
529
- for (const arg of vertex_1.DfgVertex.isFunctionCall(statements) && vertex_1.DfgVertex.hasOrigin(statements, built_in_proc_name_1.BuiltInProcName.ExpressionList) ? statements.args : []) {
530
- acc.cds.push(...this.dependencies(graph_1.FunctionArgument.getReference(arg)));
554
+ const isBlock = vertex_1.DfgVertex.isFunctionCall(statements) && vertex_1.DfgVertex.hasOrigin(statements, built_in_proc_name_1.BuiltInProcName.ExpressionList);
555
+ for (const ref of isBlock ? statements.args.map(graph_1.FunctionArgument.getReference) : [block]) {
556
+ acc.cds.push(...this.dependencies(ref));
531
557
  }
532
558
  return this.classifyCdsAndReturn(call, acc.build(call.id));
533
559
  }
@@ -563,6 +589,19 @@ class InputClassifier {
563
589
  cds: cdTypes.length > 0 ? (0, arrays_1.uniqueArray)(cdTypes) : undefined
564
590
  }));
565
591
  }
592
+ /** Whether the value at `ref` is the standard input: `stdin()`, or `file("stdin")` and the path `"stdin"`, which R opens as it. */
593
+ isStdin(ref) {
594
+ const sources = ref === undefined ? undefined : df_helper_1.Dataflow.valueSources(ref, this.whole);
595
+ return sources !== undefined && sources.length > 0 && sources.every(at => {
596
+ const node = this.whole.idMap?.get(at);
597
+ const vertex = this.whole.getVertex(at);
598
+ if (r_string_1.RString.is(node)) {
599
+ return node.content.str === 'stdin';
600
+ }
601
+ return vertex_1.DfgVertex.isFunctionCall(vertex) && (this.matches(vertex, StdinConnection)
602
+ || (this.matches(vertex, FileConnection) && this.isStdin(this.argumentReference(vertex, 0, 'description'))));
603
+ });
604
+ }
566
605
  /**
567
606
  * What a call of a function the analyzed code defines yields: what its exit points do, as a function whose
568
607
  * body is `system(x)` runs a system command whether the call reads `f(cmd)` or `system(cmd)`. `undefined`
@@ -648,8 +687,9 @@ class InputClassifier {
648
687
  }
649
688
  const classified = this.classifyAt(o.id) ?? this.classifyInFullGraph(o.id);
650
689
  if (classified?.types.every(isConstantLike)) {
651
- /* a constant held as a function (`get("readLines")`, a body set to quoted code) says nothing about what calling it yields */
652
- return sourceOf(call.id, input_types_1.InputType.Unknown);
690
+ /* a built-in held as a function (`rl <- readline; rl()`) is that built-in, other constants (a body set to quoted code) say nothing about what calling it yields */
691
+ const held = this.heldBuiltIn(call);
692
+ return held === undefined ? sourceOf(call.id, input_types_1.InputType.Unknown) : this.classifyFunctionCall({ ...call, name: held });
653
693
  }
654
694
  else if (classified !== undefined && !classified.types.includes(input_types_1.InputType.Unknown)) {
655
695
  return classified;
@@ -657,6 +697,23 @@ class InputClassifier {
657
697
  }
658
698
  return undefined;
659
699
  }
700
+ /** without its choices, `match.arg(type)` takes the default of the parameter `type` of the function calling it */
701
+ formalDefault(call) {
702
+ const idMap = this.whole.idMap;
703
+ const arg = idMap.get(this.argumentReference(call, 0, 'arg'));
704
+ const fn = r_symbol_1.RSymbol.is(arg) ? model_1.RNode.iterateParents(arg, idMap).find(r_function_definition_1.RFunctionDefinition.is) : undefined;
705
+ return fn?.parameters.find(p => p.name.content === arg?.content)?.defaultValue?.info.id;
706
+ }
707
+ /** the one built-in the call runs, if it is called by another name */
708
+ heldBuiltIn(call) {
709
+ const callees = df_helper_1.Dataflow.targetsOf(this.whole, call.id, edge_1.EdgeType.Calls);
710
+ if (callees.length !== 1 || !node_id_1.NodeId.isBuiltIn(callees[0])) {
711
+ return undefined;
712
+ }
713
+ const [pkg, fn] = node_id_1.NodeId.toPkgFn(callees[0]) ?? [undefined, node_id_1.NodeId.fromBuiltIn(callees[0])];
714
+ const held = identifier_1.Identifier.make(fn, pkg);
715
+ return identifier_1.Identifier.getName(held) === identifier_1.Identifier.getName(call.name) ? undefined : held;
716
+ }
660
717
  /**
661
718
  * A shiny `v <- reactiveVal(init)` holds what it starts with and whatever a call `v(x)` sets later on, so a call
662
719
  * `v()` yields any of these; handed on as a value (`helper(v)`), anything may be set elsewhere.
@@ -807,13 +864,23 @@ class InputClassifier {
807
864
  const node = this.whole.idMap?.get(id);
808
865
  return r_symbol_1.RSymbol.is(node) && identifier_1.Identifier.isDotDotDotAccess(node.content) && node.content !== identifier_1.Identifier.dotdotdot();
809
866
  }
810
- /** whether the function binding the parameter is used as a value, so it may be called with anything the visible callers do not pass */
867
+ /**
868
+ * whether the function binding the parameter is used as a value, so it may be called with anything the visible callers do not pass;
869
+ * handed to an apply (`lapply(x, f)`), it is called there
870
+ */
811
871
  escapes(parameter) {
812
872
  const idMap = this.whole.idMap;
813
873
  const fn = idMap === undefined ? undefined : model_1.RNode.iterateParents(idMap.get(parameter), idMap).find(r_function_definition_1.RFunctionDefinition.is);
814
- const holders = fn === undefined ? [] : df_helper_1.Dataflow.sourcesOf(this.whole, fn.info.id, edge_1.EdgeType.DefinedBy);
815
- return holders.length === 0 || holders.some(h => df_helper_1.Dataflow.sourcesOf(this.whole, h, edge_1.EdgeType.Reads)
816
- .some(r => !r_function_call_1.RFunctionCall.is(idMap?.get(r)) || !df_helper_1.Dataflow.hasEdge(this.whole, r, fn?.info.id, edge_1.EdgeType.Calls)));
874
+ if (fn === undefined) {
875
+ return true;
876
+ }
877
+ const holders = df_helper_1.Dataflow.sourcesOf(this.whole, fn.info.id, edge_1.EdgeType.DefinedBy);
878
+ const uses = (holders.length === 0 ? [fn.info.id] : holders).flatMap(h => df_helper_1.Dataflow.sourcesOf(this.whole, h, edge_1.EdgeType.Reads));
879
+ return (holders.length === 0 && uses.length === 0) || uses.some(r => !df_helper_1.Dataflow.hasEdge(this.whole, r, fn.info.id, edge_1.EdgeType.Calls) || !(r_function_call_1.RFunctionCall.is(idMap?.get(r)) || this.calledByApply(r)));
880
+ }
881
+ /** whether an apply calls what `id` holds: `lapply` itself, or the call it makes of an argument it is handed */
882
+ calledByApply(id) {
883
+ return [id, ...df_helper_1.Dataflow.sourcesOf(this.whole, id, edge_1.EdgeType.Argument)].some(c => vertex_1.DfgVertex.hasOrigin(this.whole.getVertex(c), built_in_proc_name_1.BuiltInProcName.Apply));
817
884
  }
818
885
  /** whether code flowR cannot follow (`load`, `eval`, `e$x <-`) may rebind the variable before the use */
819
886
  mayBeRebound(use, definitions) {
@@ -822,11 +889,19 @@ class InputClassifier {
822
889
  const fn = model_1.RNode.iterateParents(node, idMap).find(r_function_definition_1.RFunctionDefinition.is);
823
890
  const within = (id) => fn !== undefined && model_1.RNode.iterateParents(idMap.get(id), idMap).some(p => p === fn);
824
891
  const local = definitions.length > 0 && definitions.every(within);
825
- /* an unconditional definition after the effect binds anew */
826
- const overwritten = (e) => definitions.length > 0 && definitions.every(d => !this.whole.getVertex(d)?.cds?.length
827
- && runsAfter(idMap.get(idMap.get(d)?.info.parent), idMap.get(e), idMap));
892
+ /* where a definition binds: once its value is computed, for the variable of a for loop before its body runs */
893
+ const binds = (d) => {
894
+ const parent = idMap.get(idMap.get(d)?.info.parent);
895
+ return r_for_loop_1.RForLoop.is(parent) ? parent.variable : parent;
896
+ };
897
+ /* an unconditional definition after the effect binds anew, whatever a loop redefines later on */
898
+ const overwritten = (e) => definitions.some(d => !this.whole.getVertex(d)?.cds?.length && runsAfter(binds(d), idMap.get(e), idMap));
899
+ /* a for loop binds its variable anew before each run of its body, so what the body does after the use cannot change it */
900
+ const loop = idMap.get(idMap.get(definitions[0])?.info.parent);
901
+ const body = r_for_loop_1.RForLoop.is(loop) && definitions.every(d => d === loop.variable.info.id) ? loop.body : undefined;
828
902
  return this.rebinders().some(([e, fields]) => (fields === undefined || fields.some(f => f === undefined || f === model_1.RNode.lexeme(node)))
829
- && (!local || within(e)) && !overwritten(e) && !((fn === undefined || local) && runsAfter(idMap.get(e), node, idMap)));
903
+ && (!local || within(e)) && !overwritten(e) && !((fn === undefined || local) && runsAfter(idMap.get(e), node, idMap))
904
+ && !(body !== undefined && runsAfter(idMap.get(e), node, idMap, body)));
830
905
  }
831
906
  /** the unknown side effects, and the replacements `e$x <- v` with the names they may bind (`undefined` for any) */
832
907
  rebinders() {
@@ -865,7 +940,6 @@ class InputClassifier {
865
940
  if (src.cds?.length === 0) {
866
941
  delete src.cds;
867
942
  }
868
- this.cache.set(vtx.id, src);
869
943
  return src;
870
944
  }
871
945
  }
@@ -901,14 +975,32 @@ const PlumberEndpoint = /^#\*\s*@(?:get|post|put|delete|patch|head|options|any|u
901
975
  /** the replacements that may write a binding of an environment, as `e$x <- v` does */
902
976
  const FieldReplacements = new Set(['$<-', '[[<-']);
903
977
  const LoopTypes = new Set([type_1.RType.ForLoop, type_1.RType.WhileLoop, type_1.RType.RepeatLoop]);
904
- /** whether `later` surely runs after `node`: later in the same file, in no shared loop, not deferred into a function apart from it */
905
- function runsAfter(later, node, idMap) {
978
+ /**
979
+ * whether `later` surely runs after `node`: later in the same file, in no shared loop, not deferred into a function apart from it;
980
+ * with a `scope`, both have to be within it and only one run of it counts (one run of a loop body)
981
+ */
982
+ function runsAfter(later, node, idMap, scope) {
906
983
  const [first, second] = [model_1.RNode.span(node), model_1.RNode.span(later)];
907
984
  if (first === undefined || second === undefined || node?.info.file !== later?.info.file || !(range_1.SourceRange.startsCompletelyBefore(first, second) || range_1.SourceRange.isSubsetOf(first, second))) {
908
985
  return false;
909
986
  }
910
987
  const around = new Set(model_1.RNode.iterateParents(later, idMap));
911
- return model_1.RNode.iterateParents(node, idMap).every(p => r_function_definition_1.RFunctionDefinition.is(p) ? around.has(p) : !LoopTypes.has(p.type) || !around.has(p));
988
+ if (scope !== undefined && !around.has(scope)) {
989
+ return false;
990
+ }
991
+ let child = node;
992
+ for (const p of model_1.RNode.iterateParents(node, idMap)) {
993
+ /* the vector of a for loop is evaluated once, before its body runs */
994
+ const once = r_for_loop_1.RForLoop.is(p) && child === p.vector;
995
+ if (p === scope) {
996
+ return true;
997
+ }
998
+ else if (r_function_definition_1.RFunctionDefinition.is(p) ? !around.has(p) : LoopTypes.has(p.type) && around.has(p) && !once) {
999
+ return false;
1000
+ }
1001
+ child = p;
1002
+ }
1003
+ return scope === undefined;
912
1004
  }
913
1005
  function declarationKey(object, field) {
914
1006
  return `${object}\u0000${field}`;
@@ -917,6 +1009,12 @@ function declarationKey(object, field) {
917
1009
  function fieldIsInput(obj, field) {
918
1010
  return obj.fields === undefined || (field !== undefined && obj.fields.includes(field));
919
1011
  }
1012
+ /**
1013
+ * This is either an {@link NodeId|id} of a known functions all of that category (e.g., you can issue a dependencies query before and then pass all
1014
+ * identified ids to this query here).
1015
+ */
1016
+ const StdinConnection = [identifier_1.Identifier.make('stdin')];
1017
+ const FileConnection = [identifier_1.Identifier.make('file')];
920
1018
  /**
921
1019
  * Whether a call by the name `called` means `id`. A `pkg::fn` call has to match exactly, while a bare call only
922
1020
  * means a namespaced entry if that package is attached - just like in R, where the search path decides.
@@ -3,6 +3,6 @@
3
3
  * @module
4
4
  */
5
5
  import { SemVer } from 'semver';
6
- export declare const versionDate = "2026-11-22T17:14:43Z";
6
+ export declare const versionDate = "2026-11-24T12:32:21Z";
7
7
  /** Retrieves the current flowR version as a new {@link SemVer} object. */
8
8
  export declare function flowrVersion(): SemVer;
@@ -8,9 +8,9 @@ exports.flowrVersion = flowrVersion;
8
8
  */
9
9
  const semver_1 = require("semver");
10
10
  // this is automatically replaced with the current version by release-it
11
- const version = '2.17.11';
11
+ const version = '2.17.12';
12
12
  // this is automatically replaced with the release date by release-it (scripts/bump-version.ts)
13
- exports.versionDate = '2026-11-22T17:14:43Z';
13
+ exports.versionDate = '2026-11-24T12:32:21Z';
14
14
  /** Retrieves the current flowR version as a new {@link SemVer} object. */
15
15
  function flowrVersion() {
16
16
  return new semver_1.SemVer(version);
@@ -74,7 +74,7 @@ exports.Mermaid = {
74
74
  * @param edit - if true, the url will point to the editor, otherwise it will point to the viewer
75
75
  */
76
76
  codeToUrl(code, edit = false) {
77
- const obj = { code, mermaid: { autoSync: true } };
77
+ const obj = { code, mermaid: JSON.stringify({ autoSync: true }) };
78
78
  /* `btoa` rather than a `Buffer`, so the pages flowR ships can build this url as well */
79
79
  return `https://mermaid.live/${edit ? 'edit' : 'view'}#base64:${(0, url_encoding_1.toBase64)(new TextEncoder().encode(JSON.stringify(obj)))}`;
80
80
  }