UNPKG

cannabis

Version:
62 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ts_simple_ast_extra_1 = require("ts-simple-ast-extra"); var adapter_1 = require("../adapter/adapter"); var util_1 = require("../adapter/util"); var config_1 = require("./config"); var file_1 = require("./file"); exports.TypeGuards = ts_simple_ast_extra_1.tsMorph.TypeGuards; /** * It will create and execute a new query defined by [[q]] on nodes defined by [[codeOrNode]] as follows. If * it's a string, then a new source file will be created with that content. If it's a ts.Node, then that node * will be used (internally creating a ts-morph node). If it's a ASTNode, it could be a Directory, a file or a * node and that will be used to issue the query. */ function queryAst(q, codeOrNode, options) { var timings = { parseAst: -1, compileQuery: -1, executeQuery: -1 }; var parseAstT0 = util_1.now(); var node = file_1.getFile(codeOrNode); timings.parseAst = util_1.now() - parseAstT0; var executeQueryT0 = -1; // TODO: query cache so we dont compile each time or astq does already have it ? try { var compileQueryT0 = util_1.now(); if (options) { config_1.setConfig(options); } var astq = adapter_1.getTypeScriptAstq(); var trace = config_1.getConfig('trace'); var query = astq.compile(q, trace); timings.compileQuery = util_1.now() - compileQueryT0; executeQueryT0 = util_1.now(); var result = astq.execute(node, query, config_1.getConfig('params'), trace); timings.executeQuery = util_1.now() - executeQueryT0; return { result: result, query: query, ast: node, timings: timings }; } catch (error) { timings.executeQuery = util_1.now() - executeQueryT0; return { error: error, ast: node, timings: timings }; } } exports.queryAst = queryAst; /** * Async version of [[queryAst]]. */ function queryAstAsync(q, codeOrNode, options) { return new Promise(function (resolve) { resolve(queryAst(q, codeOrNode, options)); }); } exports.queryAstAsync = queryAstAsync; //# sourceMappingURL=queryAst.js.map