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

[TypeScript] const enum should be hoisted #3648

Closed
magic-akari opened this issue Feb 20, 2022 · 9 comments
Closed

[TypeScript] const enum should be hoisted #3648

magic-akari opened this issue Feb 20, 2022 · 9 comments
Labels

Comments

@magic-akari
Copy link
Member

Describe the bug

TS Playground

"use strict";
console.log(1 /* A */);

Input code

console.log(Foo.A);

const enum Foo {
    A = 1
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2022"
  },
  "module": {
    "type": "es6"
  },
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.2.143&code=H4sIAAAAAAAAA0vOzyvOz0nVy8lP13DLz9dz1LTm4koGCpYopOaV5ioAxRSquRSAwFHBVsGQqxYA10T3ajEAAAA%3D&config=H4sIAAAAAAAAAy2LQQ6AIAwE%2F9KzB8PBA3%2FwEQ1Wg0ElbUk0hL9bjLfdmd0KuwTwFTKyEPckz6l4gwd9MkngmBUGUDG0YhJqVpA3UpuQuNE5MHRcS0nU%2F%2F32qanzKPNvlAu1F9feoEVwAAAA

Expected behavior

see TypeScript output

Actual behavior

No response

Version

1.2.143

Additional context

No response

@kdy1 kdy1 added this to the v1.2.144 milestone Feb 20, 2022
@magic-akari
Copy link
Member Author

another case:

TypeScript REPL

console.log(Foo.Bar.A)

namespace Foo {
    export const enum Bar {
        A = 1
    }

    console.log("A")
}

console.log(Foo.Bar.B)

namespace Foo {
    export const enum Bar {
        B = 2
    }

    console.log("B")
}

expected output

console.log(1 /* A */);
var Foo;
(function (Foo) {
    console.log("A");
})(Foo || (Foo = {}));
console.log(2 /* B */);
(function (Foo) {
    console.log("B");
})(Foo || (Foo = {}));

@magic-akari
Copy link
Member Author

magic-akari commented Jun 5, 2022

I think it's partially fixed by #3647. When the const enum is defined and used in the same file, the const enum will be inlined directly, and the hoist issue will disappear.

@kdy1
Copy link
Member

kdy1 commented Jun 5, 2022

Do you think we should close this issue?

@magic-akari
Copy link
Member Author

playground

console.log(Foo.A);
console.log(Bar.Foo.A)

const enum Foo {
    A = 1
}

namespace Bar {
    export const enum Foo {
        A = 1
    }

    console.log("A")
}
console.log(1);
console.log(Bar.Foo.A);
var Foo;
(function(Foo) {
    Foo[Foo["A"] = 1] = "A";
})(Foo || (Foo = {}));
var Bar1;
(function(Bar) {
    let Foo;
    (function(Foo) {
        Foo[Foo["A"] = 1] = "A";
    })(Foo = Bar.Foo || (Bar.Foo = {}));
    console.log("A");
})(Bar1 || (Bar1 = {}));

@C0kkie

This comment has been minimized.

@magic-akari
Copy link
Member Author

Currently, there is no progress. Furthermore, the new TypeScript 5.0 has made enums even more complex, as opaque computed enum members can now be used.

Fp330ZqaUAEHk_Y

Reference: microsoft/TypeScript#50528

We cannot completely eliminate const enums like TS, but we can make every effort to replace their references.

@magic-akari
Copy link
Member Author

This issue should be closed.
I will submit a new issue about the complicated enum usage.

@C0kkie

This comment has been minimized.

@swc-project swc-project temporarily blocked C0kkie Apr 19, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented May 19, 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 May 19, 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