move instanceof functionality to separate plugin - fixes #2745
This commit is contained in:
parent
1c3b4aa410
commit
a9ac3b0c94
3
packages/babel-core/test/fixtures/transformation/es6.spec.symbols/instanceof/options.json
vendored
Normal file
3
packages/babel-core/test/fixtures/transformation/es6.spec.symbols/instanceof/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["external-helpers-2", "transform-es2015-instanceof"]
|
||||||
|
}
|
||||||
@ -181,7 +181,7 @@ export let inherits = template(`
|
|||||||
|
|
||||||
export let _instanceof = template(`
|
export let _instanceof = template(`
|
||||||
(function (left, right) {
|
(function (left, right) {
|
||||||
if (right != null && right[Symbol.hasInstance]) {
|
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
||||||
return right[Symbol.hasInstance](left);
|
return right[Symbol.hasInstance](left);
|
||||||
} else {
|
} else {
|
||||||
return left instanceof right;
|
return left instanceof right;
|
||||||
|
|||||||
@ -0,0 +1,3 @@
|
|||||||
|
src
|
||||||
|
test
|
||||||
|
node_modules
|
||||||
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "babel-plugin-transform-es2015-typeof-symbol",
|
"name": "babel-plugin-transform-es2015-instanceof",
|
||||||
"version": "6.0.15",
|
"version": "6.0.15",
|
||||||
"description": "",
|
"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",
|
"license": "MIT",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|||||||
@ -1,9 +1,12 @@
|
|||||||
export default function ({ types: t }) {
|
export default function ({ types: t }) {
|
||||||
|
let IGNORE = Symbol();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
visitor: {
|
visitor: {
|
||||||
UnaryExpression(path) {
|
UnaryExpression(path) {
|
||||||
let { node, parent } = 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) {
|
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
|
// 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()) {
|
if (path.get("argument").isIdentifier()) {
|
||||||
let undefLiteral = t.stringLiteral("undefined");
|
let undefLiteral = t.stringLiteral("undefined");
|
||||||
let unary = t.unaryExpression("typeof", node.argument);
|
let unary = t.unaryExpression("typeof", node.argument);
|
||||||
unary._ignoreSpecSymbols = true;
|
unary[IGNORE] = true;
|
||||||
path.replaceWith(t.conditionalExpression(
|
path.replaceWith(t.conditionalExpression(
|
||||||
t.binaryExpression("===", unary, undefLiteral),
|
t.binaryExpression("===", unary, undefLiteral),
|
||||||
undefLiteral,
|
undefLiteral,
|
||||||
@ -28,17 +31,6 @@ export default function ({ types: t }) {
|
|||||||
path.replaceWith(call);
|
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user