Skip to content

Class member has empty decorators array #9024

@OskarLebuda

Description

@OskarLebuda

Describe the bug

We are working on moving into Rspack . To make that happen we have to rewrite our custom TypeScript transformer (AST) into SWC plugin. The plugin for reproductions is available here

What is the problem?

The problem is that class member has as empty array of decorators even if they exists (see example)
In the SWC Playground decorators also exists in as an filled array.
But inside of our code the decorators array is empty:
image

The prop of member class is ClassProp(ClassProp {
    span: 84..118#0,
    key: Ident(Ident {
        span: 104..108#0,
        sym: "test", optional: false
    }),
    value: None,
    type_ann: None,
    is_static: false,
    ==========================
    decorators: [],
    ==========================
    accessibility: None,
    is_abstract: false,
    is_optional: false,
    is_override: false,
    readonly: false,
    declare: false,
    definite: false
})

The problem is that all tests are working properly and transforms class property decorator into new value but the example code does not.

How to reproduce?

We've been created an reproduction repository.

The example code is available in example directory. To run the test you have to install all required packages then run build command:

$ pnpm install
$ cd example && pnpm install
$ pnpm build

What is the expected output?

We are trying to make plugin that will add into Prop decorator the annotation type e.g.
Before:

export default class Test {
  
  @Prop()
  public firstProp: boolean;

  @Prop({ default: boolean })
  public secondProp: boolean;

  @Prop()
  public thirdProp: string;

}

After:

export default class Test {
  
  @Prop({ type: Boolean })
  public firstProp: boolean;

  @Prop({ default: boolean, type: Boolean })
  public secondProp: boolean;

  @Prop({ type: String })
  public thirdProp: string;

}

Input code

import { Prop } from "./decorators/Prop";

export default class TestComponent {

    @Prop()
    private test: boolean;

}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "target": "es2019",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    },
    "experimental": {
        "plugins": [
            ["@enp/swc-plugin-vue-auto-prop", {}],
        ]
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.5.24&code=H4sIAAAAAAAAAx2MMQqEQAxF%2B5wiWGmz9toIXsBiL5DVCAPjZMhklwWZuxv91Yf%2F%2FgtHFjU8F5VccVc5sHn1G6%2BiZKKlv4dmBOD%2FA2680zcarpFKwTcXm8UViZNLANAz3Ze2e3rW8CNjNAcH%2FIhEpuS2ChePRyj4eQAAAA%3D%3D&config=H4sIAAAAAAAAA2WPSw7CMAxE9z1F5TULYIEEd%2BAQUepWQc1HtisRVb17kzYNIHbxvHjGMzdtCy%2FW8Gjn9ExDUMRIdU4KRyfqnRSQGJA1mSBwOqhwRr0aGavWofakxBMnJDThBpadgygaULIf8vV8uRcvGL1n%2FPUCa5zp4%2Fc12ttAyPwXapUbxrq%2FJzYlFazvpg2WlrnJfsENPp%2BOsGoMhp%2FH5tZjWQEa0ouIMQEAAA%3D%3D

SWC Info output

    Operating System:
        Platform: darwin
        Arch: arm64
        Machine Type: arm64
        Version: Darwin Kernel Version 23.5.0: Wed May  1 20:14:38 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6020
        CPU: (10 cores)
            Models: Apple M2 Pro

    Binaries:
        Node: 16.20.2
        npm: 8.19.4
        Yarn: 1.22.21
        pnpm: 9.1.3

    Relevant Packages:
        @swc/core: 1.5.25
        @swc/helpers: N/A
        @swc/types: 0.1.7


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

    Next.js info:
        output: N/A

Expected behavior

The decorators arrays are filled.

Actual behavior

No response

Version

latest

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions