mooz / js2-mode

Improved JavaScript editing mode for GNU Emacs
GNU General Public License v3.0
1.32k stars 186 forks source link

Support top-level await #593

Closed DamienCassou closed 1 year ago

DamienCassou commented 1 year ago

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await

This is part of ECMAScript 2022: https://en.wikipedia.org/wiki/ECMAScript#ES2022

dgutov commented 1 year ago

Thanks!

jordigh commented 1 year ago

Hm, this only works inside ECMAscript modules, though. It should still be an error inside CommonJS files. I can't see anything in js2-mode about distinguishing the two kinds of javascript files, though.

DamienCassou commented 1 year ago

@jordigh My PR was only about the syntactical aspect of the problem. We could probably introduce some semantic check as well as JS2 as some already. My opinion is that semantic validation shouldn't be the responsibility of a major mode but that's just my opinion. If you care you might want to open an issue.

jordigh commented 1 year ago

I mean, it's a syntax error if the file isn't an ECMAscript module. It would be like saying that new is a valid keyword in a C file because you don't have a mechanism to tell C and C++ files apart.

dgutov commented 1 year ago

Is there a way to distinguish at the lexical or the major mode level?

jordigh commented 1 year ago

No, unfortunately, in order to be able to tell if a file is an ESM file or a CJS file you have to look at either the filename (.cjs is guaranteed CJS and .mjs is guaranteed ESM), or you have to parse the nearest package.json to see what is the correct file type.

dgutov commented 1 year ago

Someone motivated could add that check (which would look at the extension, and then also look around for package.json and its contents). As long as that won't bring false negatives in the end.