From 2aee34d2019d3b84e3b77142e7eac3e110b77930 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 28 May 2019 11:55:33 +0100 Subject: [PATCH] Could it really be that a traverse only visits the given node's children, not the node itself? --- packages/syntax/src/plugin.js | 44 ++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/syntax/src/plugin.js b/packages/syntax/src/plugin.js index f3ee8ee..6392423 100644 --- a/packages/syntax/src/plugin.js +++ b/packages/syntax/src/plugin.js @@ -199,21 +199,37 @@ function compilePattern(state, patternPath) { } function instantiatePatternToPattern(state, patternPath) { + + function visitCallExpression(path) { + if (!isCaptureIdentifier(path.node.callee)) return true; + path.replaceWith(t.identifier(path.node.callee.name.slice(1))); + path.skip(); + return false; + } + function visitIdentifier(path) { + if (!isCaptureIdentifier(path.node)) return true; + path.replaceWith(t.identifier(path.node.name.slice(1))); + path.skip(); + return false; + } + patternPath.node = cloneDeep(patternPath.node); - patternPath.traverse({ - CallExpression(path) { - if (isCaptureIdentifier(path.node.callee)) { - path.replaceWith(t.identifier(path.node.callee.name.slice(1))); - path.skip(); - } - }, - Identifier(path) { - if (isCaptureIdentifier(path.node)) { - path.replaceWith(t.identifier(path.node.name.slice(1))); - path.skip(); - } - }, - }); + // 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; }