Skip to content

Commit

Permalink
fix: visitor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cha0s committed Feb 14, 2024
1 parent 477bb74 commit 696c0c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
2 changes: 0 additions & 2 deletions packages/dox/build/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -99,7 +98,6 @@ exports.parseNormalSource = async (path, source, root, request, options) => {
return {
buildFiles,
config: configs,
hookBases,
hookImplementations,
hookInvocations,
todos,
Expand Down
49 changes: 26 additions & 23 deletions packages/dox/build/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,49 @@ 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) {
const {left, right} = path.node;
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);
}
});
}
Expand Down

0 comments on commit 696c0c8

Please sign in to comment.