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

[bug] mako doesn't working with @opensumi/di 's example #1172

Closed
stormslowly opened this issue May 17, 2024 · 2 comments · Fixed by #1181
Closed

[bug] mako doesn't working with @opensumi/di 's example #1172

stormslowly opened this issue May 17, 2024 · 2 comments · Fixed by #1181
Assignees

Comments

@stormslowly
Copy link
Member

package.json

{
       "name": "dead-simple",
       "dependencies": {
               "@opensumi/di": "^2.1.0",
               "reflect-metadata": "^0.2.2"
       }
}

src/index.ts

import "reflect-metadata"

import { Injectable, Autowired, Injector } from '@opensumi/di';

interface Drivable {
  drive(): void;
}

@Injectable()
class Bike implements Drivable {
  drive() {
    console.log('Bike is driving');
  }
}

@Injectable("Student")
class Student {
  @Autowired('Drivable')
  mBike: Drivable;

  goToSchool() {
    console.log('go to school');
    this.mBike.drive();
  }
}

const injector = new Injector();
injector.addProviders(
  { token: 'Drivable', useClass: Bike },
  { token: 'Student',  useClass: Student }
);

const student = injector.get('Student');
student.goToSchool(); 

expect log

go to school
Bike is driving

but error

                    this.mBike.drive();
                               ^

TypeError: Cannot read properties of undefined (reading 'drive')
@stormslowly
Copy link
Member Author

原因
mako 使用默认的 assumption 配置

assumption.set_public_class_fields = false;
assumption.set_class_methods = false;

相当于 swcrc 配置 ref

  "transform": {
      "legacyDecorator": true,
      "useDefineForClassFields":true <----- here

产物中

class Student {
    mBike;  <-----   直接覆盖了 decorator 结果
    goToSchool() {
        console.log('go to school');
        this.mBike.drive();
    }
}

修改 useDefineForClassFields -> false

产物正常执行

@stormslowly stormslowly self-assigned this May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant