Skip to content

Commit

Permalink
refactor: stop parsing functions to extract dependencies
Browse files Browse the repository at this point in the history
closes #77
BREAKING CHANGE: will now need to explicitly delcare dependencies or use TS inference
  • Loading branch information
Jack Ellis committed Aug 23, 2020
1 parent aa661fe commit 75a23c8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Change Log
- global dependencies such as `Window` and `Document` are now automatically resolved (unless you register your own dependency of the same name)
- you can now control dependency resolution with config flags `nodeModules` and `globals`
- you can also specify whether dependencies should be optional-by-default with an `optional` flag
- dependencies are no longer determined by reading the factory function. Either use `TS` inference, or explicitly pass an array of deps

#### Breaking Changes
- if you attempt to resolve a global like `Window` without registering it first, rather than throw an error, you will now get the global variable
- You can no longer do `jpex.factory('foo', (depA, depB) => { ... })` as we no longer parse the function and extract the dependencies

### 3.5.0
- add some deprecation warnings for pre-4.0.0 changes
Expand Down
11 changes: 5 additions & 6 deletions src/encase.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { JpexInstance, Dependency, AnyFunction } from './types';
import { extractParameters } from './utils';
import { allResolved, resolveDependencies } from './resolver';

const encase = (
const encase = <F extends AnyFunction, G extends AnyFunction<F>>(
jpex: JpexInstance,
_deps: string[],
_deps: any,
_fn?: any,
): any => {
const [ dependencies, fn ] = ((): [ Dependency[], any ] => {
const [ dependencies, fn ] = ((): [ Dependency[], G ] => {
if (typeof _deps === 'function') {
return [ extractParameters(_deps), _deps ];
return [ [], _deps ];
}
return [ _deps, _fn ];
})();
let result: AnyFunction;

const encased = function(...args: Parameters<any>) {
const encased = function(...args: Parameters<F>) {
/* eslint-disable no-invalid-this */
if (result && allResolved(jpex, dependencies)) {
return result.apply(this, args);
Expand Down
4 changes: 2 additions & 2 deletions src/registers/factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JpexInstance, Factory } from '../types';
import wrapper, { Wrapper } from './wrapper';
import { extractParameters, isString, isFunction } from '../utils';
import { isString, isFunction } from '../utils';

function factory(
jpex: JpexInstance,
Expand All @@ -21,7 +21,7 @@ function factory(
if (dependencies) {
dependencies = [].concat(dependencies);
} else {
dependencies = extractParameters(fn);
dependencies = [];
}
if (!dependencies.length) {
dependencies = null;
Expand Down
4 changes: 2 additions & 2 deletions src/registers/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JpexInstance } from '../types';
import { extractParameters, instantiate, isFunction } from '../utils';
import { instantiate, isFunction } from '../utils';

function service(
jpex: JpexInstance,
Expand All @@ -18,7 +18,7 @@ function service(
if (dependencies) {
dependencies = [].concat(dependencies);
} else {
dependencies = extractParameters(fn);
dependencies = [];
}

function factory(...args: any[]) {
Expand Down
38 changes: 0 additions & 38 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AnyFunction } from '../types';

const getType = (obj: any) => Object.prototype.toString.call(obj);
export const isObject = (obj: any): obj is object => getType(obj) === '[object Object]';

Expand Down Expand Up @@ -36,42 +34,6 @@ export const unsafeRequire = (target: string) => {
return new Function('require', 'target', 'return require.main.require(target)')(require, target);
};

const REG_COMMENTS = /\/\/(.*?)\n|\/\*([\S\s]*?)\*\//g;
export const extractParameters = (fn: AnyFunction) => {
const CHR_OPEN = '(';
const CHR_CLOSE = ')';
const CHR_ARROW = '=>';
const CHR_DELIMETER = ',';

let str = fn.toString();

// Remove comments
str = str.replace(REG_COMMENTS, '');

// Find the start and end of the parameters
const open = str.indexOf(CHR_OPEN);
const close = str.indexOf(CHR_CLOSE);
const arrow = str.indexOf(CHR_ARROW);

// Arrow functions may or may not contain brackets
if (arrow > -1 && (arrow < open || open < 0)) {
str = str.substring(0, arrow).trim();
if (!str) {
return [];
}
return [ str ];
}

// Pull out the parameters
str = str.substring(open + 1, close);

if (!str) {
return [];
}

return str.split(CHR_DELIMETER).map((s) => s.trim());
};

interface GetLast {
(str: string): string,
<T>(arr: T[]): T,
Expand Down

0 comments on commit 75a23c8

Please sign in to comment.