From fedba5b2a2540b9dfc136911528c55b73c7dc97b Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 29 Aug 2016 17:18:43 +0200 Subject: [PATCH] Add fix for other types of nodes in referencePaths + (Close #122) + (Close #105) --- .../src/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/babel-plugin-minify-mangle-names/src/index.js b/packages/babel-plugin-minify-mangle-names/src/index.js index e045130cf..837a73ccc 100644 --- a/packages/babel-plugin-minify-mangle-names/src/index.js +++ b/packages/babel-plugin-minify-mangle-names/src/index.js @@ -163,9 +163,21 @@ module.exports = ({ types: t }) => { const path = refs[i]; const {node} = path; if (!path.isIdentifier()) { - // if this occurs, then it is - // probably an upstream bug (in babel) - throw new Error("Unexpected " + path.node.type + ". Expected an Identifier"); + // Ideally, this should not happen + // it happens in these places now - + // case 1: Export Statements + // This is a bug in babel + // https://github.com/babel/babel/pull/3629 + // case 2: Replacements in other plugins + // eg: https://github.com/babel/babili/issues/122 + // replacement in dce from `x` to `!x` gives referencePath as `!x` + path.traverse({ + ReferencedIdentifier(refPath) { + if (refPath.node.name === oldName && refPath.scope === scope) { + refPath.node.name = newName; + } + } + }); } if (!isLabelIdentifier(path)) { node.name = newName;