Skip to content

Decorator init order is incorrect #7404

Open
@jridgewell

Description

@jridgewell

Describe the bug

From today's TC39 meeting, the committee has decided to reverse the order of initializers.

Input code

function minusTwo({ set, get }) {
  return {
    set(v) {
      set.call(this, v - 2)
    },
    init(v) {
      return v - 2;
    }
  }	
}

function timesFour({ set, get }) {
  return {
    set(v) {
      set.call(this, v * 4)
    },
    init(v) {
      return v * 4;
    }
  }	
}

class Foo {
  @minusTwo @timesFour accessor bar = 5;
}

const foo = new Foo();
console.log({ init: foo.bar });

foo.bar = 5;	
console.log({ set: foo.bar });

Config

{
  "jsc": {
    "transform": {
      "useDefineForClassFields": true,
      "decoratorVersion": "2022-03"
    },
    "parser": {
      "syntax": "typescript",
      "decorators": true
    },
    "target": "es2015",
    "loose": false,
  },
  "module": {
    "type": "es6"
  },
  "isModule": true,
  "minify": false
}

Playground link

https://play.swc.rs/?version=1.3.58&code=H4sIAAAAAAAAA6WQQQ7CIBBF13KKv2xN7cLoxqaJq57AC1RClYQyCUPtwnB3Aa2JrkzcAH%2Fm%2FckfhslKr8li1Hbi00zFHax8hYvyCCXuAnDKT87mJ1KzuJUvkWUte2MKf9Vc4YYNtmXuhSpf2uoPw2tYBpsnGM%2BwEkGIYQnj9ai4o8n9m2aN3W9pIvidRpqeGR1Rpo%2FLB%2BH4TodeSsVMDufeocW%2ByT6y7DFEXwur5jShKJtcJqNqQ5e4VQpySFCdrCH2xSLSnNUXHhf7pB%2B9EpXkuAEAAA%3D%3D&config=H4sIAAAAAAAAA2WPMQ%2BCMBCFd34FuVkTrNHBVcPm6t7A1dSUHrkriYTw3y1gCcbt7vV77%2FWGLM%2FhJRVc8iGOcQmsvRjiZpWi2Ane0FiPJfHVaZHSoqslIoE73CWsxopYB%2BIHsljy8R1UodS%2BOMLMjAsKrWZB3jZI74N%2BT4bQtygV2zbAf3Cq%2FEkLmp8YJi%2BKKg6nrw8ckWCUjXYyf3I2QEN153BzcSxczGdYISv3hKUTobHemj4FZuMHZWKX0j0BAAA%3D

Expected behavior

The set and init logs should be equivalent, because init runs minusTwo's init first then timesFour's.

"{init: 12}"
"{set: 12}"

Actual behavior

The set and init logs are different, because init runs timesFour's init first then minusTwo's.

"{init: 18}"
"{set: 12}"

Version

1.3.58

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions