UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

58 lines 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowrAnalyzerLoadingOrderDescriptionFilePlugin = void 0; const flowr_analyzer_description_file_plugin_1 = require("../file-plugins/flowr-analyzer-description-file-plugin"); const semver_1 = require("semver"); const flowr_analyzer_loading_order_plugin_1 = require("./flowr-analyzer-loading-order-plugin"); const flowr_file_1 = require("../../context/flowr-file"); /** * This plugin extracts loading order information from R `DESCRIPTION` files. * It looks at the `Collate` field to determine the order in which files should be loaded. * If no `Collate` field is present, it does nothing. */ class FlowrAnalyzerLoadingOrderDescriptionFilePlugin extends flowr_analyzer_loading_order_plugin_1.FlowrAnalyzerLoadingOrderPlugin { name = 'flowr-analyzer-package-version-description-file-plugin'; description = 'This plugin determines loading order based on the Collate field in DESCRIPTION files.'; version = new semver_1.SemVer('0.1.0'); process(ctx) { const descFiles = ctx.files.getFilesByRole(flowr_file_1.FileRole.Description); if (descFiles.length === 0) { flowr_analyzer_description_file_plugin_1.descriptionFileLog.debug('No description file found, cannot determine loading order from Collate field.'); return; } else if (descFiles.length > 1) { flowr_analyzer_description_file_plugin_1.descriptionFileLog.warn(`Found ${descFiles.length} description files, expected exactly one.`); } /** this will do the caching etc. for me */ const collate = descFiles[0].collate(); if (collate) { /* we probably have to do some more guesswork here */ const unordered = ctx.files.loadingOrder.getUnorderedRequests(); // sort them by their path index in the Collate field const sorted = unordered.slice().sort((a, b) => { const aPath = a.request === 'file' ? a.content : undefined; const bPath = b.request === 'file' ? b.content : undefined; const aIndex = aPath ? collate.findIndex(c => aPath.endsWith(c.trim())) : -1; const bIndex = bPath ? collate.findIndex(c => bPath.endsWith(c.trim())) : -1; if (aIndex === -1 && bIndex === -1) { return 0; // both not found, keep original order } else if (aIndex === -1) { return 1; // a not found, b found -> a after b } else if (bIndex === -1) { return -1; // b not found, a found -> a before b } else { return aIndex - bIndex; // both found, sort by index } }); ctx.files.loadingOrder.addGuess(sorted, true); } else { flowr_analyzer_description_file_plugin_1.descriptionFileLog.info(`No Collate field in DESCRIPTION file ${descFiles[0].path().toString()}`); } } } exports.FlowrAnalyzerLoadingOrderDescriptionFilePlugin = FlowrAnalyzerLoadingOrderDescriptionFilePlugin; //# sourceMappingURL=flowr-analyzer-loading-order-description-file-plugin.js.map