@jqassistant/ts-lce
Version:
Tool to extract language concepts from a TypeScript codebase and export them to a JSON file.
77 lines (76 loc) • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VariableDeclaratorProcessor = exports.VariableDeclarationProcessor = void 0;
const utils_1 = require("@typescript-eslint/utils");
const concept_1 = require("../concept");
const value_concept_1 = require("../concepts/value.concept");
const variable_declaration_concept_1 = require("../concepts/variable-declaration.concept");
const context_1 = require("../context");
const execution_condition_1 = require("../execution-condition");
const processor_1 = require("../processor");
const processor_utils_1 = require("../utils/processor.utils");
const variable_declaration_traverser_1 = require("../traversers/variable-declaration.traverser");
const dependency_resolution_processor_1 = require("./dependency-resolution.processor");
const type_utils_1 = require("./type.utils");
const code_coordinate_utils_1 = require("./code-coordinate.utils");
const context_keys_1 = require("../context.keys");
class VariableDeclarationProcessor extends processor_1.Processor {
static VARIABLE_DECLARATION_KIND_CONTEXT = "variable-declaration-type";
executionCondition = new execution_condition_1.ExecutionCondition([utils_1.AST_NODE_TYPES.VariableDeclaration], ({ node }) => {
return (!!node.parent &&
(node.parent.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration ||
node.parent.type === utils_1.AST_NODE_TYPES.ExportDefaultDeclaration ||
node.parent.type === utils_1.AST_NODE_TYPES.Program));
});
preChildrenProcessing({ localContexts, node }) {
if (node.type === utils_1.AST_NODE_TYPES.VariableDeclaration) {
localContexts.currentContexts.set(VariableDeclarationProcessor.VARIABLE_DECLARATION_KIND_CONTEXT, node.kind);
}
}
}
exports.VariableDeclarationProcessor = VariableDeclarationProcessor;
class VariableDeclaratorProcessor extends processor_1.Processor {
static VARIABLE_DECLARATOR_FQN_CONTEXT = "variable-declarator-fqn";
executionCondition = new execution_condition_1.ExecutionCondition([utils_1.AST_NODE_TYPES.VariableDeclarator], ({ localContexts }) => {
return !!localContexts.parentContexts?.get(VariableDeclarationProcessor.VARIABLE_DECLARATION_KIND_CONTEXT);
});
preChildrenProcessing({ localContexts, node }) {
if (node.type === utils_1.AST_NODE_TYPES.VariableDeclarator && node.id.type === utils_1.AST_NODE_TYPES.Identifier) {
if (node.init)
localContexts.currentContexts.set(context_keys_1.CoreContextKeys.VALUE_PROCESSING_FLAG, true);
localContexts.currentContexts.set(VariableDeclaratorProcessor.VARIABLE_DECLARATOR_FQN_CONTEXT, dependency_resolution_processor_1.DependencyResolutionProcessor.constructDeclarationFQN(localContexts, node.parent, node.id.name));
if (dependency_resolution_processor_1.DependencyResolutionProcessor.isDefaultDeclaration(localContexts, node.parent, node.id.name)) {
dependency_resolution_processor_1.DependencyResolutionProcessor.addScopeContext(localContexts, context_1.FQN.id("default"));
}
else {
dependency_resolution_processor_1.DependencyResolutionProcessor.addScopeContext(localContexts, context_1.FQN.id(node.id.name));
}
dependency_resolution_processor_1.DependencyResolutionProcessor.createDependencyIndex(localContexts);
}
}
postChildrenProcessing({ node, localContexts, globalContext, ...unusedProcessingContext }, childConcepts) {
// TODO: add destructuring assignment support
if (node.type === utils_1.AST_NODE_TYPES.VariableDeclarator && node.id.type === utils_1.AST_NODE_TYPES.Identifier) {
let init;
if (node.init) {
if (childConcepts.has(variable_declaration_traverser_1.VariableDeclaratorTraverser.INIT_PROP)) {
const values = (0, processor_utils_1.getAndDeleteAllValueChildConcepts)(variable_declaration_traverser_1.VariableDeclaratorTraverser.INIT_PROP, childConcepts);
if (values.length === 1) {
init = values[0];
}
}
else {
init = new value_concept_1.LCEValueComplex(globalContext.services.esTreeNodeToTSNodeMap.get(node.init).getText());
}
}
const name = node.id.name;
const fqn = localContexts.currentContexts.get(VariableDeclaratorProcessor.VARIABLE_DECLARATOR_FQN_CONTEXT);
dependency_resolution_processor_1.DependencyResolutionProcessor.registerDeclaration(localContexts, name, fqn, true);
const kind = localContexts.parentContexts?.get(VariableDeclarationProcessor.VARIABLE_DECLARATION_KIND_CONTEXT);
const varDecl = new variable_declaration_concept_1.LCEVariableDeclaration(name, fqn, kind, (0, type_utils_1.parseESNodeType)({ node, localContexts, globalContext, ...unusedProcessingContext }, node, name), init, code_coordinate_utils_1.CodeCoordinateUtils.getCodeCoordinates(globalContext, node, true));
return (0, concept_1.mergeConceptMaps)((0, concept_1.singleEntryConceptMap)(variable_declaration_concept_1.LCEVariableDeclaration.conceptId, varDecl), dependency_resolution_processor_1.DependencyResolutionProcessor.getRegisteredDependencies(localContexts));
}
return new Map();
}
}
exports.VariableDeclaratorProcessor = VariableDeclaratorProcessor;