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

Decorators on methods in object literals #3062

Closed
withoutmeat opened this issue Dec 17, 2021 · 9 comments
Closed

Decorators on methods in object literals #3062

withoutmeat opened this issue Dec 17, 2021 · 9 comments
Labels

Comments

@withoutmeat
Copy link

Describe the bug

I want change babel to swc in a vue2 project. But i have a little problem.

index.vue

export default {
  methods: {
    @loading 
    getName() {
      console.log('name')
    }
  }
}

.swcrc

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "decorators": true
    },
    "transform": {
      "legacyDecorator": true
    }
  }
}

swc will throw an error:
Error: error: Unexpected token @. Expected identifier, string literal, numeric literal or [ for the computed key

but when i use babel and enable legacy mode, it works.

anyway, thank you first

Input code

No response

Config

No response

Playground link

https://play.swc.rs/?version=1.2.120&code=H4sIAAAAAAAAA0utKMgvKlFISU1LLM0pUajmUlDITS3JyE8ptgJzFBQccvITUzLz0hXAvPTUEr%2FE3FQNTaisgkJyfl5xfk6qXk5%2BuoZ6HlBOXRMsU8sFwrVcACMr6%2F9iAAAA&config=H4sIAAAAAAAAAzXLSwqAMAxF0b28cVfQsRsJMYpiPyQRLKV7l4LO7uDcjtMYsaOSmugsa9npQYRwImM9qiNgFS5KXtQQXW8ZAa6UbSua5nXJTtyWX31ojBcy%2BVj2YQAAAA%3D%3D

Expected behavior

it works

Version

1.2.120

Additional context

No response

@Austaras
Copy link
Member

Wow, I don't know legacy decorator could be applied to object literals until today

@withoutmeat
Copy link
Author

Wow, I don't know legacy decorator could be applied to object literals until today

Me, too.
Is there a plan to support it, which prevents me from migrating from Babel to SWC

@kdy1
Copy link
Member

kdy1 commented Dec 20, 2021

Yeah, I.want to support it.

@dobromyslov
Copy link

Same issue with class-transformer decorators like @Type().

import 'reflect-metadata';
import { Type } from 'class-transformer';

export class DeliveryGenericData {
    /** Packages. */
    @Type(() => PackageDto)
    packages?: PackageDto[];
}

SWC error (using it with Webpack):

ERROR in ./libs/retailcrm/src/lib/dto/delivery/delivery-generic-data.dto.ts
Module build failed (from ./node_modules/swc-loader/src/index.js):
Error: error: Unexpected token `@`. Expected identifier, string literal, numeric literal or [ for the computed key

   |
56 |   @Type(() => PackageDto)
   |   ^

.swcrc

{
  "jsc": {
    "target": "es2020",
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "transform": {
      "decoratorMetadata": true,
      "legacyDecorator": true
    },
    "keepClassNames": true,
    "externalHelpers": true,
    "loose": false
  },
  "module": {
    "type": "es6",
    "strict": true,
    "noInterop": true
  },
  "exclude": ["./src/**/.*.spec.ts$", "./**/.*.js$"],
  "sourceMaps": true
}

@kdy1 kdy1 added this to the Planned milestone Mar 12, 2022
@kdy1 kdy1 changed the title Error: error: Unexpected token @. Expected identifier, string literal, numeric literal or [ for the computed key Decorators on methods in object literals Apr 24, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.2.199 Jun 11, 2022
@alexn-s
Copy link

alexn-s commented Aug 17, 2022

just encountered the same bug using class-transformer lib and decorators ( @Expose({ name: 'purchase-date' })).

are there any updates on this issue?

@polyhb
Copy link

polyhb commented Oct 18, 2022

Same problem with typegraphql

@miso-belica
Copy link

From Typescript v5, new stage 3 ECMA script decorators are implemented by default but it seems @swc/jest does not support it. Is there any plan for this?

@kdy1
Copy link
Member

kdy1 commented Apr 6, 2023

@miso-belica The stage 3 proposal is implemented, but it does not include decorators in object literals. Decorators on object literals will not be supported unless they get promoted to stage 3

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented May 7, 2023

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

No branches or pull requests

8 participants