js2-mode
js2-mode copied to clipboard
SyntaxError vs. TypeError
Here's some bad code:
class Doo {
async foo() {
yield 123;
return 456;
}
}
js2-mode reports TypeError: anonymous legacy generator function returns a value but runtimes (e.g. node) report it as a syntax error, SyntaxError: Unexpected strict mode reserved word, i.e. since yield keyword can only be used inside Generator functions.
Is that really important? We get most of our error messages from SpiderMonkey.
I don't think Firefox supports async yet, or I would have suggested to test there. But a similar error message related to generators says "TypeError".
On Firefox 52.0.2 I get SyntaxError: yield is a reserved identifier. If I take out the yield, such that it's a proper async method, it works okay.
My thought was that one might intend to implement async *foo() on a class, but forgot the * and so ended up using yield in a non-generator async method, which is a syntax error. The error that's given currently doesn't really prompt the user to check for a missing *, i.e. it gives the impression the error was encountered inside a generator.