From 1d080da23b4721dbcefbe82c24be5f22eccc41d1 Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Tue, 16 Mar 2021 20:39:32 -0700 Subject: [PATCH] wip --- lib/colocated-babel-plugin.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/colocated-babel-plugin.js b/lib/colocated-babel-plugin.js index 51de9c1a..f34fa63e 100644 --- a/lib/colocated-babel-plugin.js +++ b/lib/colocated-babel-plugin.js @@ -1,3 +1,25 @@ +function registerRefs(newPath, getRefPaths) { + if (Array.isArray(newPath)) { + if (newPath.length > 1) { + throw new Error( + 'registerRefs is only meant to handle single node transformations. Received more than one path node.' + ); + } + + newPath = newPath[0]; + } + + let refPaths = getRefPaths(newPath); + + for (let ref of refPaths) { + let binding = ref.scope.getBinding(ref.node.name); + if (binding !== undefined) { + binding.reference(ref); + } + } +} + + // For ease of debuggin / tweaking: // https://astexplorer.net/#/gist/bcca584efdab6c981a75618642c76a22/1e1d262eaeb47b7da66150e0781a02b96e597b25 module.exports = function (babel) { @@ -39,7 +61,7 @@ module.exports = function (babel) { .find(({ node }) => { return exportName === 'default' ? t.isImportDefaultSpecifier(node) - : node.imported.name === exportName; + : node.imported && node.imported.name === exportName; }); if (importSpecifier) {