eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

prefer-top-level-await flags code that can't be top-level-awaited

Open VlaDexa opened this issue 2 years ago • 2 comments

In class definitions top-level-await is not available but this rule still marks the code as wrong

The rule: unicorn/prefer-top-level-await

class Example {
    property = Promise // Can't await here
        .resolve(1)
        .then(i => i+1); // Error here <- Prefer top-level await over using a promise chain.
}

VlaDexa avatar Jul 17 '22 16:07 VlaDexa

I'm confused, I can't find anything forbid TLA in class member, the spec, acorn PR, MDN.

But Node.js/browsers/parsers all(except @typescript-eslint/parser which can't be trust) failed to pase class A {foo = await 1}.

So the real question is where the TLA expression can be?

fisker avatar Sep 19 '22 08:09 fisker

Because when parsing class member, the parser enters a non-async function? Need more time to learn the spec.

fisker avatar Sep 19 '22 08:09 fisker

Maybe the Initializer which runs for ClassFields is a part of the constructor which doesn't allow async?

VlaDexa avatar Sep 25 '22 22:09 VlaDexa