@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
145 lines (124 loc) • 5.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RegisteredQueries = void 0;
exports.showQuery = showQuery;
exports.registerQueryDocumentation = registerQueryDocumentation;
exports.sliceQueryShorthand = sliceQueryShorthand;
exports.linkToQueryOfName = linkToQueryOfName;
exports.tocForQueryType = tocForQueryType;
exports.explainQueries = explainQueries;
const json_1 = require("../../util/json");
const ansi_1 = require("../../util/text/ansi");
const doc_files_1 = require("./doc-files");
const doc_dfg_1 = require("./doc-dfg");
const doc_code_1 = require("./doc-code");
const time_1 = require("../../util/text/time");
const query_print_1 = require("../../queries/query-print");
const flowr_analyzer_builder_1 = require("../../project/flowr-analyzer-builder");
const doc_cli_option_1 = require("./doc-cli-option");
/**
* Visualizes a query and its results in markdown format.
*/
async function showQuery(parser, code, queries, { showCode, collapseResult, collapseQuery, shorthand } = {}) {
const now = performance.now();
const analyzer = await new flowr_analyzer_builder_1.FlowrAnalyzerBuilder().setParser(parser).build();
analyzer.addRequest(code);
const results = await analyzer.query(queries);
const duration = performance.now() - now;
const metaInfo = `
The analysis required _${(0, time_1.printAsMs)(duration)}_ (including parsing and normalization and the query) within the generation environment.
`.trim();
const str = JSON.stringify(queries, json_1.jsonReplacer, collapseQuery ? ' ' : 2);
return `
${(0, doc_code_1.codeBlock)('json', collapseQuery ? str.split('\n').join(' ').replace(/([{[])\s{2,}/g, '$1 ').replace(/\s{2,}([\]}])/g, ' $1') : str)}
${(function () {
if ((queries.length === 1 && Object.keys(queries[0]).length === 1) || shorthand) {
return `(This can be shortened to \`@${queries[0].type}${shorthand ? ' ' + shorthand : ''}\` when used with the REPL command ${(0, doc_cli_option_1.getReplCommand)('query')}).`;
}
else {
return '';
}
})()}
${collapseResult ? ' <details> <summary style="color:gray">Show Results</summary>' : ''}
_Results (prettified and summarized):_
${await (0, query_print_1.asciiSummaryOfQueryResult)(ansi_1.markdownFormatter, duration, results, analyzer, queries)}
<details> <summary style="color:gray">Show Detailed Results as Json</summary>
${metaInfo}
In general, the JSON contains the Ids of the nodes in question as they are present in the normalized AST or the dataflow graph of flowR.
Please consult the [Interface](${doc_files_1.FlowrWikiBaseRef}/Interface) wiki page for more information on how to get those.
${(0, doc_code_1.jsonWithLimit)(results)}
</details>
${showCode ? `
<details> <summary style="color:gray">Original Code</summary>
${await (0, doc_dfg_1.printDfGraphForCode)(parser, code, { switchCodeAndGraph: true })}
</details>
` : ''}
${collapseResult ? '</details>' : ''}
`;
}
exports.RegisteredQueries = {
'active': new Map(),
'virtual': new Map()
};
/**
* Registers a new documentation for a query.
*/
function registerQueryDocumentation(query, doc) {
const map = exports.RegisteredQueries[doc.type];
if (map.has(query)) {
throw new Error(`Query ${query} already registered`);
}
map.set(query, doc);
}
/**
* Creates a REPL shorthand for the given slicing criteria and R code.
*/
function sliceQueryShorthand(criteria, code, forward) {
return `(${(criteria.join(';'))})${forward ? 'f' : ''} "${code}"`;
}
function linkify(name) {
return name.toLowerCase().replace(/ /g, '-');
}
/**
*
*/
function linkToQueryOfName(id) {
const query = exports.RegisteredQueries.active.get(id) ?? exports.RegisteredQueries.virtual.get(id);
if (!query) {
throw new Error(`Query ${id} not found`);
}
return `[${query.name}](#${linkify(query.name)})`;
}
/**
*
*/
function tocForQueryType(type) {
const queries = [...exports.RegisteredQueries[type].entries()].sort(([, { name: a }], [, { name: b }]) => a.localeCompare(b));
const result = [];
for (const [id, { name, shortDescription }] of queries) {
result.push(`1. [${name}](#${linkify(name)}) (\`${id}\`):\\\n ${shortDescription}`);
}
return result.join('\n');
}
async function explainQuery(shell, ctx, { name, functionName, functionFile, buildExplanation }) {
return `
### ${name}
${await buildExplanation(shell, ctx)}
<details>
<summary style="color:gray">Implementation Details</summary>
Responsible for the execution of the ${name} query is \`${functionName}\` in ${(0, doc_files_1.getFilePathMd)(functionFile)}.
</details>
`;
}
/**
*
*/
async function explainQueries(shell, ctx, type) {
const queries = [...exports.RegisteredQueries[type].entries()].sort(([, { name: a }], [, { name: b }]) => a.localeCompare(b));
const result = [];
for (const [, doc] of queries) {
result.push(await explainQuery(shell, ctx, doc));
}
return result.join(`\n${'-'.repeat(5)}\n\n`);
}
//# sourceMappingURL=doc-query.js.map