Skip to content
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

Differing transpilation than tsc for constructor parameter properties #7574

Closed
phiresky opened this issue Jun 23, 2023 · 4 comments
Closed
Assignees
Labels
Milestone

Comments

@phiresky
Copy link
Contributor

Describe the bug

swc results in different runtime behaviour than tsc for constructor(readonly x) {}. This is not related to #7435.

Input code

class Foo {
	constructor(readonly game: string) {}

	x = this.game.toUpperCase();
}

Config

swc --config jsc.target=es2022

Playground link

https://play.swc.rs/?version=1.3.61&code=H4sIAAAAAAAAA0vOSSwuVnDLz1eo5uJMzs8rLikqTS7JL9IoSk1Myc%2FLqVRIT8xNtVIAimfmpWsqVNdycXFWKNgqlGRkFuuB5PRK8kMLClKLnBOLUzU0rblquQCXZn2sVAAAAA%3D%3D&config=H4sIAAAAAAAAA1VPOw7DIAzdOQXy3KFi6NA79BCIOhERAYQdqSjK3QsJpM1mv4%2Ff8yqkhIkMPOVaxrJEnQjTuReEsmf9KQhwjkgm2chw6yxTpQbtCHdoOxhgnUbk6kJSd6WaA1wIhN3RsNl6O%2BT%2FTBPmmJDoKqxS7UeH10TRUmEO72Un2y%2B179HgAT9RDzsPg6VXd3JaUGxfBMLf3xcBAAA%3D

Expected behavior

tsc outputs the following:

class Foo {
    game;
    constructor(game) {
        this.game = game;
    }
    x = this.game.toUpperCase();
}

this code is broken because the this.game assignment happens after the x assignment.

Actual behavior

class Foo {
    game;
    constructor(game){
        this.game = game;
        this.x = this.game.toUpperCase();
    }
    x;
}

this code works because the assignment of x is moved into the constructor by swc.

Version

1.3.66

Additional context

No response

@phiresky phiresky added the C-bug label Jun 23, 2023
@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2023
@kdy1 kdy1 reopened this Jun 24, 2023
@kdy1
Copy link
Member

kdy1 commented Jun 24, 2023

@magic-akari AFAIK, Implicit class properties in typescript have no specification about execution order, right?

@kdy1 kdy1 added this to the Planned milestone Jun 24, 2023
@kdy1 kdy1 self-assigned this Jun 24, 2023
@magic-akari
Copy link
Member

magic-akari commented Jun 24, 2023

tsc input:

class Foo {
	constructor(readonly game: string) {}
	x = this.game.toUpperCase();
}

tsc output:

"use strict";
// @target: es2022
// @useDefineForClassFields: true
class Foo {
    game;
    constructor(game) {
        this.game = game;
    }
    x = this.game.toUpperCase();
}

"use strict";
// @target: es6
// @useDefineForClassFields: true
class Foo {
    constructor(game) {
        Object.defineProperty(this, "game", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: game
        });
        Object.defineProperty(this, "x", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: this.game.toUpperCase()
        });
    }
}

"use strict";
// @target: es2022
// @useDefineForClassFields: false
class Foo {
    constructor(game) {
        this.game = game;
        this.x = this.game.toUpperCase();
    }
}


"use strict";
// @target: es6
// @useDefineForClassFields: false
class Foo {
    constructor(game) {
        this.game = game;
        this.x = this.game.toUpperCase();
    }
}

For more details: #7055 (comment)

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2023
@kdy1
Copy link
Member

kdy1 commented Jun 24, 2023

Thank you!

@kdy1 kdy1 modified the milestones: Planned, v1.3.67 Jun 29, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Jul 29, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Jul 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

4 participants