Skip to content

Commit

Permalink
Fix tslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Jul 26, 2018
1 parent 0274483 commit 151da14
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@ export const Reflection = Object.assign(Reflect, {
});

export type Decorator = ClassDecorator | MemberDecorator;
export type MemberDecorator = <T>(target: Target, propertyKey: PropertyKey, descriptor?: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
export type MemberDecorator = <T>(target: Target,
propertyKey: PropertyKey,
descriptor?: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
export type MetadataKey = string;
export type MetadataValue = Function;
export type PropertyKey = string | symbol;
export type Target = object | Function;

const Metadata = new WeakMap();

export function defineMetadata(metadataKey: MetadataKey, metadataValue: MetadataValue, target: Target, propertyKey?: PropertyKey) {
export function defineMetadata(metadataKey: MetadataKey,
metadataValue: MetadataValue,
target: Target, propertyKey?: PropertyKey) {
return ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
}

export function decorate(decorators: ClassDecorator[], target: Function): Function
export function decorate(decorators: MemberDecorator[], target: object, propertyKey?: PropertyKey, attributes?: PropertyDescriptor): PropertyDescriptor | undefined
export function decorate(decorators: Decorator[], target: Target, propertyKey?: PropertyKey, attributes?: PropertyDescriptor): Function | PropertyDescriptor | undefined {
if (decorators.length === 0) throw new TypeError();
export function decorate(decorators: ClassDecorator[], target: Function): Function;
export function decorate(decorators: MemberDecorator[],
target: object,
propertyKey?: PropertyKey,
attributes?: PropertyDescriptor): PropertyDescriptor | undefined;
export function decorate(decorators: Decorator[],
target: Target,
propertyKey?: PropertyKey,
attributes?: PropertyDescriptor): Function | PropertyDescriptor | undefined {
if (decorators.length === 0) { throw new TypeError(); }

if (typeof target === 'function') {
return decorateConstructor(decorators as ClassDecorator[], target);
Expand Down Expand Up @@ -61,30 +71,40 @@ function decorateConstructor(decorators: ClassDecorator[], target: Function): Fu
return target;
}

function decorateProperty(decorators: MemberDecorator[], target: Target, propertyKey: PropertyKey, descriptor?: PropertyDescriptor): PropertyDescriptor | undefined {
function decorateProperty(decorators: MemberDecorator[],
target: Target,
propertyKey: PropertyKey,
descriptor?: PropertyDescriptor): PropertyDescriptor | undefined {
decorators.reverse().forEach((decorator: MemberDecorator) => {
descriptor = decorator(target, propertyKey, descriptor) || descriptor;
});
return descriptor;
}

function ordinaryDefineOwnMetadata(metadataKey: MetadataKey, metadataValue: MetadataValue, target: Target, propertyKey?: PropertyKey): void {
if (propertyKey && !['string', 'symbol'].includes(typeof propertyKey)) throw new TypeError();
function ordinaryDefineOwnMetadata(metadataKey: MetadataKey,
metadataValue: MetadataValue,
target: Target,
propertyKey?: PropertyKey): void {
if (propertyKey && !['string', 'symbol'].includes(typeof propertyKey)) { throw new TypeError(); }

(getMetadataMap(target, propertyKey) || createMetadataMap(target, propertyKey))
.set(metadataKey, metadataValue);
}

function ordinaryGetMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): Function | undefined {
function ordinaryGetMetadata(metadataKey: MetadataKey,
target: Target,
propertyKey?: PropertyKey): Function | undefined {
return !!ordinaryGetOwnMetadata(metadataKey, target, propertyKey)
? ordinaryGetOwnMetadata(metadataKey, target, propertyKey)
: Object.getPrototypeOf(target)
? ordinaryGetMetadata(metadataKey, Object.getPrototypeOf(target), propertyKey)
: undefined;
}

function ordinaryGetOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): Function | undefined {
if (target === undefined) throw new TypeError();
function ordinaryGetOwnMetadata(metadataKey: MetadataKey,
target: Target,
propertyKey?: PropertyKey): Function | undefined {
if (target === undefined) { throw new TypeError(); }
const metadataMap = getMetadataMap(target, propertyKey);
return metadataMap && metadataMap.get(metadataKey);
}
Expand Down

0 comments on commit 151da14

Please sign in to comment.