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(`
|
||||
(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;
|
||||
|
||||
@ -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",
|
||||
"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": [
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user