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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task
tbroyer opened this issue Feb 18, 2024 · 2 comments 路 Fixed by #16252
Closed
1 task

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

tbroyer opened this issue Feb 18, 2024 · 2 comments 路 Fixed by #16252

Comments

@tbroyer
Copy link

tbroyer commented Feb 18, 2024

馃捇

  • 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.

@babel-bot
Copy link
Collaborator

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.

@liuxingbaoyu
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants