Skip to content

Commit

Permalink
fix: class services with constructors failed to build
Browse files Browse the repository at this point in the history
fixes #87
  • Loading branch information
Jack Ellis authored and jackmellis committed Sep 25, 2020
1 parent f4b73d0 commit 103890a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module": "dist/es/jpex.js",
"types": "dist/es/index.d.ts",
"scripts": {
"clear-cache": "rm -rf node_modules/.cache",
"test": "ava",
"test:debug": "ava debug",
"coverage": "nyc ava",
Expand Down
17 changes: 14 additions & 3 deletions plugin/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,20 @@ const linkedVariableVisitor: Visitor<{
programPath: NodePath<t.Program>,
}> = {
Class(path, state) {
const { deps, name } = state;
if (path.node.id.name === name) {
path.traverse(classConstructorVisitor, { deps });
const {
deps,
name,
filename,
programPath,
publicPath,
} = state;
if (path.node.id?.name === name) {
path.traverse(classConstructorVisitor, {
deps,
filename,
programPath,
publicPath,
});
}
},
ArrowFunctionExpression(path, state) {
Expand Down
15 changes: 10 additions & 5 deletions src/Jpex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import {
import encase from './encase';
import clearCache from './clearCache';

const defaultConfig = {
lifecycle: 'class' as const,
precedence: 'active' as const,
globals: true,
nodeModules: true,
optional: false,
};

class Jpex implements IJpex {
decorate: any;
$$config: IJpex['$$config'];
Expand All @@ -36,11 +44,8 @@ class Jpex implements IJpex {

this.$$parent = parent;
this.$$config = {
lifecycle: (inherit ? parent?.$$config.lifecycle : void 0) ?? 'class',
precedence: 'active',
globals: true,
nodeModules: true,
optional: false,
...defaultConfig,
...(inherit ? parent?.$$config : {}),
...config,
};

Expand Down
37 changes: 31 additions & 6 deletions src/__tests__/ts/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/* global global */
/* eslint-disable no-invalid-this */
import anyTest, { TestInterface } from 'ava';
import fs from 'fs';
import base, { JpexInstance } from '../..';

type NodeModuleType<S extends string, T = any> = T;
type GlobalType<S extends string, T = any> = T;

const test: TestInterface<{
jpex: JpexInstance,
}> = anyTest;
Expand All @@ -22,6 +26,12 @@ test('resolves factories and services', (t) => {
type Dependent = { val: string };
type Master = { val: string, sub: string };
type Constant = string;
class ComplexConcrete {
val: string;
constructor(dep: Dependent) {
this.val = dep.val;
}
}

jpex.factory<Factory>(() => 'FACTORY');
jpex.service<Service>(class {
Expand All @@ -34,17 +44,20 @@ test('resolves factories and services', (t) => {
this.val = 'MASTER';
this.sub = d.val;
});
jpex.service<ComplexConcrete>(ComplexConcrete);
jpex.constant<Constant>('CONSTANT');

const f = jpex.resolve<Factory>();
const s = jpex.resolve<Service>();
const m = jpex.resolve<Master>();
const cc = jpex.resolve<ComplexConcrete>();
const c = jpex.resolve(jpex.infer<Constant>());

t.is(f, 'FACTORY');
t.is(s.val, 'SERVICE');
t.is(m.val, 'MASTER');
t.is(m.sub, 'DEPENDENT');
t.is(cc.val, 'DEPENDENT');
t.is(c, 'CONSTANT');
});

Expand Down Expand Up @@ -124,20 +137,21 @@ test('resolves array-like dependencies', (t) => {
t.is(value, 'hello');
});

test.skip('resolves a node module', (t) => {
test.failing('resolves a node module', (t) => {
const { jpex } = t.context;

const value = jpex.resolve('fs');
type Fs = NodeModuleType<'fs', typeof fs>;
const value = jpex.resolve<Fs>();

t.is(value, fs);
});

test.skip('prefers a registered dependency over a node module', (t) => {
test('prefers a registered dependency over a node module', (t) => {
const { jpex } = t.context;
type Fs = NodeModuleType<'fs', typeof fs>;
const fakeFs = {};
jpex.factory('fs', [], () => fakeFs as any);
jpex.factory<Fs>(() => fakeFs as any);

const value = jpex.resolve('fs');
const value = jpex.resolve<Fs>();

t.not(value, fs);
t.is(value, fakeFs);
Expand All @@ -161,3 +175,14 @@ test('prefers a registered dependency over a global', (t) => {
t.not(value, window);
t.is(value, fakeWindow);
});

test.failing('allows a custom global variable', (t) => {
const { jpex } = t.context;
// @ts-ignore
global.foo = 'hello';
type Foo = GlobalType<'foo', any>;

const value = jpex.resolve<Foo>();

t.is(value, 'hello');
});

0 comments on commit 103890a

Please # to comment.