tc39 / ecma262

Status, process, and documents for ECMA-262
https://tc39.es/ecma262/
Other
15.04k stars 1.28k forks source link

Generator Method Definitions and ASI #3130

Open 12Me21 opened 1 year ago

12Me21 commented 1 year ago

Generator method definitions require a semicolon, when written immediately after a field definition. e.g:

class MyClass {
    abc = 5

    ; * func() {
        yield this.abc
    }
}

Without that semicolon, this is a syntax error: the * is parsed as a multiplication operator, which causes the expression to continue across the linebreak (abc = 5 * func() {⚠️ ...)

I believe this should be mentioned in the "Interesting Cases of Automatic Semicolon Insertion" section?

ljharb commented 1 year ago

This is true for all class fields where certain things follow it; that seems worth mentioning i guess.

12Me21 commented 1 year ago

I believe this can only happen with generator methods? oh- and methods/fields defined using ["name"] …, I suppose?

But encountering a syntax error from a generator method in particular caught me off guard, since * isn't on the list of "unsafe" characters (I assume this is because the only 'statement' which can begin with * is a generator method inside a class block)

claudepache commented 1 year ago

But encountering a syntax error from a generator method in particular caught me off guard, since * isn't on the list of "unsafe" characters

Inside class { ... }, there are more hazards than “unsafe” characters. If you have a field named static, get or set:

class foo {
    static; // <-- non-guessable semicolon here
    bar() { }
}

And yes, there should be a dedicated subsection inside “Interesting Cases of Automatic Semicolon Insertion” dedicated to class element lists.

12Me21 commented 1 year ago

Inside class { ... }, there are more hazards than “unsafe” characters. If you have a field named static, get or set:

Ah, gosh that's a terrifying edge case... (Though I'd probably quote the field name instead of using a semicolon, here)