UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

68 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.statisticsFileProvider = void 0; exports.extractNodeContent = extractNodeContent; exports.initFileProvider = initFileProvider; exports.initDummyFileProvider = initDummyFileProvider; exports.appendStatisticsFile = appendStatisticsFile; const file_provider_1 = require("./file-provider"); const log_1 = require("../../util/log"); /** * Requires source information to be attached on parsing! * <p> * Returns the content of the node (i.e., the text content excluding the children) */ function extractNodeContent(node) { let result = node.textContent; if (node.hasChildNodes()) { const firstChild = node.childNodes.item(0); if (firstChild.nodeType === 3 /* text node */) { result = firstChild.textContent; } } return result?.trim()?.replaceAll('\n', '\\n') ?? '<unknown>'; } initDummyFileProvider(); /** * Make the statistics write to a given output directory. */ function initFileProvider(outputDirectory) { log_1.log.debug(`Initializing file provider for output directory ${outputDirectory}`); exports.statisticsFileProvider = new file_provider_1.StatisticFileProvider(outputDirectory); } /** * Either ignore the statistics or write them to a given map (e.g., for testing). * @param map - The map to write to, will not persist calls if no map is given */ function initDummyFileProvider(map) { exports.statisticsFileProvider = new file_provider_1.DummyAppendProvider(map); } /** * Append the content of all nodes to the storage file for the given feature * @param name - The name of the feature {@link Feature#name} * @param fn - The name of the feature-aspect to record * @param nodes - The nodes to append, you may pass already transformed string contents * @param context - The context of the information retrieval (e.g. the name of the file that contained the R source code) * @param unique - Should duplicate entries be removed on addition */ function appendStatisticsFile(name, fn, nodes, context, unique = false) { if (nodes.length === 0) { return; } let values; if (typeof nodes[0] === 'string') { values = nodes; } else if ('nodeType' in nodes[0]) { values = nodes.map(extractNodeContent); } else { values = nodes; } if (unique) { values = [...new Set(values)]; } values = values.map(value => JSON.stringify(context === undefined ? [value] : [value, context])); exports.statisticsFileProvider.append(name, fn, values.join('\n')); } //# sourceMappingURL=statistics-file.js.map