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

useDefineForClassFields: false can set members in wrong constructor #8830

Closed
kryops opened this issue Apr 9, 2024 · 2 comments · Fixed by #8835
Closed

useDefineForClassFields: false can set members in wrong constructor #8830

kryops opened this issue Apr 9, 2024 · 2 comments · Fixed by #8835
Assignees
Labels
Milestone

Comments

@kryops
Copy link

kryops commented Apr 9, 2024

Describe the bug

Hi there,

we noticed a transpilation error when executing our code via swc-node.

We have a class that returns a different class in one of its methods. The issue we noticed was that some class members were missing at runtime. After some debugging, we noticed that the property assignment is placed inside the wrong constructor (i.e. the one from the other class that is returned in a method). It looks like the properties are always put into the first constructor inside the class declaration - it works correctly if I place the class' own constructor above the method.

The issue only seems to occur when using "useDefineForClassFields": false, which is the default setting in swc-node.

Thanks for looking into it!

Input code

class MyClass {
 // also same behavior for static methods
 methodReturningClass() {
    return class SomeOtherClass {
      constructor() {
        // Assignment to foo goes here
      }
    };
  }
  
  constructor(args) {
    // Assignment to foo should go here
  }

  foo = 'bar'
}

Config

{
  "jsc": {
    "target": "es2018",
    "externalHelpers": false,
    "parser": {
      "syntax": "ecmascript",
      "jsx": true
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": false,
      "useDefineForClassFields": false,
      "react": {
        "runtime": "classic"
      }
    },
    "keepClassNames": true,
    "paths": {},
    "loose": false,
    "minify": {
      "compress": false
    }
  },
  "minify": false,
  "isModule": true,
  "module": {
    "type": "commonjs",
    "noInterop": false
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.12&code=H4sIAAAAAAAAA22OzQrCMAzH732K3KanPYB4EM8i6BN0XWwLWwNJJoj03e3qNhD8Qz7Ixy9xgxWBy%2Btc49tA24IdhEDsiNBhsM9IDI9iolajgxE1UC9mSW6oE6eYfCXs9jOjiGsZXMXeacSrBuTtSpWjJMqTU%2BJtb1Z54SQSfRoxKSiV6wSeUKAgcBnLNeaD%2BabmF2fZy4r8i5NA09AX6srMpri5c4Sms9yY%2FAE1Rc7IGwEAAA%3D%3D&config=H4sIAAAAAAAAA2WRy07DQAxF9%2F2KaNYsgBViS1XBovyDNbkpU%2BYl25EaRfl3JmkSEtjNXNvn%2BtEfqspcxZrXqi%2FP8lHiC7T8DeT58enFPNx13BQcyb%2FDZ7CUhIa8YI5mYgGvlKJIF5VuE8cGEssu68yaLMeQcotJGWaMMkVpEoctyeNCtjvCJiZNPNetqHoJnKFUk9K%2BtZLRCo5oXMQp8ZsnkZODr%2BVfHoOsbpxHqY3qAsYx7FjprJmDw67xbyBP6E8KkF2LZTf6NUr9kuxTEvxZYHDRNd12bJtCZsja5mFxnTC%2FBSvFODmnuvXY2JuwKMt5u3yfJoWQ4lWW88b0EcuBU97YDYfhB0yjMHcgAgAA

SWC Info output

No response

Expected behavior

"use strict";
let MyClass = class MyClass {
    methodReturningClass() {
        return class SomeOtherClass {
            constructor(){}
        };
    }
    constructor(args){
         this.foo = 'bar';
    }
};

Actual behavior

"use strict";
let MyClass = class MyClass {
    methodReturningClass() {
        return class SomeOtherClass {
            constructor(){
                this.foo = 'bar';
            }
        };
    }
    constructor(args){}
};

Version

1.4.12

Additional context

No response

@kryops kryops added the C-bug label Apr 9, 2024
@kdy1 kdy1 modified the milestones: v1.4.13, Planned Apr 9, 2024
@kdy1 kdy1 assigned kdy1 and unassigned kdy1 Apr 9, 2024
@kryops
Copy link
Author

kryops commented Apr 9, 2024

Update: The issue also seems to occur when setting "loose": true instead of "useDefineForClassFields": false

@magic-akari magic-akari self-assigned this Apr 9, 2024
kdy1 pushed a commit that referenced this issue Apr 11, 2024
@kdy1 kdy1 modified the milestones: Planned, 1.4.14 Apr 15, 2024
@swc-bot
Copy link
Collaborator

swc-bot commented May 15, 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 May 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

4 participants