@eagleoutice/flowr 2.13.2 → 2.13.4

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.
Files changed (146) hide show
  1. package/README.md +45 -43
  2. package/abstract-interpretation/absint-visitor.js +2 -2
  3. package/abstract-interpretation/data-frame/mappers/access-mapper.js +1 -0
  4. package/abstract-interpretation/data-frame/mappers/arguments.js +2 -2
  5. package/abstract-interpretation/data-frame/mappers/function-mapper.d.ts +1 -1
  6. package/abstract-interpretation/data-frame/mappers/function-mapper.js +3 -0
  7. package/abstract-interpretation/data-frame/mappers/replacement-mapper.js +1 -1
  8. package/abstract-interpretation/domains/state-domain-like.d.ts +1 -1
  9. package/abstract-interpretation/unsupported-functions.js +1 -1
  10. package/config.d.ts +8 -0
  11. package/config.js +9 -1
  12. package/control-flow/cfg-dead-code.js +72 -11
  13. package/control-flow/control-flow-graph.js +2 -1
  14. package/control-flow/extract-cfg.js +85 -8
  15. package/control-flow/semantic-cfg-guided-visitor.d.ts +1 -1
  16. package/control-flow/semantic-cfg-guided-visitor.js +2 -0
  17. package/core/steps/all/core/11-normalize-tree-sitter.d.ts +1 -1
  18. package/dataflow/environments/append.d.ts +1 -1
  19. package/dataflow/environments/built-in-proc-name.d.ts +4 -0
  20. package/dataflow/environments/built-in-proc-name.js +4 -0
  21. package/dataflow/environments/built-in.d.ts +5 -1
  22. package/dataflow/environments/built-in.js +5 -1
  23. package/dataflow/environments/clone.d.ts +1 -1
  24. package/dataflow/environments/default-builtin-config.d.ts +59 -4
  25. package/dataflow/environments/default-builtin-config.js +23 -11
  26. package/dataflow/environments/environment.d.ts +28 -4
  27. package/dataflow/environments/environment.js +66 -11
  28. package/dataflow/environments/identifier.d.ts +1 -0
  29. package/dataflow/environments/overwrite.d.ts +1 -1
  30. package/dataflow/eval/resolve/alias-tracking.d.ts +3 -1
  31. package/dataflow/eval/resolve/alias-tracking.js +8 -1
  32. package/dataflow/eval/resolve/resolve.d.ts +14 -0
  33. package/dataflow/eval/resolve/resolve.js +47 -0
  34. package/dataflow/fn/higher-order-function.js +3 -3
  35. package/dataflow/fn/recursive-function.js +1 -1
  36. package/dataflow/graph/dataflowgraph-builder.d.ts +1 -1
  37. package/dataflow/graph/df-helper.js +1 -1
  38. package/dataflow/graph/vertex.d.ts +32 -25
  39. package/dataflow/graph/vertex.js +40 -36
  40. package/dataflow/internal/process/functions/call/built-in/built-in-access.d.ts +1 -0
  41. package/dataflow/internal/process/functions/call/built-in/built-in-access.js +7 -4
  42. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +14 -0
  43. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +28 -29
  44. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +1 -1
  45. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +41 -48
  46. package/dataflow/internal/process/functions/call/built-in/built-in-attach.js +1 -1
  47. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.d.ts +14 -0
  48. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.js +45 -0
  49. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.d.ts +14 -1
  50. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.js +80 -0
  51. package/dataflow/internal/process/functions/call/built-in/built-in-eval.d.ts +1 -1
  52. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +63 -3
  53. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.d.ts +1 -1
  54. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +63 -16
  55. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +29 -9
  56. package/dataflow/internal/process/functions/call/built-in/built-in-library.d.ts +5 -2
  57. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +15 -9
  58. package/dataflow/internal/process/functions/call/built-in/built-in-list.d.ts +4 -0
  59. package/dataflow/internal/process/functions/call/built-in/built-in-list.js +35 -0
  60. package/dataflow/internal/process/functions/call/built-in/built-in-new-env.js +2 -2
  61. package/dataflow/internal/process/functions/call/built-in/built-in-quote.d.ts +1 -1
  62. package/dataflow/internal/process/functions/call/built-in/built-in-recall.js +1 -1
  63. package/dataflow/internal/process/functions/call/built-in/built-in-register-hook.d.ts +1 -1
  64. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-dispatch.d.ts +1 -1
  65. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.d.ts +9 -1
  66. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.js +55 -2
  67. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.d.ts +1 -1
  68. package/dataflow/internal/process/functions/call/built-in/built-in-stack-env.js +5 -3
  69. package/dataflow/internal/process/functions/call/built-in/built-in-vector.d.ts +1 -1
  70. package/dataflow/internal/process/functions/call/unnamed-call-handling.js +24 -0
  71. package/dataflow/internal/process/functions/process-parameter.js +2 -1
  72. package/documentation/doc-readme.js +14 -12
  73. package/documentation/doc-util/doc-normalized-ast.d.ts +1 -1
  74. package/documentation/wiki-dataflow-graph.js +4 -24
  75. package/documentation/wiki-query.js +47 -0
  76. package/documentation/wiki-signature-database.js +3 -2
  77. package/linter/rules/absolute-path.js +1 -1
  78. package/linter/rules/dataframe-access-validation.d.ts +1 -1
  79. package/linter/rules/function-finder-util.js +1 -1
  80. package/linter/rules/no-leaked-credentials.js +1 -1
  81. package/linter/rules/undefined-symbol-util.js +3 -1
  82. package/linter/rules/undefined-symbol.js +4 -5
  83. package/linter/rules/unused-definition.js +15 -10
  84. package/linter/rules/useless-loop.js +1 -1
  85. package/package.json +21 -9
  86. package/project/context/flowr-analyzer-context.d.ts +4 -4
  87. package/project/context/flowr-analyzer-context.js +3 -3
  88. package/project/context/flowr-analyzer-dependencies-context.d.ts +11 -5
  89. package/project/context/flowr-analyzer-dependencies-context.js +8 -3
  90. package/project/context/flowr-analyzer-meta-context.d.ts +1 -0
  91. package/project/dependency-version-space.d.ts +118 -26
  92. package/project/dependency-version-space.js +343 -87
  93. package/project/plugins/file-plugins/files/flowr-description-file.d.ts +4 -0
  94. package/project/plugins/file-plugins/files/flowr-description-file.js +8 -1
  95. package/project/plugins/file-plugins/files/flowr-rda-file.js +27 -25
  96. package/project/plugins/file-plugins/notebooks/flowr-analyzer-rmd-file-plugin.d.ts +1 -1
  97. package/project/plugins/file-plugins/notebooks/flowr-analyzer-sweave-file-plugin.d.ts +1 -1
  98. package/project/plugins/package-version-plugins/flowr-analyzer-meta-description-file-plugin.js +2 -1
  99. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.d.ts +1 -1
  100. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +4 -4
  101. package/project/plugins/package-version-plugins/package.d.ts +27 -10
  102. package/project/plugins/package-version-plugins/package.js +50 -25
  103. package/project/sigdb/reader.d.ts +26 -16
  104. package/project/sigdb/reader.js +52 -21
  105. package/queries/catalog/call-context-query/identify-link-to-last-call-relation.js +1 -1
  106. package/queries/catalog/call-context-query/identify-link-to-nested-call-relation.js +1 -1
  107. package/queries/catalog/dependencies-query/dependencies-query-executor.js +1 -1
  108. package/queries/catalog/dependencies-query/dependencies-query-format.d.ts +2 -2
  109. package/queries/catalog/dependencies-query/dependencies-query-format.js +2 -2
  110. package/queries/catalog/dependencies-query/function-info/read-functions.js +1 -1
  111. package/queries/catalog/dependencies-query/function-info/write-functions.js +23 -24
  112. package/queries/catalog/does-call-query/does-call-query-format.d.ts +1 -1
  113. package/queries/catalog/guess-dep-versions-query/guess-dep-versions-query-executor.js +39 -77
  114. package/queries/catalog/guess-dep-versions-query/guess-dep-versions-query-format.d.ts +12 -0
  115. package/queries/catalog/guess-dep-versions-query/guess-dep-versions-query-format.js +53 -23
  116. package/queries/catalog/input-sources-query/input-source-functions.d.ts +14 -0
  117. package/queries/catalog/input-sources-query/input-source-functions.js +109 -20
  118. package/queries/catalog/input-sources-query/input-sources-query-executor.js +45 -4
  119. package/queries/catalog/input-sources-query/input-sources-query-format.js +22 -3
  120. package/queries/catalog/input-sources-query/input-types.d.ts +58 -0
  121. package/queries/catalog/input-sources-query/input-types.js +64 -0
  122. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +76 -53
  123. package/queries/catalog/input-sources-query/simple-input-classifier.js +358 -101
  124. package/queries/catalog/project-query/project-query-executor.js +1 -1
  125. package/queries/catalog/signature-query/signature-query-executor.d.ts +7 -1
  126. package/queries/catalog/signature-query/signature-query-executor.js +112 -39
  127. package/queries/catalog/signature-query/signature-query-format.d.ts +5 -1
  128. package/queries/catalog/signature-query/signature-query-format.js +1 -1
  129. package/queries/catalog/signature-query/signature-query-render.js +8 -3
  130. package/r-bridge/data/data.d.ts +2 -2
  131. package/r-bridge/data/data.js +2 -2
  132. package/r-bridge/lang-4.x/ast/parser/json/format.js +1 -1
  133. package/r-bridge/retriever.d.ts +1 -1
  134. package/reconstruct/inline/source-inline-map.js +1 -1
  135. package/search/flowr-search-printer.js +10 -8
  136. package/slicing/static/static-slicer.d.ts +2 -2
  137. package/util/mermaid/dfg.js +1 -1
  138. package/util/objects.js +6 -4
  139. package/util/r-base-packages.d.ts +2 -0
  140. package/util/r-base-packages.js +3 -0
  141. package/util/r-license.js +1 -4
  142. package/util/r-version.d.ts +12 -0
  143. package/util/r-version.js +16 -0
  144. package/util/range.d.ts +6 -2
  145. package/util/range.js +33 -15
  146. package/util/version.js +1 -1
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InputTraceType = exports.InputType = void 0;
3
+ exports.InputType = exports.InputTraceType = void 0;
4
4
  exports.classifyInput = classifyInput;
5
+ const node_id_1 = require("../../../r-bridge/lang-4.x/ast/model/processing/node-id");
6
+ const input_types_1 = require("./input-types");
7
+ var input_types_2 = require("./input-types");
8
+ Object.defineProperty(exports, "InputTraceType", { enumerable: true, get: function () { return input_types_2.InputTraceType; } });
9
+ Object.defineProperty(exports, "InputType", { enumerable: true, get: function () { return input_types_2.InputType; } });
5
10
  const graph_1 = require("../../../dataflow/graph/graph");
6
11
  const objects_1 = require("../../../util/objects");
7
12
  const vertex_1 = require("../../../dataflow/graph/vertex");
@@ -17,8 +22,12 @@ const r_string_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-string"
17
22
  const r_logical_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-logical");
18
23
  const r_symbol_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol");
19
24
  const convert_values_1 = require("../../../r-bridge/lang-4.x/convert-values");
25
+ const model_1 = require("../../../r-bridge/lang-4.x/ast/model/model");
26
+ const r_function_definition_1 = require("../../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
27
+ /** how far a function handed to an entry point may be passed along before we give up resolving it */
28
+ const MaxFunctionResolveDepth = 4;
20
29
  function isConstantLike(type) {
21
- return type === InputType.Constant || type === InputType.DerivedConstant;
30
+ return type === input_types_1.InputType.Constant || type === input_types_1.InputType.DerivedConstant;
22
31
  }
23
32
  /** Returns the common value shared by all defined entries, or `undefined` if they disagree or all are `undefined`. */
24
33
  function singleValue(values) {
@@ -53,17 +62,17 @@ class ClassificationAccumulator {
53
62
  if (c.cds) {
54
63
  this.cds.push(...c.cds);
55
64
  }
56
- if (c.trace !== InputTraceType.Pure) {
65
+ if (c.trace !== input_types_1.InputTraceType.Pure) {
57
66
  this.allPure = false;
58
67
  }
59
68
  }
60
69
  pushUnknown() {
61
- this.types.push(InputType.Unknown);
70
+ this.types.push(input_types_1.InputType.Unknown);
62
71
  this.values.push(undefined);
63
72
  }
64
73
  build(id) {
65
- const types = this.types.length === 0 ? [InputType.Unknown] : (0, arrays_1.uniqueArray)(this.types);
66
- const trace = this.allPure ? InputTraceType.Pure : InputTraceType.Alias;
74
+ const types = this.types.length === 0 ? [input_types_1.InputType.Unknown] : (0, arrays_1.uniqueArray)(this.types);
75
+ const trace = this.allPure ? input_types_1.InputTraceType.Pure : input_types_1.InputTraceType.Alias;
67
76
  const src = { id, types, trace };
68
77
  const cds = this.cds.length === 0 ? undefined : (0, arrays_1.uniqueArray)(this.cds);
69
78
  if (cds) {
@@ -83,14 +92,171 @@ class InputClassifier {
83
92
  config;
84
93
  cache = new Map();
85
94
  fullDfg;
86
- constructor(dfg, config, fullDfg) {
95
+ /** the packages attached in the analyzed program, `undefined` if that is not known (then everything may match) */
96
+ packages;
97
+ fullClassifier;
98
+ declarationIndex;
99
+ entryPointIndex;
100
+ constructor(dfg, config, fullDfg, packages) {
87
101
  this.dfg = dfg;
88
102
  this.config = config;
89
103
  this.fullDfg = fullDfg;
104
+ this.packages = packages;
105
+ }
106
+ matches(call, list) {
107
+ return matchesList(call, list, this.packages);
108
+ }
109
+ /** whether the package the given entry needs is attached (unknown package information lets everything through) */
110
+ hasPackage(name) {
111
+ return name === undefined || this.packages === undefined || this.packages.has(name);
112
+ }
113
+ /**
114
+ * Returns the specification of the {@link LinkedInputObject|linked input object} the given id refers to
115
+ * (e.g., shiny's `input`), or `undefined` if it refers to something else.
116
+ */
117
+ matchLinkedObject(id) {
118
+ const idMap = this.dfg.idMap;
119
+ const node = idMap?.get(id);
120
+ if (idMap === undefined || !r_symbol_1.RSymbol.is(node)) {
121
+ return undefined;
122
+ }
123
+ // the framework may bind the object by position, in a function it is handed (`shinyApp(ui, server)`)
124
+ const positional = this.boundByEntryPoint(node, idMap);
125
+ if (positional !== undefined) {
126
+ return positional;
127
+ }
128
+ return this.config.linkedObjects?.find(o => o.name === node.content && this.hasPackage(o.requires) && isBoundAsLinkedObject(node, o, idMap));
129
+ }
130
+ /**
131
+ * The object a framework binds at this symbol's position, when the function binding it is handed to one of the
132
+ * {@link LinkedInputEntryPoint|entry points} - this is how R passes them, so the parameter names do not matter.
133
+ */
134
+ boundByEntryPoint(node, idMap) {
135
+ if (!this.config.linkedEntryPoints?.length) {
136
+ return undefined;
137
+ }
138
+ for (const fn of enclosingFunctions(node, idMap)) {
139
+ const index = fn.parameters.findIndex(p => p.name.content === node.content);
140
+ if (index < 0) {
141
+ continue; // not bound by this function, so keep looking outwards
142
+ }
143
+ const bound = this.entryPoints().get(fn.info.id)?.[index];
144
+ // the entry point names the object, so it keeps its fields and declarations no matter what the parameter is called
145
+ return bound === undefined ? undefined : this.config.linkedObjects?.find(o => o.name === bound);
146
+ }
147
+ return undefined;
148
+ }
149
+ /** The function definitions handed to an {@link LinkedInputEntryPoint|entry point}, with how it binds their parameters. */
150
+ entryPoints() {
151
+ if (this.entryPointIndex !== undefined) {
152
+ return this.entryPointIndex;
153
+ }
154
+ const index = new Map();
155
+ this.entryPointIndex = index;
156
+ for (const [, call] of (this.fullDfg ?? this.dfg).verticesOfType(vertex_1.VertexType.FunctionCall)) {
157
+ for (const entry of this.config.linkedEntryPoints ?? []) {
158
+ if (!this.matches(call, [entry.call])) {
159
+ continue;
160
+ }
161
+ const handed = this.argumentReference(call, entry.argIdx, entry.argName);
162
+ for (const fn of this.functionDefinitionsAt(handed)) {
163
+ index.set(fn, entry.params);
164
+ }
165
+ }
166
+ }
167
+ return index;
168
+ }
169
+ /** the function definitions the given id may hold, be it one directly or a variable a definition was assigned to */
170
+ *functionDefinitionsAt(id, depth = 0) {
171
+ const graph = this.fullDfg ?? this.dfg;
172
+ const vtx = id === undefined || depth > MaxFunctionResolveDepth ? undefined : graph.getVertex(id);
173
+ if (vtx === undefined) {
174
+ return;
175
+ }
176
+ else if (vtx.tag === vertex_1.VertexType.FunctionDefinition) {
177
+ yield vtx.id;
178
+ }
179
+ else if (vtx.tag === vertex_1.VertexType.VariableDefinition) {
180
+ for (const source of vtx.source ?? []) {
181
+ yield* this.functionDefinitionsAt(source, depth + 1);
182
+ }
183
+ }
184
+ else {
185
+ for (const origin of df_helper_1.Dataflow.origin(graph, vtx.id) ?? []) {
186
+ if (origin.type === 0 /* OriginType.ReadVariableOrigin */ || origin.type === 1 /* OriginType.WriteVariableOrigin */ || origin.type === 2 /* OriginType.FunctionCallOrigin */) {
187
+ yield* this.functionDefinitionsAt(origin.id, depth + 1);
188
+ }
189
+ }
190
+ }
191
+ }
192
+ /** the id of the argument named `argName`, or of the one at `argIdx` if it is passed positionally */
193
+ argumentReference(call, argIdx, argName) {
194
+ const named = call.args.find(a => graph_1.FunctionArgument.isNamed(a) && graph_1.FunctionArgument.getName(a) === argName);
195
+ const arg = named ?? call.args[argIdx];
196
+ if (arg === undefined || graph_1.FunctionArgument.isEmpty(arg) || (named === undefined && graph_1.FunctionArgument.isNamed(arg))) {
197
+ return undefined;
198
+ }
199
+ return graph_1.FunctionArgument.getReference(arg);
200
+ }
201
+ /** the linked object the id refers to, but only where reading the object as a whole already is an input */
202
+ matchWholeLinkedObject(id) {
203
+ const obj = this.matchLinkedObject(id);
204
+ return obj !== undefined && fieldIsInput(obj, undefined) ? obj : undefined;
205
+ }
206
+ /**
207
+ * All declarations of framework entries in the program, keyed by object and entry name, built once on first use.
208
+ * This is what links a read of `input$n` back to the `textInput("n", …)` defining it.
209
+ */
210
+ declarations() {
211
+ if (this.declarationIndex !== undefined) {
212
+ return this.declarationIndex;
213
+ }
214
+ const index = new Map();
215
+ this.declarationIndex = index;
216
+ const specs = this.config.linkedObjects?.filter(o => o.declaredBy !== undefined) ?? [];
217
+ if (specs.length === 0) {
218
+ return index;
219
+ }
220
+ for (const [, call] of (this.fullDfg ?? this.dfg).verticesOfType(vertex_1.VertexType.FunctionCall)) {
221
+ for (const obj of specs) {
222
+ const spec = obj.declaredBy;
223
+ if (!this.matches(call, spec.calls)) {
224
+ continue;
225
+ }
226
+ const name = this.argumentValue(call, spec.argIdx, spec.argName);
227
+ if (typeof name === 'string') {
228
+ const key = declarationKey(obj.name, name);
229
+ index.set(key, [...(index.get(key) ?? []), call.id]);
230
+ }
231
+ }
232
+ }
233
+ return index;
234
+ }
235
+ /** the value of the argument named `argName`, or of the one at `argIdx` if it is passed positionally */
236
+ argumentValue(call, argIdx, argName) {
237
+ const ref = this.argumentReference(call, argIdx, argName);
238
+ const vtx = ref === undefined ? undefined : (this.fullDfg ?? this.dfg).getVertex(ref);
239
+ return vtx === undefined ? undefined : this.classifyEntry(vtx).value;
90
240
  }
91
241
  isDefinedByOnCall(id) {
242
+ return this.definedByOnCallTargets(id).length > 0;
243
+ }
244
+ /** the ids the given one is linked to by {@link EdgeType.DefinedByOnCall}, e.g. a parameter to the arguments it is bound to */
245
+ definedByOnCallTargets(id) {
92
246
  const out = (this.fullDfg ?? this.dfg).outgoingEdges(id) ?? new Map();
93
- return out.values().some(e => edge_1.DfEdge.includesType(e, edge_1.EdgeType.DefinedByOnCall));
247
+ return out.entries().filter(([, e]) => edge_1.DfEdge.includesType(e, edge_1.EdgeType.DefinedByOnCall)).map(([to]) => to).toArray();
248
+ }
249
+ /**
250
+ * Classifies the given id against the full graph, for everything the reduced graph of the criterion cannot see
251
+ * (the enclosing scopes and the callers of the function the criterion is in).
252
+ */
253
+ classifyInFullGraph(id) {
254
+ if (this.fullDfg === undefined || this.fullDfg === this.dfg) {
255
+ return undefined;
256
+ }
257
+ this.fullClassifier ??= new InputClassifier(this.fullDfg, this.config, undefined, this.packages);
258
+ const vtx = this.fullDfg.getVertex(id);
259
+ return vtx ? this.fullClassifier.classifyEntry(vtx) : undefined;
94
260
  }
95
261
  extractConstantValue(id) {
96
262
  const node = this.dfg.idMap?.get(id);
@@ -117,10 +283,10 @@ class InputClassifier {
117
283
  return cached;
118
284
  }
119
285
  // insert temporary unknown to break cycles
120
- this.cache.set(vertex.id, { id: vertex.id, types: [InputType.Unknown], trace: InputTraceType.Unknown });
286
+ this.cache.set(vertex.id, { id: vertex.id, types: [input_types_1.InputType.Unknown], trace: input_types_1.InputTraceType.Unknown });
121
287
  switch (vertex.tag) {
122
288
  case vertex_1.VertexType.Value: {
123
- const src = { id: vertex.id, types: [InputType.Constant], trace: InputTraceType.Unknown };
289
+ const src = { id: vertex.id, types: [input_types_1.InputType.Constant], trace: input_types_1.InputTraceType.Unknown };
124
290
  const v = this.extractConstantValue(vertex.id);
125
291
  if (v !== undefined) {
126
292
  src.value = v;
@@ -134,11 +300,63 @@ class InputClassifier {
134
300
  case vertex_1.VertexType.Use:
135
301
  return this.classifyVariable(vertex);
136
302
  default:
137
- return this.classifyCdsAndReturn(vertex, { id: vertex.id, types: [InputType.Unknown], trace: InputTraceType.Unknown });
303
+ return this.classifyCdsAndReturn(vertex, { id: vertex.id, types: [input_types_1.InputType.Unknown], trace: input_types_1.InputTraceType.Unknown });
138
304
  }
139
305
  }
306
+ /**
307
+ * Accesses like `input$n` or `input[["n"]]` are reported as a single source of the accessed object,
308
+ * carrying the accessed field as its {@link InputSource.name|name}.
309
+ */
310
+ classifyLinkedObjectAccess(call) {
311
+ if (!call.origin.includes(built_in_proc_name_1.BuiltInProcName.Access)) {
312
+ return undefined;
313
+ }
314
+ const accessed = graph_1.FunctionArgument.isEmpty(call.args[0]) ? undefined : graph_1.FunctionArgument.getReference(call.args[0]);
315
+ const linked = accessed === undefined ? undefined : this.matchLinkedObject(accessed);
316
+ const field = this.accessedField(call);
317
+ if (linked === undefined || !fieldIsInput(linked, field)) {
318
+ return undefined;
319
+ }
320
+ const src = { id: call.id, types: [linked.type], trace: input_types_1.InputTraceType.Unknown };
321
+ if (field !== undefined) {
322
+ src.name = field;
323
+ const declaredAt = linked.declaredBy && this.declarations().get(declarationKey(linked.name, field));
324
+ if (declaredAt) {
325
+ src.declaredAt = declaredAt;
326
+ }
327
+ }
328
+ return src;
329
+ }
330
+ accessedField(call) {
331
+ const arg = call.args[1];
332
+ if (arg === undefined || graph_1.FunctionArgument.isEmpty(arg)) {
333
+ return undefined;
334
+ }
335
+ const ref = graph_1.FunctionArgument.getReference(arg);
336
+ const node = ref === undefined ? undefined : this.dfg.idMap?.get(ref);
337
+ if (r_string_1.RString.is(node)) {
338
+ return node.content.str;
339
+ }
340
+ else if (r_symbol_1.RSymbol.is(node)) {
341
+ return identifier_1.Identifier.getName(node.content);
342
+ }
343
+ return undefined;
344
+ }
140
345
  classifyFunctionCall(call) {
141
- if (call.origin.includes(built_in_proc_name_1.BuiltInProcName.IfThenElse) || call.origin.includes(built_in_proc_name_1.BuiltInProcName.WhileLoop)) {
346
+ const linkedAccess = this.classifyLinkedObjectAccess(call);
347
+ if (linkedAccess) {
348
+ return this.classifyCdsAndReturn(call, linkedAccess);
349
+ }
350
+ if (call.origin.includes(built_in_proc_name_1.BuiltInProcName.ExpressionList)) {
351
+ // `{ a; b }` evaluates to its last expression, just like in R
352
+ const last = call.args.findLast(a => !graph_1.FunctionArgument.isEmpty(a));
353
+ const value = last === undefined ? undefined : graph_1.FunctionArgument.getReference(last);
354
+ const vtx = value === undefined ? undefined : this.dfg.getVertex(value);
355
+ if (vtx) {
356
+ return this.classifyCdsAndReturn(call, { ...this.classifyEntry(vtx), id: call.id });
357
+ }
358
+ }
359
+ else if (call.origin.includes(built_in_proc_name_1.BuiltInProcName.IfThenElse) || call.origin.includes(built_in_proc_name_1.BuiltInProcName.WhileLoop)) {
142
360
  const condition = graph_1.FunctionArgument.getReference(call.args[0]);
143
361
  if (condition) {
144
362
  const vtx = this.dfg.getVertex(condition);
@@ -156,15 +374,38 @@ class InputClassifier {
156
374
  }
157
375
  }
158
376
  }
159
- if (!matchesList(call, this.config.pure)) {
377
+ else if (call.origin.includes(built_in_proc_name_1.BuiltInProcName.Get) && !(this.fullDfg ?? this.dfg).unknownSideEffects.has(node_id_1.NodeId.normalize(call.id))) {
378
+ // a statically resolved `get("x")` yields the value of the retrieved variable, read via its first argument
379
+ const ref = graph_1.FunctionArgument.getReference(call.args[0]);
380
+ const vtx = ref === undefined ? undefined : this.dfg.getVertex(ref);
381
+ if (vtx) {
382
+ return this.classifyCdsAndReturn(call, { ...this.classifyEntry(vtx), id: call.id });
383
+ }
384
+ }
385
+ // a narrowing function returns a bounded value: either one of a specific argument's values (e.g. `match.arg`
386
+ // -> its `choices`), or - with no bounding argument - a content-independent value like a count/index/logical
387
+ for (const narrow of this.config.narrowing ?? []) {
388
+ if (!this.matches(call, [narrow.call])) {
389
+ continue;
390
+ }
391
+ if (narrow.argIdx === undefined) {
392
+ return this.classifyCdsAndReturn(call, (0, objects_1.compactRecord)({ id: call.id, types: [input_types_1.InputType.DerivedConstant], trace: input_types_1.InputTraceType.Pure }));
393
+ }
394
+ const ref = this.argumentReference(call, narrow.argIdx, narrow.argName ?? '');
395
+ const vtx = ref === undefined ? undefined : this.dfg.getVertex(ref);
396
+ if (vtx) {
397
+ return this.classifyCdsAndReturn(call, { ...this.classifyEntry(vtx), id: call.id });
398
+ }
399
+ }
400
+ if (!this.matches(call, this.config.pure)) {
160
401
  const types = [];
161
- for (const [type, entry] of record_1.Record.entries(this.config)) {
162
- if (record_1.Record.values(InputType).includes(type) && matchesList(call, entry)) {
402
+ for (const type of record_1.Record.values(input_types_1.InputType)) {
403
+ if (this.matches(call, this.config[type])) {
163
404
  types.push(type);
164
405
  }
165
406
  }
166
407
  // if a File-typed call reads from a temp path, replace File with TempFile
167
- if (types.includes(InputType.File) && !types.includes(InputType.TempFile)) {
408
+ if (types.includes(input_types_1.InputType.File) && !types.includes(input_types_1.InputType.TempFile)) {
168
409
  for (const arg of call.args) {
169
410
  if (graph_1.FunctionArgument.isEmpty(arg)) {
170
411
  continue;
@@ -174,18 +415,23 @@ class InputClassifier {
174
415
  continue;
175
416
  }
176
417
  const argVtx = this.dfg.getVertex(ref);
177
- if (argVtx && this.classifyEntry(argVtx).types.includes(InputType.TempFile)) {
178
- types.splice(types.indexOf(InputType.File), 1);
179
- types.push(InputType.TempFile);
418
+ if (argVtx && this.classifyEntry(argVtx).types.includes(input_types_1.InputType.TempFile)) {
419
+ types.splice(types.indexOf(input_types_1.InputType.File), 1);
420
+ types.push(input_types_1.InputType.TempFile);
180
421
  break;
181
422
  }
182
423
  }
183
424
  }
184
425
  if (types.length === 0) {
426
+ // a call of something the code produced itself, like a shiny `reactive()`, yields what that produced
427
+ const callee = this.classifyCallee(call);
428
+ if (callee !== undefined) {
429
+ return this.classifyCdsAndReturn(call, { ...callee, id: call.id });
430
+ }
185
431
  // if it is not pure, we cannot classify based on the inputs, in that case we do not know!
186
- types.push(InputType.Unknown);
432
+ types.push(input_types_1.InputType.Unknown);
187
433
  }
188
- return this.classifyCdsAndReturn(call, { id: call.id, types, trace: InputTraceType.Unknown });
434
+ return this.classifyCdsAndReturn(call, { id: call.id, types, trace: input_types_1.InputTraceType.Unknown });
189
435
  }
190
436
  // Otherwise, classify by arguments; pure functions get Known/Pure handling
191
437
  const argTypes = [];
@@ -196,12 +442,12 @@ class InputClassifier {
196
442
  }
197
443
  const ref = graph_1.FunctionArgument.getReference(arg);
198
444
  if (ref === undefined) {
199
- argTypes.push(InputType.Unknown);
445
+ argTypes.push(input_types_1.InputType.Unknown);
200
446
  continue;
201
447
  }
202
448
  const argVtx = this.dfg.getVertex(ref);
203
449
  if (!argVtx) {
204
- argTypes.push(InputType.Unknown);
450
+ argTypes.push(input_types_1.InputType.Unknown);
205
451
  continue;
206
452
  }
207
453
  const classified = this.classifyEntry(argVtx);
@@ -215,20 +461,43 @@ class InputClassifier {
215
461
  // all arguments only contain constant-like types -> derived constant
216
462
  const allConstLike = argTypes.length > 0 && argTypes.every(isConstantLike);
217
463
  if (allConstLike) {
218
- return this.classifyCdsAndReturn(call, (0, objects_1.compactRecord)({ id: call.id, types: [InputType.DerivedConstant], trace: InputTraceType.Pure, cds }));
464
+ return this.classifyCdsAndReturn(call, (0, objects_1.compactRecord)({ id: call.id, types: [input_types_1.InputType.DerivedConstant], trace: input_types_1.InputTraceType.Pure, cds }));
219
465
  }
220
- argTypes.push(InputType.DerivedConstant);
221
- return this.classifyCdsAndReturn(call, (0, objects_1.compactRecord)({ id: call.id, types: (0, arrays_1.uniqueArray)(argTypes), trace: InputTraceType.Known, cds }));
466
+ argTypes.push(input_types_1.InputType.DerivedConstant);
467
+ return this.classifyCdsAndReturn(call, (0, objects_1.compactRecord)({ id: call.id, types: (0, arrays_1.uniqueArray)(argTypes), trace: input_types_1.InputTraceType.Known, cds }));
468
+ }
469
+ /** classifies what a call of a variable (e.g. a shiny reactive `n()`) yields, by what that variable holds */
470
+ classifyCallee(call) {
471
+ for (const o of df_helper_1.Dataflow.origin(this.dfg, call.id) ?? []) {
472
+ if (o.type !== 0 /* OriginType.ReadVariableOrigin */ && o.type !== 1 /* OriginType.WriteVariableOrigin */) {
473
+ continue;
474
+ }
475
+ const vtx = this.dfg.getVertex(o.id);
476
+ const classified = vtx ? this.classifyEntry(vtx) : this.classifyInFullGraph(o.id);
477
+ if (classified !== undefined && !classified.types.includes(input_types_1.InputType.Unknown)) {
478
+ return classified;
479
+ }
480
+ }
481
+ return undefined;
222
482
  }
223
483
  classifyVariable(vtx) {
484
+ const linked = this.matchWholeLinkedObject(vtx.id);
485
+ if (linked) {
486
+ return this.classifyCdsAndReturn(vtx, { id: vtx.id, types: [linked.type], trace: input_types_1.InputTraceType.Unknown });
487
+ }
224
488
  const origins = df_helper_1.Dataflow.origin(this.dfg, vtx.id);
225
- if (origins === undefined) {
226
- return this.classifyCdsAndReturn(vtx, { id: vtx.id, types: this.isDefinedByOnCall(vtx.id) ? [InputType.Scope] : [InputType.Unknown], trace: InputTraceType.Unknown });
489
+ if (origins === undefined || origins.length === 0) {
490
+ if (this.isDefinedByOnCall(vtx.id)) {
491
+ return this.classifyCdsAndReturn(vtx, { id: vtx.id, types: [input_types_1.InputType.Scope], trace: input_types_1.InputTraceType.Unknown });
492
+ }
493
+ // the definition is not part of the criterion's function, so it has to come from an enclosing scope
494
+ const outer = this.classifyInFullGraph(vtx.id);
495
+ return this.classifyCdsAndReturn(vtx, outer ? { ...outer, id: vtx.id } : { id: vtx.id, types: [input_types_1.InputType.Unknown], trace: input_types_1.InputTraceType.Unknown });
227
496
  }
228
497
  const acc = new ClassificationAccumulator();
229
498
  for (const o of origins) {
230
499
  if (o.type === 4 /* OriginType.ConstantOrigin */) {
231
- acc.types.push(InputType.DerivedConstant);
500
+ acc.types.push(input_types_1.InputType.DerivedConstant);
232
501
  acc.values.push(this.extractConstantValue(o.id));
233
502
  }
234
503
  else if (o.type === 0 /* OriginType.ReadVariableOrigin */ || o.type === 1 /* OriginType.WriteVariableOrigin */) {
@@ -253,16 +522,24 @@ class InputClassifier {
253
522
  acc.pushUnknown();
254
523
  return;
255
524
  }
256
- // if the referenced definition is linked via defined-by-on-call to another
257
- // id (e.g., a parameter linked to a caller argument), mark it as a Scope origin
258
- if (this.isDefinedByOnCall(v.id)) {
259
- acc.types.push(InputType.Scope);
525
+ // if the referenced definition is linked via defined-by-on-call to another id (e.g., a parameter linked to a
526
+ // caller argument), follow it into the caller; only if that leads nowhere is it an opaque Scope origin
527
+ const onCall = this.definedByOnCallTargets(v.id);
528
+ if (onCall.length > 0) {
529
+ const callers = onCall.map(t => this.classifyInFullGraph(t))
530
+ .filter(assert_1.isNotUndefined)
531
+ .filter(c => !c.types.includes(input_types_1.InputType.Unknown));
532
+ if (callers.length > 0) {
533
+ callers.forEach(c => acc.merge(c));
534
+ return;
535
+ }
536
+ acc.types.push(input_types_1.InputType.Scope);
260
537
  acc.values.push(undefined);
261
538
  acc.allPure = false;
262
539
  }
263
540
  // if this is a variable definition that is a parameter, classify as Parameter
264
541
  if (v.tag === vertex_1.VertexType.VariableDefinition && this.dfg.idMap?.get(v.id)?.info.role === "param-n" /* RoleInParent.ParameterName */) {
265
- acc.types.push(InputType.Parameter);
542
+ acc.types.push(this.matchWholeLinkedObject(v.id)?.type ?? input_types_1.InputType.Parameter);
266
543
  acc.values.push(undefined);
267
544
  return;
268
545
  }
@@ -280,12 +557,13 @@ class InputClassifier {
280
557
  classifyVariableDefinition(vtx) {
281
558
  // parameter definitions are classified as Parameter
282
559
  if (this.dfg.idMap?.get(vtx.id)?.info.role === "param-n" /* RoleInParent.ParameterName */) {
283
- return this.classifyCdsAndReturn(vtx, { id: vtx.id, types: [InputType.Parameter], trace: InputTraceType.Unknown });
560
+ const types = [this.matchWholeLinkedObject(vtx.id)?.type ?? input_types_1.InputType.Parameter];
561
+ return this.classifyCdsAndReturn(vtx, { id: vtx.id, types, trace: input_types_1.InputTraceType.Unknown });
284
562
  }
285
563
  const sources = vtx.source;
286
564
  if (sources === undefined || sources.length === 0) {
287
565
  // fallback to unknown if we cannot find the value
288
- return this.classifyCdsAndReturn(vtx, { id: vtx.id, types: [InputType.Unknown], trace: InputTraceType.Unknown });
566
+ return this.classifyCdsAndReturn(vtx, { id: vtx.id, types: [input_types_1.InputType.Unknown], trace: input_types_1.InputTraceType.Unknown });
289
567
  }
290
568
  const acc = new ClassificationAccumulator();
291
569
  for (const tid of sources) {
@@ -320,86 +598,65 @@ class InputClassifier {
320
598
  return src;
321
599
  }
322
600
  }
323
- /**
324
- * Lattice flattening until we have a taint engine :)
325
- * Please note that the classifier considers this basis with a set-lift,
326
- * joining differing lattice elements.
327
- *
328
- *```
329
- * [ Unknown ]
330
- * |
331
- * [Param] [File] [Net] [User], ...
332
- * | | | |
333
- * | [TempFile] | |
334
- * +---------+------+------+- ...
335
- * |
336
- * [ DerivedConstant ]
337
- * |
338
- * [ Constant ]
339
- *```
340
- *
341
- */
342
- var InputType;
343
- (function (InputType) {
344
- InputType["Parameter"] = "param";
345
- InputType["File"] = "file";
346
- /** Temporary file paths produced by tempfile()/tempdir() and equivalents; a sub-type of {@link File} */
347
- InputType["TempFile"] = "tempfile";
348
- InputType["Network"] = "net";
349
- InputType["Random"] = "rand";
350
- /** Calls to system/system2 and similar */
351
- InputType["System"] = "system";
352
- /** Calls to .C / Fortran interfaces (foreign function interfaces) */
353
- InputType["Ffi"] = "ffi";
354
- /** Language objects (quote/substitute/etc.) */
355
- InputType["Lang"] = "lang";
356
- /** Global options / option accessors (options, getOption) */
357
- InputType["Options"] = "options";
358
- /** Interactive user input (file choosers, prompts, dialogs, menu selections) */
359
- InputType["User"] = "user";
360
- InputType["Constant"] = "const";
361
- /** Read from environment/call scope */
362
- InputType["Scope"] = "scope";
363
- /** Pure calculations from constants that lead to a constant */
364
- InputType["DerivedConstant"] = "dconst";
365
- InputType["Unknown"] = "unknown";
366
- })(InputType || (exports.InputType = InputType = {}));
367
- var InputTraceType;
368
- (function (InputTraceType) {
369
- /** Derived only from aliasing */
370
- InputTraceType["Alias"] = "alias";
371
- /** Derived from pure function chains */
372
- InputTraceType["Pure"] = "pure";
373
- /** Derived from known but not necessarily all pure function chains */
374
- InputTraceType["Known"] = "known";
375
- /** Not fully known origin */
376
- InputTraceType["Unknown"] = "unknown";
377
- })(InputTraceType || (exports.InputTraceType = InputTraceType = {}));
378
- function matchesList(fn, list) {
379
- if (list === undefined || list.length === 0) {
380
- return false;
381
- }
382
- for (const id of list) {
383
- if (fn.id === id || (identifier_1.Identifier.is(id) && (identifier_1.Identifier.matches(id, fn.name) || (identifier_1.Identifier.getNamespace(fn.name) === undefined && identifier_1.Identifier.matches(fn.name, id))))) {
384
- return true;
601
+ /** the function definitions the given node is nested in, innermost first */
602
+ function* enclosingFunctions(node, idMap) {
603
+ for (const parent of model_1.RNode.iterateParents(node, idMap)) {
604
+ if (r_function_definition_1.RFunctionDefinition.is(parent)) {
605
+ yield parent;
606
+ }
607
+ }
608
+ }
609
+ /** whether the given occurrence of `obj` is bound by a function matching {@link LinkedInputObject.withParams} */
610
+ function isBoundAsLinkedObject(node, obj, idMap) {
611
+ if (!obj.withParams?.length) {
612
+ return true;
613
+ }
614
+ for (const fn of enclosingFunctions(node, idMap)) {
615
+ const params = new Set(fn.parameters.map(p => p.name.content));
616
+ if (params.has(obj.name)) {
617
+ return obj.withParams.every(p => params.has(p));
385
618
  }
386
619
  }
387
620
  return false;
388
621
  }
622
+ function declarationKey(object, field) {
623
+ return `${object}\u0000${field}`;
624
+ }
625
+ /** whether reading `field` of the given object is an input; an object restricted to {@link LinkedInputObject.fields} is none as a whole */
626
+ function fieldIsInput(obj, field) {
627
+ return obj.fields === undefined || (field !== undefined && obj.fields.includes(field));
628
+ }
629
+ /**
630
+ * Whether a call by the name `called` means `id`. A `pkg::fn` call has to match exactly, while a bare call only
631
+ * means a namespaced entry if that package is attached - just like in R, where the search path decides.
632
+ * With `packages` left out (no package information at all) any bare call may mean it.
633
+ */
634
+ function callMeans(called, id, packages) {
635
+ if (identifier_1.Identifier.matches(id, called)) {
636
+ return true;
637
+ }
638
+ const namespace = identifier_1.Identifier.getNamespace(id);
639
+ return identifier_1.Identifier.getNamespace(called) === undefined && identifier_1.Identifier.matches(called, id)
640
+ && (namespace === undefined || packages === undefined || packages.has(namespace));
641
+ }
642
+ function matchesList(fn, list, packages) {
643
+ return list?.some(id => fn.id === id || (identifier_1.Identifier.is(id) && callMeans(fn.name, id, packages))) ?? false;
644
+ }
389
645
  /**
390
646
  * Takes the given id which is expected to either be:
391
647
  * - a function call - in this case all arguments are considered to be inputs (additionally to all read edges from the function call in the dataflow graph)
392
648
  * - anything else - in that case the node itself is considered as an "input" - please note that in these scenarios the *return* value will only contain one mapping - that for the id you passed in.
393
649
  *
394
650
  * This method traces the dependencies in the dataflow graph using the specification of functions passed in.
395
- * For the scope escape analysis, pass on the full, non-reduced DFG as `fullDfg`.
651
+ * For the scope escape analysis, pass on the full, non-reduced DFG as `fullDfg`, and the packages attached in the
652
+ * program as `packages` so that bare calls only match the entries of packages that are actually in scope.
396
653
  */
397
- function classifyInput(id, dfg, config, fullDfg) {
654
+ function classifyInput(id, dfg, config, fullDfg, packages) {
398
655
  const vtx = dfg.getVertex(id);
399
656
  if (!vtx) {
400
657
  return [];
401
658
  }
402
- const c = new InputClassifier(dfg, config, fullDfg);
659
+ const c = new InputClassifier(dfg, config, fullDfg, packages);
403
660
  if (vtx.tag === vertex_1.VertexType.FunctionCall) {
404
661
  const ret = [];
405
662
  const args = vtx.args;
@@ -12,7 +12,7 @@ function collectDependencyStats(declared, deps, basePackages) {
12
12
  const imports = declared.imports ?? [];
13
13
  const dependsAll = declared.depends ?? [];
14
14
  const rEntry = dependsAll.find(d => d.name === 'R');
15
- const rVersion = rEntry?.derivedVersion?.raw;
15
+ const rVersion = rEntry?.derivedRange?.raw;
16
16
  const depends = dependsAll.filter(d => d.name !== 'R');
17
17
  const suggests = declared.suggests ?? [];
18
18
  const linkingTo = declared.linkingTo ?? [];
@@ -6,6 +6,12 @@ import type { CommandCompletions } from '../../../cli/repl/core';
6
6
  export declare function cranPageUrl(pkg: string): string;
7
7
  /** deep-link a definition into the CRAN mirror at the package's version tag (falling back to `HEAD`) */
8
8
  export declare function cranMirrorSourceUrl(pkg: string, version: string | undefined, file: string, line?: number): string;
9
+ /**
10
+ * The mirror ref holding an R version. The mirror carries no tags, only a `R-<major>-<minor>-branch` per release
11
+ * series, so a link is exact to the minor release and points at its latest patch; `trunk` stands in when the version
12
+ * is unknown.
13
+ */
14
+ export declare function rSourceRef(version: string | undefined): string;
9
15
  /** best-effort rdrr.io documentation link: `/r/<pkg>/<fn>` for base R, `/cran/<pkg>/man/<fn>` for CRAN */
10
16
  export declare function rdrrDocUrl(pkg: string, fn: string, opts: {
11
17
  base: boolean;
@@ -13,7 +19,7 @@ export declare function rdrrDocUrl(pkg: string, fn: string, opts: {
13
19
  }): string | undefined;
14
20
  /**
15
21
  * The detailed view of a single function within a package: its signature (parameters, forced/optional,
16
- * defaults), properties, definition location, call graph, and -- for a CRAN package -- a deep link into the
22
+ * defaults), properties, definition location, call graph, and for a CRAN package a deep link into the
17
23
  * read-only CRAN GitHub mirror. `version` defaults to the source's latest; `undefined` when the source does
18
24
  * not carry that function.
19
25
  */