Skip to content

Commit

Permalink
chore: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Mar 1, 2024
1 parent e1a8f2f commit 0de0a72
Show file tree
Hide file tree
Showing 28 changed files with 2,943 additions and 2,943 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ root = true
# Tab indentation
[*]
end_of_line = lf
indent_style = tab
indent_style = space
trim_trailing_whitespace = true
indent_size = 2

Expand Down
154 changes: 77 additions & 77 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DependencyDescriptor } from './dependencyDescriptor'
import {
DependencyIdentifier,
IdentifierDecorator,
IdentifierDecoratorSymbol,
DependencyIdentifier,
IdentifierDecorator,
IdentifierDecoratorSymbol,
} from './dependencyIdentifier'
import { Ctor, prettyPrintIdentifier } from './dependencyItem'
import { LookUp, Quantity } from './types'
Expand All @@ -12,88 +12,88 @@ export const TARGET = Symbol('$$TARGET')
export const DEPENDENCIES = Symbol('$$DEPENDENCIES')

class DependencyDescriptorNotFoundError extends RediError {
constructor(index: number, target: Ctor<any>) {
const msg = `Could not find dependency registered on the ${index} (indexed) parameter of the constructor of "${prettyPrintIdentifier(
target
)}".`
constructor(index: number, target: Ctor<any>) {
const msg = `Could not find dependency registered on the ${index} (indexed) parameter of the constructor of "${prettyPrintIdentifier(
target
)}".`

super(msg)
}
super(msg)
}
}

export class IdentifierUndefinedError extends RediError {
constructor(target: Ctor<any>, index: number) {
const msg = `It seems that you register "undefined" as dependency on the ${
index + 1
} parameter of "${prettyPrintIdentifier(
target
)}". Please make sure that there is not cyclic dependency among your TypeScript files, or consider using "forwardRef". For more info please visit our website https://redi.wendell.fun/docs/debug#could-not-find-dependency-registered-on`

super(msg)
}
constructor(target: Ctor<any>, index: number) {
const msg = `It seems that you register "undefined" as dependency on the ${
index + 1
} parameter of "${prettyPrintIdentifier(
target
)}". Please make sure that there is not cyclic dependency among your TypeScript files, or consider using "forwardRef". For more info please visit our website https://redi.wendell.fun/docs/debug#could-not-find-dependency-registered-on`

super(msg)
}
}

/**
* @internal
*/
export function getDependencies<T>(
registerTarget: Ctor<T>
registerTarget: Ctor<T>
): DependencyDescriptor<any>[] {
const target = registerTarget as any
return target[DEPENDENCIES] || []
const target = registerTarget as any
return target[DEPENDENCIES] || []
}

/**
* @internal
*/
export function getDependencyByIndex<T>(
registerTarget: Ctor<T>,
index: number
registerTarget: Ctor<T>,
index: number
): DependencyDescriptor<any> {
const allDependencies = getDependencies(registerTarget)
const dep = allDependencies.find(
(descriptor) => descriptor.paramIndex === index
)
const allDependencies = getDependencies(registerTarget)
const dep = allDependencies.find(
(descriptor) => descriptor.paramIndex === index
)

if (!dep) {
throw new DependencyDescriptorNotFoundError(index, registerTarget)
}
if (!dep) {
throw new DependencyDescriptorNotFoundError(index, registerTarget)
}

return dep
return dep
}

/**
* @internal
*/
export function setDependency<T, U>(
registerTarget: Ctor<U>,
identifier: DependencyIdentifier<T>,
paramIndex: number,
quantity: Quantity = Quantity.REQUIRED,
lookUp?: LookUp
registerTarget: Ctor<U>,
identifier: DependencyIdentifier<T>,
paramIndex: number,
quantity: Quantity = Quantity.REQUIRED,
lookUp?: LookUp
): void {
const descriptor: DependencyDescriptor<T> = {
paramIndex,
identifier,
quantity,
lookUp,
withNew: false,
}

// sometimes identifier could be 'undefined' if user meant to pass in an ES class
// this is related to how classes are transpiled
if (typeof identifier === 'undefined') {
throw new IdentifierUndefinedError(registerTarget, paramIndex)
}

const target = registerTarget as any
// deal with inheritance, subclass need to declare dependencies on its on
if (target[TARGET] === target) {
target[DEPENDENCIES].push(descriptor)
} else {
target[DEPENDENCIES] = [descriptor]
target[TARGET] = target
}
const descriptor: DependencyDescriptor<T> = {
paramIndex,
identifier,
quantity,
lookUp,
withNew: false,
}

// sometimes identifier could be 'undefined' if user meant to pass in an ES class
// this is related to how classes are transpiled
if (typeof identifier === 'undefined') {
throw new IdentifierUndefinedError(registerTarget, paramIndex)
}

const target = registerTarget as any
// deal with inheritance, subclass need to declare dependencies on its on
if (target[TARGET] === target) {
target[DEPENDENCIES].push(descriptor)
} else {
target[DEPENDENCIES] = [descriptor]
target[TARGET] = target
}
}

const knownIdentifiers = new Set<string>()
Expand All @@ -105,30 +105,30 @@ const knownIdentifiers = new Set<string>()
* @returns Identifier that could also be used as a decorator
*/
export function createIdentifier<T>(id: string): IdentifierDecorator<T> {
if (knownIdentifiers.has(id)) {
throw new RediError(`Identifier "${id}" already exists.`)
} else {
knownIdentifiers.add(id)
}

const decorator = (<any>(
function (registerTarget: Ctor<T>, _key: string, index: number): void {
setDependency(registerTarget, decorator, index)
}
)) as IdentifierDecorator<T> // decorator as an identifier

// TODO: @wzhudev should assign a name to the function so it would be easy to debug in inspect tools
// decorator.name = `[redi]: ${id}`;
decorator.toString = () => id
decorator[IdentifierDecoratorSymbol] = true

return decorator
if (knownIdentifiers.has(id)) {
throw new RediError(`Identifier "${id}" already exists.`)
} else {
knownIdentifiers.add(id)
}

const decorator = (<any>(
function (registerTarget: Ctor<T>, _key: string, index: number): void {
setDependency(registerTarget, decorator, index)
}
)) as IdentifierDecorator<T> // decorator as an identifier

// TODO: @wzhudev should assign a name to the function so it would be easy to debug in inspect tools
// decorator.name = `[redi]: ${id}`;
decorator.toString = () => id
decorator[IdentifierDecoratorSymbol] = true

return decorator
}

/**
* @internal
*/
/* istanbul ignore next */
export function TEST_ONLY_clearKnownIdentifiers(): void {
knownIdentifiers.clear()
knownIdentifiers.clear()
}
Loading

0 comments on commit 0de0a72

Please sign in to comment.