eval-estree-expression icon indicating copy to clipboard operation
eval-estree-expression copied to clipboard

Enable toplevel await

Open Attacler opened this issue 2 years ago • 2 comments

Hi there,

Is it possible to add toplevel await? Right now babel says the following, so im not sure if its supported:

await' is only allowed within async functions and at the top levels of modules. (1:12)

The code:

"use strict";

const start = Date.now();
process.on("exit", () => console.log('Time: ' + Date.now() - start + 'ms'));

// const esprima = require('esprima');
const babel = require("@babel/parser");
const { evaluate } = require("eval-estree-expression");
const { generate } = require("escodegen");

// const parse = (input, options) => esprima.parse(input).body[0].expression;
const parse = (input, options) => babel.parseExpression(input, options);

const context = {
  n: 6,
  foo: async function (x) {
    return (await x) * 100;
  },
  bar: async (x, y) => (await x) * 100 * y,
  obj: { x: { y: 555 } },
  z: 3,
  console,
};

const input = 'console.log(await foo(1));';
const tree = parse(input);

evaluate(tree, context, { functions: true, generate })
  .then(console.log)
  .catch(console.error);

Attacler avatar Sep 27 '21 08:09 Attacler