Skip to content

Support computed property names in enums #11160

@dsherret

Description

@dsherret

Describe the bug

The following code works in TypeScript, but not in swc.

Input code

enum CHAR {
    ["\t"] = 0x09,
    ["\n"] = 0x0A,
    ["\v"] = 0x0B,
    ["\f"] = 0x0C,
    ["\r"] = 0x0D,
    [" "]  = 0x20,

    ["-"] = 0x2D,
    ["["] = 0x5B
}

const c = "\n"

console.log(c.charCodeAt(0) === CHAR["\n"]);

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.20&code=H4sIAAAAAAAAA0vNK81VcPZwDFKo5uVSAIJopZgSpVgFWwWDCgNLHbhYHkzMESFWBhNzQoilwcScEWJFMDEXuJgCUAgsZmQAFIOJ6kIVGiEURkOFTJ14uWpBKpPz84pLFJKBgiBHwUTyc1L1cvLTNZL1kjMSi5zzU1IdSzQMNBVsbW3BvoP4QNMaABH2NC7vAAAA&config=H4sIAAAAAAAAA1WPSw7DIAwF9zkF8rrbdtE79BCIOhERP9mOVBTl7oUE0maH3xszsA5KwcwGnmotxzIkTYx0ziXhHER%2FSgKSE7IhmwRuvRWu1agd4x5tRwOiaUKpW8j3hoOLkbHjLfM22DH%2FC030iZD5ClZUh8nhVTc0Jfj4XvayfaQ%2B9tA%2F4Ad12XkxWH71TaEFh%2B0LYuVI0xQBAAA%3D

SWC Info output

No response

Expected behavior

Should not error.

Actual behavior

x Computed property names are not allowed in enums
,-[input.ts:2:1]
1 | enum CHAR {
2 | ["\t"] = 0x09,
: ^^^^^
3 | ["\n"] = 0x0A,
4 | ["\v"] = 0x0B,
4 | ["\f"] = 0x0C,
`----

Version

1.13.20

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions