Skip to content

fail to parse decorator with computed name in class #10978

Closed

Description

@laysent

Bug Report

  • I would like to work on a fix!

Current Behavior

Babel is throwing syntax error (Unexpected token) when parsing decorator (legacy mode) with computed class property.

Input Code

class Example {
  @computed ['name'] = 1;
}

Expected behavior/code

Babel should parse the above code without throwing out syntax error.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
module.exports = env => ({
  "presets": [
    "@babel/preset-env",
    "@babel/preset-typescript",
    "@babel/preset-react"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", {
      "legacy": true
    }],
    ["@babel/plugin-proposal-class-properties", {
      "loose": true
    }],
  ],
  "sourceMaps": true
});

Environment

  • Babel version(s): v7.7.7
  • Node/npm version: Node v10.16.0 / npm 6.9.0
  • OS: OSX 10.14.6
  • Monorepo: no
  • How you are using Babel: cli, loader

It also fails on online demo (try here)

Possible Solution

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

Activity

  1. babel-bot commented on Jan 10, 2020

    @babel-bot
    Collaborator

    Hey @laysent! 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."

  2. nicolo-ribaudo commented on Jan 10, 2020

    @nicolo-ribaudo
    Member

    The problem is that they are ambiguous:

    class A {
      @computed["name"]
      foo
    }

    Am I decorating the name field with @computed, or the field foo with @computed["name"]?

    A decision had to be made, and it was decided that [...] are part of the decorator.

  3. JLHwung commented on Jan 10, 2020

    @JLHwung
    Contributor

    Closing this as it works as expected.

  4. added
    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue
    on Apr 11, 2020
  5. locked as resolved and limited conversation to collaborators on Apr 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: invalidoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions