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

Compiler declares variable with let causing ReferenceError when trying to access it before initialization #8787

Closed
sergii-nosachenko opened this issue Mar 28, 2024 · 4 comments
Labels

Comments

@sergii-nosachenko
Copy link

Describe the bug

When trying to execute the compiled project it fails with ReferenceError: Cannot access 'X' before initialization. The reason is that the variable is declared using let (not hoisted), so accessing it from the code above is forbidden.

Input code

import {
  Column, Table, AllowNull,
} from 'sequelize-typescript';

@Table({
  tableName: 'chat_messages',
})
export class ChatMessage extends ModelBase<ChatMessage> {
  @AllowNull(false)
  @Column({
    field: 'text',
  })
  text: string;
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true,
    },
    "target": "es2021",
    "transform": {
      "legacyDecorator": false,
      "decoratorMetadata": true
    },
    "loose": false,
    "externalHelpers": false,
    "keepClassNames": true,
    "baseUrl": "./src",
    "paths": {
      "@/*": [
        "./*"
      ]
    }
  },
  "module": {
    "type": "commonjs"
  },
  "sourceMaps": true,
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.11&code=H4sIAAAAAAAAA02OwQ6CMBBE7%2F2KvQEJ%2FgAYg3qGk3dTYcEmW4rdElHDv9uWxHjb3ZmdN0pPxjr4CICzoVmPOVzkjTCHI5F5NjNRLlbordGQMD5mJPXGnXtNyK1Vk0tKIar4koYQF6ZGaiwgae%2FSXTUyywE58TGZwCXiWpLMcPZ6vcmAi8OxY6hNh3SSjPs%2F9RD7Vb9GaS%2BJMQu3rXREA%2FQKqfNg59M8D2ANnrAVwM6qcSjFKr54bfeu8wAAAA%3D%3D&config=H4sIAAAAAAAAA3VRPU%2FDQAzd%2Bysijwi10JEJCQaWsDEhBnNxSsp9yXakRlX%2FO3dpLk0Gprv33tnv2XfeVBUcxcBTdU7XBCKyEM84MTJ4xVNiQIdIYriLCvdFVclSi1Zo5hoygVEDS5KU%2B0m5XA9Q5ANpbkiyf9g%2FQuEZvbSB3dLd0gHN8Fo6%2Fu9Vk2KDipPlytGGILQuBTopsUf7RjbSmHSp%2FhLFF4si7%2BhoPcaof6PQB9s8xHYnbMoMEfVHlvmfd3cJft4qt4mY0Nc15GYKCi40vaXbX%2BR9ZwcTnAv%2BKDC%2FlNCzoRrjMhq4znftsJwEOqlL03Erlz%2FvHLIU8gEAAA%3D%3D

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 23.4.0: Wed Feb 21 21:44:43 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T6000
    CPU: (8 cores)
        Models: Apple M1 Pro

Binaries:
    Node: 20.11.0
    npm: 10.2.4
    Yarn: N/A
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.4.11
    @swc/helpers: N/A
    @swc/types: 0.1.6
    typescript: 4.3.2

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ChatMessage", {
    enumerable: true,
    get: function() {
        return ChatMessage;
    }
});

...

var ChatMessage = class ChatMessage extends ModelBase {
    constructor(...args){
        super(...args);
        _define_property(this, "text", void 0);
    }
};

or to define properties at the end of a file.

Actual behavior

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ChatMessage", {
    enumerable: true,
    get: function() {
        return ChatMessage;
    }
});

...

let ChatMessage = class ChatMessage extends ModelBase {
    constructor(...args){
        super(...args);
        _define_property(this, "text", void 0);
    }
};

Version

1.4.11

Additional context

No response

@kdy1
Copy link
Member

kdy1 commented Mar 28, 2024

I think you have cyclic imports.
Usage of let is correct here

@kdy1 kdy1 closed this as completed Mar 28, 2024
@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2024
@kdy1
Copy link
Member

kdy1 commented Mar 28, 2024

#7435

@sergii-nosachenko
Copy link
Author

@kdy1 Thanks, it was circular deps in Sequalize models.
This thread has helped me a lot to understand how to fix it.

@swc-bot
Copy link
Collaborator

swc-bot commented Apr 28, 2024

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 Apr 28, 2024
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