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

fix: should respect legacy decorators #67

Merged
merged 5 commits into from
Oct 7, 2024
Merged

Conversation

nonzzz
Copy link
Contributor

@nonzzz nonzzz commented Oct 7, 2024

TL;DR

In [email protected] the default decorators is using stage3, but it allowed using legacy decorators. difference This pull request fix fixes the wrong decorators setting and update legacy decorators unit test suite.

Relative

#66

@nonzzz nonzzz marked this pull request as draft October 7, 2024 16:59
@nonzzz
Copy link
Contributor Author

nonzzz commented Oct 7, 2024

For legacy decorator code

function trace(_target: any, _name: string, descriptor: PropertyDescriptor) {
    const original = descriptor.value;
    descriptor.value = function () {
      console.log('trace');
      return original.call(this);
    };
  
    return descriptor;
  }
  
  class People {
    xzy: string;
    constructor() {
      this.xzy = 'xzy';
    }
  
    @trace
    test() {
      return this.xzy;
    }
  }
  
  const p = new People();
  
  p.test();
  
  1. tsc output
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
function trace(_target, _name, descriptor) {
    var original = descriptor.value;
    descriptor.value = function () {
        console.log('trace');
        return original.call(this);
    };
    return descriptor;
}
var People = /** @class */ (function () {
    function People() {
        this.xzy = 'xzy';
    }
    People.prototype.test = function () {
        return this.xzy;
    };
    __decorate([
        trace,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], People.prototype, "test", null);
    return People;
}());
var p = new People();
p.test();
  1. swc output
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === \\"object\\" && typeof Reflect.decorate === \\"function\\") r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if (typeof Reflect === \\"object\\" && typeof Reflect.metadata === \\"function\\") return Reflect.metadata(k, v);
}
function trace(_target, _name, descriptor) {
    const original = descriptor.value;
    descriptor.value = function() {
        console.log('trace');
        return original.call(this);
    };
    return descriptor;
}
class People {
    test() {
        return this.xzy;
    }
    constructor(){
        _define_property(this, \\"xzy\\", void 0);
        this.xzy = 'xzy';
    }
}
_ts_decorate([
    trace,
    _ts_metadata(\\"design:type\\", Function),
    _ts_metadata(\\"design:paramtypes\\", []),
    _ts_metadata(\\"design:returntype\\", void 0)
], People.prototype, \\"test\\", null);
const p = new People();
p.test();

@nonzzz
Copy link
Contributor Author

nonzzz commented Oct 7, 2024

The test snapshots did not match. I don't know why. So i have had to add new flag --update

@nonzzz nonzzz marked this pull request as ready for review October 7, 2024 17:40
@SukkaW
Copy link
Owner

SukkaW commented Oct 7, 2024

The test snapshots did not match. I don't know why. So i have had to add new flag --update

You only need to manually update the snapshot once, you don't have to add --update to the scripts.

package.json Outdated Show resolved Hide resolved
@SukkaW SukkaW merged commit a477f50 into SukkaW:master Oct 7, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants