babel/babel

[Bug]: decorator on symbol-keyed member throws error at evaluation time

tbroyer opened this issue · 2 comments

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

function decorator(target, context) {
  console.log(context.name);
};

const FOO = Symbol();

class Test {
    @decorator accessor #foo = 42;
  
    @decorator accessor ["computed"] = 42;

    @decorator accessor [FOO] = 42;
}

REPL

Try commenting out each property.

Configuration file name

No response

Configuration

No response

Current and expected behavior

Generated code throws error Cannot convert a Symbol value to a string at line

var key = name + "/" + isStatic;

Environment

System:
OS: Linux 6.7 Arch Linux
Binaries:
Node: 20.11.1 - /usr/bin/node
Yarn: 1.22.21 - /usr/bin/yarn
npm: 10.4.0 - /usr/bin/npm
pnpm: 8.6.0 - /usr/bin/pnpm

Possible solution

The concatenation is used as a key in a Map, and the property name is concatenated with a boolean, so it could be possible to use two maps instead of one (one map for the static members, one for the non-static members) directly keyed by the property name (either a string or a symbol)

Additional context

I encountered this while migrating a Lit element from static properties to using decorators through Babel; with static properties I couldn't use a private property (as the property from the static properties object value has to match a property in the enclosing class) so I had used a symbol (I could also have used any mangled, non-conflicting, name); I could migrate to using a private property with decorators but unfortunately Lit decorators don't seem to work with those, so in the mean time I can use a non-conflicting name (e.g. _-prefixed), so at least I have a workaround.

Hey @tbroyer! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

Thank you for your report!
This has been fixed at #16270.
Expected to be released at v7.24.0.