loganfsmyth / babel-plugin-transform-decorators-legacy

A plugin for Babel 6 that (mostly) replicates the old decorator behavior from Babel 5
MIT License
817 stars 57 forks source link

Cannot assign to read only property of object #57

Open pleerock opened 7 years ago

pleerock commented 7 years ago

I have such error: Cannot assign to read only property 'name' of object '#<Category>' when I use non initialized properties decorated with decorators and then trying to assign values to these properties.

import {Entity, PrimaryColumn, Column} from "typeorm";

@Entity()
export class Category {

    @PrimaryColumn("int", { generated: true })
    id;

    @Column("string")
    name;

}

Solution is to do it this way:

import {Entity, PrimaryColumn, Column} from "typeorm";

@Entity()
export class Category {

    @PrimaryColumn("int", { generated: true })
    id = undefined;

    @Column("string")
    name = "";

}

Which is ugly. I guess the reason is that it does not make descriptor writable. Is there any better solution for this problem?

loganfsmyth commented 7 years ago

This seems like an issue with the typeorm decorators, rather than an issue with this module?

pleerock commented 7 years ago

No, I think its not. Simple example of reproduction without typeorm:

function dec(id) {
    return (target, property, descriptor) => console.log("executed", id);
}

export class Category {

    @dec()
    id = 0;

    @dec()
    name;

}

const category = new Category();
category.id = 1;
category.name = "hello";

produces:

/Users/pleerock/www/opensource/typeorm/babel-example/dist/index.js:79
category.name = "hello";
              ^

TypeError: Cannot assign to read only property 'name' of object '#<Category>'
    at Object.<anonymous> (/Users/pleerock/www/opensource/typeorm/babel-example/dist/index.js:79:15)

Note that category.id = 1 worked because id was initialized in the class.

loganfsmyth commented 7 years ago

Would you be able to put together a simple example repo? Dropping that into a repo with .babelrc

"transform-class-properties",
"transform-decorators-legacy",

logs

executed undefined
executed undefined

for me, so something else may be interfering?

pleerock commented 7 years ago

why do you put "transform-class-properties" before "transform-decorators-legacy"?

pleerock commented 7 years ago

Here is my configuration:

{
  "presets": [
    "es2015"
  ],
  "plugins": [
    "transform-decorators-legacy",
    "transform-class-properties"
  ]
}
loganfsmyth commented 7 years ago

You are right, I tried both to make sure and then copy-pasted the wrong one.

I am able to reproduce with es2015. Thanks for the info.

hwaterke commented 7 years ago

The problem originates from this check: index.js (LINE 54)

I'm not familiar enough with the code to be sure if it can be safely removed. Any opinion?

loganfsmyth commented 7 years ago

Yeah that's definitely the line. That was carried over from Babel 5.x here: https://github.com/babel/babel/blob/5.x/packages/babel/src/transformation/templates/helper-create-decorated-class.js#L15 and the same bug reproduces in Babel 5 too.

SPAHI4 commented 7 years ago

Any solutions?

chbdetta commented 6 years ago

Is this solved?