Could it really be that a traverse only visits the given node's children, not the node itself?

This commit is contained in:
Tony Garnock-Jones 2019-05-28 11:55:33 +01:00
parent 73e5ee0944
commit 2aee34d201
1 changed files with 30 additions and 14 deletions

View File

@ -199,21 +199,37 @@ function compilePattern(state, patternPath) {
} }
function instantiatePatternToPattern(state, patternPath) { function instantiatePatternToPattern(state, patternPath) {
patternPath.node = cloneDeep(patternPath.node);
patternPath.traverse({ function visitCallExpression(path) {
CallExpression(path) { if (!isCaptureIdentifier(path.node.callee)) return true;
if (isCaptureIdentifier(path.node.callee)) {
path.replaceWith(t.identifier(path.node.callee.name.slice(1))); path.replaceWith(t.identifier(path.node.callee.name.slice(1)));
path.skip(); path.skip();
return false;
} }
}, function visitIdentifier(path) {
Identifier(path) { if (!isCaptureIdentifier(path.node)) return true;
if (isCaptureIdentifier(path.node)) {
path.replaceWith(t.identifier(path.node.name.slice(1))); path.replaceWith(t.identifier(path.node.name.slice(1)));
path.skip(); path.skip();
return false;
} }
},
patternPath.node = cloneDeep(patternPath.node);
// OK I must have misunderstood something, because could it really
// be the case that traverse recursively visits all the CHILDREN of
// the given node but NOT THE GIVEN NODE ITSELF???
let doTraverse = true;
if (doTraverse && (patternPath.node.type === 'CallExpression')) {
doTraverse = visitCallExpression(patternPath);
}
if (doTraverse && (patternPath.node.type === 'Identifier')) {
doTraverse = visitIdentifier(patternPath);
}
if (doTraverse) {
patternPath.traverse({
CallExpression(path) { visitCallExpression(path) },
Identifier(path) { visitIdentifier(path) },
}); });
}
return patternPath.node; return patternPath.node;
} }