swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.09k stars 1.22k forks source link

`decorator` of method in hoc lead to runtime error after compilation #9350

Closed Mumujianguang closed 2 months ago

Mumujianguang commented 2 months ago

Describe the bug

the Design.prototype parameter of _ts_decorate method is undefined in scope

export default function design(base) {
    var _class;
    return _class = class Design extends base {
        copy() {
            console.log("copy");
        }
        render() {
            super.render();
            console.log("design render");
        }
    }, _ts_decorate([
        registerHook('beforeRender'),
        registerHook('afterRender')
    ], Design.prototype, "render", null), _class;
}

the compiled code can not run in browser

image

Input code

function registerHook(key) {
    return (...args) => console.log(args)
}

export default function design(base: typeof Component) {
    return class Design extends base {
        copy() {
            console.log("copy");
        }

        @registerHook('beforeRender')
        @registerHook('afterRender')
        render() {
            super.render();
            console.log("design render");
        }
    }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "tsx": false
    },
    "target": "es2016",
    "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.7.3&code=H4sIAAAAAAAAA3WQTY7CMAyF9z2FxYZkkwOAGI00LFhzg9C%2BVBWdOLJTiQpxd%2FpDZ4ZBeGFZL5%2FtF4culrnhSIK60Qw5MJ%2FNGb2la0FDCHInkYxzzkutlnYfVHJUbuFars0kFreiwCWxZKoQfNdmCsvgCtrU0Zy8YkO5T%2BBAX%2FydOCLmf1vK1qvSfuogXDJipTR2PrAxSk69sX%2BEWfx1tBqJld3%2BAIO5pfx8%2Bub6hMCC47AGsrbvKB%2BG8gWSSXhxol2CuOVx%2B97lfJfHmGe7c77dAYePf1ucAQAA&config=H4sIAAAAAAAAA1VPOw6DMAzdOUXkuUPbgaF36CGiYFAqSCLbSEWIu5NAkpbNfh8%2Fv7VRCj5s4KXWOMYlaGKkukeEFyf6GxGQJSAbskHgVtgOjSctnjgqhGasjHAy9XpkPKDtZEA0DSjpHvLz%2FmjzLRi9ZyyOjE3W2X75%2F8b4KRAyX4VJqt0w4jWxyakw%2BW4%2ByNwyNTk%2FaOEnKmH1MFh%2BF2fq1mw7L9dvyDEBAAA%3D

SWC Info output

No response

Expected behavior

the compiled code should be as below

export default function design(base) {
    var _class;
    return _class = class Design extends base {
        copy() {
            console.log("copy");
        }
        render() {
            super.render();
            console.log("design render");
        }
    }, _ts_decorate([
        registerHook('beforeRender'),
        registerHook('afterRender')
    ], _class.prototype, "render", null), _class;
}

Actual behavior

No response

Version

1.7.3

Additional context

No response

Mumujianguang commented 2 months ago

@kdy1 @magic-akari I‘m using v1.7.6 of swc in the playground,the compiled code still has not changed

image
magic-akari commented 2 months ago

All crates depending on swc_ecma_transforms_proposal need to be updated, but swc_core has not been updated. @kdy1

kdy1 commented 2 months ago

I verified that it's applied in the SWC playground

swc-bot commented 1 month ago

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.