From b60eca0a7693d2571bbb488fad11520ac8af13c0 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 15 Jan 2015 02:24:32 +1100 Subject: [PATCH] better typeof symbol transformer --- .../transformers/optional-typeof-symbol.js | 14 +++++++++++++- .../optional-typeof-symbol/basic/expected.js | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/6to5/transformation/transformers/optional-typeof-symbol.js b/lib/6to5/transformation/transformers/optional-typeof-symbol.js index 55be121b08..36b8458936 100644 --- a/lib/6to5/transformation/transformers/optional-typeof-symbol.js +++ b/lib/6to5/transformation/transformers/optional-typeof-symbol.js @@ -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; + } } }; diff --git a/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js b/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js index c33ed64571..a93f2923c6 100644 --- a/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js +++ b/test/fixtures/transformation/optional-typeof-symbol/basic/expected.js @@ -5,4 +5,4 @@ var _typeof = function (obj) { }; var s = Symbol("s"); -assert.equal(_typeof(s), "symbol"); +assert.equal(typeof s === "undefined" ? "undefined" : _typeof(s), "symbol");