Skip to content

No error on this expressions in static property declaration initialisers (ESNext+useDefineForClassFields) #36781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2020

Conversation

sandersn
Copy link
Member

No error on this expressions in static property declaration initialisers when targetting ESNext and with useDefineForClassFields. In this case the emit is correct and the types are correct, so the error should not be issued.

Addresses #36267 but only for ESNext+useDefineForClassFields.

No error on `this` expressions in static property declaration
initialisers when targetting ESNext and with useDefineForClassFields. In
this case the emit is correct and the types are correct, so the error
should not be issued.
@sandersn
Copy link
Member Author

ping @elibarzilay @rbuckton

@sandersn sandersn added the For Backlog Bug PRs that fix a backlog bug label Feb 24, 2020
@sandersn sandersn added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Backlog Bug PRs that fix a backlog bug labels Feb 24, 2020
Copy link
Member

@rbuckton rbuckton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we considered changing our downlevel emit to support this case? We could either replace this with the class name depending on the context, or just wrap static initializers in something like:

(function() {
  Foo.t = this;
  Foo.at = () => this;
}).call(Foo);

Not necessary for this PR, but some food for thought.

@sandersn
Copy link
Member Author

Yes, I thought a bit about changing the emit and had thought of rewriting to the class name, but not about the IIFE technique.

One consideration is that the error and emit have been this way forever and there's been little demand for the feature so far.

@sandersn sandersn merged commit af901ba into master Feb 26, 2020
@sandersn sandersn deleted the no-error-on-this-expr-in-static-class-fields branch February 26, 2020 17:07
@Kingwl
Copy link
Contributor

Kingwl commented Mar 5, 2021

//// [awaitAndYieldInProperty.ts]
async function* test(x: Promise<string>) {
    class C {
        [await x] = await x;
        static [await x] = await x;

        [yield 1] = yield 2;
        static [yield 3] = yield 4;
    }

    return class {
        [await x] = await x;
        static [await x] = await x;

        [yield 1] = yield 2;
        static [yield 3] = yield 4;
    }
}

//// [awaitAndYieldInProperty.js]
async function* test(x) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    class C {
        constructor() {
            this[_a] = await x;
            this[_c] = yield 2;
        }
    }
    _a = await x, _b = await x, _c = yield 1, _d = yield 3;
    (function () {
        C[_b] = await x; // here
        C[_d] = yield 4; // here
    }).call(C);
    return _j = class {
            constructor() {
                this[_e] = await x;
                this[_g] = yield 2;
            }
        },
        _e = await x,
        _f = await x,
        _g = yield 1,
        _h = yield 3,
        _j[_f] = await x,
        _j[_h] = yield 4,
        _j;
}

Seems we cannot just use IIFE-like expression because:

_a = await x, _b = await x, _c = yield 1, _d = yield 3;
    (function () {
        C[_b] = await x; // here
        C[_d] = yield 4; // here
    }).call(C);

@Kingwl
Copy link
Contributor

Kingwl commented Mar 5, 2021

It's not in await context anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants