@eagleoutice/flowr 2.17.8 → 2.17.10
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/cli/repl/core.js +4 -0
- package/config.d.ts +11 -1
- package/config.js +45 -8
- package/control-flow/control-flow-graph.js +11 -1
- package/dataflow/environments/default-builtin-config/guards-primitive-lang.js +1 -1
- package/dataflow/environments/default-builtin-config/guards.js +6 -1
- package/dataflow/environments/default-builtin-config/index.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/network-access.d.ts +1 -1
- package/dataflow/environments/default-builtin-config/network-access.js +2 -0
- package/dataflow/environments/environment.d.ts +2 -0
- package/dataflow/environments/environment.js +5 -0
- package/dataflow/environments/frame-memory.d.ts +9 -0
- package/dataflow/environments/frame-memory.js +37 -0
- package/dataflow/environments/group-generics.d.ts +2 -0
- package/dataflow/environments/group-generics.js +7 -0
- package/dataflow/environments/identifier.d.ts +2 -0
- package/dataflow/environments/identifier.js +16 -0
- package/dataflow/environments/pkg-name.d.ts +1 -0
- package/dataflow/environments/query-fn-props.d.ts +4 -0
- package/dataflow/environments/query-fn-props.js +21 -4
- package/dataflow/environments/resolve-by-name.js +5 -3
- package/dataflow/extractor.js +26 -4
- package/dataflow/fn/guards/call-contracts.js +8 -4
- package/dataflow/fn/guards/function-guards.d.ts +13 -4
- package/dataflow/fn/guards/function-guards.js +147 -52
- package/dataflow/fn/guards/guard-term.d.ts +6 -3
- package/dataflow/fn/guards/guard-term.js +12 -6
- package/dataflow/graph/graph.d.ts +2 -0
- package/dataflow/graph/graph.js +9 -5
- package/dataflow/internal/dispatch-linking.d.ts +6 -0
- package/dataflow/internal/dispatch-linking.js +25 -0
- package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +32 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +52 -21
- package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +28 -19
- package/dataflow/internal/process/functions/call/built-in/built-in-with.js +75 -0
- package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +20 -6
- package/dataflow/processor.d.ts +3 -0
- package/documentation/data/faq/faqs.js +4 -0
- package/documentation/wiki-analyzer.js +13 -2
- package/documentation/wiki-interface.js +1 -0
- package/documentation/wiki-linter.js +3 -0
- package/linter/linter-executor.js +4 -1
- package/linter/linter-format.d.ts +5 -5
- package/linter/linter-rules.d.ts +47 -13
- package/linter/linter-rules.js +3 -1
- package/linter/rules/absolute-file-paths.js +1 -1
- package/linter/rules/autoload-files.d.ts +33 -0
- package/linter/rules/autoload-files.js +94 -0
- package/linter/rules/cmd-check/description-metadata.d.ts +2 -2
- package/linter/rules/cmd-check/description-metadata.js +1 -1
- package/linter/rules/cmd-check/documentation-mismatch.d.ts +2 -2
- package/linter/rules/cmd-check/documentation-mismatch.js +1 -1
- package/linter/rules/cmd-check/file-checks.d.ts +6 -11
- package/linter/rules/cmd-check/file-checks.js +8 -3
- package/linter/rules/cmd-check/package-data.d.ts +2 -2
- package/linter/rules/cmd-check/package-data.js +1 -1
- package/linter/rules/cmd-check/package-metafiles.js +1 -1
- package/linter/rules/cmd-check/package-structure.d.ts +2 -2
- package/linter/rules/cmd-check/package-structure.js +1 -1
- package/linter/rules/cmd-check/rd-files.js +1 -1
- package/linter/rules/naming-convention.d.ts +9 -2
- package/linter/rules/process-state-in-session.js +4 -1
- package/linter/rules/software-has-license.d.ts +2 -2
- package/linter/rules/software-has-license.js +1 -2
- package/linter/rules/stop-call.d.ts +8 -3
- package/linter/rules/useless-loop.d.ts +1 -1
- package/package.json +1 -1
- package/project/cache/flowr-analyzer-cache.d.ts +3 -0
- package/project/cache/flowr-analyzer-cache.js +9 -0
- package/project/context/flowr-analyzer-context.d.ts +4 -3
- package/project/context/flowr-analyzer-context.js +18 -6
- package/project/context/flowr-analyzer-dependencies-context.d.ts +9 -0
- package/project/context/flowr-analyzer-dependencies-context.js +35 -6
- package/project/context/flowr-analyzer-environment-context.d.ts +1 -0
- package/project/context/flowr-analyzer-environment-context.js +1 -0
- package/project/context/flowr-analyzer-files-context.d.ts +4 -0
- package/project/context/flowr-analyzer-files-context.js +24 -0
- package/project/context/flowr-analyzer-functions-context.d.ts +1 -0
- package/project/context/flowr-analyzer-functions-context.js +6 -0
- package/project/context/flowr-analyzer-gas-context.d.ts +1 -0
- package/project/context/flowr-analyzer-gas-context.js +1 -0
- package/project/context/flowr-analyzer-incremental-analysis-context.d.ts +24 -0
- package/project/context/flowr-analyzer-incremental-analysis-context.js +54 -0
- package/project/context/flowr-analyzer-loading-order-context.d.ts +1 -0
- package/project/context/flowr-analyzer-loading-order-context.js +14 -0
- package/project/context/flowr-analyzer-meta-context.d.ts +1 -0
- package/project/context/flowr-analyzer-meta-context.js +11 -0
- package/project/context/flowr-file.d.ts +2 -1
- package/project/context/flowr-file.js +5 -2
- package/project/flowr-analyzer.d.ts +22 -3
- package/project/flowr-analyzer.js +36 -0
- package/project/incremental/incremental-dataflow/incremental-dataflow.d.ts +142 -0
- package/project/incremental/incremental-dataflow/incremental-dataflow.js +330 -0
- package/project/plugins/flowr-analyzer-plugin.d.ts +22 -2
- package/project/plugins/flowr-analyzer-plugin.js +16 -1
- package/project/plugins/incremental/incremental-dataflow/flowr-analyzer-incremental-dataflow-heuristic-plugin.d.ts +52 -0
- package/project/plugins/incremental/incremental-dataflow/flowr-analyzer-incremental-dataflow-heuristic-plugin.js +50 -0
- package/project/plugins/incremental/incremental-dataflow/flowr-analyzer-incremental-dataflow-update-type-plugin.d.ts +77 -0
- package/project/plugins/incremental/incremental-dataflow/flowr-analyzer-incremental-dataflow-update-type-plugin.js +151 -0
- package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-plugin.d.ts +2 -0
- package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-plugin.js +4 -0
- package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.d.ts +1 -0
- package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +3 -0
- package/project/plugins/plugin-registry.d.ts +5 -3
- package/project/plugins/plugin-registry.js +7 -2
- package/project/sigdb/decode.js +4 -1
- package/project/sigdb/dict.d.ts +7 -4
- package/project/sigdb/dict.js +20 -6
- package/project/sigdb/guarded-source.js +10 -0
- package/project/sigdb/reader.d.ts +8 -2
- package/project/sigdb/reader.js +44 -30
- package/queries/catalog/input-sources-query/simple-input-classifier.js +84 -10
- package/util/flowr-version.d.ts +1 -1
- package/util/flowr-version.js +2 -2
- package/util/objects.d.ts +14 -0
- package/util/objects.js +100 -0
|
@@ -33,6 +33,8 @@ const r_function_call_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-
|
|
|
33
33
|
const r_argument_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
|
|
34
34
|
const range_1 = require("../../../util/range");
|
|
35
35
|
const type_1 = require("../../../r-bridge/lang-4.x/ast/model/type");
|
|
36
|
+
/** shiny's reactive value, see {@link InputClassifier#classifyReactiveValue} */
|
|
37
|
+
const ReactiveVal = identifier_1.Identifier.from(['reactiveVal', 'shiny']);
|
|
36
38
|
/** how far a function handed to an entry point may be passed along before we give up resolving it */
|
|
37
39
|
const MaxFunctionResolveDepth = 4;
|
|
38
40
|
function isConstantLike(type) {
|
|
@@ -108,9 +110,9 @@ class InputClassifier {
|
|
|
108
110
|
/* a call of a definition in the code does not mean the built-in of the same name */
|
|
109
111
|
return list?.some(id => call.id === id || (identifier_1.Identifier.is(id) && callMeans(call.name, id, this.packages) && !this.callsDefinition(call))) ?? false;
|
|
110
112
|
}
|
|
111
|
-
/** whether the callee is a definition of the code rather than one handed in as an argument */
|
|
113
|
+
/** whether the callee is a definition of the code rather than one handed in as an argument (or one the signature database stands in for, like `built-in:shiny:isolate`) */
|
|
112
114
|
callsDefinition(call) {
|
|
113
|
-
return df_helper_1.Dataflow.calleeIdsOf(this.whole, call.id).some(d => !df_helper_1.Dataflow.hasEdge(this.whole, call.id, d, edge_1.EdgeType.Argument));
|
|
115
|
+
return df_helper_1.Dataflow.calleeIdsOf(this.whole, call.id).some(d => !node_id_1.NodeId.isBuiltIn(d) && !df_helper_1.Dataflow.hasEdge(this.whole, call.id, d, edge_1.EdgeType.Argument));
|
|
114
116
|
}
|
|
115
117
|
/** whether the package the given entry needs is attached (unknown package information lets everything through) */
|
|
116
118
|
hasPackage(name) {
|
|
@@ -139,6 +141,15 @@ class InputClassifier {
|
|
|
139
141
|
}
|
|
140
142
|
return this.classifyAt(ref);
|
|
141
143
|
}
|
|
144
|
+
/** classifies a parameter by what the callers of its function bind to it, see {@link classifyVariableOrigin} */
|
|
145
|
+
classifyBoundParameter(id) {
|
|
146
|
+
if (!this.dfg.hasVertex(id)) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
const acc = new ClassificationAccumulator();
|
|
150
|
+
this.classifyVariableOrigin(id, acc);
|
|
151
|
+
return acc.build(id);
|
|
152
|
+
}
|
|
142
153
|
/** classifies the function a call goes through: for `add2 <- adder(2); add2(5)` the value of `add2`; `undefined` if the call reads no variable */
|
|
143
154
|
classifyHeldFunction(call) {
|
|
144
155
|
const held = (df_helper_1.Dataflow.origin(this.dfg, call.id) ?? []).filter(o => o.type === 0 /* OriginType.ReadVariableOrigin */ || o.type === 1 /* OriginType.WriteVariableOrigin */);
|
|
@@ -631,6 +642,10 @@ class InputClassifier {
|
|
|
631
642
|
if ((o.type !== 0 /* OriginType.ReadVariableOrigin */ && o.type !== 1 /* OriginType.WriteVariableOrigin */) || !this.functionDefinitionsAt(o.id).next().done) {
|
|
632
643
|
continue;
|
|
633
644
|
}
|
|
645
|
+
const cell = this.classifyReactiveValue(o.id);
|
|
646
|
+
if (cell !== undefined) {
|
|
647
|
+
return cell;
|
|
648
|
+
}
|
|
634
649
|
const classified = this.classifyAt(o.id) ?? this.classifyInFullGraph(o.id);
|
|
635
650
|
if (classified?.types.every(isConstantLike)) {
|
|
636
651
|
/* a constant held as a function (`get("readLines")`, a body set to quoted code) says nothing about what calling it yields */
|
|
@@ -642,6 +657,32 @@ class InputClassifier {
|
|
|
642
657
|
}
|
|
643
658
|
return undefined;
|
|
644
659
|
}
|
|
660
|
+
/**
|
|
661
|
+
* A shiny `v <- reactiveVal(init)` holds what it starts with and whatever a call `v(x)` sets later on, so a call
|
|
662
|
+
* `v()` yields any of these; handed on as a value (`helper(v)`), anything may be set elsewhere.
|
|
663
|
+
*/
|
|
664
|
+
classifyReactiveValue(definition) {
|
|
665
|
+
const created = df_helper_1.Dataflow.targetsOf(this.whole, definition, edge_1.EdgeType.DefinedBy).map(t => this.whole.getVertex(t));
|
|
666
|
+
const cell = created.find(c => vertex_1.DfgVertex.isFunctionCall(c) && this.matches(c, [ReactiveVal]));
|
|
667
|
+
if (cell === undefined) {
|
|
668
|
+
return undefined;
|
|
669
|
+
}
|
|
670
|
+
const acc = new ClassificationAccumulator();
|
|
671
|
+
const initial = cell.args.filter(graph_1.FunctionArgument.isNotEmpty).map(graph_1.FunctionArgument.getReference);
|
|
672
|
+
if (initial.length === 0) {
|
|
673
|
+
acc.push(input_types_1.InputType.Constant, null);
|
|
674
|
+
}
|
|
675
|
+
initial.forEach(i => acc.merge(this.classifyAt(i)));
|
|
676
|
+
for (const use of df_helper_1.Dataflow.sourcesOf(this.whole, definition, edge_1.EdgeType.Reads)) {
|
|
677
|
+
const vtx = this.whole.getVertex(use);
|
|
678
|
+
if (!vertex_1.DfgVertex.isFunctionCall(vtx)) {
|
|
679
|
+
acc.merge(undefined);
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
vtx.args.filter(graph_1.FunctionArgument.isNotEmpty).forEach(a => acc.merge(this.classifyAt(graph_1.FunctionArgument.getReference(a))));
|
|
683
|
+
}
|
|
684
|
+
return acc.build(cell.id);
|
|
685
|
+
}
|
|
645
686
|
classifyVariable(vtx) {
|
|
646
687
|
const linked = this.matchWholeLinkedObject(vtx.id);
|
|
647
688
|
if (linked) {
|
|
@@ -649,20 +690,32 @@ class InputClassifier {
|
|
|
649
690
|
}
|
|
650
691
|
const origins = df_helper_1.Dataflow.origin(this.dfg, vtx.id);
|
|
651
692
|
if (origins === undefined || origins.length === 0) {
|
|
652
|
-
|
|
653
|
-
|
|
693
|
+
const onCall = df_helper_1.Dataflow.targetsOf(this.whole, vtx.id, edge_1.EdgeType.DefinedByOnCall);
|
|
694
|
+
if (onCall.length > 0) {
|
|
695
|
+
/* what the definitions active at the call sites hold flows in as well, like the field an R6 `initialize` sets for `self$c` */
|
|
696
|
+
const carried = onCall.flatMap(t => this.classifyAt(t)?.types ?? []).filter(t => !isConstantLike(t) && t !== input_types_1.InputType.Unknown);
|
|
697
|
+
return this.classifyCdsAndReturn(vtx, { ...sourceOf(vtx.id, input_types_1.InputType.Scope), types: (0, arrays_1.uniqueArray)([input_types_1.InputType.Scope, ...carried]) });
|
|
698
|
+
}
|
|
699
|
+
/* a placeholder bound by its caller, like `.x` in `purrr::map(xs, ~ f(.x))`, reads the value it is bound to */
|
|
700
|
+
const bound = this.boundValuesOf(vtx.id);
|
|
701
|
+
if (bound.length > 0) {
|
|
702
|
+
const acc = new ClassificationAccumulator();
|
|
703
|
+
bound.forEach(b => acc.merge(this.classifyAt(b)));
|
|
704
|
+
return this.classifyCdsAndReturn(vtx, acc.build(vtx.id));
|
|
654
705
|
}
|
|
655
706
|
// the definition is not part of the criterion's function, so it has to come from an enclosing scope
|
|
656
707
|
const outer = this.classifyInFullGraph(vtx.id);
|
|
657
708
|
return this.classifyCdsAndReturn(vtx, outer ? { ...outer, id: vtx.id } : sourceOf(vtx.id, input_types_1.InputType.Unknown));
|
|
658
709
|
}
|
|
659
710
|
const acc = new ClassificationAccumulator();
|
|
711
|
+
/* the callers bind the arguments at the position of `..2` to that read itself, not to the `...` it reads */
|
|
712
|
+
const elementRead = this.isDotsElementRead(vtx.id) ? vtx.id : undefined;
|
|
660
713
|
for (const o of origins) {
|
|
661
714
|
if (o.type === 4 /* OriginType.ConstantOrigin */) {
|
|
662
715
|
acc.push(input_types_1.InputType.DerivedConstant, this.extractConstantValue(o.id));
|
|
663
716
|
}
|
|
664
717
|
else if (o.type === 0 /* OriginType.ReadVariableOrigin */ || o.type === 1 /* OriginType.WriteVariableOrigin */) {
|
|
665
|
-
this.classifyVariableOrigin(o.id, acc);
|
|
718
|
+
this.classifyVariableOrigin(o.id, acc, elementRead);
|
|
666
719
|
}
|
|
667
720
|
else if (o.type === 3 /* OriginType.BuiltInFunctionOrigin */ && o.id === vtx.id) {
|
|
668
721
|
/* a built-in used as a value, like `sum` in `identity(sum)`, is a fixed function and so a constant */
|
|
@@ -709,7 +762,7 @@ class InputClassifier {
|
|
|
709
762
|
* Resolves a variable definition or use origin, handling the special cases of
|
|
710
763
|
* scope-escaped variables (DefinedByOnCall) and parameter definitions.
|
|
711
764
|
*/
|
|
712
|
-
classifyVariableOrigin(definitionId, acc) {
|
|
765
|
+
classifyVariableOrigin(definitionId, acc, boundAt = definitionId) {
|
|
713
766
|
const v = this.dfg.getVertex(definitionId);
|
|
714
767
|
if (!v) {
|
|
715
768
|
acc.merge(undefined);
|
|
@@ -717,12 +770,12 @@ class InputClassifier {
|
|
|
717
770
|
}
|
|
718
771
|
// if the referenced definition is linked via defined-by-on-call to another id (e.g., a parameter linked to a
|
|
719
772
|
// caller argument), follow it into the caller; only if that leads nowhere is it an opaque Scope origin
|
|
720
|
-
const onCall = df_helper_1.Dataflow.targetsOf(this.whole,
|
|
773
|
+
const onCall = df_helper_1.Dataflow.targetsOf(this.whole, boundAt, edge_1.EdgeType.DefinedByOnCall);
|
|
721
774
|
if (onCall.length > 0) {
|
|
722
|
-
const callers = onCall.map(t => this.classifyInFullGraph(t)).filter(assert_1.isNotUndefined);
|
|
775
|
+
const callers = onCall.map(t => this.classifyInFullGraph(t) ?? this.classifyAt(t)).filter(assert_1.isNotUndefined);
|
|
723
776
|
const known = callers.filter(c => !c.types.includes(input_types_1.InputType.Unknown));
|
|
724
777
|
known.forEach(c => acc.merge(c));
|
|
725
|
-
if (known.length > 0 && known.length === callers.length && !this.escapes(
|
|
778
|
+
if (known.length > 0 && known.length === callers.length && !this.escapes(boundAt)) {
|
|
726
779
|
return;
|
|
727
780
|
}
|
|
728
781
|
acc.push(input_types_1.InputType.Scope);
|
|
@@ -737,6 +790,23 @@ class InputClassifier {
|
|
|
737
790
|
acc.merge(this.classifyEntry(v));
|
|
738
791
|
}
|
|
739
792
|
}
|
|
793
|
+
/**
|
|
794
|
+
* the values (no definitions) the given use reads directly, leaving out the calls it is part of: what a placeholder
|
|
795
|
+
* like `.x` in a formula called as a function is bound to, or the element `x` of the `with(list(x = ...), ...)`
|
|
796
|
+
* it is evaluated in; a data-masked use (one of a column of data flowR does not know) may read anything else
|
|
797
|
+
*/
|
|
798
|
+
boundValuesOf(id) {
|
|
799
|
+
const idMap = this.whole.idMap;
|
|
800
|
+
const enclosing = new Set(model_1.RNode.iterateParents(idMap.get(id), idMap).map(p => p.info.id));
|
|
801
|
+
const masked = df_helper_1.Dataflow.sourcesOf(this.whole, id, edge_1.EdgeType.NonStandardEvaluation).length > 0;
|
|
802
|
+
return masked ? [] : df_helper_1.Dataflow.targetsOf(this.whole, id, edge_1.EdgeType.Reads).filter(t => !node_id_1.NodeId.isBuiltIn(t) && !enclosing.has(t)
|
|
803
|
+
&& this.whole.hasVertex(t) && !vertex_1.DfgVertex.isVariableDefinition(this.whole.getVertex(t)));
|
|
804
|
+
}
|
|
805
|
+
/** whether the given use reads one element of the dots by its position, like `..2` */
|
|
806
|
+
isDotsElementRead(id) {
|
|
807
|
+
const node = this.whole.idMap?.get(id);
|
|
808
|
+
return r_symbol_1.RSymbol.is(node) && identifier_1.Identifier.isDotDotDotAccess(node.content) && node.content !== identifier_1.Identifier.dotdotdot();
|
|
809
|
+
}
|
|
740
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 */
|
|
741
811
|
escapes(parameter) {
|
|
742
812
|
const idMap = this.whole.idMap;
|
|
@@ -876,8 +946,12 @@ function classifyInput(id, dfg, config, fullDfg, packages, environment) {
|
|
|
876
946
|
return vtx ? [c.classifyEntry(vtx)] : [];
|
|
877
947
|
}
|
|
878
948
|
const ret = [];
|
|
949
|
+
/* a dispatch hands on the parameters of its function (`S7_dispatch()`), so it gets what the callers bind to them */
|
|
950
|
+
const dispatches = vertex_1.DfgVertex.hasOrigin(vtx, built_in_proc_name_1.BuiltInProcName.S7Dispatch) || vertex_1.DfgVertex.hasOrigin(vtx, built_in_proc_name_1.BuiltInProcName.S3Dispatch);
|
|
879
951
|
for (const arg of vtx.args) {
|
|
880
|
-
const
|
|
952
|
+
const ref = graph_1.FunctionArgument.getReference(arg);
|
|
953
|
+
const id = graph_1.FunctionArgument.getId(arg);
|
|
954
|
+
const entry = dispatches && ref === undefined && id !== undefined ? c.classifyBoundParameter(id) : c.classifyArgument(vtx, ref);
|
|
881
955
|
if (entry === undefined) {
|
|
882
956
|
continue;
|
|
883
957
|
}
|
package/util/flowr-version.d.ts
CHANGED
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
* @module
|
|
4
4
|
*/
|
|
5
5
|
import { SemVer } from 'semver';
|
|
6
|
-
export declare const versionDate = "2026-11-
|
|
6
|
+
export declare const versionDate = "2026-11-22T14:31:31Z";
|
|
7
7
|
/** Retrieves the current flowR version as a new {@link SemVer} object. */
|
|
8
8
|
export declare function flowrVersion(): SemVer;
|
package/util/flowr-version.js
CHANGED
|
@@ -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
|
+
const version = '2.17.10';
|
|
12
12
|
// this is automatically replaced with the release date by release-it (scripts/bump-version.ts)
|
|
13
|
-
exports.versionDate = '2026-11-
|
|
13
|
+
exports.versionDate = '2026-11-22T14:31:31Z';
|
|
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);
|
package/util/objects.d.ts
CHANGED
|
@@ -86,4 +86,18 @@ export declare function setOnPath(obj: object, path: string, value: unknown): vo
|
|
|
86
86
|
* ```
|
|
87
87
|
*/
|
|
88
88
|
export declare function enumMembers<T extends object>(enumObject: T): [name: string, value: T[keyof T]][];
|
|
89
|
+
/** How {@link deepCloneSharing} copies the properties of a class instance that are not cloned as they are. */
|
|
90
|
+
export interface ClonedFields {
|
|
91
|
+
/** the properties left out of the clone */
|
|
92
|
+
readonly drop?: ReadonlySet<PropertyKey>;
|
|
93
|
+
/** the properties copied by the given copier, which may hand values on to the clone */
|
|
94
|
+
readonly replace?: ReadonlyMap<PropertyKey, (value: unknown, clone: (v: unknown) => unknown) => unknown>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Deep clones `obj` and keeps the shape of what it reaches: an object reached twice is cloned once, cycles are kept,
|
|
98
|
+
* and class instances (including those of `Map`, `Set`, and `Array` subclasses) keep their prototype.
|
|
99
|
+
* Functions, and every object `keep` says so of, are shared by reference instead of cloned.
|
|
100
|
+
* `fields` may state for a class instance which of its properties are left out or copied in a way of their own, see {@link ClonedFields}.
|
|
101
|
+
*/
|
|
102
|
+
export declare function deepCloneSharing<T>(obj: T, keep: (o: object) => boolean, fields?: (owner: object) => ClonedFields | undefined): T;
|
|
89
103
|
export {};
|
package/util/objects.js
CHANGED
|
@@ -11,6 +11,7 @@ exports.looselyCompareObjects = looselyCompareObjects;
|
|
|
11
11
|
exports.getOnPath = getOnPath;
|
|
12
12
|
exports.setOnPath = setOnPath;
|
|
13
13
|
exports.enumMembers = enumMembers;
|
|
14
|
+
exports.deepCloneSharing = deepCloneSharing;
|
|
14
15
|
const assert_1 = require("./assert");
|
|
15
16
|
const json_1 = require("./json");
|
|
16
17
|
const log_1 = require("./log");
|
|
@@ -259,4 +260,103 @@ function setOnPath(obj, path, value) {
|
|
|
259
260
|
function enumMembers(enumObject) {
|
|
260
261
|
return Object.entries(enumObject).filter(([name]) => Number.isNaN(Number(name)));
|
|
261
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Deep clones `obj` and keeps the shape of what it reaches: an object reached twice is cloned once, cycles are kept,
|
|
265
|
+
* and class instances (including those of `Map`, `Set`, and `Array` subclasses) keep their prototype.
|
|
266
|
+
* Functions, and every object `keep` says so of, are shared by reference instead of cloned.
|
|
267
|
+
* `fields` may state for a class instance which of its properties are left out or copied in a way of their own, see {@link ClonedFields}.
|
|
268
|
+
*/
|
|
269
|
+
function deepCloneSharing(obj, keep, fields) {
|
|
270
|
+
const seen = new Map();
|
|
271
|
+
function clone(value) {
|
|
272
|
+
if (typeof value !== 'object' || value === null) {
|
|
273
|
+
return value;
|
|
274
|
+
}
|
|
275
|
+
const known = seen.get(value);
|
|
276
|
+
if (known !== undefined) {
|
|
277
|
+
return known;
|
|
278
|
+
}
|
|
279
|
+
if (keep(value) || value instanceof RegExp) {
|
|
280
|
+
return value;
|
|
281
|
+
}
|
|
282
|
+
const proto = Object.getPrototypeOf(value);
|
|
283
|
+
if (proto === Array.prototype) {
|
|
284
|
+
const source = value;
|
|
285
|
+
const arr = new Array(source.length);
|
|
286
|
+
seen.set(value, arr);
|
|
287
|
+
for (let i = 0; i < source.length; i++) {
|
|
288
|
+
arr[i] = clone(source[i]);
|
|
289
|
+
}
|
|
290
|
+
return arr;
|
|
291
|
+
}
|
|
292
|
+
else if (proto === Map.prototype) {
|
|
293
|
+
const map = new Map();
|
|
294
|
+
seen.set(value, map);
|
|
295
|
+
for (const [k, v] of value) {
|
|
296
|
+
map.set(clone(k), clone(v));
|
|
297
|
+
}
|
|
298
|
+
return map;
|
|
299
|
+
}
|
|
300
|
+
else if (proto === Set.prototype) {
|
|
301
|
+
const set = new Set();
|
|
302
|
+
seen.set(value, set);
|
|
303
|
+
for (const v of value) {
|
|
304
|
+
set.add(clone(v));
|
|
305
|
+
}
|
|
306
|
+
return set;
|
|
307
|
+
}
|
|
308
|
+
if (proto === Object.prototype) {
|
|
309
|
+
const record = {};
|
|
310
|
+
seen.set(value, record);
|
|
311
|
+
for (const key in value) {
|
|
312
|
+
record[key] = clone(value[key]);
|
|
313
|
+
}
|
|
314
|
+
return record;
|
|
315
|
+
}
|
|
316
|
+
const ctor = (proto?.constructor ?? Object);
|
|
317
|
+
let result;
|
|
318
|
+
if (value instanceof Map) {
|
|
319
|
+
const map = Reflect.construct(Map, [], ctor);
|
|
320
|
+
seen.set(value, map);
|
|
321
|
+
for (const [k, v] of value) {
|
|
322
|
+
map.set(clone(k), clone(v));
|
|
323
|
+
}
|
|
324
|
+
result = map;
|
|
325
|
+
}
|
|
326
|
+
else if (value instanceof Set) {
|
|
327
|
+
const set = Reflect.construct(Set, [], ctor);
|
|
328
|
+
seen.set(value, set);
|
|
329
|
+
for (const v of value) {
|
|
330
|
+
set.add(clone(v));
|
|
331
|
+
}
|
|
332
|
+
result = set;
|
|
333
|
+
}
|
|
334
|
+
else if (Array.isArray(value)) {
|
|
335
|
+
const arr = Reflect.construct(Array, [], ctor);
|
|
336
|
+
seen.set(value, arr);
|
|
337
|
+
for (const v of value) {
|
|
338
|
+
arr.push(clone(v));
|
|
339
|
+
}
|
|
340
|
+
return arr;
|
|
341
|
+
}
|
|
342
|
+
else if (value instanceof Date) {
|
|
343
|
+
return new Date(value.getTime());
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
result = Object.create(proto);
|
|
347
|
+
seen.set(value, result);
|
|
348
|
+
}
|
|
349
|
+
const special = fields?.(value);
|
|
350
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
351
|
+
if (special?.drop?.has(key)) {
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
const copier = special?.replace?.get(key);
|
|
355
|
+
const field = value[key];
|
|
356
|
+
result[key] = copier === undefined ? clone(field) : copier(field, clone);
|
|
357
|
+
}
|
|
358
|
+
return result;
|
|
359
|
+
}
|
|
360
|
+
return clone(obj);
|
|
361
|
+
}
|
|
262
362
|
//# sourceMappingURL=objects.js.map
|