@jqassistant/ts-lce
Version:
Tool to extract language concepts from a TypeScript codebase and export them to a JSON file.
33 lines (32 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactHookProcessor = void 0;
const utils_1 = require("@typescript-eslint/utils");
const concept_1 = require("../../core/concept");
const execution_condition_1 = require("../../core/execution-condition");
const processor_1 = require("../../core/processor");
const react_hook_concept_1 = require("../concepts/react-hook.concept");
const context_keys_1 = require("../../core/context.keys");
class ReactHookProcessor extends processor_1.Processor {
executionCondition = new execution_condition_1.ExecutionCondition([utils_1.AST_NODE_TYPES.VariableDeclarator], () => true);
postChildrenProcessing({ node, localContexts }) {
if (node.type === utils_1.AST_NODE_TYPES.VariableDeclarator &&
node.init &&
node.init.type === utils_1.AST_NODE_TYPES.CallExpression &&
node.init.callee.type === utils_1.AST_NODE_TYPES.Identifier &&
node.init.callee.name.startsWith("use")) {
const hookName = node.init.callee.name;
if (hookName === "useState" &&
node.id.type === utils_1.AST_NODE_TYPES.ArrayPattern &&
node.id.elements[0] &&
node.id.elements[0].type === utils_1.AST_NODE_TYPES.Identifier &&
node.id.elements[1] &&
node.id.elements[1].type === utils_1.AST_NODE_TYPES.Identifier) {
const fqn = localContexts.getNextContext(context_keys_1.CoreContextKeys.DEPENDENCY_GLOBAL_SOURCE_FQN)?.[0];
return (0, concept_1.singleEntryConceptMap)(react_hook_concept_1.LCEReactStateHook.conceptId, new react_hook_concept_1.LCEReactStateHook(fqn, node.id.elements[0].name, node.id.elements[1].name));
}
}
return new Map();
}
}
exports.ReactHookProcessor = ReactHookProcessor;