move instanceof functionality to separate plugin - fixes #2745

This commit is contained in:
Sebastian McKenzie 2015-11-03 19:22:51 +00:00
parent 1c3b4aa410
commit a9ac3b0c94
8 changed files with 40 additions and 16 deletions

View File

@ -0,0 +1,3 @@
{
"plugins": ["external-helpers-2", "transform-es2015-instanceof"]
}

View File

@ -181,7 +181,7 @@ export let inherits = template(`
export let _instanceof = template(`
(function (left, right) {
if (right != null && right[Symbol.hasInstance]) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return right[Symbol.hasInstance](left);
} else {
return left instanceof right;

View File

@ -0,0 +1,3 @@
src
test
node_modules

View File

@ -0,0 +1,14 @@
{
"name": "babel-plugin-transform-es2015-typeof-symbol",
"version": "6.0.15",
"description": "",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-typeof-symbol",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"babel-runtime": "^5.0.0"
}
}

View File

@ -0,0 +1,12 @@
export default function ({ types: t }) {
return {
visitor: {
BinaryExpression(path) {
let { node } = path;
if (node.operator === "instanceof") {
path.replaceWith(t.callExpression(this.addHelper("instanceof"), [node.left, node.right]));
}
}
}
};
}

View File

@ -1,8 +1,8 @@
{
"name": "babel-plugin-transform-es2015-typeof-symbol",
"name": "babel-plugin-transform-es2015-instanceof",
"version": "6.0.15",
"description": "",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-typeof-symbol",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-instanceof",
"license": "MIT",
"main": "lib/index.js",
"keywords": [

View File

@ -1,9 +1,12 @@
export default function ({ types: t }) {
let IGNORE = Symbol();
return {
visitor: {
UnaryExpression(path) {
let { node, parent } = path;
if (node._ignoreSpecSymbols) return;
if (node[IGNORE]) return;
if (path.find(path => !!path.node._generated)) return;
if (path.parentPath.isBinaryExpression() && t.EQUALITY_BINARY_OPERATORS.indexOf(parent.operator) >= 0) {
// optimise `typeof foo === "string"` since we can determine that they'll never need to handle symbols
@ -18,7 +21,7 @@ export default function ({ types: t }) {
if (path.get("argument").isIdentifier()) {
let undefLiteral = t.stringLiteral("undefined");
let unary = t.unaryExpression("typeof", node.argument);
unary._ignoreSpecSymbols = true;
unary[IGNORE] = true;
path.replaceWith(t.conditionalExpression(
t.binaryExpression("===", unary, undefLiteral),
undefLiteral,
@ -28,17 +31,6 @@ export default function ({ types: t }) {
path.replaceWith(call);
}
}
},
BinaryExpression(path) {
let { node } = path;
if (node.operator === "instanceof") {
path.replaceWith(t.callExpression(this.addHelper("instanceof"), [node.left, node.right]));
}
},
"VariableDeclaration|FunctionDeclaration"(path) {
if (path.node._generated) path.skip();
}
}
};