Skip to content

Commit

Permalink
Merge pull request #14 from abraham/tslint
Browse files Browse the repository at this point in the history
Add tslint
  • Loading branch information
abraham authored Jul 26, 2018
2 parents a9f714c + 151da14 commit 2cc464a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"scripts": {
"build": "tsc && rollup -c",
"clean": "rimraf dist",
"lint": "tslint --project tsconfig.json",
"prebuild": "npm run clean",
"prepare": "npm run build",
"prepublishOnly": "pkg-ok",
"start": "jest --watch",
"test": "jest"
"test": "jest && npm run lint"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,6 +41,7 @@
"rimraf": "2.6.2",
"rollup": "0.63.4",
"ts-jest": "23.0.1",
"tslint": "5.11.0",
"typescript": "2.9.2"
},
"jest": {
Expand Down
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
19 changes: 19 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"quotemark": [true, "single"],
"ban-types": [
true,
["Object", "Avoid using the `Object` type. Did you mean `object`?"],
["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"],
["Number", "Avoid using the `Number` type. Did you mean `number`?"],
["String", "Avoid using the `String` type. Did you mean `string`?"],
["Symbol", "Avoid using the `Symbol` type. Did you mean `symbol`?"]
]
},
"rulesDirectory": []
}

0 comments on commit 2cc464a

Please sign in to comment.