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

Circular reference with decorators metadata fails #6176

Closed
matthieuMay opened this issue Oct 17, 2022 · 2 comments
Closed

Circular reference with decorators metadata fails #6176

matthieuMay opened this issue Oct 17, 2022 · 2 comments
Labels

Comments

@matthieuMay
Copy link

Describe the bug

Using typeorm we are often lead to use circular reference with decorators.
This configuration fails with the error "ReferenceError: Cannot access 'XXX' before initialization"
This is due to the metadata export that tries to set the exported class before it is initialized.

I made a 3 files reproduction repository here https://github.com/matthieuMay/swc-repro.

In this repro we have two class that references each other with decorated fields. Running the tests with npm run test fails.

Input code

https://github.com/matthieuMay/swc-repro

Config

{
    "jsc": {
      "parser": {
        "syntax": "typescript",
        "tsx": false,
        "decorators": true,
        "dynamicImport": true
      },
      "transform": {
        "legacyDecorator": true,
        "decoratorMetadata": true
      },
      "target": "es2020",
      "loose": false,
      "minify": {
        "compress": false,
        "mangle": false
      }
    }
  }

Playground link

https://play.swc.rs/?version=1.3.8&code=H4sIAAAAAAAAA21PPWvDMBDdDf4PjyyRwSS7Q4rbUAoZ2g7dSimufSGC6BTOMtQY%2F%2Fdakp0sPYGGe5%2BnzdWKw4Bndtr1OY5W88FeOsM53pg%2B7PTleBdtKulfiEkqR01kYMRJrMHK9VeyYla7NNGL4eFSte3TjbLZ1mHhOf6VMVBlaUK%2FQRLwKHvEkCaYpvw%2F2Ks8rJsC3JkfkuAaFEtppTLsH%2BYaM7%2B8X6c%2BB3BlqMDaujPJt27WOYROJMT1EvQaGROG8SsDok0QFLP1Lbm23DrpamdF3Ytlyyl%2B3Fm3G91gPzXfxfWYJuMflgCMP4UBAAA%3D&config=H4sIAAAAAAAAA0WOQQ7DIAwE%2F%2BJzDhWHHvhDH4GIE1EBRrYjFSH%2BXoLS5mbt7ozc4C0ebIPiWJDPS2pW9wELWguK51AUFlAZ0eai4AIremKnxAJW%2BcA%2Basc76oBQzMOYAUQiwT%2BSQg5bPfWeUmEUuSuX9%2Fhb9uFKtB5n0OYH0%2FmEfjsuLsjrGs4fvucDfqLKAAAA

Expected behavior

Reading tsc output we see that tsc exports empty classes before anything and fix the exports when the class is correctly defined. Therefore, the playground link should be fixed with

exports.ClassA = void 0;
var __decorate = this && this.__decorate || function(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = this && this.__metadata || function(k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from "typeorm";
import { ClassB } from "./classB";
let ClassA = class ClassA {
    constructor(id){
        this.id = id;
    }
};
__decorate([
    PrimaryGeneratedColumn(),
    __metadata("design:type", Number)
], ClassA.prototype, "id", void 0);
__decorate([
    OneToOne(()=>ClassB),
    JoinColumn([
        {
            name: 'other_id',
            referencedColumnName: 'id'
        }
    ]),
    __metadata("design:type", typeof ClassB === "undefined" ? Object : ClassB)
], ClassA.prototype, "other", void 0);
ClassA = __decorate([
    Entity(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [
        Number
    ])
], ClassA);
exports.ClassA = ClassA;

Actual behavior

No response

Version

1.3.8

Additional context

No response

@kdy1
Copy link
Member

kdy1 commented Oct 17, 2022

#5047

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Nov 16, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants