From 696c0c885ad1ee7c2046475562405c5bfd81012c Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 14 Feb 2024 08:35:57 -0600 Subject: [PATCH] fix: visitor and tests --- packages/dox/build/parser.js | 2 -- packages/dox/build/visitors.js | 49 ++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/dox/build/parser.js b/packages/dox/build/parser.js index 2b52434..d9b23fd 100644 --- a/packages/dox/build/parser.js +++ b/packages/dox/build/parser.js @@ -30,7 +30,6 @@ exports.parseNormalSource = async (path, source, root, request, options) => { const ast = await exports.parseCode(source, options); const buildFiles = []; const configs = []; - const hookBases = []; const hookImplementations = []; const hookInvocations = []; const todos = []; @@ -99,7 +98,6 @@ exports.parseNormalSource = async (path, source, root, request, options) => { return { buildFiles, config: configs, - hookBases, hookImplementations, hookInvocations, todos, diff --git a/packages/dox/build/visitors.js b/packages/dox/build/visitors.js index db90796..f77c6e5 100644 --- a/packages/dox/build/visitors.js +++ b/packages/dox/build/visitors.js @@ -24,6 +24,29 @@ function visitProperties(properties, fn) { }); } +// Test Flecks.hooks(require.context(...)) +function testRight(right, fn) { + if (isCallExpression(right)) { + if (isMemberExpression(right.callee)) { + if ( + isIdentifier(right.callee.object) && 'Flecks' === right.callee.object.name + && isIdentifier(right.callee.property) && 'hooks' === right.callee.property.name + ) { + if (isCallExpression(right.arguments[0])) { + if ( + isIdentifier(right.arguments[0].callee.object) + && 'require' === right.arguments[0].callee.object.name + && isIdentifier(right.arguments[0].callee.property) + && 'context' === right.arguments[0].callee.property.name + ) { + fn(right.arguments[0].arguments); + } + } + } + } + } +} + exports.hookBaseVisitor = (fn) => ({ // exports.hooks = Flecks.hooks(require.context(...)) AssignmentExpression(path) { @@ -31,39 +54,19 @@ exports.hookBaseVisitor = (fn) => ({ if (isMemberExpression(left)) { if (isIdentifier(left.object) && 'exports' === left.object.name) { if (isIdentifier(left.property) && 'hooks' === left.property.name) { - if (isCallExpression(right)) { - if (isMemberExpression(right.callee)) { - if ( - isIdentifier(right.callee.object) && 'Flecks' === right.callee.object.name - && isIdentifier(right.callee.property) && 'hooks' === right.callee.property.name - ) { - if (isCallExpression(right.arguments[0])) { - if ( - isIdentifier(right.arguments[0].callee.object) - && 'require' === right.arguments[0].callee.object.name - && isIdentifier(right.arguments[0].callee.property) - && 'context' === right.arguments[0].callee.property.name - ) { - fn(right.arguments[0].arguments); - } - } - } - } - } + testRight(right, fn); } } } }, - // export const hooks = Flecks.hooks(...) + // export const hooks = Flecks.hooks(require.context(...)) ExportNamedDeclaration(path) { const {declaration} = path.node; if (isVariableDeclaration(declaration)) { const {declarations} = declaration; declarations.forEach((declarator) => { if ('hooks' === declarator.id.name) { - if (isObjectExpression(declarator.init)) { - visitProperties(declarator.init.properties, fn); - } + testRight(declarator.init, fn); } }); }