From 40fdd2a828e64bc2fdcad9776f7c71abc1e126a9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 10 Nov 2014 00:51:46 +1100 Subject: [PATCH] dry up types.getIds --- lib/6to5/types/index.js | 51 +++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 175fa2c117..5938db953f 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -162,31 +162,17 @@ t.getIds = function (node, map) { while (search.length) { var id = search.shift(); + if (!id) continue; + + var nodeKey = t.getIds.nodes[id.type]; + var arrKey = t.getIds.arrays[id.type]; if (t.isIdentifier(id)) { ids[id.name] = id; - } else if (t.isArrayPattern(id)) { - _.each(id.elements, function (elem) { - search.push(elem); - }); - } else if (t.isAssignmentExpression(id)) { - search.push(id.left); - } else if (t.isObjectPattern(id)) { - _.each(id.properties, function (prop) { - search.push(prop.value); - }); - } else if (t.isVariableDeclaration(id)) { - search = search.concat(id.declarations); - } else if (t.isImportSpecifier(id) || t.isExportSpecifier(id) || t.isVariableDeclarator(id) || t.isFunctionDeclaration(id) || t.isClassDeclaration(id)) { - search.push(id.id); - } else if (t.isSpreadElement(id)) { - search.push(id.argument); - } else if (t.isExportDeclaration(id) || t.isImportDeclaration(id)) { - search = search.concat(id.specifiers); - } else if (t.isMemberExpression(id)) { - search.push(id.object); - } else if (t.isParenthesizedExpression(id)) { - search.push(id.expression); + } else if (nodeKey) { + if (id[nodeKey]) search.push(id[nodeKey]); + } else if (arrKey) { + search = search.concat(id[arrKey] || []); } } @@ -194,6 +180,27 @@ t.getIds = function (node, map) { return ids; }; +t.getIds.nodes = { + AssignmentExpression: "left", + ImportSpecifier: "id", + ExportSpecifier: "id", + VariableDeclarator: "id", + FunctionDeclaration: "id", + ClassDeclaration: "id", + ParenthesizedExpression: "expression", + MemeberExpression: "object", + SpreadElement: "argument", + Property: "value" +}; + +t.getIds.arrays = { + ExportDeclaration: "specifiers", + ImportDeclaration: "specifiers", + VariableDeclaration: "declarations", + ArrayPattern: "elements", + ObjectPattern: "properties" +}; + t.inherits = function (child, parent) { child.loc = parent.loc; child.end = parent.end;