Unhelpful Error Message - Should be missing 'async' keyword instead of Expected ';'
(Originally reported through VS forum, told to report here instead.)
If I have the following JavaScript Vue.js code in a UWP app:
EventBus.$on("erase-history", (event) => {
await clearCacheAsync().then(() => console.log("Cleared Cache"), (e) => console.error(`Error Clearing Cache ${e}`));
});
Then I get an error message running my app:
JavaScript critical error at line 94, column 19 in ms-appx://js/mycode.js \n\nSCRIPT1004: Expected ';'
(that's the line with the await on it)
The real problem is I'm missing the 'async' keyword on the line before:
EventBus.$on("erase-history", async (event) => {
So my error message should be 'Expected async' to actually tell me what the real problem is. As otherwise, I look at semi-colons and I have nothing wrong.
Agreed, that's a pretty lackluster error message. I imagine its because outside of async functions, await is treated like any other identifier name, so function () { await call() } no different than saying function () { x call() }. With that being said, Firefox seems to special-case this and provides a much better error as a result.
This relates to the use of ERRnoSemic in Parse.cpp there are only 4 uses of that in the file - to fix this would need to determine which one is hit in this case and precede it with a special case to check for await - should be fairly easy thing to do, hence marking as a good first issue.
Without async context await should not be a keyword, which means that you can declare a function or variable named await and invoke it without problem.
var await = function (a) { ... }
await(123); // it's ok in non-async context because `tkID tkLParen tkIntCon tkRParen` is a valid invocation
await 123; // it's not ok because `tkID tkIntCon` is neither invocation nor await expression in non-async context
I don't think the error message should be "missing async", instead, it should be "missing (". For more clarity, it can be "Missing (. Maybe you forget async in function declaration?". However, is that worth re-scanning the expression only for reporting an error message?
Indeed the await token outside of an async function is parsed as an identifier not a keyword BUT the suggestion here is that it is probably intended by the developer as a keyword and they forgot to make the function async - hence detecting it specifically would be useful.
It actually shouldn't be too hard to do, when the error is thrown the Parser will be sitting on the token after the the await so should be able to step back one token, check if it is an identifier, if it is, compare specifically against "await" and throw a specific error for it something like "await" is not a keyword outside of an async function.
Would like to try fixing this issue. Is anyone working on it at the moment?
@ylukomskyi no one else has claimed it, feel free to give it a go.
Hi, @ylukomskyi are you still working on it? If not then should I give it a try?
Hi @cavishek39, i am not working on this atm, give it a try