feat: optional chaining with function call
This commit is contained in:
parent
ed15443dba
commit
6bd3bf4d2f
@ -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);
|
||||
},
|
||||
|
||||
@ -0,0 +1 @@
|
||||
foo?.bar()
|
||||
@ -0,0 +1,3 @@
|
||||
var _temp;
|
||||
|
||||
((_temp = foo) != null ? _temp.bar : Function())();
|
||||
Loading…
x
Reference in New Issue
Block a user