Skip to content

New Rule: no-top-level-await #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
DMartens opened this issue Apr 2, 2025 · 2 comments · Fixed by #440
Closed
1 task done

New Rule: no-top-level-await #439

DMartens opened this issue Apr 2, 2025 · 2 comments · Fixed by #440
Labels
enhancement help wanted rule:new A new rule for the package

Comments

@DMartens
Copy link

DMartens commented Apr 2, 2025

Rule details

It would warn about using top-level awaits as it conflicts with require(esm).
Unfortunately no-restricted-syntax cannot be used as the following should be ignored:

  • files with a shebang or without exports
  • files called from package.json scripts

What type of rule is this?

Warns about a potential problem

Example code

await fn();

for await (const x of asyncGenerator()) {
}

Participation

  • I am willing to submit a pull request to implement this rule.

Additional comments

This rule was discussed in the eslint repo but declined with a reference to open here.

@scagood scagood added enhancement help wanted rule:new A new rule for the package labels Apr 5, 2025
@scagood
Copy link

scagood commented Apr 5, 2025

I like this idea, I think it'd be best to keep it reasonably simple at the start though (eg detecting cjs/esm and erroring only for cjs)

ignores is always a nice option to have :)

@voxpelli
Copy link
Member

voxpelli commented Apr 5, 2025

It's in ESM files it should error, as any ESM file with a top level await can't be used with the new ESM-support in require() 🙂

Maybe there should be an (optional) exception for CLI-scripts, as they will never be used with require() and top level await is quite helpful in those

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted rule:new A new rule for the package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants