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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decorators execution does not come at the right time #11834

Closed
1 task done
y-nk opened this issue Jul 14, 2020 · 1 comment
Closed
1 task done

decorators execution does not come at the right time #11834

y-nk opened this issue Jul 14, 2020 · 1 comment
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@y-nk
Copy link

y-nk commented Jul 14, 2020

Bug Report

Input Code

function decorated(target, key, descriptor) {
  target.foo += "bar"

  const _ = target[key].bind(target)
  descriptor.value = () => {
    console.log("decorated")
    _()
  }
}

class Foo {
  foo = "foo"
}

class Bar extends Foo {
  @decorated bar() {
    console.log(this.foo)
  }
}

const foo = new Bar()
foo.bar()

Current behavior

Code stack executes as following:

  1. Decorator is executed with an unknown/undesired instance of Foo without class properties set (not Bar, and target.foo === undefined)
  2. Create a new Bar instance
  3. Call constructor of Bar
  4. Call constructor of Foo
  5. Class properties are injected
  6. foo.bar() is called with decorated version

Expected behavior

Code stack should execute as following:

  1. Create a new Bar instance
  2. Call constructor of Bar
  3. Call constructor of Foo
  4. Class properties are injected
  5. Decorators are called
  6. foo.bar() is called with decorated version

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

  • Filename: babel.config.js
{
  "presets": [
    ["@babel/preset-env", {
      "targets": "last 2 versions"
    }]
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", {
      "legacy": true
    }],
    ["@babel/plugin-proposal-class-properties", {
      "loose": true
    }]
  ]
}

Environment

  • Babel version(s): @latest (v7.10.4)
  • Node/npm version: not relevant (also fails in browser)
  • OS: tested on macOS, but assumed cross-platform
  • How you are using Babel: babel-loader with webpack (codesandbox uses parcel)
@babel-bot
Copy link
Collaborator

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

@y-nk y-nk closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2022
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Mar 31, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

2 participants