UNPKG

@jqassistant/ts-lce

Version:

Tool to extract language concepts from a TypeScript codebase and export them to a JSON file.

65 lines (64 loc) 2.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runTraverserForNode = runTraverserForNode; exports.runTraverserForNodes = runTraverserForNodes; const concept_1 = require("../concept"); const features_1 = require("../features"); /** * Tries to find an appropriate `Traverser` for the given node and calls its `traverse` method on the node. * * @param node child node of the node of the current processing context * @param traverserContext `TraverserContext` containing the property name of provided child node * @param parentNode/unusedProcessingContext current processing context * @param processors processors provided to a traverser * @param conceptMaps array of `ConceptMap`s to which extracted child concepts will be added under the property name of the parent node * @returns the concepts generated for the node and/or its children or `undefined` if no `Traverser` could be found */ function runTraverserForNode(node, traverserContext, { node: parentNode, ...unusedProcessingContext }, processors, conceptMaps) { const traverser = features_1.TRAVERSERS.get(node.type); if (traverser) { node.parent = parentNode; const result = traverser.traverse(traverserContext, { node, ...unusedProcessingContext }, processors); if (conceptMaps) conceptMaps.push((0, concept_1.unifyConceptMap)(result, traverserContext.parentPropName)); return result; } else { return undefined; } } /** * Runs`runTraverserForNode` for the given nodes. * Also provides index information of the parent node property to the traversers. * * @param nodes child nodes of the node of the current processing context * @param traverserContext `TraverserContext` containing the property name of provided child nodes * @param processingContext current processing context * @param processors processors provided to a traverser * @param conceptMaps array of `ConceptMap`s to which extracted child concepts will be added */ function runTraverserForNodes(nodes, { parentPropName }, processingContext, processors, conceptMaps) { const unifiedConcepts = []; let rawResults = new Map(); for (let i = 0; i < nodes.length; i++) { const n = nodes[i]; if (n) { const result = runTraverserForNode(n, { parentPropName, parentPropIndex: i }, processingContext, processors, unifiedConcepts); if (result) { rawResults = (0, concept_1.mergeConceptMaps)(result, rawResults); } } } if (unifiedConcepts.length > 0) { const mergedUnifiedConcepts = (0, concept_1.mergeConceptMaps)(...unifiedConcepts); if (conceptMaps) conceptMaps.push(mergedUnifiedConcepts); return rawResults; } else { return undefined; } }