feat: optional chaining with function call

This commit is contained in:
Sven SAULEAU 2017-04-29 20:15:11 +02:00 committed by Justin Ridgewell
parent ed15443dba
commit 6bd3bf4d2f
3 changed files with 24 additions and 6 deletions

View File

@ -22,6 +22,7 @@ export default function ({ types: t }) {
return {
visitor: {
MemberExpression(path, state) {
if (!isNodeOptional(path.node)) {
return;
@ -36,12 +37,25 @@ export default function ({ types: t }) {
path.scope.push({ id });
}
const remplacement = createCondition(
state.optionalTemp,
object,
property,
t.identifier("undefined")
);
let remplacement;
if (t.isCallExpression(path.parent)) {
remplacement = createCondition(
state.optionalTemp,
object,
property,
t.callExpression(t.identifier("Function"), [])
);
} else {
remplacement = createCondition(
state.optionalTemp,
object,
property,
t.identifier("undefined")
);
}
path.replaceWith(remplacement);
},

View File

@ -0,0 +1,3 @@
var _temp;
((_temp = foo) != null ? _temp.bar : Function())();