better typeof symbol transformer

This commit is contained in:
Sebastian McKenzie
2015-01-15 02:24:32 +11:00
parent 44f06c0b4c
commit b60eca0a76
2 changed files with 14 additions and 2 deletions

View File

@@ -3,7 +3,19 @@ var t = require("../../types");
exports.optional = true;
exports.UnaryExpression = function (node, parent, file) {
this.skip();
if (node.operator === "typeof") {
return t.callExpression(file.addHelper("typeof"), [node.argument]);
var call = t.callExpression(file.addHelper("typeof"), [node.argument]);
if (t.isIdentifier(node.argument)) {
var undefLiteral = t.literal("undefined");
return t.conditionalExpression(
t.binaryExpression("===", t.unaryExpression("typeof", node.argument), undefLiteral),
undefLiteral,
call
);
} else {
return call;
}
}
};