From 737c34977558cc61be522388a55000c7c2d1d770 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 13 Oct 2014 05:34:18 +1100 Subject: [PATCH] add util.isReferenced to check if a node is directly referenced --- lib/6to5/util.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 4e042bc748..b7016ad0d9 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -150,6 +150,24 @@ exports.template = function (name, nodes, keepExpression) { } }; +exports.isReferenced = function (node, parent) { + // we're a property key + if (parent.type === "Property" && parent.key === node) return false; + + var isMemberExpression = parent.type === "MemberExpression"; + + // we're in a member expression and we're the computed property so we're referenced + var isComputedProperty = isMemberExpression && parent.property === node && parent.computed; + + // we're in a member expression and we're the object so we're referenced + var isObject = isMemberExpression && parent.object === node; + + // we are referenced + if (!isMemberExpression || isComputedProperty || isObject) return true; + + return false; +}; + exports.codeFrame = function (lines, lineNumber, colNumber) { colNumber = Math.max(colNumber, 0);