denoland / deno_lint

Blazing fast linter for JavaScript and TypeScript written in Rust
https://lint.deno.land/
MIT License
1.53k stars 172 forks source link

`await` in non-async function should report as syntax error. #1139

Closed gaeulbyul closed 1 year ago

gaeulbyul commented 1 year ago

Lint Name

SyntaxError?

Code Snippet

function invalid() {
  await Promise.resolve(true)
}

invalid()

Expected Result

It should report as syntax error

Actual Result

Deno doesn't find any problem with that code.

Additional Info

Version

magurotuna commented 1 year ago

You can get a wanted error from TypeScript by running deno check your_script.ts.

$ cat script.ts
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: script.ts
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ function invalid() {
   2   │   await Promise.resolve(true)
   3   │ }
   4   │
   5   │ invalid()
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

$ deno check script.ts
Check file:///private/tmp/script.ts
error: TS1308 [ERROR]: 'await' expressions are only allowed within async functions and at the top levels of modules.
  await Promise.resolve(true)
  ~~~~~
    at file:///private/tmp/script.ts:2:3

TS1356 [ERROR]:     Did you mean to mark this function as 'async'?
    function invalid() {
             ~~~~~~~
        at file:///private/tmp/script.ts:1:10

Basically deno_lint attempts to discover possibly problematic pieces of code that TypeScript is not covering. So putting awaitin non-async functions is what TypeScript is responsible for, not deno_lint.

dsherret commented 1 year ago

This will also fail at runtime:

> deno run main.js
error: Uncaught SyntaxError: Unexpected reserved word
  await Promise.resolve(true)
  ^
    at <anonymous> (file:///V:/deno_lint/main.js:2:3)