added instanceOf plugin to preset es2015 (#6257)

* added instanceOf plugin to preset es2015

* fixed test cases
This commit is contained in:
Ajay Narain Mathur
2017-09-18 07:33:42 +10:00
committed by Justin Ridgewell
parent f5ad86c5c6
commit 3cdb7d7f0f
18 changed files with 58 additions and 22 deletions

View File

@@ -4,12 +4,20 @@ export default function({ types: t }) {
BinaryExpression(path) {
const { node } = path;
if (node.operator === "instanceof") {
path.replaceWith(
t.callExpression(this.addHelper("instanceof"), [
node.left,
node.right,
]),
);
const helper = this.addHelper("instanceof");
const isUnderHelper = path.findParent(path => {
return (
(path.isVariableDeclarator() && path.node.id === helper) ||
(path.isFunctionDeclaration() &&
path.node.id.name === helper.name)
);
});
if (isUnderHelper) {
return;
} else {
path.replaceWith(t.callExpression(helper, [node.left, node.right]));
}
}
},
},