fix: eval?.() is indirect (#11850)

This commit is contained in:
Huáng Jùnliàng 2020-07-20 17:06:27 -04:00 committed by GitHub
parent 238cadda4c
commit e51a91e131
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 1 deletions

View File

@ -78,7 +78,11 @@ export default declare((api, options) => {
let ref; let ref;
let check; let check;
if (loose && isCall && isSimpleMemberExpression(chain)) { if (isCall && t.isIdentifier(chain, { name: "eval" })) {
check = ref = chain;
// `eval?.()` is an indirect eval call transformed to `(0,eval)()`
node[replaceKey] = t.sequenceExpression([t.numericLiteral(0), ref]);
} else if (loose && isCall && isSimpleMemberExpression(chain)) {
// If we are using a loose transform (avoiding a Function#call) and we are at the call, // If we are using a loose transform (avoiding a Function#call) and we are at the call,
// we can avoid a needless memoize. We only do this if the callee is a simple member // we can avoid a needless memoize. We only do this if the callee is a simple member
// expression, to avoid multiple calls to nested call expressions. // expression, to avoid multiple calls to nested call expressions.

View File

@ -0,0 +1,22 @@
var foo;
/* indirect eval calls */
eval?.(foo);
(eval)?.(foo);
eval?.()();
eval?.().foo;
/* direct eval calls */
eval()?.();
eval()?.foo;
/* plain function calls */
foo.eval?.(foo);
eval.foo?.(foo);

View File

@ -0,0 +1,3 @@
{
"plugins": [["proposal-optional-chaining", { "loose": true }]]
}

View File

@ -0,0 +1,17 @@
var _eval, _eval2;
var foo;
/* indirect eval calls */
eval == null ? void 0 : (0, eval)(foo);
eval == null ? void 0 : (0, eval)(foo);
eval == null ? void 0 : (0, eval)()();
eval == null ? void 0 : (0, eval)().foo;
/* direct eval calls */
(_eval = eval()) == null ? void 0 : _eval();
(_eval2 = eval()) == null ? void 0 : _eval2.foo;
/* plain function calls */
foo.eval == null ? void 0 : foo.eval(foo);
eval.foo == null ? void 0 : eval.foo(foo);

View File

@ -0,0 +1,22 @@
var foo;
/* indirect eval calls */
eval?.(foo);
(eval)?.(foo);
eval?.()();
eval?.().foo;
/* direct eval calls */
eval()?.();
eval()?.foo;
/* plain function calls */
foo.eval?.(foo);
eval.foo?.(foo);

View File

@ -0,0 +1,17 @@
var _eval, _eval2, _foo$eval, _eval$foo;
var foo;
/* indirect eval calls */
eval === null || eval === void 0 ? void 0 : (0, eval)(foo);
eval === null || eval === void 0 ? void 0 : (0, eval)(foo);
eval === null || eval === void 0 ? void 0 : (0, eval)()();
eval === null || eval === void 0 ? void 0 : (0, eval)().foo;
/* direct eval calls */
(_eval = eval()) === null || _eval === void 0 ? void 0 : _eval();
(_eval2 = eval()) === null || _eval2 === void 0 ? void 0 : _eval2.foo;
/* plain function calls */
(_foo$eval = foo.eval) === null || _foo$eval === void 0 ? void 0 : _foo$eval.call(foo, foo);
(_eval$foo = eval.foo) === null || _eval$foo === void 0 ? void 0 : _eval$foo.call(eval, foo);