Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-06-02 22:36:25 -05:00
parent 82e7a1f299
commit 1d5056b753
149 changed files with 6104 additions and 3114 deletions

View File

@@ -1,11 +1,11 @@
import {DependencyRequirement, PropertyDependency} from "../types";
import { Collection } from "../../util";
import {DependencyKey, DependencyRequirement, PropertyDependency} from '../types'
import { Collection } from '../../util'
/**
* Abstract base class for dependency container factories.
* @abstract
*/
export abstract class AbstractFactory {
export abstract class AbstractFactory<T> {
protected constructor(
/**
* Token that was registered for this factory. In most cases, this is the static
@@ -13,7 +13,7 @@ export abstract class AbstractFactory {
* @var
* @protected
*/
protected token: any
protected token: DependencyKey,
) {}
/**
@@ -21,14 +21,14 @@ export abstract class AbstractFactory {
* @param {Array} dependencies - the resolved dependencies, in order
* @param {Array} parameters - the bound constructor parameters, in order
*/
abstract produce(dependencies: any[], parameters: any[]): any
abstract produce(dependencies: any[], parameters: any[]): T
/**
* Should return true if the given identifier matches the token for this factory.
* @param something
* @return boolean
*/
abstract match(something: any): boolean
abstract match(something: unknown): boolean
/**
* Get the dependency requirements required by this factory's token.

View File

@@ -1,6 +1,6 @@
import {AbstractFactory} from "./AbstractFactory";
import {DependencyRequirement, PropertyDependency, StaticClass} from "../types";
import {Collection} from "../../util";
import {AbstractFactory} from './AbstractFactory'
import {DependencyKey, DependencyRequirement, PropertyDependency} from '../types'
import {Collection} from '../../util'
/**
* A factory whose token is produced by calling a function.
@@ -17,19 +17,19 @@ import {Collection} from "../../util";
* fact.produce([], []) // => 4
* ```
*/
export class ClosureFactory extends AbstractFactory {
export class ClosureFactory<T> extends AbstractFactory<T> {
constructor(
protected readonly name: string | StaticClass<any, any>,
protected readonly token: () => any,
protected readonly name: DependencyKey,
protected readonly token: () => T,
) {
super(token)
}
produce(dependencies: any[], parameters: any[]): any {
produce(): any {
return this.token()
}
match(something: any) {
match(something: unknown): boolean {
return something === this.name
}

View File

@@ -1,12 +1,12 @@
import {AbstractFactory} from "./AbstractFactory";
import {AbstractFactory} from './AbstractFactory'
import {
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY,
DependencyRequirement,
Instantiable,
PropertyDependency
} from "../types";
import {Collection} from "../../util";
PropertyDependency,
} from '../types'
import {Collection} from '../../util'
import 'reflect-metadata'
/**
@@ -29,9 +29,9 @@ import 'reflect-metadata'
* fact.produce([myServiceInstance], []) // => A { myService: myServiceInstance }
* ```
*/
export class Factory extends AbstractFactory {
export class Factory<T> extends AbstractFactory<T> {
constructor(
protected readonly token: Instantiable<any>
protected readonly token: Instantiable<T>,
) {
super(token)
}
@@ -40,13 +40,15 @@ export class Factory extends AbstractFactory {
return new this.token(...dependencies, ...parameters)
}
match(something: any) {
match(something: unknown): boolean {
return something === this.token // || (something?.name && something.name === this.token.name)
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.token)
if ( meta ) return meta
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
@@ -56,7 +58,9 @@ export class Factory extends AbstractFactory {
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) meta.concat(loadedMeta)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)

View File

@@ -1,12 +1,12 @@
import {Factory} from "./Factory";
import {Instantiable} from "../types";
import {Factory} from './Factory'
import {Instantiable} from '../types'
/**
* Container factory that produces an instance of the token, however the token
* is identified by a string name rather than a class reference.
* @extends Factory
*/
export default class NamedFactory extends Factory {
export default class NamedFactory<T> extends Factory<T> {
constructor(
/**
* The name identifying this factory in the container.
@@ -18,12 +18,12 @@ export default class NamedFactory extends Factory {
* The token to be instantiated.
* @type {Instantiable}
*/
protected token: Instantiable<any>,
protected token: Instantiable<T>,
) {
super(token)
}
match(something: any) {
match(something: unknown): boolean {
return something === this.name
}
}

View File

@@ -1,6 +1,6 @@
import { Factory } from './Factory'
import { Collection } from '../../util'
import {DependencyRequirement, PropertyDependency} from "../types";
import {DependencyKey, DependencyRequirement, PropertyDependency} from '../types'
/**
* Container factory which returns its token as its value, without attempting
@@ -19,29 +19,23 @@ import {DependencyRequirement, PropertyDependency} from "../types";
*
* @extends Factory
*/
export default class SingletonFactory extends Factory {
export default class SingletonFactory<T> extends Factory<T> {
constructor(
/**
* Instantiated value of this factory.
* @type FunctionConstructor
* Token identifying this singleton.
*/
protected token: FunctionConstructor,
protected token: DependencyKey,
/**
* String name of this singleton identifying it in the container.
* @type string
* The value of this singleton.
*/
protected key: string,
protected value: T,
) {
super(token)
}
produce(dependencies: any[], parameters: any[]) {
return this.token
}
match(something: any) {
return something === this.key
produce(): T {
return this.value
}
getDependencyKeys(): Collection<DependencyRequirement> {