@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
100 lines • 4.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlowrAnalyzerLoadingOrderContext = void 0;
const abstract_flowr_analyzer_context_1 = require("./abstract-flowr-analyzer-context");
const log_1 = require("../../util/log");
const arrays_1 = require("../../util/collections/arrays");
const flowr_analyzer_loading_order_plugin_1 = require("../plugins/loading-order-plugins/flowr-analyzer-loading-order-plugin");
const loadingOrderLog = log_1.log.getSubLogger({ name: 'loading-order' });
/**
* This context is responsible for managing the loading order of script files in a project, including guesses and known orders provided by {@link FlowrAnalyzerLoadingOrderPlugin}s.
*
* If you are interested in inspecting these orders, refer to {@link ReadOnlyFlowrAnalyzerLoadingOrderContext}.
* Plugins, however, can use this context directly to modify order guesses.
*/
class FlowrAnalyzerLoadingOrderContext extends abstract_flowr_analyzer_context_1.AbstractFlowrAnalyzerContext {
name = 'flowr-analyzer-loading-order-context';
rerunRequired;
constructor(ctx, plugins) {
super(ctx, flowr_analyzer_loading_order_plugin_1.FlowrAnalyzerLoadingOrderPlugin.defaultPlugin(), plugins);
this.rerunRequired = this.plugins.length > 0;
}
knownOrder;
guesses = [];
/** just the base collection of requests we know nothing about the order! */
unordered = [];
reset() {
this.knownOrder = undefined;
this.guesses.length = 0;
this.unordered.length = 0;
this.rerunRequired = this.plugins.length > 0;
}
/**
* Add one or multiple requests to the context.
* These are considered unordered (i.e., ordered implicitly by the order of addition) until a plugin provides either a guess or a known order.
*
* This is a batched version of {@link addRequest}.
*/
addRequests(requests) {
this.unordered.push(...requests);
if (this.knownOrder || this.guesses.length > 0) {
loadingOrderLog.warn(`Adding requests ${requests.map(r => r.request).join(', ')} after known order!`);
this.rerunRequired = true;
}
}
/**
* Add a single request to the context.
* This is considered unordered (i.e., ordered implicitly by the order of addition) until a plugin provides either a guess or a known order.
*
* If you want to add multiple requests, consider using {@link addRequests} instead for efficiency.
*/
addRequest(request) {
this.unordered.push(request);
if (this.knownOrder || this.guesses.length > 0) {
loadingOrderLog.warn(`Adding request ${request.request} ${request.content} after known order!`);
this.rerunRequired = true;
}
}
/**
* Add a guess for the loading order. This is mostly for plugins to use.
* In case you have a certain order, use the `certain` flag to indicate this -- but please take care of *really* being certain!
*/
addGuess(guess, certain) {
if (certain) {
if (this.knownOrder) {
loadingOrderLog.warn(`Adding certain guess ${guess.map(g => g.request).join(', ')} after known order!`);
if (!(0, arrays_1.arrayEqual)(this.knownOrder, guess)) {
loadingOrderLog.error(`Certain guess ${guess.map(g => g.request).join(', ')} does not match known order ${this.knownOrder.map(g => g.request).join(', ')}`);
this.guesses.push(guess);
}
}
else {
this.knownOrder = guess;
}
}
else {
this.guesses.push(guess);
}
}
currentGuesses() {
return this.guesses;
}
currentKnownOrder() {
return this.knownOrder;
}
peekLoadingOrder() {
return this.knownOrder ?? (this.guesses.length > 0 ? this.guesses[0] : undefined);
}
getUnorderedRequests() {
return this.unordered;
}
getLoadingOrder() {
if (this.rerunRequired) {
this.rerunRequired = false;
this.applyPlugins(undefined);
}
return this.peekLoadingOrder() ?? this.unordered;
}
}
exports.FlowrAnalyzerLoadingOrderContext = FlowrAnalyzerLoadingOrderContext;
//# sourceMappingURL=flowr-analyzer-loading-order-context.js.map