From f3acedbf08565a7b0b3796f6ef6fb95150359602 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 5 Jun 2015 08:43:41 +0100 Subject: [PATCH] enable es6.spec.symbols by default --- .../transformers/deprecated.json | 4 +- .../transformers/es6/spec.symbols.js | 37 ---------------- .../transformers/es6/symbols.js | 44 +++++++++++++++++++ .../transformation/transformers/index.js | 2 +- .../es6.spec.symbols/options.json | 4 -- .../instanceof/actual.js | 0 .../instanceof/exec.js | 0 .../instanceof/expected.js | 0 .../transformation/es6.symbols/options.json | 3 ++ .../typeof/actual.js | 0 .../typeof/exec.js | 0 .../typeof/expected.js | 0 12 files changed, 50 insertions(+), 44 deletions(-) delete mode 100644 src/babel/transformation/transformers/es6/spec.symbols.js create mode 100644 src/babel/transformation/transformers/es6/symbols.js delete mode 100644 test/core/fixtures/transformation/es6.spec.symbols/options.json rename test/core/fixtures/transformation/{es6.spec.symbols => es6.symbols}/instanceof/actual.js (100%) rename test/core/fixtures/transformation/{es6.spec.symbols => es6.symbols}/instanceof/exec.js (100%) rename test/core/fixtures/transformation/{es6.spec.symbols => es6.symbols}/instanceof/expected.js (100%) create mode 100644 test/core/fixtures/transformation/es6.symbols/options.json rename test/core/fixtures/transformation/{es6.spec.symbols => es6.symbols}/typeof/actual.js (100%) rename test/core/fixtures/transformation/{es6.spec.symbols => es6.symbols}/typeof/exec.js (100%) rename test/core/fixtures/transformation/{es6.spec.symbols => es6.symbols}/typeof/expected.js (100%) diff --git a/src/babel/transformation/transformers/deprecated.json b/src/babel/transformation/transformers/deprecated.json index 3677c988f5..4774ad0c40 100644 --- a/src/babel/transformation/transformers/deprecated.json +++ b/src/babel/transformation/transformers/deprecated.json @@ -2,11 +2,11 @@ "selfContained": "runtime", "unicode-regex": "regex.unicode", "spec.typeofSymbol": "es6.spec.symbols", - "es6.symbols": "es6.spec.symbols", + "es6.spec.symbols": "es6.symbols", "es6.blockScopingTDZ": "es6.spec.blockScoping", "utility.inlineExpressions": "minification.constantFolding", "utility.deadCodeElimination": "minification.deadCodeElimination", "utility.removeConsoleCalls": "minification.removeConsole", - "utility.removeDebugger": "minification.removeDebugger" + "utility.removeDebugger": "minification.removeDebugger", } diff --git a/src/babel/transformation/transformers/es6/spec.symbols.js b/src/babel/transformation/transformers/es6/spec.symbols.js deleted file mode 100644 index 953b29a18f..0000000000 --- a/src/babel/transformation/transformers/es6/spec.symbols.js +++ /dev/null @@ -1,37 +0,0 @@ -import * as t from "../../../types"; - -export var metadata = { - optional: true -}; - -export function UnaryExpression(node, parent, scope, file) { - if (node._ignoreSpecSymbols) return; - - if (node.operator === "typeof") { - var call = t.callExpression(file.addHelper("typeof"), [node.argument]); - if (this.get("argument").isIdentifier()) { - var undefLiteral = t.literal("undefined"); - var unary = t.unaryExpression("typeof", node.argument); - unary._ignoreSpecSymbols = true; - return t.conditionalExpression( - t.binaryExpression("===", unary, undefLiteral), - undefLiteral, - call - ); - } else { - return call; - } - } -} - -export function BinaryExpression(node, parent, scope, file) { - if (node.operator === "instanceof") { - return t.callExpression(file.addHelper("instanceof"), [node.left, node.right]); - } -} - -export function VariableDeclaration(node) { - if (node._generated) this.skip(); -} - -export { VariableDeclaration as FunctionDeclaration }; diff --git a/src/babel/transformation/transformers/es6/symbols.js b/src/babel/transformation/transformers/es6/symbols.js new file mode 100644 index 0000000000..7e071a9129 --- /dev/null +++ b/src/babel/transformation/transformers/es6/symbols.js @@ -0,0 +1,44 @@ +import * as t from "../../../types"; + +export var metadata = { + group: "builtin-pre" +}; + +const BINARY_COMPARISON_OPERATORS = ["!=", "==", "!==", "==="]; + +export function UnaryExpression(node, parent, scope, file) { + if (node._ignoreSpecSymbols || node.operator !== "typeof") return; + + if (t.isBinaryExpression(parent) && BINARY_COMPARISON_OPERATORS.indexOf(parent.operator) >= 0 && t.isLiteral(parent.right)) { + // don't touch binary comparisons with a string that doesn't equal symbol, we can statically determine + // that this condition will never be met which means faster code! + if (parent.right.value !== "symbol") return; + } + + + var call = t.callExpression(file.addHelper("typeof"), [node.argument]); + if (this.get("argument").isIdentifier()) { + var undefLiteral = t.literal("undefined"); + var unary = t.unaryExpression("typeof", node.argument); + unary._ignoreSpecSymbols = true; + return t.conditionalExpression( + t.binaryExpression("===", unary, undefLiteral), + undefLiteral, + call + ); + } else { + return call; + } +} + +export function BinaryExpression(node, parent, scope, file) { + if (node.operator === "instanceof") { + return t.callExpression(file.addHelper("instanceof"), [node.left, node.right]); + } +} + +export function VariableDeclaration(node) { + if (node._generated) this.skip(); +} + +export { VariableDeclaration as FunctionDeclaration }; diff --git a/src/babel/transformation/transformers/index.js b/src/babel/transformation/transformers/index.js index 903bd084b1..b9a98fdefa 100644 --- a/src/babel/transformation/transformers/index.js +++ b/src/babel/transformation/transformers/index.js @@ -16,6 +16,7 @@ export default { "spec.functionName": require("./spec/function-name"), "es6.spec.templateLiterals": require("./es6/spec.template-literals"), "es6.templateLiterals": require("./es6/template-literals"), + "es6.symbols": require("./es6/symbols"), //- builtin-basic // this is where the bulk of the ES6 transformations take place, none of them require traversal state @@ -51,7 +52,6 @@ export default { "es7.exportExtensions": require("./es7/export-extensions"), "spec.protoToAssign": require("./spec/proto-to-assign"), "es7.doExpressions": require("./es7/do-expressions"), - "es6.spec.symbols": require("./es6/spec.symbols"), "es7.functionBind": require("./es7/function-bind"), "spec.undefinedToVoid": require("./spec/undefined-to-void"), diff --git a/test/core/fixtures/transformation/es6.spec.symbols/options.json b/test/core/fixtures/transformation/es6.spec.symbols/options.json deleted file mode 100644 index e007f6a10a..0000000000 --- a/test/core/fixtures/transformation/es6.spec.symbols/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "externalHelpers": true, - "optional": ["es6.spec.symbols"] -} diff --git a/test/core/fixtures/transformation/es6.spec.symbols/instanceof/actual.js b/test/core/fixtures/transformation/es6.symbols/instanceof/actual.js similarity index 100% rename from test/core/fixtures/transformation/es6.spec.symbols/instanceof/actual.js rename to test/core/fixtures/transformation/es6.symbols/instanceof/actual.js diff --git a/test/core/fixtures/transformation/es6.spec.symbols/instanceof/exec.js b/test/core/fixtures/transformation/es6.symbols/instanceof/exec.js similarity index 100% rename from test/core/fixtures/transformation/es6.spec.symbols/instanceof/exec.js rename to test/core/fixtures/transformation/es6.symbols/instanceof/exec.js diff --git a/test/core/fixtures/transformation/es6.spec.symbols/instanceof/expected.js b/test/core/fixtures/transformation/es6.symbols/instanceof/expected.js similarity index 100% rename from test/core/fixtures/transformation/es6.spec.symbols/instanceof/expected.js rename to test/core/fixtures/transformation/es6.symbols/instanceof/expected.js diff --git a/test/core/fixtures/transformation/es6.symbols/options.json b/test/core/fixtures/transformation/es6.symbols/options.json new file mode 100644 index 0000000000..7d6e6cda1c --- /dev/null +++ b/test/core/fixtures/transformation/es6.symbols/options.json @@ -0,0 +1,3 @@ +{ + "externalHelpers": true +} diff --git a/test/core/fixtures/transformation/es6.spec.symbols/typeof/actual.js b/test/core/fixtures/transformation/es6.symbols/typeof/actual.js similarity index 100% rename from test/core/fixtures/transformation/es6.spec.symbols/typeof/actual.js rename to test/core/fixtures/transformation/es6.symbols/typeof/actual.js diff --git a/test/core/fixtures/transformation/es6.spec.symbols/typeof/exec.js b/test/core/fixtures/transformation/es6.symbols/typeof/exec.js similarity index 100% rename from test/core/fixtures/transformation/es6.spec.symbols/typeof/exec.js rename to test/core/fixtures/transformation/es6.symbols/typeof/exec.js diff --git a/test/core/fixtures/transformation/es6.spec.symbols/typeof/expected.js b/test/core/fixtures/transformation/es6.symbols/typeof/expected.js similarity index 100% rename from test/core/fixtures/transformation/es6.spec.symbols/typeof/expected.js rename to test/core/fixtures/transformation/es6.symbols/typeof/expected.js