question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@babel/parser(ts): literal computed property names in enums

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current behavior Babel fails if computed property is provided as key of enum, this synatx should be allowed only for computed properties with string literal value or template literal without arguments.

Input Code

// should work
export enum Foo { ['baz'] }
export enum Foo { [`baz`] }

// should fail
export enum Foo { [foo] }               // Computed property names are not allowed in enums
export enum Foo { [1] }                 // An enum member cannot have a numeric name.
export enum Foo { [`test${foo}`] }      // Computed property names are not allowed in enums.

Expected behavior Babel should allow computed property or template literals

Note In my opinion this syntax is a issue in typescript as Computed property names are not allowed in enums (1164), but as for now its parsed correctly and results in correct output


  • Babel version(s): v7.12.0
  • Node/npm version: 10-14
  • OS: ----
  • Monorepo: ----
  • How you are using Babel: @babel/parser -> parse

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
fedecicommented, Feb 24, 2021

Should I submit a PR for this?

0reactions
armano2commented, May 6, 2022

Should I submit a PR for this?

@fedeci yes :>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object literals combining enums and computed property names
I have an enum enum Action { action1 = 'action1', action2 = 'action2' };. whose values I use as computed property names in...
Read more >
babel/types
value : string (required). Aliases: Standardized , Expression , Pureish , Literal , Immutable ... memberExpression(object, property, computed, optional);.
Read more >
@babel/plugin-transform-computed-properties | Yarn - Package ...
Fast, reliable, and secure dependency management.
Read more >
@babel/plugin-transform-shorthand-properties - NPM Package ...
v7.18.6 (2022-06-27). :eyeglasses: Spec Compliance. babel-parser. #14650 [ts] Disallow property access after instantiation expression (@nicolo-ribaudo) ...
Read more >
typescript.rs - source - Docs.rs
(self); // Computed property names are grammar errors in an enum, so accept just string // literal or identifier. let id = match...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found