babel / babel

🐠 Babel is a compiler for writing next generation JavaScript.
https://babel.dev
MIT License
43.27k stars 5.65k forks source link

[Bug]: Error Throw: Cannot Call a Class as a Function #14399

Open clhiker opened 2 years ago

clhiker commented 2 years ago

πŸ’»

How are you using Babel?

@babel/cli

Input code

class Polygon {
    constructor() {
        this.name = 'Polygon';
    }
}
class Square extends Polygon {
    constructor() {
        super();
    }
}
class Rectangle {
}
Object.setPrototypeOf(Square.prototype, Rectangle.prototype);
let v21 = new Square();
console.log(v21.name);

Here is REPL

Configuration file name

babel.config.json

Configuration

{
  "sourceType": "unambiguous",
  "presets": ["@babel/preset-env"],
  "plugins": [
    "@babel/plugin-transform-runtime"
  ]
}

Current and expected behavior

After transforming by babel, the code throw a error:

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
                                                                                            ^

TypeError: Cannot call a class as a function

But the original code can be run correctly by node.

Environment

Possible solution

No response

Additional context

No response

babel-bot commented 2 years ago

Hey @clhiker! 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.

LavanyaBurlagadda commented 1 year ago

@clhiker

Is that issue fixed, currently I am also facing the issue.

https://github.com/microsoft/vscode/issues/177039

liuxingbaoyu commented 1 year ago

@LavanyaBurlagadda Can you provide a minimal reproducible code?

liuxingbaoyu commented 8 months ago

I investigated again and found that this issue may be difficult to fix. Fortunately this is relatively rare and anyone who encounters this problem can disable https://babel.dev/docs/assumptions#noclasscalls to solve it.

heyikang commented 1 month ago

I investigated again and found that this issue may be difficult to fix. Fortunately this is relatively rare and anyone who encounters this problem can disable https://babel.dev/docs/assumptions#noclasscalls to solve it.

Solved my problem