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

Wrong private fields context in decorators #8654

Open
nicolo-ribaudo opened this issue Feb 21, 2024 · 0 comments
Open

Wrong private fields context in decorators #8654

nicolo-ribaudo opened this issue Feb 21, 2024 · 0 comments
Labels
Milestone

Comments

@nicolo-ribaudo
Copy link

nicolo-ribaudo commented Feb 21, 2024

Describe the bug

SWC does not properly compile references to private names inside decorators

Input code

class A {
  #x = console.log.bind(console, "outer #x");
  foo() {
    let after;

    @(new A().#x)
    class B {
      #x = console.log.bind(console, "inner #x");
      @(after = () => new B().#x(this instanceof A), () => {}) f;
    }

    after();
  }
}

new A().foo();

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": true
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.2&code=H4sIAAAAAAAAA4WPwQqDMBBEz%2FUrBr0kIP6AWKp%2FkqZJGwi70KRUEP%2B9cbXQW487Oztv1kaTEkYsFdDMGGCZEkfXRb5310A3dQgtan5l9yyuWvfF7ZmVljsgugzjy7avZL4ocm%2BMSnfNrEWxwpmK%2F7SN%2F1CB6Ae1RwqgnBXqcMYGmASg8iMkBErZkHXsMer2MC2rht8T1r2ZhChJXauifXvKN%2F0HgdSLUA4BAAA%3D&config=H4sIAAAAAAAAA1VPuw6DMAzc%2BQrkuUOVoUP%2FoR8RBYOCyEO2kYoQ%2F94EkrTd7LvznW%2Fv%2Bh5mNvDs9zSmJWpipLYnhDcv%2Bp0QkC0iG7JR4FZZ4UwJrdigAU0gLYG4MCdxXDyIpgkl2yGru1LFCpYQGBM86oWLFzjr7bj9PmOCi4TM%2F8Is1X5a2v2V2JVUcGFYT7KUzEWuDx7wFdWwZgyWX%2FXy7HF8AF26uLkwAQAA

SWC Info output

No response

Expected behavior

outer #x, class B {}, { kind: 'class', ... }
inner #x true

Actual behavior

Uncaught:
TypeError: Cannot read private member #x from an object whose class did not declare it
class A {
    #x = console.log.bind(console, "outer #x");
    foo() {
        let after;
        class B {
            #x = console.log.bind(console, "inner #x");
            f;
        }
        _ts_decorate([
            (after = ()=>new B().#x(this instanceof A), ()=>{})
        ], B.prototype, "f", void 0);
        B = _ts_decorate([
            new A().#x
        ], B);
        after();
    }
}
new A().foo();

Version

1.4.2

Additional context

No response

@kdy1 kdy1 added this to the Planned milestone Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants