Giter Club home page Giter Club logo

didi's People

Contributors

felixlinker avatar gustavjf avatar makepanic avatar nikku avatar philippfromme avatar vojtajina avatar yfwz100 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

didi's Issues

Creates incorrect injections for class without class name

Describe the Bug

Hi,
I noticed that minified output sometimes inlines classes I pass to didi.
In this case, it generates an unexpected dependency called class.

Steps to Reproduce

  1. parse annotations with a class without name.
        expect(parseAnnotations(class{})).to.eql([ ]);
  1. expect no dependencies
  2. parseAnnotations in this example returns [ "class" ]
      AssertionError: expected [ 'class' ] to deeply equal []
      + expected - actual

      -[
      -  "class"
      -]
      +[]

Expected Behavior

Parsing this class should return an empty array or an array without class

Environment

  • Host (Browser/Node version), if applicable: Node 16.17.0
  • OS: linux
  • Library version: 8.0.1

Possible solution

It seems like this happens because didi determines that a class is a class by checking if it starts with class .
In the inlined case, the class can start with class without an extra space

Would it be ok to change the heuristic to check for class or class{? I can create a PR with a test if that's ok.
I.e. a solution like the following would solve this issue:

var CLASS_PATTERN = /^class[ {]/;` 

I've created #18 which implements this solution

Add ability to resolve services to null

This is handy if components are built in a way that they can handle the non-existance of a service.

At the moment, there is no way to do this and an error will be thrown (and has to be caught) _all the time.

Support for auto-annotate ES classes

I would expect this to work:

var di = require('didi');

class Car {
    start() {
        console.log('success!');
    }
}

var module = {
    'car': ['type', Car],
};

var injector = new di.Injector([module]);
injector.invoke(['car', function (car) {
    car.start();
}]);

But what I get is this error:

node_modules\didi\dist\didi.js:125
  return match[1] && match[1].split(',').map(function (arg) {
              ^

TypeError: Cannot read property '1' of null
    at parse (c:\z\github\nikku\tmp\node_modules\didi\dist\didi.js:125:15)

The problem seems to be with annotation.js, where the toString of the class is expected to include this regex:

/^function\s*[^(]*\(\s*([^)]*)\)/m;

But the toString is the class definition above and thus does not comply to the regex.

Wrong Type Declaration for Injector#invoke

Describe the Bug

The context and locals parameters of Injector#invoke are optional. The Type declaration doesn't reflect that:

invoke<T>(func: (...args: unknown[]) => T, context: InjectionContext, locals: LocalsMap): T;

Expected Behavior

The declaration should be:

invoke<T>(func: (...args: unknown[]) => T, context?: InjectionContext, locals?: LocalsMap): T;

Destructured Function Parameter Injection

Would be great to support unpacking or destructured objects for function parameters.
Working on a PR here with update to the readme.

// destructured object parameter wont require any annotations. 
function createEngine({power}) { ... }

//then in module config can take the simple route as well since function params are parsed and $inject is automatically added
const carModule = {
  engine: ['factory', createEngine ],
  power:  ['value', { horses: 400 }]
};

Let me know what you think. Would this be something your interested in merging in. Plan on finishing tests today and will convert PR off draft once done.
Great project, thanks.

Installing this repository errors on git bash (Windows)

Running npm i in the repository errors on windows.

$ git clone [email protected]:nikku/didi.git
$ cd didi
$ npm i

> [email protected] prepare C:\z\utrechtjs\didi
> run-s bundle


> [email protected] bundle C:\z\utrechtjs\didi
> NODE_ENV=production rollup -c

'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] bundle: `NODE_ENV=production rollup -c`

Is the bundle step important to run on as "prepare" script? Can we maybe use cross-env to ensure it works on windows and linux?

on Bundle and minify using web pack not able to resolve module

hi

nice library to apply DI, for plain js development

Issue:- on applying minify to js bundle, it seem not able to resolve the module, any pointer, its actually not matching the name in constructor of class from look up, any pointer?

thanks
Vimal

Support instantiating ES classes

I would expect this code example to work:

var di = require('didi');

class Car {
    start() {
        console.log('success!');
    }
}

Car.$inject = [];

var module = {
    'car': ['type', Car],
};


var injector = new di.Injector([module]);
injector.invoke(['car', function (car) {
    car.start();
}]);

Instead i get this error:

didi\dist\didi.js:316
    return fn.apply(context, dependencies);
              ^

TypeError: Class constructor Car cannot be invoked without 'new'

Which is pretty much self explanatory, apparently classes are not supported (yet).

This is contradicted in the readme however:

// if an object asks for 'car', the injector will call new Car(...) to produce it
'car': ['type', Car],

didi types cause errors in build

Describe the Bug

In a project using didi for dependency injection, the new version with types included causes TS errors. It seems that the errors are so terrible to TypeScript that it actually will actually refuse to ignore the errors even with node_modules excluded.

The errors are the following three:

node_modules/didi/dist/types/lib/types.d.ts:56:3 - error TS2411: Property '__exports__' of type 'string[] | undefined' is not assignable to string index type 'string[] | ModuleDeclaration[] | ["value", any] | ["value", any, "private"] | ["type", TypeDeclaration<any>] | ["type", TypeDeclaration<any>, "private"] | [...] | [...]'.

56   __exports__?: string[],
     ~~~~~~~~~~~

node_modules/didi/dist/types/lib/types.d.ts:57:3 - error TS2411: Property '__modules__' of type 'ModuleDeclaration[] | undefined' is not assignable to string index type 'string[] | ModuleDeclaration[] | ["value", any] | ["value", any, "private"] | ["type", TypeDeclaration<any>] | ["type", TypeDeclaration<any>, "private"] | [...] | [...]'.

57   __modules__?: ModuleDeclaration[],
     ~~~~~~~~~~~

node_modules/didi/dist/types/lib/types.d.ts:58:38 - error TS8020: JSDoc types can only be used inside documentation comments.

58   [name: string]: ServiceDeclaration<?> | ModuleDeclaration[] | string[]

Steps to Reproduce

  1. Setup a new project
  2. Install [email protected]
  3. Setup a tsconfig with "module": "es2020" and "strict": true (possibly "target": "es2020" and "moduleResolution": "node" as well). Exclude node_modules.
  4. Create a main.ts where "didi" is dynamically imported with const { Injector } = await import("didi");.
  5. Run tsc build
  6. Errors should appear in build.

Expected Behavior

Library TS definitions should not contain errors.

It seems that

  1. The ModuleDeclaration arrays should not be optional.
  2. The ? in the ServiceDeclaration's type parameter seems to be an obvious JSDoc oversight.

Environment

  • Ubuntu 18.04 WSL2 env

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.