Skip to content

Classic injection constructor parser fails with static method #130

@aleho

Description

@aleho

Dependency injection fails here with parsing the constructor arguments.

The example below is able to reconstruct this problem:

const awilix = require('awilix');


class Service1 {
    /**
     * This constructor is parsed correctly.
     *
     * @param {function} injectedService
     */
    constructor(injectedService) {
        injectedService(this);
    }
}

class Service2 {
    /**
     * Comment out the return statement and parsing works again.
     *
     * @param {string} aParam
     */
    static someStaticMethod(aParam) {
        return typeof aParam !== 'undefined' ? aParam : 'no param';
    }

    /**
     * This constructor fails because of the static method above.
     *
     * @param {function} injectedService
     */
    constructor(injectedService) {
        console.log(Service2.someStaticMethod(this));
        injectedService(this);
    }
}



const container = awilix.createContainer({
    injectionMode: awilix.InjectionMode.CLASSIC,
});

container.register({
    injectedService: awilix.asFunction(() => instance => console.log(instance.constructor.name)),
    service1:        awilix.asClass(Service1),
    service2:        awilix.asClass(Service2),
});


container.resolve('service1');
container.resolve('service2');

npm list:

awilix-test@1.0.0 /home/alex/Dev/test
└─┬ awilix@4.2.1
  ├─┬ camel-case@3.0.0
  │ ├─┬ no-case@2.3.2
  │ │ └── lower-case@1.1.4
  │ └── upper-case@1.1.3
  └─┬ glob@7.1.3
    ├── fs.realpath@1.0.0
    ├─┬ inflight@1.0.6
    │ ├── once@1.4.0 deduped
    │ └── wrappy@1.0.2
    ├── inherits@2.0.3
    ├─┬ minimatch@3.0.4
    │ └─┬ brace-expansion@1.1.11
    │   ├── balanced-match@1.0.0
    │   └── concat-map@0.0.1
    ├─┬ once@1.4.0
    │ └── wrappy@1.0.2 deduped
    └── path-is-absolute@1.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions