swc-project / swc

Rust-based platform for the Web

Home Page:https://swc.rs

Repository from Github https://github.comswc-project/swcRepository from Github https://github.comswc-project/swc

`design:type`/`design:paramtypes`/`design:returntype` is wrong for Enums in a different module

sapphi-red opened this issue · comments

commented

Describe the bug

When using a type from a different module in a property or method that has a decorator and emitDecoratorMetadata is enabled, SWC uses typeof Namespace === "undefined" || typeof Namespace.TypeName === "undefined" ? Object : Namespace.TypeName as the value to pass to _ts_metadata.

This is wrong because Namespace.TypeName could be a enum. If it's an enum, the value should be Number or String depending on the definition of that enum. But the code emit by SWC returns Object.

TypeScript emits typeof (_b = typeof Enums !== "undefined" && Enums.Options) === "function" ? _b : Object, which does not have this problem.

Input code

import * as Enums from './enums'

function decorate() {}

class Foo {
  @decorate()
  foo(options: Enums.Options): Enums.Options {}

  @decorate()
  bar: Enums.Options
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "target": "es2024",
    "loose": false,
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.5&code=H4sIAAAAAAAAA12MMQ6DMBAE%2B3vFdkAK0lPRkDZvcBxbQoq9yGcqxN%2FBoUByOdqdmcPClPGAUUxxDQqfGND0T1eoEfFrtHlmxNdZJpNd22HbRezPqOJFYhNgvNeTPNlyKZYOV7Z%2FX9hV%2FE%2FV%2Bsek6ia7HJRxAryqAAAA&config=H4sIAAAAAAAAA22QMQ6DMAxFd06BPHeoUNWhc1cOYQWDUhES2UYqQty9AQKlolvs77z%2F7THLc3iJgUc%2BxmcsArIQ73XsyNApvmMHdAgkhm1QuGyqyizV2ArtvYqMZ1TPEiXlnhZhWnVQ5IZ05pEU1%2BKWWNB6L%2FTLAmXspPbsjoFaatAMz80keZzdS1KsUPFfCGc7Ww9HqvEuMImctnHYNe0ebKVkiQTOV%2F0ipvPNJ1pXu8N3aDPbwWCl3H4u2aYP7z5OtooBAAA%3D

SWC Info output

No response

Expected behavior

The output uses typeof (_b = typeof Enums !== "undefined" && Enums.Options) === "function" ? _b : Object, which avoids this problem.

Actual behavior

The output uses typeof Enums === "undefined" || typeof Enums.Options === "undefined" ? Object : Enums.Options, which has this problem.

Version

1.13.5

Additional context

No response