js2-mode icon indicating copy to clipboard operation
js2-mode copied to clipboard

SyntaxError vs. TypeError

Open michaelsbradleyjr opened this issue 8 years ago • 3 comments

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.

michaelsbradleyjr avatar Apr 10 '17 17:04 michaelsbradleyjr

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".

dgutov avatar Apr 10 '17 21:04 dgutov

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.

michaelsbradleyjr avatar Apr 10 '17 21:04 michaelsbradleyjr

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.

michaelsbradleyjr avatar Apr 10 '17 21:04 michaelsbradleyjr